1ab4382d2SGreg Kroah-Hartman /* 272ce5732SAndy Shevchenko * Driver for Atmel AT91 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/uaccess.h> 42bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h> 432e68c22fSElen Song #include <linux/timer.h> 44354e57f3SLinus Walleij #include <linux/gpio.h> 45e0b0baadSRichard Genoud #include <linux/gpio/consumer.h> 46e0b0baadSRichard Genoud #include <linux/err.h> 47ab5e4e41SRichard Genoud #include <linux/irq.h> 482c7af5baSBoris BREZILLON #include <linux/suspend.h> 492b5cf14bSGeliang Tang #include <linux/mm.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" 748961df89SRichard Genoud #include "atmel_serial.h" 75e0b0baadSRichard Genoud 76ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port); 77ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port); 78ab4382d2SGreg Kroah-Hartman 79ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT 80ab4382d2SGreg Kroah-Hartman 81ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 82ab4382d2SGreg Kroah-Hartman * should coexist with the 8250 driver, such as if we have an external 16C550 83ab4382d2SGreg Kroah-Hartman * UART. */ 84ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR 204 85ab4382d2SGreg Kroah-Hartman #define MINOR_START 154 86ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyAT" 87ab4382d2SGreg Kroah-Hartman 88ab4382d2SGreg Kroah-Hartman #else 89ab4382d2SGreg Kroah-Hartman 90ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 91ab4382d2SGreg Kroah-Hartman * name, but it is legally reserved for the 8250 driver. */ 92ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR TTY_MAJOR 93ab4382d2SGreg Kroah-Hartman #define MINOR_START 64 94ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME "ttyS" 95ab4382d2SGreg Kroah-Hartman 96ab4382d2SGreg Kroah-Hartman #endif 97ab4382d2SGreg Kroah-Hartman 98ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT 256 99ab4382d2SGreg Kroah-Hartman 100ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer { 101ab4382d2SGreg Kroah-Hartman unsigned char *buf; 102ab4382d2SGreg Kroah-Hartman dma_addr_t dma_addr; 103ab4382d2SGreg Kroah-Hartman unsigned int dma_size; 104ab4382d2SGreg Kroah-Hartman unsigned int ofs; 105ab4382d2SGreg Kroah-Hartman }; 106ab4382d2SGreg Kroah-Hartman 107ab4382d2SGreg Kroah-Hartman struct atmel_uart_char { 108ab4382d2SGreg Kroah-Hartman u16 status; 109ab4382d2SGreg Kroah-Hartman u16 ch; 110ab4382d2SGreg Kroah-Hartman }; 111ab4382d2SGreg Kroah-Hartman 112637ba54fSLudovic Desroches /* 113637ba54fSLudovic Desroches * Be careful, the real size of the ring buffer is 114637ba54fSLudovic Desroches * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer 115637ba54fSLudovic Desroches * can contain up to 1024 characters in PIO mode and up to 4096 characters in 116637ba54fSLudovic Desroches * DMA mode. 117637ba54fSLudovic Desroches */ 118ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024 119ab4382d2SGreg Kroah-Hartman 120ab4382d2SGreg Kroah-Hartman /* 1219af92fbfSAlexandre Belloni * at91: 6 USARTs and one DBGU port (SAM9260) 122432f9748SAlexandre Belloni * samx7: 3 USARTs and 5 UARTs 1239af92fbfSAlexandre Belloni */ 124432f9748SAlexandre 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 179488ae82dSAlexandre Belloni #ifdef CONFIG_PM 1806a5f0e2fSAlexandre Belloni struct { 1816a5f0e2fSAlexandre Belloni u32 cr; 1826a5f0e2fSAlexandre Belloni u32 mr; 1836a5f0e2fSAlexandre Belloni u32 imr; 1846a5f0e2fSAlexandre Belloni u32 brgr; 1856a5f0e2fSAlexandre Belloni u32 rtor; 1866a5f0e2fSAlexandre Belloni u32 ttgr; 1876a5f0e2fSAlexandre Belloni u32 fmr; 1886a5f0e2fSAlexandre Belloni u32 fimr; 1896a5f0e2fSAlexandre Belloni } cache; 190488ae82dSAlexandre Belloni #endif 1916a5f0e2fSAlexandre Belloni 192a930e528SElen Song int (*prepare_rx)(struct uart_port *port); 193a930e528SElen Song int (*prepare_tx)(struct uart_port *port); 194a930e528SElen Song void (*schedule_rx)(struct uart_port *port); 195a930e528SElen Song void (*schedule_tx)(struct uart_port *port); 196a930e528SElen Song void (*release_rx)(struct uart_port *port); 197a930e528SElen Song void (*release_tx)(struct uart_port *port); 198ab4382d2SGreg Kroah-Hartman }; 199ab4382d2SGreg Kroah-Hartman 200ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 201503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); 202ab4382d2SGreg Kroah-Hartman 203ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ 204ab4382d2SGreg Kroah-Hartman static struct console atmel_console; 205ab4382d2SGreg Kroah-Hartman #endif 206ab4382d2SGreg Kroah-Hartman 2075fbe46b6SNicolas Ferre #if defined(CONFIG_OF) 2085fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = { 2095fbe46b6SNicolas Ferre { .compatible = "atmel,at91rm9200-usart" }, 2105fbe46b6SNicolas Ferre { .compatible = "atmel,at91sam9260-usart" }, 2115fbe46b6SNicolas Ferre { /* sentinel */ } 2125fbe46b6SNicolas Ferre }; 2135fbe46b6SNicolas Ferre #endif 2145fbe46b6SNicolas Ferre 215ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port * 216ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart) 217ab4382d2SGreg Kroah-Hartman { 218ab4382d2SGreg Kroah-Hartman return container_of(uart, struct atmel_uart_port, uart); 219ab4382d2SGreg Kroah-Hartman } 220ab4382d2SGreg Kroah-Hartman 2214e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg) 2224e7decdaSCyrille Pitchen { 2234e7decdaSCyrille Pitchen return __raw_readl(port->membase + reg); 2244e7decdaSCyrille Pitchen } 2254e7decdaSCyrille Pitchen 2264e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value) 2274e7decdaSCyrille Pitchen { 2284e7decdaSCyrille Pitchen __raw_writel(value, port->membase + reg); 2294e7decdaSCyrille Pitchen } 2304e7decdaSCyrille Pitchen 231a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port) 232a6499435SCyrille Pitchen { 233a6499435SCyrille Pitchen return __raw_readb(port->membase + ATMEL_US_RHR); 234a6499435SCyrille Pitchen } 235a6499435SCyrille Pitchen 236a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value) 237a6499435SCyrille Pitchen { 238a6499435SCyrille Pitchen __raw_writeb(value, port->membase + ATMEL_US_THR); 239a6499435SCyrille Pitchen } 240a6499435SCyrille Pitchen 241ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC 24264e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 243ab4382d2SGreg Kroah-Hartman { 244ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 245ab4382d2SGreg Kroah-Hartman 24664e22ebeSElen Song return atmel_port->use_pdc_rx; 247ab4382d2SGreg Kroah-Hartman } 248ab4382d2SGreg Kroah-Hartman 24964e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 250ab4382d2SGreg Kroah-Hartman { 251ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 252ab4382d2SGreg Kroah-Hartman 25364e22ebeSElen Song return atmel_port->use_pdc_tx; 254ab4382d2SGreg Kroah-Hartman } 255ab4382d2SGreg Kroah-Hartman #else 25664e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port) 257ab4382d2SGreg Kroah-Hartman { 258ab4382d2SGreg Kroah-Hartman return false; 259ab4382d2SGreg Kroah-Hartman } 260ab4382d2SGreg Kroah-Hartman 26164e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port) 262ab4382d2SGreg Kroah-Hartman { 263ab4382d2SGreg Kroah-Hartman return false; 264ab4382d2SGreg Kroah-Hartman } 265ab4382d2SGreg Kroah-Hartman #endif 266ab4382d2SGreg Kroah-Hartman 26708f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port) 26808f738beSElen Song { 26908f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 27008f738beSElen Song 27108f738beSElen Song return atmel_port->use_dma_tx; 27208f738beSElen Song } 27308f738beSElen Song 27434df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port) 27534df42f5SElen Song { 27634df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 27734df42f5SElen Song 27834df42f5SElen Song return atmel_port->use_dma_rx; 27934df42f5SElen Song } 28034df42f5SElen Song 2815be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port) 2825be605acSAlexandre Belloni { 2835be605acSAlexandre Belloni struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2845be605acSAlexandre Belloni 2855be605acSAlexandre Belloni return atmel_port->fifo_size; 2865be605acSAlexandre Belloni } 2875be605acSAlexandre Belloni 28898f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port, 28998f2082cSNicolas Ferre struct tasklet_struct *t) 29098f2082cSNicolas Ferre { 29198f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) 29298f2082cSNicolas Ferre tasklet_schedule(t); 29398f2082cSNicolas Ferre } 29498f2082cSNicolas Ferre 295e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port) 296e0b0baadSRichard Genoud { 297e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 298e0b0baadSRichard Genoud unsigned int status, ret = 0; 299e0b0baadSRichard Genoud 3004e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 301e0b0baadSRichard Genoud 302e0b0baadSRichard Genoud mctrl_gpio_get(atmel_port->gpios, &ret); 303e0b0baadSRichard Genoud 304e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 305e0b0baadSRichard Genoud UART_GPIO_CTS))) { 306e0b0baadSRichard Genoud if (ret & TIOCM_CTS) 307e0b0baadSRichard Genoud status &= ~ATMEL_US_CTS; 308e0b0baadSRichard Genoud else 309e0b0baadSRichard Genoud status |= ATMEL_US_CTS; 310e0b0baadSRichard Genoud } 311e0b0baadSRichard Genoud 312e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 313e0b0baadSRichard Genoud UART_GPIO_DSR))) { 314e0b0baadSRichard Genoud if (ret & TIOCM_DSR) 315e0b0baadSRichard Genoud status &= ~ATMEL_US_DSR; 316e0b0baadSRichard Genoud else 317e0b0baadSRichard Genoud status |= ATMEL_US_DSR; 318e0b0baadSRichard Genoud } 319e0b0baadSRichard Genoud 320e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 321e0b0baadSRichard Genoud UART_GPIO_RI))) { 322e0b0baadSRichard Genoud if (ret & TIOCM_RI) 323e0b0baadSRichard Genoud status &= ~ATMEL_US_RI; 324e0b0baadSRichard Genoud else 325e0b0baadSRichard Genoud status |= ATMEL_US_RI; 326e0b0baadSRichard Genoud } 327e0b0baadSRichard Genoud 328e0b0baadSRichard Genoud if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 329e0b0baadSRichard Genoud UART_GPIO_DCD))) { 330e0b0baadSRichard Genoud if (ret & TIOCM_CD) 331e0b0baadSRichard Genoud status &= ~ATMEL_US_DCD; 332e0b0baadSRichard Genoud else 333e0b0baadSRichard Genoud status |= ATMEL_US_DCD; 334e0b0baadSRichard Genoud } 335e0b0baadSRichard Genoud 336e0b0baadSRichard Genoud return status; 337e0b0baadSRichard Genoud } 338e0b0baadSRichard Genoud 339ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */ 34013bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port, 34113bd3e6fSRicardo Ribalda Delgado struct serial_rs485 *rs485conf) 342ab4382d2SGreg Kroah-Hartman { 343ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 344ab4382d2SGreg Kroah-Hartman unsigned int mode; 345ab4382d2SGreg Kroah-Hartman 346ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 3474e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 348ab4382d2SGreg Kroah-Hartman 3494e7decdaSCyrille Pitchen mode = atmel_uart_readl(port, ATMEL_US_MR); 350ab4382d2SGreg Kroah-Hartman 351ab4382d2SGreg Kroah-Hartman /* Resetting serial mode to RS232 (0x0) */ 352ab4382d2SGreg Kroah-Hartman mode &= ~ATMEL_US_USMODE; 353ab4382d2SGreg Kroah-Hartman 35413bd3e6fSRicardo Ribalda Delgado port->rs485 = *rs485conf; 355ab4382d2SGreg Kroah-Hartman 356ab4382d2SGreg Kroah-Hartman if (rs485conf->flags & SER_RS485_ENABLED) { 357ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS485\n"); 358ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 3594e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 3604e7decdaSCyrille Pitchen rs485conf->delay_rts_after_send); 361ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 362ab4382d2SGreg Kroah-Hartman } else { 363ab4382d2SGreg Kroah-Hartman dev_dbg(port->dev, "Setting UART to RS232\n"); 36464e22ebeSElen Song if (atmel_use_pdc_tx(port)) 365ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | 366ab4382d2SGreg Kroah-Hartman ATMEL_US_TXBUFE; 367ab4382d2SGreg Kroah-Hartman else 368ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 369ab4382d2SGreg Kroah-Hartman } 3704e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 371ab4382d2SGreg Kroah-Hartman 372ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 3734e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 374ab4382d2SGreg Kroah-Hartman 37513bd3e6fSRicardo Ribalda Delgado return 0; 376ab4382d2SGreg Kroah-Hartman } 377ab4382d2SGreg Kroah-Hartman 378ab4382d2SGreg Kroah-Hartman /* 379ab4382d2SGreg Kroah-Hartman * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 380ab4382d2SGreg Kroah-Hartman */ 381ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port) 382ab4382d2SGreg Kroah-Hartman { 3834e7decdaSCyrille Pitchen return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ? 3844e7decdaSCyrille Pitchen TIOCSER_TEMT : 3854e7decdaSCyrille Pitchen 0; 386ab4382d2SGreg Kroah-Hartman } 387ab4382d2SGreg Kroah-Hartman 388ab4382d2SGreg Kroah-Hartman /* 389ab4382d2SGreg Kroah-Hartman * Set state of the modem control output lines 390ab4382d2SGreg Kroah-Hartman */ 391ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 392ab4382d2SGreg Kroah-Hartman { 393ab4382d2SGreg Kroah-Hartman unsigned int control = 0; 3944e7decdaSCyrille Pitchen unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR); 3951cf6e8fcSCyrille Pitchen unsigned int rts_paused, rts_ready; 396ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 397ab4382d2SGreg Kroah-Hartman 3981cf6e8fcSCyrille Pitchen /* override mode to RS485 if needed, otherwise keep the current mode */ 3991cf6e8fcSCyrille Pitchen if (port->rs485.flags & SER_RS485_ENABLED) { 4004e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 4014e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 4021cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_USMODE; 4031cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_RS485; 4041cf6e8fcSCyrille Pitchen } 4051cf6e8fcSCyrille Pitchen 4061cf6e8fcSCyrille Pitchen /* set the RTS line state according to the mode */ 4071cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 4081cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 4091cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSEN; 4101cf6e8fcSCyrille Pitchen 4111cf6e8fcSCyrille Pitchen /* give the control of the RTS line back to the hardware */ 4121cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSDIS; 4131cf6e8fcSCyrille Pitchen } else { 4141cf6e8fcSCyrille Pitchen /* force RTS line to high level */ 4151cf6e8fcSCyrille Pitchen rts_paused = ATMEL_US_RTSDIS; 4161cf6e8fcSCyrille Pitchen 4171cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 4181cf6e8fcSCyrille Pitchen rts_ready = ATMEL_US_RTSEN; 4191cf6e8fcSCyrille Pitchen } 4201cf6e8fcSCyrille Pitchen 421ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_RTS) 4221cf6e8fcSCyrille Pitchen control |= rts_ready; 423ab4382d2SGreg Kroah-Hartman else 4241cf6e8fcSCyrille Pitchen control |= rts_paused; 425ab4382d2SGreg Kroah-Hartman 426ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_DTR) 427ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTREN; 428ab4382d2SGreg Kroah-Hartman else 429ab4382d2SGreg Kroah-Hartman control |= ATMEL_US_DTRDIS; 430ab4382d2SGreg Kroah-Hartman 4314e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, control); 432ab4382d2SGreg Kroah-Hartman 433e0b0baadSRichard Genoud mctrl_gpio_set(atmel_port->gpios, mctrl); 434e0b0baadSRichard Genoud 435ab4382d2SGreg Kroah-Hartman /* Local loopback mode? */ 4361cf6e8fcSCyrille Pitchen mode &= ~ATMEL_US_CHMODE; 437ab4382d2SGreg Kroah-Hartman if (mctrl & TIOCM_LOOP) 438ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_LOC_LOOP; 439ab4382d2SGreg Kroah-Hartman else 440ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHMODE_NORMAL; 441ab4382d2SGreg Kroah-Hartman 4424e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 443ab4382d2SGreg Kroah-Hartman } 444ab4382d2SGreg Kroah-Hartman 445ab4382d2SGreg Kroah-Hartman /* 446ab4382d2SGreg Kroah-Hartman * Get state of the modem control input lines 447ab4382d2SGreg Kroah-Hartman */ 448ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port) 449ab4382d2SGreg Kroah-Hartman { 450e0b0baadSRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 451e0b0baadSRichard Genoud unsigned int ret = 0, status; 452ab4382d2SGreg Kroah-Hartman 4534e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 454ab4382d2SGreg Kroah-Hartman 455ab4382d2SGreg Kroah-Hartman /* 456ab4382d2SGreg Kroah-Hartman * The control signals are active low. 457ab4382d2SGreg Kroah-Hartman */ 458ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DCD)) 459ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CD; 460ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_CTS)) 461ab4382d2SGreg Kroah-Hartman ret |= TIOCM_CTS; 462ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_DSR)) 463ab4382d2SGreg Kroah-Hartman ret |= TIOCM_DSR; 464ab4382d2SGreg Kroah-Hartman if (!(status & ATMEL_US_RI)) 465ab4382d2SGreg Kroah-Hartman ret |= TIOCM_RI; 466ab4382d2SGreg Kroah-Hartman 467e0b0baadSRichard Genoud return mctrl_gpio_get(atmel_port->gpios, &ret); 468ab4382d2SGreg Kroah-Hartman } 469ab4382d2SGreg Kroah-Hartman 470ab4382d2SGreg Kroah-Hartman /* 471ab4382d2SGreg Kroah-Hartman * Stop transmitting. 472ab4382d2SGreg Kroah-Hartman */ 473ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port) 474ab4382d2SGreg Kroah-Hartman { 475ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 476ab4382d2SGreg Kroah-Hartman 47764e22ebeSElen Song if (atmel_use_pdc_tx(port)) { 478ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 4794e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 480ab4382d2SGreg Kroah-Hartman } 48189d82324SRichard Genoud 48289d82324SRichard Genoud /* 48389d82324SRichard Genoud * Disable the transmitter. 48489d82324SRichard Genoud * This is mandatory when DMA is used, otherwise the DMA buffer 48589d82324SRichard Genoud * is fully transmitted. 48689d82324SRichard Genoud */ 48789d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS); 48889d82324SRichard Genoud 489ab4382d2SGreg Kroah-Hartman /* Disable interrupts */ 4904e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 491ab4382d2SGreg Kroah-Hartman 49213bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 49313bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 494ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 495ab4382d2SGreg Kroah-Hartman } 496ab4382d2SGreg Kroah-Hartman 497ab4382d2SGreg Kroah-Hartman /* 498ab4382d2SGreg Kroah-Hartman * Start transmitting. 499ab4382d2SGreg Kroah-Hartman */ 500ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port) 501ab4382d2SGreg Kroah-Hartman { 502ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 503ab4382d2SGreg Kroah-Hartman 5040058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR) 5050058f087SAlexandre Belloni & ATMEL_PDC_TXTEN)) 506ab4382d2SGreg Kroah-Hartman /* The transmitter is already running. Yes, we 507ab4382d2SGreg Kroah-Hartman really need this.*/ 508ab4382d2SGreg Kroah-Hartman return; 509ab4382d2SGreg Kroah-Hartman 5100058f087SAlexandre Belloni if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port)) 51113bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 51213bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 513ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 514ab4382d2SGreg Kroah-Hartman 5150058f087SAlexandre Belloni if (atmel_use_pdc_tx(port)) 516ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 5174e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 5180058f087SAlexandre Belloni 519ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 5204e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 52189d82324SRichard Genoud 52289d82324SRichard Genoud /* re-enable the transmitter */ 52389d82324SRichard Genoud atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 524ab4382d2SGreg Kroah-Hartman } 525ab4382d2SGreg Kroah-Hartman 526ab4382d2SGreg Kroah-Hartman /* 527ab4382d2SGreg Kroah-Hartman * start receiving - port is in process of being opened. 528ab4382d2SGreg Kroah-Hartman */ 529ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port) 530ab4382d2SGreg Kroah-Hartman { 5314e7decdaSCyrille Pitchen /* reset status and receiver */ 5324e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 533ab4382d2SGreg Kroah-Hartman 5344e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN); 53557c36868SSiftar, Gabe 53664e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 537ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 5384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 5394e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 540ab4382d2SGreg Kroah-Hartman port->read_status_mask); 5414e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 542ab4382d2SGreg Kroah-Hartman } else { 5434e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 544ab4382d2SGreg Kroah-Hartman } 545ab4382d2SGreg Kroah-Hartman } 546ab4382d2SGreg Kroah-Hartman 547ab4382d2SGreg Kroah-Hartman /* 548ab4382d2SGreg Kroah-Hartman * Stop receiving - port is in process of being closed. 549ab4382d2SGreg Kroah-Hartman */ 550ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port) 551ab4382d2SGreg Kroah-Hartman { 5524e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS); 55357c36868SSiftar, Gabe 55464e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 555ab4382d2SGreg Kroah-Hartman /* disable PDC receive */ 5564e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS); 5574e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 5584e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 559ab4382d2SGreg Kroah-Hartman port->read_status_mask); 560ab4382d2SGreg Kroah-Hartman } else { 5614e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY); 562ab4382d2SGreg Kroah-Hartman } 563ab4382d2SGreg Kroah-Hartman } 564ab4382d2SGreg Kroah-Hartman 565ab4382d2SGreg Kroah-Hartman /* 566ab4382d2SGreg Kroah-Hartman * Enable modem status interrupts 567ab4382d2SGreg Kroah-Hartman */ 568ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port) 569ab4382d2SGreg Kroah-Hartman { 570ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 571ab5e4e41SRichard Genoud uint32_t ier = 0; 572ab5e4e41SRichard Genoud 573ab5e4e41SRichard Genoud /* 574ab5e4e41SRichard Genoud * Interrupt should not be enabled twice 575ab5e4e41SRichard Genoud */ 576ab5e4e41SRichard Genoud if (atmel_port->ms_irq_enabled) 577ab5e4e41SRichard Genoud return; 578ab5e4e41SRichard Genoud 579ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = true; 580ab5e4e41SRichard Genoud 58118dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 582ab5e4e41SRichard Genoud ier |= ATMEL_US_CTSIC; 583ab5e4e41SRichard Genoud 58418dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 585ab5e4e41SRichard Genoud ier |= ATMEL_US_DSRIC; 586ab5e4e41SRichard Genoud 58718dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 588ab5e4e41SRichard Genoud ier |= ATMEL_US_RIIC; 589ab5e4e41SRichard Genoud 59018dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 591ab5e4e41SRichard Genoud ier |= ATMEL_US_DCDIC; 592ab5e4e41SRichard Genoud 5934e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ier); 59418dfef9cSUwe Kleine-König 59518dfef9cSUwe Kleine-König mctrl_gpio_enable_ms(atmel_port->gpios); 596ab4382d2SGreg Kroah-Hartman } 597ab4382d2SGreg Kroah-Hartman 598ab4382d2SGreg Kroah-Hartman /* 59935b675b9SRichard Genoud * Disable modem status interrupts 60035b675b9SRichard Genoud */ 60135b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port) 60235b675b9SRichard Genoud { 60335b675b9SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 60435b675b9SRichard Genoud uint32_t idr = 0; 60535b675b9SRichard Genoud 60635b675b9SRichard Genoud /* 60735b675b9SRichard Genoud * Interrupt should not be disabled twice 60835b675b9SRichard Genoud */ 60935b675b9SRichard Genoud if (!atmel_port->ms_irq_enabled) 61035b675b9SRichard Genoud return; 61135b675b9SRichard Genoud 61235b675b9SRichard Genoud atmel_port->ms_irq_enabled = false; 61335b675b9SRichard Genoud 61418dfef9cSUwe Kleine-König mctrl_gpio_disable_ms(atmel_port->gpios); 61518dfef9cSUwe Kleine-König 61618dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 61735b675b9SRichard Genoud idr |= ATMEL_US_CTSIC; 61835b675b9SRichard Genoud 61918dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 62035b675b9SRichard Genoud idr |= ATMEL_US_DSRIC; 62135b675b9SRichard Genoud 62218dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 62335b675b9SRichard Genoud idr |= ATMEL_US_RIIC; 62435b675b9SRichard Genoud 62518dfef9cSUwe Kleine-König if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 62635b675b9SRichard Genoud idr |= ATMEL_US_DCDIC; 62735b675b9SRichard Genoud 6284e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, idr); 62935b675b9SRichard Genoud } 63035b675b9SRichard Genoud 63135b675b9SRichard Genoud /* 632ab4382d2SGreg Kroah-Hartman * Control the transmission of a break signal 633ab4382d2SGreg Kroah-Hartman */ 634ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state) 635ab4382d2SGreg Kroah-Hartman { 636ab4382d2SGreg Kroah-Hartman if (break_state != 0) 6374e7decdaSCyrille Pitchen /* start break */ 6384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK); 639ab4382d2SGreg Kroah-Hartman else 6404e7decdaSCyrille Pitchen /* stop break */ 6414e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK); 642ab4382d2SGreg Kroah-Hartman } 643ab4382d2SGreg Kroah-Hartman 644ab4382d2SGreg Kroah-Hartman /* 645ab4382d2SGreg Kroah-Hartman * Stores the incoming character in the ring buffer 646ab4382d2SGreg Kroah-Hartman */ 647ab4382d2SGreg Kroah-Hartman static void 648ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 649ab4382d2SGreg Kroah-Hartman unsigned int ch) 650ab4382d2SGreg Kroah-Hartman { 651ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 652ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 653ab4382d2SGreg Kroah-Hartman struct atmel_uart_char *c; 654ab4382d2SGreg Kroah-Hartman 655ab4382d2SGreg Kroah-Hartman if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 656ab4382d2SGreg Kroah-Hartman /* Buffer overflow, ignore char */ 657ab4382d2SGreg Kroah-Hartman return; 658ab4382d2SGreg Kroah-Hartman 659ab4382d2SGreg Kroah-Hartman c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 660ab4382d2SGreg Kroah-Hartman c->status = status; 661ab4382d2SGreg Kroah-Hartman c->ch = ch; 662ab4382d2SGreg Kroah-Hartman 663ab4382d2SGreg Kroah-Hartman /* Make sure the character is stored before we update head. */ 664ab4382d2SGreg Kroah-Hartman smp_wmb(); 665ab4382d2SGreg Kroah-Hartman 666ab4382d2SGreg Kroah-Hartman ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 667ab4382d2SGreg Kroah-Hartman } 668ab4382d2SGreg Kroah-Hartman 669ab4382d2SGreg Kroah-Hartman /* 670ab4382d2SGreg Kroah-Hartman * Deal with parity, framing and overrun errors. 671ab4382d2SGreg Kroah-Hartman */ 672ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 673ab4382d2SGreg Kroah-Hartman { 674ab4382d2SGreg Kroah-Hartman /* clear error */ 6754e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 676ab4382d2SGreg Kroah-Hartman 677ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 678ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 679ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 680ab4382d2SGreg Kroah-Hartman port->icount.brk++; 681ab4382d2SGreg Kroah-Hartman } 682ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 683ab4382d2SGreg Kroah-Hartman port->icount.parity++; 684ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 685ab4382d2SGreg Kroah-Hartman port->icount.frame++; 686ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 687ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 688ab4382d2SGreg Kroah-Hartman } 689ab4382d2SGreg Kroah-Hartman 690ab4382d2SGreg Kroah-Hartman /* 691ab4382d2SGreg Kroah-Hartman * Characters received (called from interrupt handler) 692ab4382d2SGreg Kroah-Hartman */ 693ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port) 694ab4382d2SGreg Kroah-Hartman { 695ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 696ab4382d2SGreg Kroah-Hartman unsigned int status, ch; 697ab4382d2SGreg Kroah-Hartman 6984e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 699ab4382d2SGreg Kroah-Hartman while (status & ATMEL_US_RXRDY) { 700a6499435SCyrille Pitchen ch = atmel_uart_read_char(port); 701ab4382d2SGreg Kroah-Hartman 702ab4382d2SGreg Kroah-Hartman /* 703ab4382d2SGreg Kroah-Hartman * note that the error handling code is 704ab4382d2SGreg Kroah-Hartman * out of the main execution path 705ab4382d2SGreg Kroah-Hartman */ 706ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 707ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK) 708ab4382d2SGreg Kroah-Hartman || atmel_port->break_active)) { 709ab4382d2SGreg Kroah-Hartman 710ab4382d2SGreg Kroah-Hartman /* clear error */ 7114e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 712ab4382d2SGreg Kroah-Hartman 713ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK 714ab4382d2SGreg Kroah-Hartman && !atmel_port->break_active) { 715ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 1; 7164e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 7174e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 718ab4382d2SGreg Kroah-Hartman } else { 719ab4382d2SGreg Kroah-Hartman /* 720ab4382d2SGreg Kroah-Hartman * This is either the end-of-break 721ab4382d2SGreg Kroah-Hartman * condition or we've received at 722ab4382d2SGreg Kroah-Hartman * least one character without RXBRK 723ab4382d2SGreg Kroah-Hartman * being set. In both cases, the next 724ab4382d2SGreg Kroah-Hartman * RXBRK will indicate start-of-break. 725ab4382d2SGreg Kroah-Hartman */ 7264e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 7274e7decdaSCyrille Pitchen ATMEL_US_RXBRK); 728ab4382d2SGreg Kroah-Hartman status &= ~ATMEL_US_RXBRK; 729ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 730ab4382d2SGreg Kroah-Hartman } 731ab4382d2SGreg Kroah-Hartman } 732ab4382d2SGreg Kroah-Hartman 733ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(port, status, ch); 7344e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 735ab4382d2SGreg Kroah-Hartman } 736ab4382d2SGreg Kroah-Hartman 73798f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 738ab4382d2SGreg Kroah-Hartman } 739ab4382d2SGreg Kroah-Hartman 740ab4382d2SGreg Kroah-Hartman /* 741ab4382d2SGreg Kroah-Hartman * Transmit characters (called from tasklet with TXRDY interrupt 742ab4382d2SGreg Kroah-Hartman * disabled) 743ab4382d2SGreg Kroah-Hartman */ 744ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port) 745ab4382d2SGreg Kroah-Hartman { 746ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 747ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 748ab4382d2SGreg Kroah-Hartman 7494e7decdaSCyrille Pitchen if (port->x_char && 7504e7decdaSCyrille Pitchen (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) { 751a6499435SCyrille Pitchen atmel_uart_write_char(port, port->x_char); 752ab4382d2SGreg Kroah-Hartman port->icount.tx++; 753ab4382d2SGreg Kroah-Hartman port->x_char = 0; 754ab4382d2SGreg Kroah-Hartman } 755ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 756ab4382d2SGreg Kroah-Hartman return; 757ab4382d2SGreg Kroah-Hartman 7584e7decdaSCyrille Pitchen while (atmel_uart_readl(port, ATMEL_US_CSR) & 7594e7decdaSCyrille Pitchen atmel_port->tx_done_mask) { 760a6499435SCyrille Pitchen atmel_uart_write_char(port, xmit->buf[xmit->tail]); 761ab4382d2SGreg Kroah-Hartman xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 762ab4382d2SGreg Kroah-Hartman port->icount.tx++; 763ab4382d2SGreg Kroah-Hartman if (uart_circ_empty(xmit)) 764ab4382d2SGreg Kroah-Hartman break; 765ab4382d2SGreg Kroah-Hartman } 766ab4382d2SGreg Kroah-Hartman 767ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 768ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 769ab4382d2SGreg Kroah-Hartman 770ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit)) 771ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 7724e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 7734e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 774ab4382d2SGreg Kroah-Hartman } 775ab4382d2SGreg Kroah-Hartman 77608f738beSElen Song static void atmel_complete_tx_dma(void *arg) 77708f738beSElen Song { 77808f738beSElen Song struct atmel_uart_port *atmel_port = arg; 77908f738beSElen Song struct uart_port *port = &atmel_port->uart; 78008f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 78108f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 78208f738beSElen Song unsigned long flags; 78308f738beSElen Song 78408f738beSElen Song spin_lock_irqsave(&port->lock, flags); 78508f738beSElen Song 78608f738beSElen Song if (chan) 78708f738beSElen Song dmaengine_terminate_all(chan); 7885f258b3eSCyrille Pitchen xmit->tail += atmel_port->tx_len; 78908f738beSElen Song xmit->tail &= UART_XMIT_SIZE - 1; 79008f738beSElen Song 7915f258b3eSCyrille Pitchen port->icount.tx += atmel_port->tx_len; 79208f738beSElen Song 79308f738beSElen Song spin_lock_irq(&atmel_port->lock_tx); 79408f738beSElen Song async_tx_ack(atmel_port->desc_tx); 79508f738beSElen Song atmel_port->cookie_tx = -EINVAL; 79608f738beSElen Song atmel_port->desc_tx = NULL; 79708f738beSElen Song spin_unlock_irq(&atmel_port->lock_tx); 79808f738beSElen Song 79908f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 80008f738beSElen Song uart_write_wakeup(port); 80108f738beSElen Song 8021842dc2eSCyrille Pitchen /* 8031842dc2eSCyrille Pitchen * xmit is a circular buffer so, if we have just send data from 8041842dc2eSCyrille Pitchen * xmit->tail to the end of xmit->buf, now we have to transmit the 8051842dc2eSCyrille Pitchen * remaining data from the beginning of xmit->buf to xmit->head. 8061842dc2eSCyrille Pitchen */ 80708f738beSElen Song if (!uart_circ_empty(xmit)) 80898f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 809b389f173SRichard Genoud else if ((port->rs485.flags & SER_RS485_ENABLED) && 810b389f173SRichard Genoud !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 811b389f173SRichard Genoud /* DMA done, stop TX, start RX for RS485 */ 812b389f173SRichard Genoud atmel_start_rx(port); 813b389f173SRichard Genoud } 81408f738beSElen Song 81508f738beSElen Song spin_unlock_irqrestore(&port->lock, flags); 81608f738beSElen Song } 81708f738beSElen Song 81808f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port) 81908f738beSElen Song { 82008f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 82108f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 82208f738beSElen Song 82308f738beSElen Song if (chan) { 82408f738beSElen Song dmaengine_terminate_all(chan); 82508f738beSElen Song dma_release_channel(chan); 82608f738beSElen Song dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 82748479148SWolfram Sang DMA_TO_DEVICE); 82808f738beSElen Song } 82908f738beSElen Song 83008f738beSElen Song atmel_port->desc_tx = NULL; 83108f738beSElen Song atmel_port->chan_tx = NULL; 83208f738beSElen Song atmel_port->cookie_tx = -EINVAL; 83308f738beSElen Song } 83408f738beSElen Song 83508f738beSElen Song /* 83608f738beSElen Song * Called from tasklet with TXRDY interrupt is disabled. 83708f738beSElen Song */ 83808f738beSElen Song static void atmel_tx_dma(struct uart_port *port) 83908f738beSElen Song { 84008f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 84108f738beSElen Song struct circ_buf *xmit = &port->state->xmit; 84208f738beSElen Song struct dma_chan *chan = atmel_port->chan_tx; 84308f738beSElen Song struct dma_async_tx_descriptor *desc; 8445f258b3eSCyrille Pitchen struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx; 8455f258b3eSCyrille Pitchen unsigned int tx_len, part1_len, part2_len, sg_len; 8465f258b3eSCyrille Pitchen dma_addr_t phys_addr; 84708f738beSElen Song 84808f738beSElen Song /* Make sure we have an idle channel */ 84908f738beSElen Song if (atmel_port->desc_tx != NULL) 85008f738beSElen Song return; 85108f738beSElen Song 85208f738beSElen Song if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 85308f738beSElen Song /* 85408f738beSElen Song * DMA is idle now. 85508f738beSElen Song * Port xmit buffer is already mapped, 85608f738beSElen Song * and it is one page... Just adjust 85708f738beSElen Song * offsets and lengths. Since it is a circular buffer, 85808f738beSElen Song * we have to transmit till the end, and then the rest. 85908f738beSElen Song * Take the port lock to get a 86008f738beSElen Song * consistent xmit buffer state. 86108f738beSElen Song */ 8625f258b3eSCyrille Pitchen tx_len = CIRC_CNT_TO_END(xmit->head, 86308f738beSElen Song xmit->tail, 86408f738beSElen Song UART_XMIT_SIZE); 8655f258b3eSCyrille Pitchen 8665f258b3eSCyrille Pitchen if (atmel_port->fifo_size) { 8675f258b3eSCyrille Pitchen /* multi data mode */ 8685f258b3eSCyrille Pitchen part1_len = (tx_len & ~0x3); /* DWORD access */ 8695f258b3eSCyrille Pitchen part2_len = (tx_len & 0x3); /* BYTE access */ 8705f258b3eSCyrille Pitchen } else { 8715f258b3eSCyrille Pitchen /* single data (legacy) mode */ 8725f258b3eSCyrille Pitchen part1_len = 0; 8735f258b3eSCyrille Pitchen part2_len = tx_len; /* BYTE access only */ 8745f258b3eSCyrille Pitchen } 8755f258b3eSCyrille Pitchen 8765f258b3eSCyrille Pitchen sg_init_table(sgl, 2); 8775f258b3eSCyrille Pitchen sg_len = 0; 8785f258b3eSCyrille Pitchen phys_addr = sg_dma_address(sg_tx) + xmit->tail; 8795f258b3eSCyrille Pitchen if (part1_len) { 8805f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 8815f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 8825f258b3eSCyrille Pitchen sg_dma_len(sg) = part1_len; 8835f258b3eSCyrille Pitchen 8845f258b3eSCyrille Pitchen phys_addr += part1_len; 8855f258b3eSCyrille Pitchen } 8865f258b3eSCyrille Pitchen 8875f258b3eSCyrille Pitchen if (part2_len) { 8885f258b3eSCyrille Pitchen sg = &sgl[sg_len++]; 8895f258b3eSCyrille Pitchen sg_dma_address(sg) = phys_addr; 8905f258b3eSCyrille Pitchen sg_dma_len(sg) = part2_len; 8915f258b3eSCyrille Pitchen } 8925f258b3eSCyrille Pitchen 8935f258b3eSCyrille Pitchen /* 8945f258b3eSCyrille Pitchen * save tx_len so atmel_complete_tx_dma() will increase 8955f258b3eSCyrille Pitchen * xmit->tail correctly 8965f258b3eSCyrille Pitchen */ 8975f258b3eSCyrille Pitchen atmel_port->tx_len = tx_len; 89808f738beSElen Song 89908f738beSElen Song desc = dmaengine_prep_slave_sg(chan, 9005f258b3eSCyrille Pitchen sgl, 9015f258b3eSCyrille Pitchen sg_len, 90208f738beSElen Song DMA_MEM_TO_DEV, 90308f738beSElen Song DMA_PREP_INTERRUPT | 90408f738beSElen Song DMA_CTRL_ACK); 90508f738beSElen Song if (!desc) { 90608f738beSElen Song dev_err(port->dev, "Failed to send via dma!\n"); 90708f738beSElen Song return; 90808f738beSElen Song } 90908f738beSElen Song 9105f258b3eSCyrille Pitchen dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE); 91108f738beSElen Song 91208f738beSElen Song atmel_port->desc_tx = desc; 91308f738beSElen Song desc->callback = atmel_complete_tx_dma; 91408f738beSElen Song desc->callback_param = atmel_port; 91508f738beSElen Song atmel_port->cookie_tx = dmaengine_submit(desc); 91608f738beSElen Song } 91708f738beSElen Song 91808f738beSElen Song if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 91908f738beSElen Song uart_write_wakeup(port); 92008f738beSElen Song } 92108f738beSElen Song 92208f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port) 92308f738beSElen Song { 92408f738beSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 92508f738beSElen Song dma_cap_mask_t mask; 92608f738beSElen Song struct dma_slave_config config; 92708f738beSElen Song int ret, nent; 92808f738beSElen Song 92908f738beSElen Song dma_cap_zero(mask); 93008f738beSElen Song dma_cap_set(DMA_SLAVE, mask); 93108f738beSElen Song 93208f738beSElen Song atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); 93308f738beSElen Song if (atmel_port->chan_tx == NULL) 93408f738beSElen Song goto chan_err; 93508f738beSElen Song dev_info(port->dev, "using %s for tx DMA transfers\n", 93608f738beSElen Song dma_chan_name(atmel_port->chan_tx)); 93708f738beSElen Song 93808f738beSElen Song spin_lock_init(&atmel_port->lock_tx); 93908f738beSElen Song sg_init_table(&atmel_port->sg_tx, 1); 94008f738beSElen Song /* UART circular tx buffer is an aligned page. */ 9412c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); 94208f738beSElen Song sg_set_page(&atmel_port->sg_tx, 94308f738beSElen Song virt_to_page(port->state->xmit.buf), 94408f738beSElen Song UART_XMIT_SIZE, 9452b5cf14bSGeliang Tang offset_in_page(port->state->xmit.buf)); 94608f738beSElen Song nent = dma_map_sg(port->dev, 94708f738beSElen Song &atmel_port->sg_tx, 94808f738beSElen Song 1, 94948479148SWolfram Sang DMA_TO_DEVICE); 95008f738beSElen Song 95108f738beSElen Song if (!nent) { 95208f738beSElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 95308f738beSElen Song goto chan_err; 95408f738beSElen Song } else { 955c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 95608f738beSElen Song sg_dma_len(&atmel_port->sg_tx), 95708f738beSElen Song port->state->xmit.buf, 958c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_tx)); 95908f738beSElen Song } 96008f738beSElen Song 96108f738beSElen Song /* Configure the slave DMA */ 96208f738beSElen Song memset(&config, 0, sizeof(config)); 96308f738beSElen Song config.direction = DMA_MEM_TO_DEV; 9645f258b3eSCyrille Pitchen config.dst_addr_width = (atmel_port->fifo_size) ? 9655f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_4_BYTES : 9665f258b3eSCyrille Pitchen DMA_SLAVE_BUSWIDTH_1_BYTE; 96708f738beSElen Song config.dst_addr = port->mapbase + ATMEL_US_THR; 968a8d4e016SLudovic Desroches config.dst_maxburst = 1; 96908f738beSElen Song 9705483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_tx, 9715483c10eSMaxime Ripard &config); 97208f738beSElen Song if (ret) { 97308f738beSElen Song dev_err(port->dev, "DMA tx slave configuration failed\n"); 97408f738beSElen Song goto chan_err; 97508f738beSElen Song } 97608f738beSElen Song 97708f738beSElen Song return 0; 97808f738beSElen Song 97908f738beSElen Song chan_err: 98008f738beSElen Song dev_err(port->dev, "TX channel not available, switch to pio\n"); 98108f738beSElen Song atmel_port->use_dma_tx = 0; 98208f738beSElen Song if (atmel_port->chan_tx) 98308f738beSElen Song atmel_release_tx_dma(port); 98408f738beSElen Song return -EINVAL; 98508f738beSElen Song } 98608f738beSElen Song 98734df42f5SElen Song static void atmel_complete_rx_dma(void *arg) 98834df42f5SElen Song { 98934df42f5SElen Song struct uart_port *port = arg; 99034df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 99134df42f5SElen Song 99298f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 99334df42f5SElen Song } 99434df42f5SElen Song 99534df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port) 99634df42f5SElen Song { 99734df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 99834df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 99934df42f5SElen Song 100034df42f5SElen Song if (chan) { 100134df42f5SElen Song dmaengine_terminate_all(chan); 100234df42f5SElen Song dma_release_channel(chan); 100334df42f5SElen Song dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 100448479148SWolfram Sang DMA_FROM_DEVICE); 100534df42f5SElen Song } 100634df42f5SElen Song 100734df42f5SElen Song atmel_port->desc_rx = NULL; 100834df42f5SElen Song atmel_port->chan_rx = NULL; 100934df42f5SElen Song atmel_port->cookie_rx = -EINVAL; 101034df42f5SElen Song } 101134df42f5SElen Song 101234df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port) 101334df42f5SElen Song { 101434df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 101566f37aafSCyrille Pitchen struct tty_port *tport = &port->state->port; 101634df42f5SElen Song struct circ_buf *ring = &atmel_port->rx_ring; 101734df42f5SElen Song struct dma_chan *chan = atmel_port->chan_rx; 101834df42f5SElen Song struct dma_tx_state state; 101934df42f5SElen Song enum dma_status dmastat; 102066f37aafSCyrille Pitchen size_t count; 102134df42f5SElen Song 102234df42f5SElen Song 102334df42f5SElen Song /* Reset the UART timeout early so that we don't miss one */ 10244e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 102534df42f5SElen Song dmastat = dmaengine_tx_status(chan, 102634df42f5SElen Song atmel_port->cookie_rx, 102734df42f5SElen Song &state); 102834df42f5SElen Song /* Restart a new tasklet if DMA status is error */ 102934df42f5SElen Song if (dmastat == DMA_ERROR) { 103034df42f5SElen Song dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 10314e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 103298f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 103334df42f5SElen Song return; 103434df42f5SElen Song } 103566f37aafSCyrille Pitchen 103666f37aafSCyrille Pitchen /* CPU claims ownership of RX DMA buffer */ 103766f37aafSCyrille Pitchen dma_sync_sg_for_cpu(port->dev, 103866f37aafSCyrille Pitchen &atmel_port->sg_rx, 103966f37aafSCyrille Pitchen 1, 1040485819b5SCyrille Pitchen DMA_FROM_DEVICE); 104134df42f5SElen Song 104234df42f5SElen Song /* 104366f37aafSCyrille Pitchen * ring->head points to the end of data already written by the DMA. 104466f37aafSCyrille Pitchen * ring->tail points to the beginning of data to be read by the 104566f37aafSCyrille Pitchen * framework. 104666f37aafSCyrille Pitchen * The current transfer size should not be larger than the dma buffer 104766f37aafSCyrille Pitchen * length. 104834df42f5SElen Song */ 104966f37aafSCyrille Pitchen ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; 105066f37aafSCyrille Pitchen BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); 105166f37aafSCyrille Pitchen /* 105266f37aafSCyrille Pitchen * At this point ring->head may point to the first byte right after the 105366f37aafSCyrille Pitchen * last byte of the dma buffer: 105466f37aafSCyrille Pitchen * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) 105566f37aafSCyrille Pitchen * 105666f37aafSCyrille Pitchen * However ring->tail must always points inside the dma buffer: 105766f37aafSCyrille Pitchen * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 105866f37aafSCyrille Pitchen * 105966f37aafSCyrille Pitchen * Since we use a ring buffer, we have to handle the case 106066f37aafSCyrille Pitchen * where head is lower than tail. In such a case, we first read from 106166f37aafSCyrille Pitchen * tail to the end of the buffer then reset tail. 106266f37aafSCyrille Pitchen */ 106366f37aafSCyrille Pitchen if (ring->head < ring->tail) { 106466f37aafSCyrille Pitchen count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; 106534df42f5SElen Song 106666f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 106766f37aafSCyrille Pitchen ring->tail = 0; 106834df42f5SElen Song port->icount.rx += count; 106934df42f5SElen Song } 107034df42f5SElen Song 107166f37aafSCyrille Pitchen /* Finally we read data from tail to head */ 107266f37aafSCyrille Pitchen if (ring->tail < ring->head) { 107366f37aafSCyrille Pitchen count = ring->head - ring->tail; 107466f37aafSCyrille Pitchen 107566f37aafSCyrille Pitchen tty_insert_flip_string(tport, ring->buf + ring->tail, count); 107666f37aafSCyrille Pitchen /* Wrap ring->head if needed */ 107766f37aafSCyrille Pitchen if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) 107866f37aafSCyrille Pitchen ring->head = 0; 107966f37aafSCyrille Pitchen ring->tail = ring->head; 108066f37aafSCyrille Pitchen port->icount.rx += count; 108166f37aafSCyrille Pitchen } 108266f37aafSCyrille Pitchen 108366f37aafSCyrille Pitchen /* USART retreives ownership of RX DMA buffer */ 108466f37aafSCyrille Pitchen dma_sync_sg_for_device(port->dev, 108566f37aafSCyrille Pitchen &atmel_port->sg_rx, 108666f37aafSCyrille Pitchen 1, 1087485819b5SCyrille Pitchen DMA_FROM_DEVICE); 108866f37aafSCyrille Pitchen 108966f37aafSCyrille Pitchen /* 109066f37aafSCyrille Pitchen * Drop the lock here since it might end up calling 109166f37aafSCyrille Pitchen * uart_start(), which takes the lock. 109266f37aafSCyrille Pitchen */ 109366f37aafSCyrille Pitchen spin_unlock(&port->lock); 109466f37aafSCyrille Pitchen tty_flip_buffer_push(tport); 109566f37aafSCyrille Pitchen spin_lock(&port->lock); 109666f37aafSCyrille Pitchen 10974e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 109834df42f5SElen Song } 109934df42f5SElen Song 110034df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port) 110134df42f5SElen Song { 110234df42f5SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 110334df42f5SElen Song struct dma_async_tx_descriptor *desc; 110434df42f5SElen Song dma_cap_mask_t mask; 110534df42f5SElen Song struct dma_slave_config config; 110634df42f5SElen Song struct circ_buf *ring; 110734df42f5SElen Song int ret, nent; 110834df42f5SElen Song 110934df42f5SElen Song ring = &atmel_port->rx_ring; 111034df42f5SElen Song 111134df42f5SElen Song dma_cap_zero(mask); 111234df42f5SElen Song dma_cap_set(DMA_CYCLIC, mask); 111334df42f5SElen Song 111434df42f5SElen Song atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); 111534df42f5SElen Song if (atmel_port->chan_rx == NULL) 111634df42f5SElen Song goto chan_err; 111734df42f5SElen Song dev_info(port->dev, "using %s for rx DMA transfers\n", 111834df42f5SElen Song dma_chan_name(atmel_port->chan_rx)); 111934df42f5SElen Song 112034df42f5SElen Song spin_lock_init(&atmel_port->lock_rx); 112134df42f5SElen Song sg_init_table(&atmel_port->sg_rx, 1); 112234df42f5SElen Song /* UART circular rx buffer is an aligned page. */ 11232c277054SLeilei Zhao BUG_ON(!PAGE_ALIGNED(ring->buf)); 112434df42f5SElen Song sg_set_page(&atmel_port->sg_rx, 112534df42f5SElen Song virt_to_page(ring->buf), 1126a510880fSLeilei Zhao sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE, 11272b5cf14bSGeliang Tang offset_in_page(ring->buf)); 112834df42f5SElen Song nent = dma_map_sg(port->dev, 112934df42f5SElen Song &atmel_port->sg_rx, 113034df42f5SElen Song 1, 113148479148SWolfram Sang DMA_FROM_DEVICE); 113234df42f5SElen Song 113334df42f5SElen Song if (!nent) { 113434df42f5SElen Song dev_dbg(port->dev, "need to release resource of dma\n"); 113534df42f5SElen Song goto chan_err; 113634df42f5SElen Song } else { 1137c8d1f022SUwe Kleine-König dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 113834df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 113934df42f5SElen Song ring->buf, 1140c8d1f022SUwe Kleine-König &sg_dma_address(&atmel_port->sg_rx)); 114134df42f5SElen Song } 114234df42f5SElen Song 114334df42f5SElen Song /* Configure the slave DMA */ 114434df42f5SElen Song memset(&config, 0, sizeof(config)); 114534df42f5SElen Song config.direction = DMA_DEV_TO_MEM; 114634df42f5SElen Song config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 114734df42f5SElen Song config.src_addr = port->mapbase + ATMEL_US_RHR; 1148a8d4e016SLudovic Desroches config.src_maxburst = 1; 114934df42f5SElen Song 11505483c10eSMaxime Ripard ret = dmaengine_slave_config(atmel_port->chan_rx, 11515483c10eSMaxime Ripard &config); 115234df42f5SElen Song if (ret) { 115334df42f5SElen Song dev_err(port->dev, "DMA rx slave configuration failed\n"); 115434df42f5SElen Song goto chan_err; 115534df42f5SElen Song } 115634df42f5SElen Song /* 115734df42f5SElen Song * Prepare a cyclic dma transfer, assign 2 descriptors, 115834df42f5SElen Song * each one is half ring buffer size 115934df42f5SElen Song */ 116034df42f5SElen Song desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 116134df42f5SElen Song sg_dma_address(&atmel_port->sg_rx), 116234df42f5SElen Song sg_dma_len(&atmel_port->sg_rx), 116334df42f5SElen Song sg_dma_len(&atmel_port->sg_rx)/2, 116434df42f5SElen Song DMA_DEV_TO_MEM, 116534df42f5SElen Song DMA_PREP_INTERRUPT); 116634df42f5SElen Song desc->callback = atmel_complete_rx_dma; 116734df42f5SElen Song desc->callback_param = port; 116834df42f5SElen Song atmel_port->desc_rx = desc; 116934df42f5SElen Song atmel_port->cookie_rx = dmaengine_submit(desc); 117034df42f5SElen Song 117134df42f5SElen Song return 0; 117234df42f5SElen Song 117334df42f5SElen Song chan_err: 117434df42f5SElen Song dev_err(port->dev, "RX channel not available, switch to pio\n"); 117534df42f5SElen Song atmel_port->use_dma_rx = 0; 117634df42f5SElen Song if (atmel_port->chan_rx) 117734df42f5SElen Song atmel_release_rx_dma(port); 117834df42f5SElen Song return -EINVAL; 117934df42f5SElen Song } 118034df42f5SElen Song 11812e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data) 11822e68c22fSElen Song { 11832e68c22fSElen Song struct uart_port *port = (void *)data; 11842e68c22fSElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 11852e68c22fSElen Song 118698f2082cSNicolas Ferre if (!atomic_read(&atmel_port->tasklet_shutdown)) { 118700e8e658SNicolas Ferre tasklet_schedule(&atmel_port->tasklet_rx); 118898f2082cSNicolas Ferre mod_timer(&atmel_port->uart_timer, 118998f2082cSNicolas Ferre jiffies + uart_poll_timeout(port)); 119098f2082cSNicolas Ferre } 11912e68c22fSElen Song } 11922e68c22fSElen Song 1193ab4382d2SGreg Kroah-Hartman /* 1194ab4382d2SGreg Kroah-Hartman * receive interrupt handler. 1195ab4382d2SGreg Kroah-Hartman */ 1196ab4382d2SGreg Kroah-Hartman static void 1197ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending) 1198ab4382d2SGreg Kroah-Hartman { 1199ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1200ab4382d2SGreg Kroah-Hartman 120164e22ebeSElen Song if (atmel_use_pdc_rx(port)) { 1202ab4382d2SGreg Kroah-Hartman /* 1203ab4382d2SGreg Kroah-Hartman * PDC receive. Just schedule the tasklet and let it 1204ab4382d2SGreg Kroah-Hartman * figure out the details. 1205ab4382d2SGreg Kroah-Hartman * 1206ab4382d2SGreg Kroah-Hartman * TODO: We're not handling error flags correctly at 1207ab4382d2SGreg Kroah-Hartman * the moment. 1208ab4382d2SGreg Kroah-Hartman */ 1209ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 12104e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 12114e7decdaSCyrille Pitchen (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)); 121298f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 121398f2082cSNicolas Ferre &atmel_port->tasklet_rx); 1214ab4382d2SGreg Kroah-Hartman } 1215ab4382d2SGreg Kroah-Hartman 1216ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1217ab4382d2SGreg Kroah-Hartman ATMEL_US_FRAME | ATMEL_US_PARE)) 1218ab4382d2SGreg Kroah-Hartman atmel_pdc_rxerr(port, pending); 1219ab4382d2SGreg Kroah-Hartman } 1220ab4382d2SGreg Kroah-Hartman 122134df42f5SElen Song if (atmel_use_dma_rx(port)) { 122234df42f5SElen Song if (pending & ATMEL_US_TIMEOUT) { 12234e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 12244e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 122598f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, 122698f2082cSNicolas Ferre &atmel_port->tasklet_rx); 122734df42f5SElen Song } 122834df42f5SElen Song } 122934df42f5SElen Song 1230ab4382d2SGreg Kroah-Hartman /* Interrupt receive */ 1231ab4382d2SGreg Kroah-Hartman if (pending & ATMEL_US_RXRDY) 1232ab4382d2SGreg Kroah-Hartman atmel_rx_chars(port); 1233ab4382d2SGreg Kroah-Hartman else if (pending & ATMEL_US_RXBRK) { 1234ab4382d2SGreg Kroah-Hartman /* 1235ab4382d2SGreg Kroah-Hartman * End of break detected. If it came along with a 1236ab4382d2SGreg Kroah-Hartman * character, atmel_rx_chars will handle it. 1237ab4382d2SGreg Kroah-Hartman */ 12384e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 12394e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK); 1240ab4382d2SGreg Kroah-Hartman atmel_port->break_active = 0; 1241ab4382d2SGreg Kroah-Hartman } 1242ab4382d2SGreg Kroah-Hartman } 1243ab4382d2SGreg Kroah-Hartman 1244ab4382d2SGreg Kroah-Hartman /* 1245ab4382d2SGreg Kroah-Hartman * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1246ab4382d2SGreg Kroah-Hartman */ 1247ab4382d2SGreg Kroah-Hartman static void 1248ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1249ab4382d2SGreg Kroah-Hartman { 1250ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1251ab4382d2SGreg Kroah-Hartman 1252ab4382d2SGreg Kroah-Hartman if (pending & atmel_port->tx_done_mask) { 1253ab4382d2SGreg Kroah-Hartman /* Either PDC or interrupt transmission */ 12544e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 12554e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 125698f2082cSNicolas Ferre atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 1257ab4382d2SGreg Kroah-Hartman } 1258ab4382d2SGreg Kroah-Hartman } 1259ab4382d2SGreg Kroah-Hartman 1260ab4382d2SGreg Kroah-Hartman /* 1261ab4382d2SGreg Kroah-Hartman * status flags interrupt handler. 1262ab4382d2SGreg Kroah-Hartman */ 1263ab4382d2SGreg Kroah-Hartman static void 1264ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending, 1265ab4382d2SGreg Kroah-Hartman unsigned int status) 1266ab4382d2SGreg Kroah-Hartman { 1267ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 12689205218eSNicolas Ferre unsigned int status_change; 1269ab4382d2SGreg Kroah-Hartman 1270ab4382d2SGreg Kroah-Hartman if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1271ab4382d2SGreg Kroah-Hartman | ATMEL_US_CTSIC)) { 12729205218eSNicolas Ferre status_change = status ^ atmel_port->irq_status_prev; 1273d033e82dSLeilei Zhao atmel_port->irq_status_prev = status; 12749205218eSNicolas Ferre 12759205218eSNicolas Ferre if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 12769205218eSNicolas Ferre | ATMEL_US_DCD | ATMEL_US_CTS)) { 12779205218eSNicolas Ferre /* TODO: All reads to CSR will clear these interrupts! */ 12789205218eSNicolas Ferre if (status_change & ATMEL_US_RI) 12799205218eSNicolas Ferre port->icount.rng++; 12809205218eSNicolas Ferre if (status_change & ATMEL_US_DSR) 12819205218eSNicolas Ferre port->icount.dsr++; 12829205218eSNicolas Ferre if (status_change & ATMEL_US_DCD) 12839205218eSNicolas Ferre uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 12849205218eSNicolas Ferre if (status_change & ATMEL_US_CTS) 12859205218eSNicolas Ferre uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 12869205218eSNicolas Ferre 12879205218eSNicolas Ferre wake_up_interruptible(&port->state->port.delta_msr_wait); 12889205218eSNicolas Ferre } 1289ab4382d2SGreg Kroah-Hartman } 1290ab4382d2SGreg Kroah-Hartman } 1291ab4382d2SGreg Kroah-Hartman 1292ab4382d2SGreg Kroah-Hartman /* 1293ab4382d2SGreg Kroah-Hartman * Interrupt handler 1294ab4382d2SGreg Kroah-Hartman */ 1295ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1296ab4382d2SGreg Kroah-Hartman { 1297ab4382d2SGreg Kroah-Hartman struct uart_port *port = dev_id; 1298ab5e4e41SRichard Genoud struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 12992c7af5baSBoris BREZILLON unsigned int status, pending, mask, pass_counter = 0; 1300ab4382d2SGreg Kroah-Hartman 13012c7af5baSBoris BREZILLON spin_lock(&atmel_port->lock_suspended); 13022c7af5baSBoris BREZILLON 1303ab4382d2SGreg Kroah-Hartman do { 1304e0b0baadSRichard Genoud status = atmel_get_lines_status(port); 13054e7decdaSCyrille Pitchen mask = atmel_uart_readl(port, ATMEL_US_IMR); 13062c7af5baSBoris BREZILLON pending = status & mask; 1307ab4382d2SGreg Kroah-Hartman if (!pending) 1308ab4382d2SGreg Kroah-Hartman break; 1309ab4382d2SGreg Kroah-Hartman 13102c7af5baSBoris BREZILLON if (atmel_port->suspended) { 13112c7af5baSBoris BREZILLON atmel_port->pending |= pending; 13122c7af5baSBoris BREZILLON atmel_port->pending_status = status; 13134e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, mask); 13142c7af5baSBoris BREZILLON pm_system_wakeup(); 13152c7af5baSBoris BREZILLON break; 13162c7af5baSBoris BREZILLON } 13172c7af5baSBoris BREZILLON 1318ab4382d2SGreg Kroah-Hartman atmel_handle_receive(port, pending); 1319ab4382d2SGreg Kroah-Hartman atmel_handle_status(port, pending, status); 1320ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(port, pending); 1321ab4382d2SGreg Kroah-Hartman } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1322ab4382d2SGreg Kroah-Hartman 13232c7af5baSBoris BREZILLON spin_unlock(&atmel_port->lock_suspended); 13242c7af5baSBoris BREZILLON 1325ab4382d2SGreg Kroah-Hartman return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1326ab4382d2SGreg Kroah-Hartman } 1327ab4382d2SGreg Kroah-Hartman 1328a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port) 1329a930e528SElen Song { 1330a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1331a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1332a930e528SElen Song 1333a930e528SElen Song dma_unmap_single(port->dev, 1334a930e528SElen Song pdc->dma_addr, 1335a930e528SElen Song pdc->dma_size, 1336a930e528SElen Song DMA_TO_DEVICE); 1337a930e528SElen Song } 1338a930e528SElen Song 1339ab4382d2SGreg Kroah-Hartman /* 1340ab4382d2SGreg Kroah-Hartman * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1341ab4382d2SGreg Kroah-Hartman */ 134264e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port) 1343ab4382d2SGreg Kroah-Hartman { 1344ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1345ab4382d2SGreg Kroah-Hartman struct circ_buf *xmit = &port->state->xmit; 1346ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1347ab4382d2SGreg Kroah-Hartman int count; 1348ab4382d2SGreg Kroah-Hartman 1349ab4382d2SGreg Kroah-Hartman /* nothing left to transmit? */ 13504e7decdaSCyrille Pitchen if (atmel_uart_readl(port, ATMEL_PDC_TCR)) 1351ab4382d2SGreg Kroah-Hartman return; 1352ab4382d2SGreg Kroah-Hartman 1353ab4382d2SGreg Kroah-Hartman xmit->tail += pdc->ofs; 1354ab4382d2SGreg Kroah-Hartman xmit->tail &= UART_XMIT_SIZE - 1; 1355ab4382d2SGreg Kroah-Hartman 1356ab4382d2SGreg Kroah-Hartman port->icount.tx += pdc->ofs; 1357ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 1358ab4382d2SGreg Kroah-Hartman 1359ab4382d2SGreg Kroah-Hartman /* more to transmit - setup next transfer */ 1360ab4382d2SGreg Kroah-Hartman 1361ab4382d2SGreg Kroah-Hartman /* disable PDC transmit */ 13624e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 1363ab4382d2SGreg Kroah-Hartman 1364ab4382d2SGreg Kroah-Hartman if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1365ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, 1366ab4382d2SGreg Kroah-Hartman pdc->dma_addr, 1367ab4382d2SGreg Kroah-Hartman pdc->dma_size, 1368ab4382d2SGreg Kroah-Hartman DMA_TO_DEVICE); 1369ab4382d2SGreg Kroah-Hartman 1370ab4382d2SGreg Kroah-Hartman count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1371ab4382d2SGreg Kroah-Hartman pdc->ofs = count; 1372ab4382d2SGreg Kroah-Hartman 13734e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TPR, 13744e7decdaSCyrille Pitchen pdc->dma_addr + xmit->tail); 13754e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, count); 1376ab4382d2SGreg Kroah-Hartman /* re-enable PDC transmit */ 13774e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 1378ab4382d2SGreg Kroah-Hartman /* Enable interrupts */ 13794e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 13804e7decdaSCyrille Pitchen atmel_port->tx_done_mask); 1381ab4382d2SGreg Kroah-Hartman } else { 138213bd3e6fSRicardo Ribalda Delgado if ((port->rs485.flags & SER_RS485_ENABLED) && 138313bd3e6fSRicardo Ribalda Delgado !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 1384ab4382d2SGreg Kroah-Hartman /* DMA done, stop TX, start RX for RS485 */ 1385ab4382d2SGreg Kroah-Hartman atmel_start_rx(port); 1386ab4382d2SGreg Kroah-Hartman } 1387ab4382d2SGreg Kroah-Hartman } 1388ab4382d2SGreg Kroah-Hartman 1389ab4382d2SGreg Kroah-Hartman if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1390ab4382d2SGreg Kroah-Hartman uart_write_wakeup(port); 1391ab4382d2SGreg Kroah-Hartman } 1392ab4382d2SGreg Kroah-Hartman 1393a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port) 1394a930e528SElen Song { 1395a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1396a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1397a930e528SElen Song struct circ_buf *xmit = &port->state->xmit; 1398a930e528SElen Song 1399a930e528SElen Song pdc->buf = xmit->buf; 1400a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1401a930e528SElen Song pdc->buf, 1402a930e528SElen Song UART_XMIT_SIZE, 1403a930e528SElen Song DMA_TO_DEVICE); 1404a930e528SElen Song pdc->dma_size = UART_XMIT_SIZE; 1405a930e528SElen Song pdc->ofs = 0; 1406a930e528SElen Song 1407a930e528SElen Song return 0; 1408a930e528SElen Song } 1409a930e528SElen Song 1410ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port) 1411ab4382d2SGreg Kroah-Hartman { 1412ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1413ab4382d2SGreg Kroah-Hartman struct circ_buf *ring = &atmel_port->rx_ring; 1414ab4382d2SGreg Kroah-Hartman unsigned int flg; 1415ab4382d2SGreg Kroah-Hartman unsigned int status; 1416ab4382d2SGreg Kroah-Hartman 1417ab4382d2SGreg Kroah-Hartman while (ring->head != ring->tail) { 1418ab4382d2SGreg Kroah-Hartman struct atmel_uart_char c; 1419ab4382d2SGreg Kroah-Hartman 1420ab4382d2SGreg Kroah-Hartman /* Make sure c is loaded after head. */ 1421ab4382d2SGreg Kroah-Hartman smp_rmb(); 1422ab4382d2SGreg Kroah-Hartman 1423ab4382d2SGreg Kroah-Hartman c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1424ab4382d2SGreg Kroah-Hartman 1425ab4382d2SGreg Kroah-Hartman ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1426ab4382d2SGreg Kroah-Hartman 1427ab4382d2SGreg Kroah-Hartman port->icount.rx++; 1428ab4382d2SGreg Kroah-Hartman status = c.status; 1429ab4382d2SGreg Kroah-Hartman flg = TTY_NORMAL; 1430ab4382d2SGreg Kroah-Hartman 1431ab4382d2SGreg Kroah-Hartman /* 1432ab4382d2SGreg Kroah-Hartman * note that the error handling code is 1433ab4382d2SGreg Kroah-Hartman * out of the main execution path 1434ab4382d2SGreg Kroah-Hartman */ 1435ab4382d2SGreg Kroah-Hartman if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1436ab4382d2SGreg Kroah-Hartman | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1437ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) { 1438ab4382d2SGreg Kroah-Hartman /* ignore side-effect */ 1439ab4382d2SGreg Kroah-Hartman status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1440ab4382d2SGreg Kroah-Hartman 1441ab4382d2SGreg Kroah-Hartman port->icount.brk++; 1442ab4382d2SGreg Kroah-Hartman if (uart_handle_break(port)) 1443ab4382d2SGreg Kroah-Hartman continue; 1444ab4382d2SGreg Kroah-Hartman } 1445ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_PARE) 1446ab4382d2SGreg Kroah-Hartman port->icount.parity++; 1447ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_FRAME) 1448ab4382d2SGreg Kroah-Hartman port->icount.frame++; 1449ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_OVRE) 1450ab4382d2SGreg Kroah-Hartman port->icount.overrun++; 1451ab4382d2SGreg Kroah-Hartman 1452ab4382d2SGreg Kroah-Hartman status &= port->read_status_mask; 1453ab4382d2SGreg Kroah-Hartman 1454ab4382d2SGreg Kroah-Hartman if (status & ATMEL_US_RXBRK) 1455ab4382d2SGreg Kroah-Hartman flg = TTY_BREAK; 1456ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_PARE) 1457ab4382d2SGreg Kroah-Hartman flg = TTY_PARITY; 1458ab4382d2SGreg Kroah-Hartman else if (status & ATMEL_US_FRAME) 1459ab4382d2SGreg Kroah-Hartman flg = TTY_FRAME; 1460ab4382d2SGreg Kroah-Hartman } 1461ab4382d2SGreg Kroah-Hartman 1462ab4382d2SGreg Kroah-Hartman 1463ab4382d2SGreg Kroah-Hartman if (uart_handle_sysrq_char(port, c.ch)) 1464ab4382d2SGreg Kroah-Hartman continue; 1465ab4382d2SGreg Kroah-Hartman 1466ab4382d2SGreg Kroah-Hartman uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1467ab4382d2SGreg Kroah-Hartman } 1468ab4382d2SGreg Kroah-Hartman 1469ab4382d2SGreg Kroah-Hartman /* 1470ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1471ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1472ab4382d2SGreg Kroah-Hartman */ 1473ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 14742e124b4aSJiri Slaby tty_flip_buffer_push(&port->state->port); 1475ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1476ab4382d2SGreg Kroah-Hartman } 1477ab4382d2SGreg Kroah-Hartman 1478a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port) 1479a930e528SElen Song { 1480a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1481a930e528SElen Song int i; 1482a930e528SElen Song 1483a930e528SElen Song for (i = 0; i < 2; i++) { 1484a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1485a930e528SElen Song 1486a930e528SElen Song dma_unmap_single(port->dev, 1487a930e528SElen Song pdc->dma_addr, 1488a930e528SElen Song pdc->dma_size, 1489a930e528SElen Song DMA_FROM_DEVICE); 1490a930e528SElen Song kfree(pdc->buf); 1491a930e528SElen Song } 1492a930e528SElen Song } 1493a930e528SElen Song 149464e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port) 1495ab4382d2SGreg Kroah-Hartman { 1496ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 149705c7cd39SJiri Slaby struct tty_port *tport = &port->state->port; 1498ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer *pdc; 1499ab4382d2SGreg Kroah-Hartman int rx_idx = atmel_port->pdc_rx_idx; 1500ab4382d2SGreg Kroah-Hartman unsigned int head; 1501ab4382d2SGreg Kroah-Hartman unsigned int tail; 1502ab4382d2SGreg Kroah-Hartman unsigned int count; 1503ab4382d2SGreg Kroah-Hartman 1504ab4382d2SGreg Kroah-Hartman do { 1505ab4382d2SGreg Kroah-Hartman /* Reset the UART timeout early so that we don't miss one */ 15064e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1507ab4382d2SGreg Kroah-Hartman 1508ab4382d2SGreg Kroah-Hartman pdc = &atmel_port->pdc_rx[rx_idx]; 15094e7decdaSCyrille Pitchen head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr; 1510ab4382d2SGreg Kroah-Hartman tail = pdc->ofs; 1511ab4382d2SGreg Kroah-Hartman 1512ab4382d2SGreg Kroah-Hartman /* If the PDC has switched buffers, RPR won't contain 1513ab4382d2SGreg Kroah-Hartman * any address within the current buffer. Since head 1514ab4382d2SGreg Kroah-Hartman * is unsigned, we just need a one-way comparison to 1515ab4382d2SGreg Kroah-Hartman * find out. 1516ab4382d2SGreg Kroah-Hartman * 1517ab4382d2SGreg Kroah-Hartman * In this case, we just need to consume the entire 1518ab4382d2SGreg Kroah-Hartman * buffer and resubmit it for DMA. This will clear the 1519ab4382d2SGreg Kroah-Hartman * ENDRX bit as well, so that we can safely re-enable 1520ab4382d2SGreg Kroah-Hartman * all interrupts below. 1521ab4382d2SGreg Kroah-Hartman */ 1522ab4382d2SGreg Kroah-Hartman head = min(head, pdc->dma_size); 1523ab4382d2SGreg Kroah-Hartman 1524ab4382d2SGreg Kroah-Hartman if (likely(head != tail)) { 1525ab4382d2SGreg Kroah-Hartman dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1526ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1527ab4382d2SGreg Kroah-Hartman 1528ab4382d2SGreg Kroah-Hartman /* 1529ab4382d2SGreg Kroah-Hartman * head will only wrap around when we recycle 1530ab4382d2SGreg Kroah-Hartman * the DMA buffer, and when that happens, we 1531ab4382d2SGreg Kroah-Hartman * explicitly set tail to 0. So head will 1532ab4382d2SGreg Kroah-Hartman * always be greater than tail. 1533ab4382d2SGreg Kroah-Hartman */ 1534ab4382d2SGreg Kroah-Hartman count = head - tail; 1535ab4382d2SGreg Kroah-Hartman 153605c7cd39SJiri Slaby tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 153705c7cd39SJiri Slaby count); 1538ab4382d2SGreg Kroah-Hartman 1539ab4382d2SGreg Kroah-Hartman dma_sync_single_for_device(port->dev, pdc->dma_addr, 1540ab4382d2SGreg Kroah-Hartman pdc->dma_size, DMA_FROM_DEVICE); 1541ab4382d2SGreg Kroah-Hartman 1542ab4382d2SGreg Kroah-Hartman port->icount.rx += count; 1543ab4382d2SGreg Kroah-Hartman pdc->ofs = head; 1544ab4382d2SGreg Kroah-Hartman } 1545ab4382d2SGreg Kroah-Hartman 1546ab4382d2SGreg Kroah-Hartman /* 1547ab4382d2SGreg Kroah-Hartman * If the current buffer is full, we need to check if 1548ab4382d2SGreg Kroah-Hartman * the next one contains any additional data. 1549ab4382d2SGreg Kroah-Hartman */ 1550ab4382d2SGreg Kroah-Hartman if (head >= pdc->dma_size) { 1551ab4382d2SGreg Kroah-Hartman pdc->ofs = 0; 15524e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr); 15534e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size); 1554ab4382d2SGreg Kroah-Hartman 1555ab4382d2SGreg Kroah-Hartman rx_idx = !rx_idx; 1556ab4382d2SGreg Kroah-Hartman atmel_port->pdc_rx_idx = rx_idx; 1557ab4382d2SGreg Kroah-Hartman } 1558ab4382d2SGreg Kroah-Hartman } while (head >= pdc->dma_size); 1559ab4382d2SGreg Kroah-Hartman 1560ab4382d2SGreg Kroah-Hartman /* 1561ab4382d2SGreg Kroah-Hartman * Drop the lock here since it might end up calling 1562ab4382d2SGreg Kroah-Hartman * uart_start(), which takes the lock. 1563ab4382d2SGreg Kroah-Hartman */ 1564ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 15652e124b4aSJiri Slaby tty_flip_buffer_push(tport); 1566ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1567ab4382d2SGreg Kroah-Hartman 15684e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 15694e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1570ab4382d2SGreg Kroah-Hartman } 1571ab4382d2SGreg Kroah-Hartman 1572a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port) 1573a930e528SElen Song { 1574a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1575a930e528SElen Song int i; 1576a930e528SElen Song 1577a930e528SElen Song for (i = 0; i < 2; i++) { 1578a930e528SElen Song struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1579a930e528SElen Song 1580a930e528SElen Song pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1581a930e528SElen Song if (pdc->buf == NULL) { 1582a930e528SElen Song if (i != 0) { 1583a930e528SElen Song dma_unmap_single(port->dev, 1584a930e528SElen Song atmel_port->pdc_rx[0].dma_addr, 1585a930e528SElen Song PDC_BUFFER_SIZE, 1586a930e528SElen Song DMA_FROM_DEVICE); 1587a930e528SElen Song kfree(atmel_port->pdc_rx[0].buf); 1588a930e528SElen Song } 1589a930e528SElen Song atmel_port->use_pdc_rx = 0; 1590a930e528SElen Song return -ENOMEM; 1591a930e528SElen Song } 1592a930e528SElen Song pdc->dma_addr = dma_map_single(port->dev, 1593a930e528SElen Song pdc->buf, 1594a930e528SElen Song PDC_BUFFER_SIZE, 1595a930e528SElen Song DMA_FROM_DEVICE); 1596a930e528SElen Song pdc->dma_size = PDC_BUFFER_SIZE; 1597a930e528SElen Song pdc->ofs = 0; 1598a930e528SElen Song } 1599a930e528SElen Song 1600a930e528SElen Song atmel_port->pdc_rx_idx = 0; 1601a930e528SElen Song 16024e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr); 16034e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE); 1604a930e528SElen Song 16054e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNPR, 16064e7decdaSCyrille Pitchen atmel_port->pdc_rx[1].dma_addr); 16074e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE); 1608a930e528SElen Song 1609a930e528SElen Song return 0; 1610a930e528SElen Song } 1611a930e528SElen Song 1612ab4382d2SGreg Kroah-Hartman /* 1613ab4382d2SGreg Kroah-Hartman * tasklet handling tty stuff outside the interrupt handler. 1614ab4382d2SGreg Kroah-Hartman */ 161500e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data) 1616ab4382d2SGreg Kroah-Hartman { 1617ab4382d2SGreg Kroah-Hartman struct uart_port *port = (struct uart_port *)data; 1618ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1619ab4382d2SGreg Kroah-Hartman 1620ab4382d2SGreg Kroah-Hartman /* The interrupt handler does not take the lock */ 1621ab4382d2SGreg Kroah-Hartman spin_lock(&port->lock); 1622a930e528SElen Song atmel_port->schedule_rx(port); 162300e8e658SNicolas Ferre spin_unlock(&port->lock); 162400e8e658SNicolas Ferre } 1625ab4382d2SGreg Kroah-Hartman 162600e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data) 162700e8e658SNicolas Ferre { 162800e8e658SNicolas Ferre struct uart_port *port = (struct uart_port *)data; 162900e8e658SNicolas Ferre struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 163000e8e658SNicolas Ferre 163100e8e658SNicolas Ferre /* The interrupt handler does not take the lock */ 163200e8e658SNicolas Ferre spin_lock(&port->lock); 163300e8e658SNicolas Ferre atmel_port->schedule_tx(port); 1634ab4382d2SGreg Kroah-Hartman spin_unlock(&port->lock); 1635ab4382d2SGreg Kroah-Hartman } 1636ab4382d2SGreg Kroah-Hartman 16374a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port, 163833d64c4fSElen Song struct platform_device *pdev) 163933d64c4fSElen Song { 164033d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 164133d64c4fSElen Song 164233d64c4fSElen Song /* DMA/PDC usage specification */ 1643490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-rx")) { 1644490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 164533d64c4fSElen Song atmel_port->use_dma_rx = true; 164633d64c4fSElen Song atmel_port->use_pdc_rx = false; 164733d64c4fSElen Song } else { 164833d64c4fSElen Song atmel_port->use_dma_rx = false; 164933d64c4fSElen Song atmel_port->use_pdc_rx = true; 165033d64c4fSElen Song } 165133d64c4fSElen Song } else { 165233d64c4fSElen Song atmel_port->use_dma_rx = false; 165333d64c4fSElen Song atmel_port->use_pdc_rx = false; 165433d64c4fSElen Song } 165533d64c4fSElen Song 1656490d5ce2SJulia Lawall if (of_property_read_bool(np, "atmel,use-dma-tx")) { 1657490d5ce2SJulia Lawall if (of_property_read_bool(np, "dmas")) { 165833d64c4fSElen Song atmel_port->use_dma_tx = true; 165933d64c4fSElen Song atmel_port->use_pdc_tx = false; 166033d64c4fSElen Song } else { 166133d64c4fSElen Song atmel_port->use_dma_tx = false; 166233d64c4fSElen Song atmel_port->use_pdc_tx = true; 166333d64c4fSElen Song } 166433d64c4fSElen Song } else { 166533d64c4fSElen Song atmel_port->use_dma_tx = false; 166633d64c4fSElen Song atmel_port->use_pdc_tx = false; 166733d64c4fSElen Song } 166833d64c4fSElen Song } 166933d64c4fSElen Song 167013bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port, 167133d64c4fSElen Song struct platform_device *pdev) 167233d64c4fSElen Song { 167333d64c4fSElen Song struct device_node *np = pdev->dev.of_node; 167433d64c4fSElen Song 167577bdec6fSJiri Slaby struct serial_rs485 *rs485conf = &port->rs485; 167633d64c4fSElen Song u32 rs485_delay[2]; 1677*92c8f7c0SAlexandre Belloni 167833d64c4fSElen Song /* rs485 properties */ 167933d64c4fSElen Song if (of_property_read_u32_array(np, "rs485-rts-delay", 168033d64c4fSElen Song rs485_delay, 2) == 0) { 168133d64c4fSElen Song rs485conf->delay_rts_before_send = rs485_delay[0]; 168233d64c4fSElen Song rs485conf->delay_rts_after_send = rs485_delay[1]; 168333d64c4fSElen Song rs485conf->flags = 0; 168477bdec6fSJiri Slaby } 168533d64c4fSElen Song 168633d64c4fSElen Song if (of_get_property(np, "rs485-rx-during-tx", NULL)) 168733d64c4fSElen Song rs485conf->flags |= SER_RS485_RX_DURING_TX; 168833d64c4fSElen Song 1689*92c8f7c0SAlexandre Belloni if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL)) 169033d64c4fSElen Song rs485conf->flags |= SER_RS485_ENABLED; 169133d64c4fSElen Song } 169233d64c4fSElen Song 1693a930e528SElen Song static void atmel_set_ops(struct uart_port *port) 1694a930e528SElen Song { 1695a930e528SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1696a930e528SElen Song 169734df42f5SElen Song if (atmel_use_dma_rx(port)) { 169834df42f5SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_dma; 169934df42f5SElen Song atmel_port->schedule_rx = &atmel_rx_from_dma; 170034df42f5SElen Song atmel_port->release_rx = &atmel_release_rx_dma; 170134df42f5SElen Song } else if (atmel_use_pdc_rx(port)) { 1702a930e528SElen Song atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1703a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_pdc; 1704a930e528SElen Song atmel_port->release_rx = &atmel_release_rx_pdc; 1705a930e528SElen Song } else { 1706a930e528SElen Song atmel_port->prepare_rx = NULL; 1707a930e528SElen Song atmel_port->schedule_rx = &atmel_rx_from_ring; 1708a930e528SElen Song atmel_port->release_rx = NULL; 1709a930e528SElen Song } 1710a930e528SElen Song 171108f738beSElen Song if (atmel_use_dma_tx(port)) { 171208f738beSElen Song atmel_port->prepare_tx = &atmel_prepare_tx_dma; 171308f738beSElen Song atmel_port->schedule_tx = &atmel_tx_dma; 171408f738beSElen Song atmel_port->release_tx = &atmel_release_tx_dma; 171508f738beSElen Song } else if (atmel_use_pdc_tx(port)) { 1716a930e528SElen Song atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1717a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_pdc; 1718a930e528SElen Song atmel_port->release_tx = &atmel_release_tx_pdc; 1719a930e528SElen Song } else { 1720a930e528SElen Song atmel_port->prepare_tx = NULL; 1721a930e528SElen Song atmel_port->schedule_tx = &atmel_tx_chars; 1722a930e528SElen Song atmel_port->release_tx = NULL; 1723a930e528SElen Song } 1724a930e528SElen Song } 1725a930e528SElen Song 1726ab4382d2SGreg Kroah-Hartman /* 1727055560b0SElen Song * Get ip name usart or uart 1728055560b0SElen Song */ 1729892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port) 1730055560b0SElen Song { 1731055560b0SElen Song struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 17324e7decdaSCyrille Pitchen int name = atmel_uart_readl(port, ATMEL_US_NAME); 1733731d9caeSNicolas Ferre u32 version; 17341d673fb9SNicolas Ferre u32 usart, dbgu_uart, new_uart; 17354b769371SNicolas Ferre /* ASCII decoding for IP version */ 17364b769371SNicolas Ferre usart = 0x55534152; /* USAR(T) */ 17374b769371SNicolas Ferre dbgu_uart = 0x44424755; /* DBGU */ 17381d673fb9SNicolas Ferre new_uart = 0x55415254; /* UART */ 1739055560b0SElen Song 17405bf5635aSLudovic Desroches /* 17415bf5635aSLudovic Desroches * Only USART devices from at91sam9260 SOC implement fractional 17422867af2dSRomain Izard * baudrate. It is available for all asynchronous modes, with the 17432867af2dSRomain Izard * following restriction: the sampling clock's duty cycle is not 17442867af2dSRomain Izard * constant. 17455bf5635aSLudovic Desroches */ 17465bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = false; 17474b769371SNicolas Ferre atmel_port->has_hw_timer = false; 1748055560b0SElen Song 17492958cceeSLudovic Desroches if (name == new_uart) { 17502958cceeSLudovic Desroches dev_dbg(port->dev, "Uart with hw timer"); 17514b769371SNicolas Ferre atmel_port->has_hw_timer = true; 17522958cceeSLudovic Desroches atmel_port->rtor = ATMEL_UA_RTOR; 17532958cceeSLudovic Desroches } else if (name == usart) { 17542958cceeSLudovic Desroches dev_dbg(port->dev, "Usart\n"); 17555bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 17562958cceeSLudovic Desroches atmel_port->has_hw_timer = true; 17572958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 17584b769371SNicolas Ferre } else if (name == dbgu_uart) { 17594b769371SNicolas Ferre dev_dbg(port->dev, "Dbgu or uart without hw timer\n"); 1760055560b0SElen Song } else { 1761731d9caeSNicolas Ferre /* fallback for older SoCs: use version field */ 17624e7decdaSCyrille Pitchen version = atmel_uart_readl(port, ATMEL_US_VERSION); 1763731d9caeSNicolas Ferre switch (version) { 1764731d9caeSNicolas Ferre case 0x302: 1765731d9caeSNicolas Ferre case 0x10213: 1766731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is usart\n"); 17675bf5635aSLudovic Desroches atmel_port->has_frac_baudrate = true; 17684b769371SNicolas Ferre atmel_port->has_hw_timer = true; 17692958cceeSLudovic Desroches atmel_port->rtor = ATMEL_US_RTOR; 1770731d9caeSNicolas Ferre break; 1771731d9caeSNicolas Ferre case 0x203: 1772731d9caeSNicolas Ferre case 0x10202: 1773731d9caeSNicolas Ferre dev_dbg(port->dev, "This version is uart\n"); 1774731d9caeSNicolas Ferre break; 1775731d9caeSNicolas Ferre default: 1776731d9caeSNicolas Ferre dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1777731d9caeSNicolas Ferre } 1778055560b0SElen Song } 1779055560b0SElen Song } 1780055560b0SElen Song 1781055560b0SElen Song /* 1782ab4382d2SGreg Kroah-Hartman * Perform initialization and enable port for reception 1783ab4382d2SGreg Kroah-Hartman */ 1784ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port) 1785ab4382d2SGreg Kroah-Hartman { 178633d64c4fSElen Song struct platform_device *pdev = to_platform_device(port->dev); 1787ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1788ab4382d2SGreg Kroah-Hartman struct tty_struct *tty = port->state->port.tty; 1789ab4382d2SGreg Kroah-Hartman int retval; 1790ab4382d2SGreg Kroah-Hartman 1791ab4382d2SGreg Kroah-Hartman /* 1792ab4382d2SGreg Kroah-Hartman * Ensure that no interrupts are enabled otherwise when 1793ab4382d2SGreg Kroah-Hartman * request_irq() is called we could get stuck trying to 1794ab4382d2SGreg Kroah-Hartman * handle an unexpected interrupt 1795ab4382d2SGreg Kroah-Hartman */ 17964e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 1797ab5e4e41SRichard Genoud atmel_port->ms_irq_enabled = false; 1798ab4382d2SGreg Kroah-Hartman 1799ab4382d2SGreg Kroah-Hartman /* 1800ab4382d2SGreg Kroah-Hartman * Allocate the IRQ 1801ab4382d2SGreg Kroah-Hartman */ 18022c7af5baSBoris BREZILLON retval = request_irq(port->irq, atmel_interrupt, 18032c7af5baSBoris BREZILLON IRQF_SHARED | IRQF_COND_SUSPEND, 1804ab4382d2SGreg Kroah-Hartman tty ? tty->name : "atmel_serial", port); 1805ab4382d2SGreg Kroah-Hartman if (retval) { 1806ddaa6037SRichard Genoud dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1807ab4382d2SGreg Kroah-Hartman return retval; 1808ab4382d2SGreg Kroah-Hartman } 1809ab4382d2SGreg Kroah-Hartman 181098f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 181198f2082cSNicolas Ferre tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func, 181298f2082cSNicolas Ferre (unsigned long)port); 181398f2082cSNicolas Ferre tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func, 181498f2082cSNicolas Ferre (unsigned long)port); 18151e125786SLeilei Zhao 1816ab5e4e41SRichard Genoud /* 1817ab4382d2SGreg Kroah-Hartman * Initialize DMA (if necessary) 1818ab4382d2SGreg Kroah-Hartman */ 181933d64c4fSElen Song atmel_init_property(atmel_port, pdev); 18204d9628a1SLeilei Zhao atmel_set_ops(port); 182133d64c4fSElen Song 1822a930e528SElen Song if (atmel_port->prepare_rx) { 1823a930e528SElen Song retval = atmel_port->prepare_rx(port); 1824a930e528SElen Song if (retval < 0) 1825a930e528SElen Song atmel_set_ops(port); 1826ab4382d2SGreg Kroah-Hartman } 1827ab4382d2SGreg Kroah-Hartman 1828a930e528SElen Song if (atmel_port->prepare_tx) { 1829a930e528SElen Song retval = atmel_port->prepare_tx(port); 1830a930e528SElen Song if (retval < 0) 1831a930e528SElen Song atmel_set_ops(port); 1832ab4382d2SGreg Kroah-Hartman } 1833ab4382d2SGreg Kroah-Hartman 1834b5199d46SCyrille Pitchen /* 1835b5199d46SCyrille Pitchen * Enable FIFO when available 1836b5199d46SCyrille Pitchen */ 1837b5199d46SCyrille Pitchen if (atmel_port->fifo_size) { 1838b5199d46SCyrille Pitchen unsigned int txrdym = ATMEL_US_ONE_DATA; 1839b5199d46SCyrille Pitchen unsigned int rxrdym = ATMEL_US_ONE_DATA; 1840b5199d46SCyrille Pitchen unsigned int fmr; 1841b5199d46SCyrille Pitchen 1842b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, 1843b5199d46SCyrille Pitchen ATMEL_US_FIFOEN | 1844b5199d46SCyrille Pitchen ATMEL_US_RXFCLR | 1845b5199d46SCyrille Pitchen ATMEL_US_TXFLCLR); 1846b5199d46SCyrille Pitchen 18475f258b3eSCyrille Pitchen if (atmel_use_dma_tx(port)) 18485f258b3eSCyrille Pitchen txrdym = ATMEL_US_FOUR_DATA; 18495f258b3eSCyrille Pitchen 1850b5199d46SCyrille Pitchen fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym); 1851b5199d46SCyrille Pitchen if (atmel_port->rts_high && 1852b5199d46SCyrille Pitchen atmel_port->rts_low) 1853b5199d46SCyrille Pitchen fmr |= ATMEL_US_FRTSC | 1854b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES(atmel_port->rts_high) | 1855b5199d46SCyrille Pitchen ATMEL_US_RXFTHRES2(atmel_port->rts_low); 1856b5199d46SCyrille Pitchen 1857b5199d46SCyrille Pitchen atmel_uart_writel(port, ATMEL_US_FMR, fmr); 1858b5199d46SCyrille Pitchen } 1859b5199d46SCyrille Pitchen 1860ab4382d2SGreg Kroah-Hartman /* Save current CSR for comparison in atmel_tasklet_func() */ 1861e0b0baadSRichard Genoud atmel_port->irq_status_prev = atmel_get_lines_status(port); 1862ab4382d2SGreg Kroah-Hartman 1863ab4382d2SGreg Kroah-Hartman /* 1864ab4382d2SGreg Kroah-Hartman * Finally, enable the serial port 1865ab4382d2SGreg Kroah-Hartman */ 18664e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1867ab4382d2SGreg Kroah-Hartman /* enable xmit & rcvr */ 18684e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 1869ab4382d2SGreg Kroah-Hartman 18702e68c22fSElen Song setup_timer(&atmel_port->uart_timer, 18712e68c22fSElen Song atmel_uart_timer_callback, 18722e68c22fSElen Song (unsigned long)port); 18738bc661bfSMarek Roszko 18748bc661bfSMarek Roszko if (atmel_use_pdc_rx(port)) { 18758bc661bfSMarek Roszko /* set UART timeout */ 18764b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 18772e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 18782e68c22fSElen Song jiffies + uart_poll_timeout(port)); 18792e68c22fSElen Song /* set USART timeout */ 18802e68c22fSElen Song } else { 18812958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 18822958cceeSLudovic Desroches PDC_RX_TIMEOUT); 18834e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1884ab4382d2SGreg Kroah-Hartman 18854e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 18864e7decdaSCyrille Pitchen ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 18872e68c22fSElen Song } 1888ab4382d2SGreg Kroah-Hartman /* enable PDC controller */ 18894e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 189034df42f5SElen Song } else if (atmel_use_dma_rx(port)) { 18912e68c22fSElen Song /* set UART timeout */ 18924b769371SNicolas Ferre if (!atmel_port->has_hw_timer) { 18932e68c22fSElen Song mod_timer(&atmel_port->uart_timer, 18942e68c22fSElen Song jiffies + uart_poll_timeout(port)); 18952e68c22fSElen Song /* set USART timeout */ 18962e68c22fSElen Song } else { 18972958cceeSLudovic Desroches atmel_uart_writel(port, atmel_port->rtor, 18982958cceeSLudovic Desroches PDC_RX_TIMEOUT); 18994e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 190034df42f5SElen Song 19014e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, 19024e7decdaSCyrille Pitchen ATMEL_US_TIMEOUT); 19032e68c22fSElen Song } 1904ab4382d2SGreg Kroah-Hartman } else { 1905ab4382d2SGreg Kroah-Hartman /* enable receive only */ 19064e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 1907ab4382d2SGreg Kroah-Hartman } 1908ab4382d2SGreg Kroah-Hartman 1909ab4382d2SGreg Kroah-Hartman return 0; 1910ab4382d2SGreg Kroah-Hartman } 1911ab4382d2SGreg Kroah-Hartman 1912ab4382d2SGreg Kroah-Hartman /* 1913479e9b94SPeter Hurley * Flush any TX data submitted for DMA. Called when the TX circular 1914479e9b94SPeter Hurley * buffer is reset. 1915479e9b94SPeter Hurley */ 1916479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port) 1917479e9b94SPeter Hurley { 1918479e9b94SPeter Hurley struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1919479e9b94SPeter Hurley 1920479e9b94SPeter Hurley if (atmel_use_pdc_tx(port)) { 19214e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_TCR, 0); 1922479e9b94SPeter Hurley atmel_port->pdc_tx.ofs = 0; 1923479e9b94SPeter Hurley } 192431ca2c63SRichard Genoud /* 192531ca2c63SRichard Genoud * in uart_flush_buffer(), the xmit circular buffer has just 192631ca2c63SRichard Genoud * been cleared, so we have to reset tx_len accordingly. 192731ca2c63SRichard Genoud */ 192831ca2c63SRichard Genoud atmel_port->tx_len = 0; 1929479e9b94SPeter Hurley } 1930479e9b94SPeter Hurley 1931479e9b94SPeter Hurley /* 1932ab4382d2SGreg Kroah-Hartman * Disable the port 1933ab4382d2SGreg Kroah-Hartman */ 1934ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port) 1935ab4382d2SGreg Kroah-Hartman { 1936ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 19370cc7c6c7SMarek Roszko 19380ae9fdefSRichard Genoud /* Disable modem control lines interrupts */ 19390ae9fdefSRichard Genoud atmel_disable_ms(port); 19400ae9fdefSRichard Genoud 194198f2082cSNicolas Ferre /* Disable interrupts at device level */ 194298f2082cSNicolas Ferre atmel_uart_writel(port, ATMEL_US_IDR, -1); 194398f2082cSNicolas Ferre 194498f2082cSNicolas Ferre /* Prevent spurious interrupts from scheduling the tasklet */ 194598f2082cSNicolas Ferre atomic_inc(&atmel_port->tasklet_shutdown); 194698f2082cSNicolas Ferre 1947ab4382d2SGreg Kroah-Hartman /* 19488bc661bfSMarek Roszko * Prevent any tasklets being scheduled during 19498bc661bfSMarek Roszko * cleanup 19508bc661bfSMarek Roszko */ 19518bc661bfSMarek Roszko del_timer_sync(&atmel_port->uart_timer); 19528bc661bfSMarek Roszko 195398f2082cSNicolas Ferre /* Make sure that no interrupt is on the fly */ 195498f2082cSNicolas Ferre synchronize_irq(port->irq); 195598f2082cSNicolas Ferre 19568bc661bfSMarek Roszko /* 19570cc7c6c7SMarek Roszko * Clear out any scheduled tasklets before 19580cc7c6c7SMarek Roszko * we destroy the buffers 19590cc7c6c7SMarek Roszko */ 196000e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 196100e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 19620cc7c6c7SMarek Roszko 19630cc7c6c7SMarek Roszko /* 19640cc7c6c7SMarek Roszko * Ensure everything is stopped and 196598f2082cSNicolas Ferre * disable port and break condition. 1966ab4382d2SGreg Kroah-Hartman */ 1967ab4382d2SGreg Kroah-Hartman atmel_stop_rx(port); 1968ab4382d2SGreg Kroah-Hartman atmel_stop_tx(port); 1969ab4382d2SGreg Kroah-Hartman 19704e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 19710cc7c6c7SMarek Roszko 1972ab4382d2SGreg Kroah-Hartman /* 1973ab4382d2SGreg Kroah-Hartman * Shut-down the DMA. 1974ab4382d2SGreg Kroah-Hartman */ 1975a930e528SElen Song if (atmel_port->release_rx) 1976a930e528SElen Song atmel_port->release_rx(port); 1977a930e528SElen Song if (atmel_port->release_tx) 1978a930e528SElen Song atmel_port->release_tx(port); 1979ab4382d2SGreg Kroah-Hartman 1980ab4382d2SGreg Kroah-Hartman /* 1981bb7e73c5SMark Deneen * Reset ring buffer pointers 1982bb7e73c5SMark Deneen */ 1983bb7e73c5SMark Deneen atmel_port->rx_ring.head = 0; 1984bb7e73c5SMark Deneen atmel_port->rx_ring.tail = 0; 1985bb7e73c5SMark Deneen 1986bb7e73c5SMark Deneen /* 1987ab5e4e41SRichard Genoud * Free the interrupts 1988ab4382d2SGreg Kroah-Hartman */ 1989ab4382d2SGreg Kroah-Hartman free_irq(port->irq, port); 1990ab5e4e41SRichard Genoud 1991479e9b94SPeter Hurley atmel_flush_buffer(port); 1992ab4382d2SGreg Kroah-Hartman } 1993ab4382d2SGreg Kroah-Hartman 1994ab4382d2SGreg Kroah-Hartman /* 1995ab4382d2SGreg Kroah-Hartman * Power / Clock management. 1996ab4382d2SGreg Kroah-Hartman */ 1997ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state, 1998ab4382d2SGreg Kroah-Hartman unsigned int oldstate) 1999ab4382d2SGreg Kroah-Hartman { 2000ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2001ab4382d2SGreg Kroah-Hartman 2002ab4382d2SGreg Kroah-Hartman switch (state) { 2003ab4382d2SGreg Kroah-Hartman case 0: 2004ab4382d2SGreg Kroah-Hartman /* 2005ab4382d2SGreg Kroah-Hartman * Enable the peripheral clock for this serial port. 2006ab4382d2SGreg Kroah-Hartman * This is called on uart_open() or a resume event. 2007ab4382d2SGreg Kroah-Hartman */ 200891f8c2d8SBoris BREZILLON clk_prepare_enable(atmel_port->clk); 2009ab4382d2SGreg Kroah-Hartman 2010ab4382d2SGreg Kroah-Hartman /* re-enable interrupts if we disabled some on suspend */ 20114e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr); 2012ab4382d2SGreg Kroah-Hartman break; 2013ab4382d2SGreg Kroah-Hartman case 3: 2014ab4382d2SGreg Kroah-Hartman /* Back up the interrupt mask and disable all interrupts */ 20154e7decdaSCyrille Pitchen atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR); 20164e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2017ab4382d2SGreg Kroah-Hartman 2018ab4382d2SGreg Kroah-Hartman /* 2019ab4382d2SGreg Kroah-Hartman * Disable the peripheral clock for this serial port. 2020ab4382d2SGreg Kroah-Hartman * This is called on uart_close() or a suspend event. 2021ab4382d2SGreg Kroah-Hartman */ 202291f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2023ab4382d2SGreg Kroah-Hartman break; 2024ab4382d2SGreg Kroah-Hartman default: 2025ddaa6037SRichard Genoud dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 2026ab4382d2SGreg Kroah-Hartman } 2027ab4382d2SGreg Kroah-Hartman } 2028ab4382d2SGreg Kroah-Hartman 2029ab4382d2SGreg Kroah-Hartman /* 2030ab4382d2SGreg Kroah-Hartman * Change the port parameters 2031ab4382d2SGreg Kroah-Hartman */ 2032ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 2033ab4382d2SGreg Kroah-Hartman struct ktermios *old) 2034ab4382d2SGreg Kroah-Hartman { 20355bf5635aSLudovic Desroches struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2036ab4382d2SGreg Kroah-Hartman unsigned long flags; 20375bf5635aSLudovic Desroches unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0; 2038ab4382d2SGreg Kroah-Hartman 20391cf6e8fcSCyrille Pitchen /* save the current mode register */ 20404e7decdaSCyrille Pitchen mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR); 20411cf6e8fcSCyrille Pitchen 20421cf6e8fcSCyrille Pitchen /* reset the mode, clock divisor, parity, stop bits and data size */ 20431cf6e8fcSCyrille Pitchen mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | 20441cf6e8fcSCyrille Pitchen ATMEL_US_PAR | ATMEL_US_USMODE); 2045ab4382d2SGreg Kroah-Hartman 2046ab4382d2SGreg Kroah-Hartman baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 2047ab4382d2SGreg Kroah-Hartman 2048ab4382d2SGreg Kroah-Hartman /* byte size */ 2049ab4382d2SGreg Kroah-Hartman switch (termios->c_cflag & CSIZE) { 2050ab4382d2SGreg Kroah-Hartman case CS5: 2051ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_5; 2052ab4382d2SGreg Kroah-Hartman break; 2053ab4382d2SGreg Kroah-Hartman case CS6: 2054ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_6; 2055ab4382d2SGreg Kroah-Hartman break; 2056ab4382d2SGreg Kroah-Hartman case CS7: 2057ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_7; 2058ab4382d2SGreg Kroah-Hartman break; 2059ab4382d2SGreg Kroah-Hartman default: 2060ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_CHRL_8; 2061ab4382d2SGreg Kroah-Hartman break; 2062ab4382d2SGreg Kroah-Hartman } 2063ab4382d2SGreg Kroah-Hartman 2064ab4382d2SGreg Kroah-Hartman /* stop bits */ 2065ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CSTOPB) 2066ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_NBSTOP_2; 2067ab4382d2SGreg Kroah-Hartman 2068ab4382d2SGreg Kroah-Hartman /* parity */ 2069ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARENB) { 2070ab4382d2SGreg Kroah-Hartman /* Mark or Space parity */ 2071ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & CMSPAR) { 2072ab4382d2SGreg Kroah-Hartman if (termios->c_cflag & PARODD) 2073ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_MARK; 2074ab4382d2SGreg Kroah-Hartman else 2075ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_SPACE; 2076ab4382d2SGreg Kroah-Hartman } else if (termios->c_cflag & PARODD) 2077ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_ODD; 2078ab4382d2SGreg Kroah-Hartman else 2079ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_EVEN; 2080ab4382d2SGreg Kroah-Hartman } else 2081ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_PAR_NONE; 2082ab4382d2SGreg Kroah-Hartman 2083ab4382d2SGreg Kroah-Hartman spin_lock_irqsave(&port->lock, flags); 2084ab4382d2SGreg Kroah-Hartman 2085ab4382d2SGreg Kroah-Hartman port->read_status_mask = ATMEL_US_OVRE; 2086ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & INPCK) 2087ab4382d2SGreg Kroah-Hartman port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2088ef8b9ddcSPeter Hurley if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2089ab4382d2SGreg Kroah-Hartman port->read_status_mask |= ATMEL_US_RXBRK; 2090ab4382d2SGreg Kroah-Hartman 209164e22ebeSElen Song if (atmel_use_pdc_rx(port)) 2092ab4382d2SGreg Kroah-Hartman /* need to enable error interrupts */ 20934e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask); 2094ab4382d2SGreg Kroah-Hartman 2095ab4382d2SGreg Kroah-Hartman /* 2096ab4382d2SGreg Kroah-Hartman * Characters to ignore 2097ab4382d2SGreg Kroah-Hartman */ 2098ab4382d2SGreg Kroah-Hartman port->ignore_status_mask = 0; 2099ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2100ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2101ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNBRK) { 2102ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_RXBRK; 2103ab4382d2SGreg Kroah-Hartman /* 2104ab4382d2SGreg Kroah-Hartman * If we're ignoring parity and break indicators, 2105ab4382d2SGreg Kroah-Hartman * ignore overruns too (for real raw support). 2106ab4382d2SGreg Kroah-Hartman */ 2107ab4382d2SGreg Kroah-Hartman if (termios->c_iflag & IGNPAR) 2108ab4382d2SGreg Kroah-Hartman port->ignore_status_mask |= ATMEL_US_OVRE; 2109ab4382d2SGreg Kroah-Hartman } 2110ab4382d2SGreg Kroah-Hartman /* TODO: Ignore all characters if CREAD is set.*/ 2111ab4382d2SGreg Kroah-Hartman 2112ab4382d2SGreg Kroah-Hartman /* update the per-port timeout */ 2113ab4382d2SGreg Kroah-Hartman uart_update_timeout(port, termios->c_cflag, baud); 2114ab4382d2SGreg Kroah-Hartman 2115ab4382d2SGreg Kroah-Hartman /* 2116ab4382d2SGreg Kroah-Hartman * save/disable interrupts. The tty layer will ensure that the 2117ab4382d2SGreg Kroah-Hartman * transmitter is empty if requested by the caller, so there's 2118ab4382d2SGreg Kroah-Hartman * no need to wait for it here. 2119ab4382d2SGreg Kroah-Hartman */ 21204e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 21214e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 2122ab4382d2SGreg Kroah-Hartman 2123ab4382d2SGreg Kroah-Hartman /* disable receiver and transmitter */ 21244e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 2125ab4382d2SGreg Kroah-Hartman 21261cf6e8fcSCyrille Pitchen /* mode */ 212713bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) { 21284e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_TTGR, 21294e7decdaSCyrille Pitchen port->rs485.delay_rts_after_send); 2130ab4382d2SGreg Kroah-Hartman mode |= ATMEL_US_USMODE_RS485; 21311cf6e8fcSCyrille Pitchen } else if (termios->c_cflag & CRTSCTS) { 21321cf6e8fcSCyrille Pitchen /* RS232 with hardware handshake (RTS/CTS) */ 21339bcffe75SRichard Genoud if (atmel_use_fifo(port) && 21349bcffe75SRichard Genoud !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) { 21359bcffe75SRichard Genoud /* 21369bcffe75SRichard Genoud * with ATMEL_US_USMODE_HWHS set, the controller will 21379bcffe75SRichard Genoud * be able to drive the RTS pin high/low when the RX 21389bcffe75SRichard Genoud * FIFO is above RXFTHRES/below RXFTHRES2. 21399bcffe75SRichard Genoud * It will also disable the transmitter when the CTS 21409bcffe75SRichard Genoud * pin is high. 21419bcffe75SRichard Genoud * This mode is not activated if CTS pin is a GPIO 21429bcffe75SRichard Genoud * because in this case, the transmitter is always 21439bcffe75SRichard Genoud * disabled (there must be an internal pull-up 21449bcffe75SRichard Genoud * responsible for this behaviour). 21459bcffe75SRichard Genoud * If the RTS pin is a GPIO, the controller won't be 21469bcffe75SRichard Genoud * able to drive it according to the FIFO thresholds, 21479bcffe75SRichard Genoud * but it will be handled by the driver. 21489bcffe75SRichard Genoud */ 21491cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_HWHS; 21509bcffe75SRichard Genoud } else { 21519bcffe75SRichard Genoud /* 21529bcffe75SRichard Genoud * For platforms without FIFO, the flow control is 21539bcffe75SRichard Genoud * handled by the driver. 21549bcffe75SRichard Genoud */ 21559bcffe75SRichard Genoud mode |= ATMEL_US_USMODE_NORMAL; 21565be605acSAlexandre Belloni } 21571cf6e8fcSCyrille Pitchen } else { 21581cf6e8fcSCyrille Pitchen /* RS232 without hadware handshake */ 21591cf6e8fcSCyrille Pitchen mode |= ATMEL_US_USMODE_NORMAL; 2160ab4382d2SGreg Kroah-Hartman } 2161ab4382d2SGreg Kroah-Hartman 21621cf6e8fcSCyrille Pitchen /* set the mode, clock divisor, parity, stop bits and data size */ 21634e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_MR, mode); 2164ab4382d2SGreg Kroah-Hartman 21651cf6e8fcSCyrille Pitchen /* 21661cf6e8fcSCyrille Pitchen * when switching the mode, set the RTS line state according to the 21671cf6e8fcSCyrille Pitchen * new mode, otherwise keep the former state 21681cf6e8fcSCyrille Pitchen */ 21691cf6e8fcSCyrille Pitchen if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) { 21701cf6e8fcSCyrille Pitchen unsigned int rts_state; 21711cf6e8fcSCyrille Pitchen 21721cf6e8fcSCyrille Pitchen if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 21731cf6e8fcSCyrille Pitchen /* let the hardware control the RTS line */ 21741cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSDIS; 21751cf6e8fcSCyrille Pitchen } else { 21761cf6e8fcSCyrille Pitchen /* force RTS line to low level */ 21771cf6e8fcSCyrille Pitchen rts_state = ATMEL_US_RTSEN; 21781cf6e8fcSCyrille Pitchen } 21791cf6e8fcSCyrille Pitchen 21804e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, rts_state); 21811cf6e8fcSCyrille Pitchen } 21821cf6e8fcSCyrille Pitchen 21835bf5635aSLudovic Desroches /* 21845bf5635aSLudovic Desroches * Set the baud rate: 21855bf5635aSLudovic Desroches * Fractional baudrate allows to setup output frequency more 21865bf5635aSLudovic Desroches * accurately. This feature is enabled only when using normal mode. 21875bf5635aSLudovic Desroches * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8)) 21885bf5635aSLudovic Desroches * Currently, OVER is always set to 0 so we get 218936131cdfSAlexey Starikovskiy * baudrate = selected clock / (16 * (CD + FP / 8)) 219036131cdfSAlexey Starikovskiy * then 219136131cdfSAlexey Starikovskiy * 8 CD + FP = selected clock / (2 * baudrate) 21925bf5635aSLudovic Desroches */ 21932867af2dSRomain Izard if (atmel_port->has_frac_baudrate) { 219436131cdfSAlexey Starikovskiy div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2); 219536131cdfSAlexey Starikovskiy cd = div >> 3; 219636131cdfSAlexey Starikovskiy fp = div & ATMEL_US_FP_MASK; 21975bf5635aSLudovic Desroches } else { 21985bf5635aSLudovic Desroches cd = uart_get_divisor(port, baud); 21995bf5635aSLudovic Desroches } 22005bf5635aSLudovic Desroches 22015bf5635aSLudovic Desroches if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 22025bf5635aSLudovic Desroches cd /= 8; 22035bf5635aSLudovic Desroches mode |= ATMEL_US_USCLKS_MCK_DIV8; 22045bf5635aSLudovic Desroches } 22055bf5635aSLudovic Desroches quot = cd | fp << ATMEL_US_FP_OFFSET; 22065bf5635aSLudovic Desroches 22074e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_BRGR, quot); 22084e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 22094e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2210ab4382d2SGreg Kroah-Hartman 2211ab4382d2SGreg Kroah-Hartman /* restore interrupts */ 22124e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2213ab4382d2SGreg Kroah-Hartman 2214ab4382d2SGreg Kroah-Hartman /* CTS flow-control and modem-status interrupts */ 2215ab4382d2SGreg Kroah-Hartman if (UART_ENABLE_MS(port, termios->c_cflag)) 221635b675b9SRichard Genoud atmel_enable_ms(port); 221735b675b9SRichard Genoud else 221835b675b9SRichard Genoud atmel_disable_ms(port); 2219ab4382d2SGreg Kroah-Hartman 2220ab4382d2SGreg Kroah-Hartman spin_unlock_irqrestore(&port->lock, flags); 2221ab4382d2SGreg Kroah-Hartman } 2222ab4382d2SGreg Kroah-Hartman 2223732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios) 222442bd7a4fSViktar Palstsiuk { 2225732a84a0SPeter Hurley if (termios->c_line == N_PPS) { 222642bd7a4fSViktar Palstsiuk port->flags |= UPF_HARDPPS_CD; 2227d41510ceSPeter Hurley spin_lock_irq(&port->lock); 222842bd7a4fSViktar Palstsiuk atmel_enable_ms(port); 2229d41510ceSPeter Hurley spin_unlock_irq(&port->lock); 223042bd7a4fSViktar Palstsiuk } else { 223142bd7a4fSViktar Palstsiuk port->flags &= ~UPF_HARDPPS_CD; 2232cab68f89SPeter Hurley if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2233cab68f89SPeter Hurley spin_lock_irq(&port->lock); 2234cab68f89SPeter Hurley atmel_disable_ms(port); 2235cab68f89SPeter Hurley spin_unlock_irq(&port->lock); 2236cab68f89SPeter Hurley } 223742bd7a4fSViktar Palstsiuk } 223842bd7a4fSViktar Palstsiuk } 223942bd7a4fSViktar Palstsiuk 2240ab4382d2SGreg Kroah-Hartman /* 2241ab4382d2SGreg Kroah-Hartman * Return string describing the specified port 2242ab4382d2SGreg Kroah-Hartman */ 2243ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port) 2244ab4382d2SGreg Kroah-Hartman { 2245ab4382d2SGreg Kroah-Hartman return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2246ab4382d2SGreg Kroah-Hartman } 2247ab4382d2SGreg Kroah-Hartman 2248ab4382d2SGreg Kroah-Hartman /* 2249ab4382d2SGreg Kroah-Hartman * Release the memory region(s) being used by 'port'. 2250ab4382d2SGreg Kroah-Hartman */ 2251ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port) 2252ab4382d2SGreg Kroah-Hartman { 2253ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2254ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2255ab4382d2SGreg Kroah-Hartman 2256ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2257ab4382d2SGreg Kroah-Hartman 2258ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2259ab4382d2SGreg Kroah-Hartman iounmap(port->membase); 2260ab4382d2SGreg Kroah-Hartman port->membase = NULL; 2261ab4382d2SGreg Kroah-Hartman } 2262ab4382d2SGreg Kroah-Hartman } 2263ab4382d2SGreg Kroah-Hartman 2264ab4382d2SGreg Kroah-Hartman /* 2265ab4382d2SGreg Kroah-Hartman * Request the memory region(s) being used by 'port'. 2266ab4382d2SGreg Kroah-Hartman */ 2267ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port) 2268ab4382d2SGreg Kroah-Hartman { 2269ab4382d2SGreg Kroah-Hartman struct platform_device *pdev = to_platform_device(port->dev); 2270ab4382d2SGreg Kroah-Hartman int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2271ab4382d2SGreg Kroah-Hartman 2272ab4382d2SGreg Kroah-Hartman if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2273ab4382d2SGreg Kroah-Hartman return -EBUSY; 2274ab4382d2SGreg Kroah-Hartman 2275ab4382d2SGreg Kroah-Hartman if (port->flags & UPF_IOREMAP) { 2276ab4382d2SGreg Kroah-Hartman port->membase = ioremap(port->mapbase, size); 2277ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2278ab4382d2SGreg Kroah-Hartman release_mem_region(port->mapbase, size); 2279ab4382d2SGreg Kroah-Hartman return -ENOMEM; 2280ab4382d2SGreg Kroah-Hartman } 2281ab4382d2SGreg Kroah-Hartman } 2282ab4382d2SGreg Kroah-Hartman 2283ab4382d2SGreg Kroah-Hartman return 0; 2284ab4382d2SGreg Kroah-Hartman } 2285ab4382d2SGreg Kroah-Hartman 2286ab4382d2SGreg Kroah-Hartman /* 2287ab4382d2SGreg Kroah-Hartman * Configure/autoconfigure the port. 2288ab4382d2SGreg Kroah-Hartman */ 2289ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags) 2290ab4382d2SGreg Kroah-Hartman { 2291ab4382d2SGreg Kroah-Hartman if (flags & UART_CONFIG_TYPE) { 2292ab4382d2SGreg Kroah-Hartman port->type = PORT_ATMEL; 2293ab4382d2SGreg Kroah-Hartman atmel_request_port(port); 2294ab4382d2SGreg Kroah-Hartman } 2295ab4382d2SGreg Kroah-Hartman } 2296ab4382d2SGreg Kroah-Hartman 2297ab4382d2SGreg Kroah-Hartman /* 2298ab4382d2SGreg Kroah-Hartman * Verify the new serial_struct (for TIOCSSERIAL). 2299ab4382d2SGreg Kroah-Hartman */ 2300ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2301ab4382d2SGreg Kroah-Hartman { 2302ab4382d2SGreg Kroah-Hartman int ret = 0; 2303ab4382d2SGreg Kroah-Hartman if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2304ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2305ab4382d2SGreg Kroah-Hartman if (port->irq != ser->irq) 2306ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2307ab4382d2SGreg Kroah-Hartman if (ser->io_type != SERIAL_IO_MEM) 2308ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2309ab4382d2SGreg Kroah-Hartman if (port->uartclk / 16 != ser->baud_base) 2310ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2311270c2adeSAndre Przywara if (port->mapbase != (unsigned long)ser->iomem_base) 2312ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2313ab4382d2SGreg Kroah-Hartman if (port->iobase != ser->port) 2314ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2315ab4382d2SGreg Kroah-Hartman if (ser->hub6 != 0) 2316ab4382d2SGreg Kroah-Hartman ret = -EINVAL; 2317ab4382d2SGreg Kroah-Hartman return ret; 2318ab4382d2SGreg Kroah-Hartman } 2319ab4382d2SGreg Kroah-Hartman 2320ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2321ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port) 2322ab4382d2SGreg Kroah-Hartman { 23234e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY)) 2324ab4382d2SGreg Kroah-Hartman cpu_relax(); 2325ab4382d2SGreg Kroah-Hartman 2326a6499435SCyrille Pitchen return atmel_uart_read_char(port); 2327ab4382d2SGreg Kroah-Hartman } 2328ab4382d2SGreg Kroah-Hartman 2329ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2330ab4382d2SGreg Kroah-Hartman { 23314e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2332ab4382d2SGreg Kroah-Hartman cpu_relax(); 2333ab4382d2SGreg Kroah-Hartman 2334a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2335ab4382d2SGreg Kroah-Hartman } 2336ab4382d2SGreg Kroah-Hartman #endif 2337ab4382d2SGreg Kroah-Hartman 23385c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = { 2339ab4382d2SGreg Kroah-Hartman .tx_empty = atmel_tx_empty, 2340ab4382d2SGreg Kroah-Hartman .set_mctrl = atmel_set_mctrl, 2341ab4382d2SGreg Kroah-Hartman .get_mctrl = atmel_get_mctrl, 2342ab4382d2SGreg Kroah-Hartman .stop_tx = atmel_stop_tx, 2343ab4382d2SGreg Kroah-Hartman .start_tx = atmel_start_tx, 2344ab4382d2SGreg Kroah-Hartman .stop_rx = atmel_stop_rx, 2345ab4382d2SGreg Kroah-Hartman .enable_ms = atmel_enable_ms, 2346ab4382d2SGreg Kroah-Hartman .break_ctl = atmel_break_ctl, 2347ab4382d2SGreg Kroah-Hartman .startup = atmel_startup, 2348ab4382d2SGreg Kroah-Hartman .shutdown = atmel_shutdown, 2349ab4382d2SGreg Kroah-Hartman .flush_buffer = atmel_flush_buffer, 2350ab4382d2SGreg Kroah-Hartman .set_termios = atmel_set_termios, 235142bd7a4fSViktar Palstsiuk .set_ldisc = atmel_set_ldisc, 2352ab4382d2SGreg Kroah-Hartman .type = atmel_type, 2353ab4382d2SGreg Kroah-Hartman .release_port = atmel_release_port, 2354ab4382d2SGreg Kroah-Hartman .request_port = atmel_request_port, 2355ab4382d2SGreg Kroah-Hartman .config_port = atmel_config_port, 2356ab4382d2SGreg Kroah-Hartman .verify_port = atmel_verify_port, 2357ab4382d2SGreg Kroah-Hartman .pm = atmel_serial_pm, 2358ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL 2359ab4382d2SGreg Kroah-Hartman .poll_get_char = atmel_poll_get_char, 2360ab4382d2SGreg Kroah-Hartman .poll_put_char = atmel_poll_put_char, 2361ab4382d2SGreg Kroah-Hartman #endif 2362ab4382d2SGreg Kroah-Hartman }; 2363ab4382d2SGreg Kroah-Hartman 2364ab4382d2SGreg Kroah-Hartman /* 2365ab4382d2SGreg Kroah-Hartman * Configure the port from the platform device resource info. 2366ab4382d2SGreg Kroah-Hartman */ 236791f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port, 2368ab4382d2SGreg Kroah-Hartman struct platform_device *pdev) 2369ab4382d2SGreg Kroah-Hartman { 237091f8c2d8SBoris BREZILLON int ret; 2371ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_port->uart; 2372ab4382d2SGreg Kroah-Hartman 23734a1e8888SLeilei Zhao atmel_init_property(atmel_port, pdev); 2374a930e528SElen Song atmel_set_ops(port); 2375a930e528SElen Song 237613bd3e6fSRicardo Ribalda Delgado atmel_init_rs485(port, pdev); 237733d64c4fSElen Song 2378ab4382d2SGreg Kroah-Hartman port->iotype = UPIO_MEM; 2379*92c8f7c0SAlexandre Belloni port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP; 2380ab4382d2SGreg Kroah-Hartman port->ops = &atmel_pops; 2381ab4382d2SGreg Kroah-Hartman port->fifosize = 1; 2382ab4382d2SGreg Kroah-Hartman port->dev = &pdev->dev; 2383ab4382d2SGreg Kroah-Hartman port->mapbase = pdev->resource[0].start; 2384ab4382d2SGreg Kroah-Hartman port->irq = pdev->resource[1].start; 238513bd3e6fSRicardo Ribalda Delgado port->rs485_config = atmel_config_rs485; 2386*92c8f7c0SAlexandre Belloni port->membase = NULL; 2387ab4382d2SGreg Kroah-Hartman 2388ab4382d2SGreg Kroah-Hartman memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2389ab4382d2SGreg Kroah-Hartman 2390ab4382d2SGreg Kroah-Hartman /* for console, the clock could already be configured */ 2391ab4382d2SGreg Kroah-Hartman if (!atmel_port->clk) { 2392ab4382d2SGreg Kroah-Hartman atmel_port->clk = clk_get(&pdev->dev, "usart"); 239391f8c2d8SBoris BREZILLON if (IS_ERR(atmel_port->clk)) { 239491f8c2d8SBoris BREZILLON ret = PTR_ERR(atmel_port->clk); 239591f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 239691f8c2d8SBoris BREZILLON return ret; 239791f8c2d8SBoris BREZILLON } 239891f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_port->clk); 239991f8c2d8SBoris BREZILLON if (ret) { 240091f8c2d8SBoris BREZILLON clk_put(atmel_port->clk); 240191f8c2d8SBoris BREZILLON atmel_port->clk = NULL; 240291f8c2d8SBoris BREZILLON return ret; 240391f8c2d8SBoris BREZILLON } 2404ab4382d2SGreg Kroah-Hartman port->uartclk = clk_get_rate(atmel_port->clk); 240591f8c2d8SBoris BREZILLON clk_disable_unprepare(atmel_port->clk); 2406ab4382d2SGreg Kroah-Hartman /* only enable clock when USART is in use */ 2407ab4382d2SGreg Kroah-Hartman } 2408ab4382d2SGreg Kroah-Hartman 2409ab4382d2SGreg Kroah-Hartman /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 241013bd3e6fSRicardo Ribalda Delgado if (port->rs485.flags & SER_RS485_ENABLED) 2411ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 241264e22ebeSElen Song else if (atmel_use_pdc_tx(port)) { 2413ab4382d2SGreg Kroah-Hartman port->fifosize = PDC_BUFFER_SIZE; 2414ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2415ab4382d2SGreg Kroah-Hartman } else { 2416ab4382d2SGreg Kroah-Hartman atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2417ab4382d2SGreg Kroah-Hartman } 241891f8c2d8SBoris BREZILLON 241991f8c2d8SBoris BREZILLON return 0; 2420ab4382d2SGreg Kroah-Hartman } 2421ab4382d2SGreg Kroah-Hartman 2422ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2423ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch) 2424ab4382d2SGreg Kroah-Hartman { 24254e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2426ab4382d2SGreg Kroah-Hartman cpu_relax(); 2427a6499435SCyrille Pitchen atmel_uart_write_char(port, ch); 2428ab4382d2SGreg Kroah-Hartman } 2429ab4382d2SGreg Kroah-Hartman 2430ab4382d2SGreg Kroah-Hartman /* 2431ab4382d2SGreg Kroah-Hartman * Interrupts are disabled on entering 2432ab4382d2SGreg Kroah-Hartman */ 2433ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count) 2434ab4382d2SGreg Kroah-Hartman { 2435ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2436ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2437ab4382d2SGreg Kroah-Hartman unsigned int status, imr; 2438ab4382d2SGreg Kroah-Hartman unsigned int pdc_tx; 2439ab4382d2SGreg Kroah-Hartman 2440ab4382d2SGreg Kroah-Hartman /* 2441ab4382d2SGreg Kroah-Hartman * First, save IMR and then disable interrupts 2442ab4382d2SGreg Kroah-Hartman */ 24434e7decdaSCyrille Pitchen imr = atmel_uart_readl(port, ATMEL_US_IMR); 24444e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, 24454e7decdaSCyrille Pitchen ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2446ab4382d2SGreg Kroah-Hartman 2447ab4382d2SGreg Kroah-Hartman /* Store PDC transmit status and disable it */ 24484e7decdaSCyrille Pitchen pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN; 24494e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 2450ab4382d2SGreg Kroah-Hartman 2451497e1e16SNicolas Ferre /* Make sure that tx path is actually able to send characters */ 2452497e1e16SNicolas Ferre atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 2453497e1e16SNicolas Ferre 2454ab4382d2SGreg Kroah-Hartman uart_console_write(port, s, count, atmel_console_putchar); 2455ab4382d2SGreg Kroah-Hartman 2456ab4382d2SGreg Kroah-Hartman /* 2457ab4382d2SGreg Kroah-Hartman * Finally, wait for transmitter to become empty 2458ab4382d2SGreg Kroah-Hartman * and restore IMR 2459ab4382d2SGreg Kroah-Hartman */ 2460ab4382d2SGreg Kroah-Hartman do { 24614e7decdaSCyrille Pitchen status = atmel_uart_readl(port, ATMEL_US_CSR); 2462ab4382d2SGreg Kroah-Hartman } while (!(status & ATMEL_US_TXRDY)); 2463ab4382d2SGreg Kroah-Hartman 2464ab4382d2SGreg Kroah-Hartman /* Restore PDC transmit status */ 2465ab4382d2SGreg Kroah-Hartman if (pdc_tx) 24664e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 2467ab4382d2SGreg Kroah-Hartman 2468ab4382d2SGreg Kroah-Hartman /* set interrupts back the way they were */ 24694e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IER, imr); 2470ab4382d2SGreg Kroah-Hartman } 2471ab4382d2SGreg Kroah-Hartman 2472ab4382d2SGreg Kroah-Hartman /* 2473ab4382d2SGreg Kroah-Hartman * If the port was already initialised (eg, by a boot loader), 2474ab4382d2SGreg Kroah-Hartman * try to determine the current setup. 2475ab4382d2SGreg Kroah-Hartman */ 2476ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2477ab4382d2SGreg Kroah-Hartman int *parity, int *bits) 2478ab4382d2SGreg Kroah-Hartman { 2479ab4382d2SGreg Kroah-Hartman unsigned int mr, quot; 2480ab4382d2SGreg Kroah-Hartman 2481ab4382d2SGreg Kroah-Hartman /* 2482ab4382d2SGreg Kroah-Hartman * If the baud rate generator isn't running, the port wasn't 2483ab4382d2SGreg Kroah-Hartman * initialized by the boot loader. 2484ab4382d2SGreg Kroah-Hartman */ 24854e7decdaSCyrille Pitchen quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD; 2486ab4382d2SGreg Kroah-Hartman if (!quot) 2487ab4382d2SGreg Kroah-Hartman return; 2488ab4382d2SGreg Kroah-Hartman 24894e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL; 2490ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_CHRL_8) 2491ab4382d2SGreg Kroah-Hartman *bits = 8; 2492ab4382d2SGreg Kroah-Hartman else 2493ab4382d2SGreg Kroah-Hartman *bits = 7; 2494ab4382d2SGreg Kroah-Hartman 24954e7decdaSCyrille Pitchen mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR; 2496ab4382d2SGreg Kroah-Hartman if (mr == ATMEL_US_PAR_EVEN) 2497ab4382d2SGreg Kroah-Hartman *parity = 'e'; 2498ab4382d2SGreg Kroah-Hartman else if (mr == ATMEL_US_PAR_ODD) 2499ab4382d2SGreg Kroah-Hartman *parity = 'o'; 2500ab4382d2SGreg Kroah-Hartman 2501ab4382d2SGreg Kroah-Hartman /* 2502ab4382d2SGreg Kroah-Hartman * The serial core only rounds down when matching this to a 2503ab4382d2SGreg Kroah-Hartman * supported baud rate. Make sure we don't end up slightly 2504ab4382d2SGreg Kroah-Hartman * lower than one of those, as it would make us fall through 2505ab4382d2SGreg Kroah-Hartman * to a much lower baud rate than we really want. 2506ab4382d2SGreg Kroah-Hartman */ 2507ab4382d2SGreg Kroah-Hartman *baud = port->uartclk / (16 * (quot - 1)); 2508ab4382d2SGreg Kroah-Hartman } 2509ab4382d2SGreg Kroah-Hartman 2510ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options) 2511ab4382d2SGreg Kroah-Hartman { 251291f8c2d8SBoris BREZILLON int ret; 2513ab4382d2SGreg Kroah-Hartman struct uart_port *port = &atmel_ports[co->index].uart; 2514ab4382d2SGreg Kroah-Hartman int baud = 115200; 2515ab4382d2SGreg Kroah-Hartman int bits = 8; 2516ab4382d2SGreg Kroah-Hartman int parity = 'n'; 2517ab4382d2SGreg Kroah-Hartman int flow = 'n'; 2518ab4382d2SGreg Kroah-Hartman 2519ab4382d2SGreg Kroah-Hartman if (port->membase == NULL) { 2520ab4382d2SGreg Kroah-Hartman /* Port not initialized yet - delay setup */ 2521ab4382d2SGreg Kroah-Hartman return -ENODEV; 2522ab4382d2SGreg Kroah-Hartman } 2523ab4382d2SGreg Kroah-Hartman 252491f8c2d8SBoris BREZILLON ret = clk_prepare_enable(atmel_ports[co->index].clk); 252591f8c2d8SBoris BREZILLON if (ret) 252691f8c2d8SBoris BREZILLON return ret; 2527ab4382d2SGreg Kroah-Hartman 25284e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_IDR, -1); 25294e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 25304e7decdaSCyrille Pitchen atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2531ab4382d2SGreg Kroah-Hartman 2532ab4382d2SGreg Kroah-Hartman if (options) 2533ab4382d2SGreg Kroah-Hartman uart_parse_options(options, &baud, &parity, &bits, &flow); 2534ab4382d2SGreg Kroah-Hartman else 2535ab4382d2SGreg Kroah-Hartman atmel_console_get_options(port, &baud, &parity, &bits); 2536ab4382d2SGreg Kroah-Hartman 2537ab4382d2SGreg Kroah-Hartman return uart_set_options(port, co, baud, parity, bits, flow); 2538ab4382d2SGreg Kroah-Hartman } 2539ab4382d2SGreg Kroah-Hartman 2540ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart; 2541ab4382d2SGreg Kroah-Hartman 2542ab4382d2SGreg Kroah-Hartman static struct console atmel_console = { 2543ab4382d2SGreg Kroah-Hartman .name = ATMEL_DEVICENAME, 2544ab4382d2SGreg Kroah-Hartman .write = atmel_console_write, 2545ab4382d2SGreg Kroah-Hartman .device = uart_console_device, 2546ab4382d2SGreg Kroah-Hartman .setup = atmel_console_setup, 2547ab4382d2SGreg Kroah-Hartman .flags = CON_PRINTBUFFER, 2548ab4382d2SGreg Kroah-Hartman .index = -1, 2549ab4382d2SGreg Kroah-Hartman .data = &atmel_uart, 2550ab4382d2SGreg Kroah-Hartman }; 2551ab4382d2SGreg Kroah-Hartman 2552ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2553ab4382d2SGreg Kroah-Hartman 2554ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2555ab4382d2SGreg Kroah-Hartman { 2556ab4382d2SGreg Kroah-Hartman return port->cons && port->cons->index == port->line; 2557ab4382d2SGreg Kroah-Hartman } 2558ab4382d2SGreg Kroah-Hartman 2559ab4382d2SGreg Kroah-Hartman #else 2560ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE NULL 2561ab4382d2SGreg Kroah-Hartman 2562ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port) 2563ab4382d2SGreg Kroah-Hartman { 2564ab4382d2SGreg Kroah-Hartman return false; 2565ab4382d2SGreg Kroah-Hartman } 2566ab4382d2SGreg Kroah-Hartman #endif 2567ab4382d2SGreg Kroah-Hartman 2568ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = { 2569ab4382d2SGreg Kroah-Hartman .owner = THIS_MODULE, 2570ab4382d2SGreg Kroah-Hartman .driver_name = "atmel_serial", 2571ab4382d2SGreg Kroah-Hartman .dev_name = ATMEL_DEVICENAME, 2572ab4382d2SGreg Kroah-Hartman .major = SERIAL_ATMEL_MAJOR, 2573ab4382d2SGreg Kroah-Hartman .minor = MINOR_START, 2574ab4382d2SGreg Kroah-Hartman .nr = ATMEL_MAX_UART, 2575ab4382d2SGreg Kroah-Hartman .cons = ATMEL_CONSOLE_DEVICE, 2576ab4382d2SGreg Kroah-Hartman }; 2577ab4382d2SGreg Kroah-Hartman 2578ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM 2579ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void) 2580ab4382d2SGreg Kroah-Hartman { 2581ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91 2582ab4382d2SGreg Kroah-Hartman return at91_suspend_entering_slow_clock(); 2583ab4382d2SGreg Kroah-Hartman #else 2584ab4382d2SGreg Kroah-Hartman return false; 2585ab4382d2SGreg Kroah-Hartman #endif 2586ab4382d2SGreg Kroah-Hartman } 2587ab4382d2SGreg Kroah-Hartman 2588ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev, 2589ab4382d2SGreg Kroah-Hartman pm_message_t state) 2590ab4382d2SGreg Kroah-Hartman { 2591ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2592ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2593ab4382d2SGreg Kroah-Hartman 2594ab4382d2SGreg Kroah-Hartman if (atmel_is_console_port(port) && console_suspend_enabled) { 2595ab4382d2SGreg Kroah-Hartman /* Drain the TX shifter */ 25964e7decdaSCyrille Pitchen while (!(atmel_uart_readl(port, ATMEL_US_CSR) & 25974e7decdaSCyrille Pitchen ATMEL_US_TXEMPTY)) 2598ab4382d2SGreg Kroah-Hartman cpu_relax(); 2599ab4382d2SGreg Kroah-Hartman } 2600ab4382d2SGreg Kroah-Hartman 26016a5f0e2fSAlexandre Belloni if (atmel_is_console_port(port) && !console_suspend_enabled) { 26026a5f0e2fSAlexandre Belloni /* Cache register values as we won't get a full shutdown/startup 26036a5f0e2fSAlexandre Belloni * cycle 26046a5f0e2fSAlexandre Belloni */ 26056a5f0e2fSAlexandre Belloni atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR); 26066a5f0e2fSAlexandre Belloni atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR); 26076a5f0e2fSAlexandre Belloni atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 26086a5f0e2fSAlexandre Belloni atmel_port->cache.rtor = atmel_uart_readl(port, 26096a5f0e2fSAlexandre Belloni atmel_port->rtor); 26106a5f0e2fSAlexandre Belloni atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR); 26116a5f0e2fSAlexandre Belloni atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR); 26126a5f0e2fSAlexandre Belloni atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR); 26136a5f0e2fSAlexandre Belloni } 26146a5f0e2fSAlexandre Belloni 2615ab4382d2SGreg Kroah-Hartman /* we can not wake up if we're running on slow clock */ 2616ab4382d2SGreg Kroah-Hartman atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 26172c7af5baSBoris BREZILLON if (atmel_serial_clk_will_stop()) { 26182c7af5baSBoris BREZILLON unsigned long flags; 26192c7af5baSBoris BREZILLON 26202c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 26212c7af5baSBoris BREZILLON atmel_port->suspended = true; 26222c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2623ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, 0); 26242c7af5baSBoris BREZILLON } 2625ab4382d2SGreg Kroah-Hartman 2626ab4382d2SGreg Kroah-Hartman uart_suspend_port(&atmel_uart, port); 2627ab4382d2SGreg Kroah-Hartman 2628ab4382d2SGreg Kroah-Hartman return 0; 2629ab4382d2SGreg Kroah-Hartman } 2630ab4382d2SGreg Kroah-Hartman 2631ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev) 2632ab4382d2SGreg Kroah-Hartman { 2633ab4382d2SGreg Kroah-Hartman struct uart_port *port = platform_get_drvdata(pdev); 2634ab4382d2SGreg Kroah-Hartman struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 26352c7af5baSBoris BREZILLON unsigned long flags; 26362c7af5baSBoris BREZILLON 26376a5f0e2fSAlexandre Belloni if (atmel_is_console_port(port) && !console_suspend_enabled) { 26386a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr); 26396a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr); 26406a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr); 26416a5f0e2fSAlexandre Belloni atmel_uart_writel(port, atmel_port->rtor, 26426a5f0e2fSAlexandre Belloni atmel_port->cache.rtor); 26436a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr); 26446a5f0e2fSAlexandre Belloni 26456a5f0e2fSAlexandre Belloni if (atmel_port->fifo_size) { 26466a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN | 26476a5f0e2fSAlexandre Belloni ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR); 26486a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FMR, 26496a5f0e2fSAlexandre Belloni atmel_port->cache.fmr); 26506a5f0e2fSAlexandre Belloni atmel_uart_writel(port, ATMEL_US_FIER, 26516a5f0e2fSAlexandre Belloni atmel_port->cache.fimr); 26526a5f0e2fSAlexandre Belloni } 26536a5f0e2fSAlexandre Belloni atmel_start_rx(port); 26546a5f0e2fSAlexandre Belloni } 26556a5f0e2fSAlexandre Belloni 26562c7af5baSBoris BREZILLON spin_lock_irqsave(&atmel_port->lock_suspended, flags); 26572c7af5baSBoris BREZILLON if (atmel_port->pending) { 26582c7af5baSBoris BREZILLON atmel_handle_receive(port, atmel_port->pending); 26592c7af5baSBoris BREZILLON atmel_handle_status(port, atmel_port->pending, 26602c7af5baSBoris BREZILLON atmel_port->pending_status); 26612c7af5baSBoris BREZILLON atmel_handle_transmit(port, atmel_port->pending); 26622c7af5baSBoris BREZILLON atmel_port->pending = 0; 26632c7af5baSBoris BREZILLON } 26642c7af5baSBoris BREZILLON atmel_port->suspended = false; 26652c7af5baSBoris BREZILLON spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2666ab4382d2SGreg Kroah-Hartman 2667ab4382d2SGreg Kroah-Hartman uart_resume_port(&atmel_uart, port); 2668ab4382d2SGreg Kroah-Hartman device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 2669ab4382d2SGreg Kroah-Hartman 2670ab4382d2SGreg Kroah-Hartman return 0; 2671ab4382d2SGreg Kroah-Hartman } 2672ab4382d2SGreg Kroah-Hartman #else 2673ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL 2674ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL 2675ab4382d2SGreg Kroah-Hartman #endif 2676ab4382d2SGreg Kroah-Hartman 2677b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port, 2678b5199d46SCyrille Pitchen struct platform_device *pdev) 2679b5199d46SCyrille Pitchen { 2680b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2681b78cd169SJaeden Amero atmel_port->rts_low = 0; 2682b78cd169SJaeden Amero atmel_port->rts_high = 0; 2683b5199d46SCyrille Pitchen 2684b5199d46SCyrille Pitchen if (of_property_read_u32(pdev->dev.of_node, 2685b5199d46SCyrille Pitchen "atmel,fifo-size", 2686b78cd169SJaeden Amero &atmel_port->fifo_size)) 2687b5199d46SCyrille Pitchen return; 2688b5199d46SCyrille Pitchen 2689b78cd169SJaeden Amero if (!atmel_port->fifo_size) 2690b5199d46SCyrille Pitchen return; 2691b5199d46SCyrille Pitchen 2692b78cd169SJaeden Amero if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) { 2693b78cd169SJaeden Amero atmel_port->fifo_size = 0; 2694b5199d46SCyrille Pitchen dev_err(&pdev->dev, "Invalid FIFO size\n"); 2695b5199d46SCyrille Pitchen return; 2696b5199d46SCyrille Pitchen } 2697b5199d46SCyrille Pitchen 2698b5199d46SCyrille Pitchen /* 2699b5199d46SCyrille Pitchen * 0 <= rts_low <= rts_high <= fifo_size 2700b5199d46SCyrille Pitchen * Once their CTS line asserted by the remote peer, some x86 UARTs tend 2701b5199d46SCyrille Pitchen * to flush their internal TX FIFO, commonly up to 16 data, before 2702b5199d46SCyrille Pitchen * actually stopping to send new data. So we try to set the RTS High 2703b5199d46SCyrille Pitchen * Threshold to a reasonably high value respecting this 16 data 2704b5199d46SCyrille Pitchen * empirical rule when possible. 2705b5199d46SCyrille Pitchen */ 2706b78cd169SJaeden Amero atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1, 2707b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET); 2708b78cd169SJaeden Amero atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2, 2709b78cd169SJaeden Amero atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET); 2710b5199d46SCyrille Pitchen 2711b5199d46SCyrille Pitchen dev_info(&pdev->dev, "Using FIFO (%u data)\n", 2712b78cd169SJaeden Amero atmel_port->fifo_size); 2713b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n", 2714b78cd169SJaeden Amero atmel_port->rts_high); 2715b5199d46SCyrille Pitchen dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n", 2716b78cd169SJaeden Amero atmel_port->rts_low); 2717b5199d46SCyrille Pitchen } 2718b5199d46SCyrille Pitchen 27199671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev) 2720ab4382d2SGreg Kroah-Hartman { 2721b78cd169SJaeden Amero struct atmel_uart_port *atmel_port; 27225fbe46b6SNicolas Ferre struct device_node *np = pdev->dev.of_node; 2723ab4382d2SGreg Kroah-Hartman void *data; 27244cbf9f48SNicolas Ferre int ret = -ENODEV; 2725bd737f87SRicardo Ribalda Delgado bool rs485_enabled; 2726ab4382d2SGreg Kroah-Hartman 2727ab4382d2SGreg Kroah-Hartman BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2728ab4382d2SGreg Kroah-Hartman 27295fbe46b6SNicolas Ferre ret = of_alias_get_id(np, "serial"); 27304cbf9f48SNicolas Ferre if (ret < 0) 27315fbe46b6SNicolas Ferre /* port id not found in platform data nor device-tree aliases: 27324cbf9f48SNicolas Ferre * auto-enumerate it */ 2733503bded9SPawel Wieczorkiewicz ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 27344cbf9f48SNicolas Ferre 2735503bded9SPawel Wieczorkiewicz if (ret >= ATMEL_MAX_UART) { 27364cbf9f48SNicolas Ferre ret = -ENODEV; 27374cbf9f48SNicolas Ferre goto err; 27384cbf9f48SNicolas Ferre } 27394cbf9f48SNicolas Ferre 2740503bded9SPawel Wieczorkiewicz if (test_and_set_bit(ret, atmel_ports_in_use)) { 27414cbf9f48SNicolas Ferre /* port already in use */ 27424cbf9f48SNicolas Ferre ret = -EBUSY; 27434cbf9f48SNicolas Ferre goto err; 27444cbf9f48SNicolas Ferre } 27454cbf9f48SNicolas Ferre 2746b78cd169SJaeden Amero atmel_port = &atmel_ports[ret]; 2747b78cd169SJaeden Amero atmel_port->backup_imr = 0; 2748b78cd169SJaeden Amero atmel_port->uart.line = ret; 2749b78cd169SJaeden Amero atmel_serial_probe_fifos(atmel_port, pdev); 2750354e57f3SLinus Walleij 275198f2082cSNicolas Ferre atomic_set(&atmel_port->tasklet_shutdown, 0); 2752b78cd169SJaeden Amero spin_lock_init(&atmel_port->lock_suspended); 27532c7af5baSBoris BREZILLON 2754b78cd169SJaeden Amero ret = atmel_init_port(atmel_port, pdev); 275591f8c2d8SBoris BREZILLON if (ret) 27566fbb9bdfSCyrille Pitchen goto err_clear_bit; 2757ab4382d2SGreg Kroah-Hartman 2758b78cd169SJaeden Amero atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0); 2759b78cd169SJaeden Amero if (IS_ERR(atmel_port->gpios)) { 2760b78cd169SJaeden Amero ret = PTR_ERR(atmel_port->gpios); 276118dfef9cSUwe Kleine-König goto err_clear_bit; 276218dfef9cSUwe Kleine-König } 276318dfef9cSUwe Kleine-König 2764b78cd169SJaeden Amero if (!atmel_use_pdc_rx(&atmel_port->uart)) { 2765ab4382d2SGreg Kroah-Hartman ret = -ENOMEM; 2766ab4382d2SGreg Kroah-Hartman data = kmalloc(sizeof(struct atmel_uart_char) 2767ab4382d2SGreg Kroah-Hartman * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2768ab4382d2SGreg Kroah-Hartman if (!data) 2769ab4382d2SGreg Kroah-Hartman goto err_alloc_ring; 2770b78cd169SJaeden Amero atmel_port->rx_ring.buf = data; 2771ab4382d2SGreg Kroah-Hartman } 2772ab4382d2SGreg Kroah-Hartman 2773b78cd169SJaeden Amero rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED; 2774bd737f87SRicardo Ribalda Delgado 2775b78cd169SJaeden Amero ret = uart_add_one_port(&atmel_uart, &atmel_port->uart); 2776ab4382d2SGreg Kroah-Hartman if (ret) 2777ab4382d2SGreg Kroah-Hartman goto err_add_port; 2778ab4382d2SGreg Kroah-Hartman 2779ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2780b78cd169SJaeden Amero if (atmel_is_console_port(&atmel_port->uart) 2781ab4382d2SGreg Kroah-Hartman && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 2782ab4382d2SGreg Kroah-Hartman /* 2783ab4382d2SGreg Kroah-Hartman * The serial core enabled the clock for us, so undo 278491f8c2d8SBoris BREZILLON * the clk_prepare_enable() in atmel_console_setup() 2785ab4382d2SGreg Kroah-Hartman */ 2786b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2787ab4382d2SGreg Kroah-Hartman } 2788ab4382d2SGreg Kroah-Hartman #endif 2789ab4382d2SGreg Kroah-Hartman 2790ab4382d2SGreg Kroah-Hartman device_init_wakeup(&pdev->dev, 1); 2791b78cd169SJaeden Amero platform_set_drvdata(pdev, atmel_port); 2792ab4382d2SGreg Kroah-Hartman 2793d4f64187SCyrille Pitchen /* 2794d4f64187SCyrille Pitchen * The peripheral clock has been disabled by atmel_init_port(): 2795d4f64187SCyrille Pitchen * enable it before accessing I/O registers 2796d4f64187SCyrille Pitchen */ 2797b78cd169SJaeden Amero clk_prepare_enable(atmel_port->clk); 2798d4f64187SCyrille Pitchen 2799bd737f87SRicardo Ribalda Delgado if (rs485_enabled) { 2800b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR, 28014e7decdaSCyrille Pitchen ATMEL_US_USMODE_NORMAL); 2802b78cd169SJaeden Amero atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR, 2803b78cd169SJaeden Amero ATMEL_US_RTSEN); 2804fc887b15SLinus Torvalds } 2805fc887b15SLinus Torvalds 2806055560b0SElen Song /* 2807055560b0SElen Song * Get port name of usart or uart 2808055560b0SElen Song */ 2809b78cd169SJaeden Amero atmel_get_ip_name(&atmel_port->uart); 2810055560b0SElen Song 2811d4f64187SCyrille Pitchen /* 2812d4f64187SCyrille Pitchen * The peripheral clock can now safely be disabled till the port 2813d4f64187SCyrille Pitchen * is used 2814d4f64187SCyrille Pitchen */ 2815b78cd169SJaeden Amero clk_disable_unprepare(atmel_port->clk); 2816d4f64187SCyrille Pitchen 2817ab4382d2SGreg Kroah-Hartman return 0; 2818ab4382d2SGreg Kroah-Hartman 2819ab4382d2SGreg Kroah-Hartman err_add_port: 2820b78cd169SJaeden Amero kfree(atmel_port->rx_ring.buf); 2821b78cd169SJaeden Amero atmel_port->rx_ring.buf = NULL; 2822ab4382d2SGreg Kroah-Hartman err_alloc_ring: 2823b78cd169SJaeden Amero if (!atmel_is_console_port(&atmel_port->uart)) { 2824b78cd169SJaeden Amero clk_put(atmel_port->clk); 2825b78cd169SJaeden Amero atmel_port->clk = NULL; 2826ab4382d2SGreg Kroah-Hartman } 28276fbb9bdfSCyrille Pitchen err_clear_bit: 2828b78cd169SJaeden Amero clear_bit(atmel_port->uart.line, atmel_ports_in_use); 28294cbf9f48SNicolas Ferre err: 2830ab4382d2SGreg Kroah-Hartman return ret; 2831ab4382d2SGreg Kroah-Hartman } 2832ab4382d2SGreg Kroah-Hartman 2833f4a8ab04SRomain Izard /* 2834f4a8ab04SRomain Izard * Even if the driver is not modular, it makes sense to be able to 2835f4a8ab04SRomain Izard * unbind a device: there can be many bound devices, and there are 2836f4a8ab04SRomain Izard * situations where dynamic binding and unbinding can be useful. 2837f4a8ab04SRomain Izard * 2838f4a8ab04SRomain Izard * For example, a connected device can require a specific firmware update 2839f4a8ab04SRomain Izard * protocol that needs bitbanging on IO lines, but use the regular serial 2840f4a8ab04SRomain Izard * port in the normal case. 2841f4a8ab04SRomain Izard */ 2842f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev) 2843f4a8ab04SRomain Izard { 2844f4a8ab04SRomain Izard struct uart_port *port = platform_get_drvdata(pdev); 2845f4a8ab04SRomain Izard struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2846f4a8ab04SRomain Izard int ret = 0; 2847f4a8ab04SRomain Izard 284800e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_rx); 284900e8e658SNicolas Ferre tasklet_kill(&atmel_port->tasklet_tx); 2850f4a8ab04SRomain Izard 2851f4a8ab04SRomain Izard device_init_wakeup(&pdev->dev, 0); 2852f4a8ab04SRomain Izard 2853f4a8ab04SRomain Izard ret = uart_remove_one_port(&atmel_uart, port); 2854f4a8ab04SRomain Izard 2855f4a8ab04SRomain Izard kfree(atmel_port->rx_ring.buf); 2856f4a8ab04SRomain Izard 2857f4a8ab04SRomain Izard /* "port" is allocated statically, so we shouldn't free it */ 2858f4a8ab04SRomain Izard 2859f4a8ab04SRomain Izard clear_bit(port->line, atmel_ports_in_use); 2860f4a8ab04SRomain Izard 2861f4a8ab04SRomain Izard clk_put(atmel_port->clk); 2862f4a8ab04SRomain Izard atmel_port->clk = NULL; 2863f4a8ab04SRomain Izard 2864f4a8ab04SRomain Izard return ret; 2865f4a8ab04SRomain Izard } 2866f4a8ab04SRomain Izard 2867ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = { 2868ab4382d2SGreg Kroah-Hartman .probe = atmel_serial_probe, 2869f4a8ab04SRomain Izard .remove = atmel_serial_remove, 2870ab4382d2SGreg Kroah-Hartman .suspend = atmel_serial_suspend, 2871ab4382d2SGreg Kroah-Hartman .resume = atmel_serial_resume, 2872ab4382d2SGreg Kroah-Hartman .driver = { 2873ab4382d2SGreg Kroah-Hartman .name = "atmel_usart", 28745fbe46b6SNicolas Ferre .of_match_table = of_match_ptr(atmel_serial_dt_ids), 2875ab4382d2SGreg Kroah-Hartman }, 2876ab4382d2SGreg Kroah-Hartman }; 2877ab4382d2SGreg Kroah-Hartman 2878ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void) 2879ab4382d2SGreg Kroah-Hartman { 2880ab4382d2SGreg Kroah-Hartman int ret; 2881ab4382d2SGreg Kroah-Hartman 2882ab4382d2SGreg Kroah-Hartman ret = uart_register_driver(&atmel_uart); 2883ab4382d2SGreg Kroah-Hartman if (ret) 2884ab4382d2SGreg Kroah-Hartman return ret; 2885ab4382d2SGreg Kroah-Hartman 2886ab4382d2SGreg Kroah-Hartman ret = platform_driver_register(&atmel_serial_driver); 2887ab4382d2SGreg Kroah-Hartman if (ret) 2888ab4382d2SGreg Kroah-Hartman uart_unregister_driver(&atmel_uart); 2889ab4382d2SGreg Kroah-Hartman 2890ab4382d2SGreg Kroah-Hartman return ret; 2891ab4382d2SGreg Kroah-Hartman } 2892c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init); 2893