xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 026cb4329df6169eb820815d220a391d97550cc5)
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 
174ea04f82aSRomain Izard 	bool			tx_stopped;
1752c7af5baSBoris BREZILLON 	bool			suspended;
1762c7af5baSBoris BREZILLON 	unsigned int		pending;
1772c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1782c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1792c7af5baSBoris BREZILLON 
180488ae82dSAlexandre Belloni #ifdef CONFIG_PM
1816a5f0e2fSAlexandre Belloni 	struct {
1826a5f0e2fSAlexandre Belloni 		u32		cr;
1836a5f0e2fSAlexandre Belloni 		u32		mr;
1846a5f0e2fSAlexandre Belloni 		u32		imr;
1856a5f0e2fSAlexandre Belloni 		u32		brgr;
1866a5f0e2fSAlexandre Belloni 		u32		rtor;
1876a5f0e2fSAlexandre Belloni 		u32		ttgr;
1886a5f0e2fSAlexandre Belloni 		u32		fmr;
1896a5f0e2fSAlexandre Belloni 		u32		fimr;
1906a5f0e2fSAlexandre Belloni 	} cache;
191488ae82dSAlexandre Belloni #endif
1926a5f0e2fSAlexandre Belloni 
193a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
194a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
195a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
196a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
197a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
198a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
199ab4382d2SGreg Kroah-Hartman };
200ab4382d2SGreg Kroah-Hartman 
201ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
202503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
203ab4382d2SGreg Kroah-Hartman 
204ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
205ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
206ab4382d2SGreg Kroah-Hartman #endif
207ab4382d2SGreg Kroah-Hartman 
2085fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
2095fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
2105fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
2115fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
2125fbe46b6SNicolas Ferre 	{ /* sentinel */ }
2135fbe46b6SNicolas Ferre };
2145fbe46b6SNicolas Ferre #endif
2155fbe46b6SNicolas Ferre 
216ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
217ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
218ab4382d2SGreg Kroah-Hartman {
219ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
220ab4382d2SGreg Kroah-Hartman }
221ab4382d2SGreg Kroah-Hartman 
2224e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2234e7decdaSCyrille Pitchen {
2244e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2254e7decdaSCyrille Pitchen }
2264e7decdaSCyrille Pitchen 
2274e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2284e7decdaSCyrille Pitchen {
2294e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2304e7decdaSCyrille Pitchen }
2314e7decdaSCyrille Pitchen 
232a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
233a6499435SCyrille Pitchen {
234a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
235a6499435SCyrille Pitchen }
236a6499435SCyrille Pitchen 
237a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
238a6499435SCyrille Pitchen {
239a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
240a6499435SCyrille Pitchen }
241a6499435SCyrille Pitchen 
242ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
24364e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
244ab4382d2SGreg Kroah-Hartman {
245ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
246ab4382d2SGreg Kroah-Hartman 
24764e22ebeSElen Song 	return atmel_port->use_pdc_rx;
248ab4382d2SGreg Kroah-Hartman }
249ab4382d2SGreg Kroah-Hartman 
25064e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
251ab4382d2SGreg Kroah-Hartman {
252ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
253ab4382d2SGreg Kroah-Hartman 
25464e22ebeSElen Song 	return atmel_port->use_pdc_tx;
255ab4382d2SGreg Kroah-Hartman }
256ab4382d2SGreg Kroah-Hartman #else
25764e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
258ab4382d2SGreg Kroah-Hartman {
259ab4382d2SGreg Kroah-Hartman 	return false;
260ab4382d2SGreg Kroah-Hartman }
261ab4382d2SGreg Kroah-Hartman 
26264e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
263ab4382d2SGreg Kroah-Hartman {
264ab4382d2SGreg Kroah-Hartman 	return false;
265ab4382d2SGreg Kroah-Hartman }
266ab4382d2SGreg Kroah-Hartman #endif
267ab4382d2SGreg Kroah-Hartman 
26808f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26908f738beSElen Song {
27008f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27108f738beSElen Song 
27208f738beSElen Song 	return atmel_port->use_dma_tx;
27308f738beSElen Song }
27408f738beSElen Song 
27534df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27634df42f5SElen Song {
27734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27834df42f5SElen Song 
27934df42f5SElen Song 	return atmel_port->use_dma_rx;
28034df42f5SElen Song }
28134df42f5SElen Song 
2825be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2835be605acSAlexandre Belloni {
2845be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2855be605acSAlexandre Belloni 
2865be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2875be605acSAlexandre Belloni }
2885be605acSAlexandre Belloni 
28998f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
29098f2082cSNicolas Ferre 				   struct tasklet_struct *t)
29198f2082cSNicolas Ferre {
29298f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown))
29398f2082cSNicolas Ferre 		tasklet_schedule(t);
29498f2082cSNicolas Ferre }
29598f2082cSNicolas Ferre 
296e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
297e0b0baadSRichard Genoud {
298e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
299e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
300e0b0baadSRichard Genoud 
3014e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
302e0b0baadSRichard Genoud 
303e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
304e0b0baadSRichard Genoud 
305e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
306e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
307e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
308e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
309e0b0baadSRichard Genoud 		else
310e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
311e0b0baadSRichard Genoud 	}
312e0b0baadSRichard Genoud 
313e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
314e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
315e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
316e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
317e0b0baadSRichard Genoud 		else
318e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
319e0b0baadSRichard Genoud 	}
320e0b0baadSRichard Genoud 
321e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
322e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
323e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
324e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
325e0b0baadSRichard Genoud 		else
326e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
327e0b0baadSRichard Genoud 	}
328e0b0baadSRichard Genoud 
329e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
330e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
331e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
332e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
333e0b0baadSRichard Genoud 		else
334e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
335e0b0baadSRichard Genoud 	}
336e0b0baadSRichard Genoud 
337e0b0baadSRichard Genoud 	return status;
338e0b0baadSRichard Genoud }
339e0b0baadSRichard Genoud 
340ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
34113bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
34213bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
343ab4382d2SGreg Kroah-Hartman {
344ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
345ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
346ab4382d2SGreg Kroah-Hartman 
347ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3484e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
349ab4382d2SGreg Kroah-Hartman 
3504e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
351ab4382d2SGreg Kroah-Hartman 
352ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
353ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
354ab4382d2SGreg Kroah-Hartman 
35513bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
356ab4382d2SGreg Kroah-Hartman 
357ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
358ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
359ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3604e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3614e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
362ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
363ab4382d2SGreg Kroah-Hartman 	} else {
364ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
36564e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
366ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
367ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
368ab4382d2SGreg Kroah-Hartman 		else
369ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
370ab4382d2SGreg Kroah-Hartman 	}
3714e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
372ab4382d2SGreg Kroah-Hartman 
373ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
375ab4382d2SGreg Kroah-Hartman 
37613bd3e6fSRicardo Ribalda Delgado 	return 0;
377ab4382d2SGreg Kroah-Hartman }
378ab4382d2SGreg Kroah-Hartman 
379ab4382d2SGreg Kroah-Hartman /*
380ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
381ab4382d2SGreg Kroah-Hartman  */
382ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
383ab4382d2SGreg Kroah-Hartman {
384ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
385ea04f82aSRomain Izard 
386ea04f82aSRomain Izard 	if (atmel_port->tx_stopped)
387ea04f82aSRomain Izard 		return TIOCSER_TEMT;
3884e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3894e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3904e7decdaSCyrille Pitchen 		0;
391ab4382d2SGreg Kroah-Hartman }
392ab4382d2SGreg Kroah-Hartman 
393ab4382d2SGreg Kroah-Hartman /*
394ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
395ab4382d2SGreg Kroah-Hartman  */
396ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
397ab4382d2SGreg Kroah-Hartman {
398ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3994e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
4001cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
401ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
402ab4382d2SGreg Kroah-Hartman 
4031cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
4041cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
4054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
4064e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
4071cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
4081cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
4091cf6e8fcSCyrille Pitchen 	}
4101cf6e8fcSCyrille Pitchen 
4111cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
4121cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
4131cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4141cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4151cf6e8fcSCyrille Pitchen 
4161cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4171cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4181cf6e8fcSCyrille Pitchen 	} else {
4191cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4201cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4211cf6e8fcSCyrille Pitchen 
4221cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4231cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4241cf6e8fcSCyrille Pitchen 	}
4251cf6e8fcSCyrille Pitchen 
426ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4271cf6e8fcSCyrille Pitchen 		control |= rts_ready;
428ab4382d2SGreg Kroah-Hartman 	else
4291cf6e8fcSCyrille Pitchen 		control |= rts_paused;
430ab4382d2SGreg Kroah-Hartman 
431ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
432ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
433ab4382d2SGreg Kroah-Hartman 	else
434ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
435ab4382d2SGreg Kroah-Hartman 
4364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
437ab4382d2SGreg Kroah-Hartman 
438e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
439e0b0baadSRichard Genoud 
440ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
4411cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
442ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
443ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
444ab4382d2SGreg Kroah-Hartman 	else
445ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
446ab4382d2SGreg Kroah-Hartman 
4474e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
448ab4382d2SGreg Kroah-Hartman }
449ab4382d2SGreg Kroah-Hartman 
450ab4382d2SGreg Kroah-Hartman /*
451ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
452ab4382d2SGreg Kroah-Hartman  */
453ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
454ab4382d2SGreg Kroah-Hartman {
455e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
456e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
457ab4382d2SGreg Kroah-Hartman 
4584e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
459ab4382d2SGreg Kroah-Hartman 
460ab4382d2SGreg Kroah-Hartman 	/*
461ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
462ab4382d2SGreg Kroah-Hartman 	 */
463ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
464ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
465ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
466ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
467ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
468ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
469ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
470ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
471ab4382d2SGreg Kroah-Hartman 
472e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
473ab4382d2SGreg Kroah-Hartman }
474ab4382d2SGreg Kroah-Hartman 
475ab4382d2SGreg Kroah-Hartman /*
476ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
477ab4382d2SGreg Kroah-Hartman  */
478ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
479ab4382d2SGreg Kroah-Hartman {
480ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
481ab4382d2SGreg Kroah-Hartman 
48264e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
483ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4844e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
485ab4382d2SGreg Kroah-Hartman 	}
48689d82324SRichard Genoud 
48789d82324SRichard Genoud 	/*
48889d82324SRichard Genoud 	 * Disable the transmitter.
48989d82324SRichard Genoud 	 * This is mandatory when DMA is used, otherwise the DMA buffer
49089d82324SRichard Genoud 	 * is fully transmitted.
49189d82324SRichard Genoud 	 */
49289d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
493ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
49489d82324SRichard Genoud 
495ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4964e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
497ab4382d2SGreg Kroah-Hartman 
49813bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
49913bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
500ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
501ab4382d2SGreg Kroah-Hartman }
502ab4382d2SGreg Kroah-Hartman 
503ab4382d2SGreg Kroah-Hartman /*
504ab4382d2SGreg Kroah-Hartman  * Start transmitting.
505ab4382d2SGreg Kroah-Hartman  */
506ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
507ab4382d2SGreg Kroah-Hartman {
508ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
509ab4382d2SGreg Kroah-Hartman 
5100058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
5110058f087SAlexandre Belloni 				       & ATMEL_PDC_TXTEN))
512ab4382d2SGreg Kroah-Hartman 		/* The transmitter is already running.  Yes, we
513ab4382d2SGreg Kroah-Hartman 		   really need this.*/
514ab4382d2SGreg Kroah-Hartman 		return;
515ab4382d2SGreg Kroah-Hartman 
5160058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
51713bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
51813bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
519ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
520ab4382d2SGreg Kroah-Hartman 
5210058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port))
522ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
5234e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
5240058f087SAlexandre Belloni 
525ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
5264e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
52789d82324SRichard Genoud 
52889d82324SRichard Genoud 	/* re-enable the transmitter */
52989d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
530ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
531ab4382d2SGreg Kroah-Hartman }
532ab4382d2SGreg Kroah-Hartman 
533ab4382d2SGreg Kroah-Hartman /*
534ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
535ab4382d2SGreg Kroah-Hartman  */
536ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
537ab4382d2SGreg Kroah-Hartman {
5384e7decdaSCyrille Pitchen 	/* reset status and receiver */
5394e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
540ab4382d2SGreg Kroah-Hartman 
5414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
54257c36868SSiftar, Gabe 
54364e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
544ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
5454e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
5464e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
547ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
5484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
549ab4382d2SGreg Kroah-Hartman 	} else {
5504e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
551ab4382d2SGreg Kroah-Hartman 	}
552ab4382d2SGreg Kroah-Hartman }
553ab4382d2SGreg Kroah-Hartman 
554ab4382d2SGreg Kroah-Hartman /*
555ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
556ab4382d2SGreg Kroah-Hartman  */
557ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
558ab4382d2SGreg Kroah-Hartman {
5594e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
56057c36868SSiftar, Gabe 
56164e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
562ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5634e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5644e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5654e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
566ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
567ab4382d2SGreg Kroah-Hartman 	} else {
5684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
569ab4382d2SGreg Kroah-Hartman 	}
570ab4382d2SGreg Kroah-Hartman }
571ab4382d2SGreg Kroah-Hartman 
572ab4382d2SGreg Kroah-Hartman /*
573ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
574ab4382d2SGreg Kroah-Hartman  */
575ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
576ab4382d2SGreg Kroah-Hartman {
577ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
578ab5e4e41SRichard Genoud 	uint32_t ier = 0;
579ab5e4e41SRichard Genoud 
580ab5e4e41SRichard Genoud 	/*
581ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
582ab5e4e41SRichard Genoud 	 */
583ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
584ab5e4e41SRichard Genoud 		return;
585ab5e4e41SRichard Genoud 
586ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
587ab5e4e41SRichard Genoud 
58818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
589ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
590ab5e4e41SRichard Genoud 
59118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
592ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
593ab5e4e41SRichard Genoud 
59418dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
595ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
596ab5e4e41SRichard Genoud 
59718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
598ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
599ab5e4e41SRichard Genoud 
6004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
60118dfef9cSUwe Kleine-König 
60218dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
603ab4382d2SGreg Kroah-Hartman }
604ab4382d2SGreg Kroah-Hartman 
605ab4382d2SGreg Kroah-Hartman /*
60635b675b9SRichard Genoud  * Disable modem status interrupts
60735b675b9SRichard Genoud  */
60835b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
60935b675b9SRichard Genoud {
61035b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
61135b675b9SRichard Genoud 	uint32_t idr = 0;
61235b675b9SRichard Genoud 
61335b675b9SRichard Genoud 	/*
61435b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
61535b675b9SRichard Genoud 	 */
61635b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
61735b675b9SRichard Genoud 		return;
61835b675b9SRichard Genoud 
61935b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
62035b675b9SRichard Genoud 
62118dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
62218dfef9cSUwe Kleine-König 
62318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
62435b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
62535b675b9SRichard Genoud 
62618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
62735b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
62835b675b9SRichard Genoud 
62918dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
63035b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
63135b675b9SRichard Genoud 
63218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
63335b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
63435b675b9SRichard Genoud 
6354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
63635b675b9SRichard Genoud }
63735b675b9SRichard Genoud 
63835b675b9SRichard Genoud /*
639ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
640ab4382d2SGreg Kroah-Hartman  */
641ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
642ab4382d2SGreg Kroah-Hartman {
643ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
6444e7decdaSCyrille Pitchen 		/* start break */
6454e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
646ab4382d2SGreg Kroah-Hartman 	else
6474e7decdaSCyrille Pitchen 		/* stop break */
6484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
649ab4382d2SGreg Kroah-Hartman }
650ab4382d2SGreg Kroah-Hartman 
651ab4382d2SGreg Kroah-Hartman /*
652ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
653ab4382d2SGreg Kroah-Hartman  */
654ab4382d2SGreg Kroah-Hartman static void
655ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
656ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
657ab4382d2SGreg Kroah-Hartman {
658ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
659ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
660ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
661ab4382d2SGreg Kroah-Hartman 
662ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
663ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
664ab4382d2SGreg Kroah-Hartman 		return;
665ab4382d2SGreg Kroah-Hartman 
666ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
667ab4382d2SGreg Kroah-Hartman 	c->status	= status;
668ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
669ab4382d2SGreg Kroah-Hartman 
670ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
671ab4382d2SGreg Kroah-Hartman 	smp_wmb();
672ab4382d2SGreg Kroah-Hartman 
673ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
674ab4382d2SGreg Kroah-Hartman }
675ab4382d2SGreg Kroah-Hartman 
676ab4382d2SGreg Kroah-Hartman /*
677ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
678ab4382d2SGreg Kroah-Hartman  */
679ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
680ab4382d2SGreg Kroah-Hartman {
681ab4382d2SGreg Kroah-Hartman 	/* clear error */
6824e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
683ab4382d2SGreg Kroah-Hartman 
684ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
685ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
686ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
687ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
688ab4382d2SGreg Kroah-Hartman 	}
689ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
690ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
691ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
692ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
693ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
694ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
695ab4382d2SGreg Kroah-Hartman }
696ab4382d2SGreg Kroah-Hartman 
697ab4382d2SGreg Kroah-Hartman /*
698ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
699ab4382d2SGreg Kroah-Hartman  */
700ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
701ab4382d2SGreg Kroah-Hartman {
702ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
703ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
704ab4382d2SGreg Kroah-Hartman 
7054e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
706ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
707a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
708ab4382d2SGreg Kroah-Hartman 
709ab4382d2SGreg Kroah-Hartman 		/*
710ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
711ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
712ab4382d2SGreg Kroah-Hartman 		 */
713ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
714ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
715ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
716ab4382d2SGreg Kroah-Hartman 
717ab4382d2SGreg Kroah-Hartman 			/* clear error */
7184e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
719ab4382d2SGreg Kroah-Hartman 
720ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
721ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
722ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
7234e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
7244e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
725ab4382d2SGreg Kroah-Hartman 			} else {
726ab4382d2SGreg Kroah-Hartman 				/*
727ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
728ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
729ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
730ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
731ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
732ab4382d2SGreg Kroah-Hartman 				 */
7334e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
7344e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
735ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
736ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
737ab4382d2SGreg Kroah-Hartman 			}
738ab4382d2SGreg Kroah-Hartman 		}
739ab4382d2SGreg Kroah-Hartman 
740ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
7414e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
742ab4382d2SGreg Kroah-Hartman 	}
743ab4382d2SGreg Kroah-Hartman 
74498f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
745ab4382d2SGreg Kroah-Hartman }
746ab4382d2SGreg Kroah-Hartman 
747ab4382d2SGreg Kroah-Hartman /*
748ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
749ab4382d2SGreg Kroah-Hartman  * disabled)
750ab4382d2SGreg Kroah-Hartman  */
751ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
752ab4382d2SGreg Kroah-Hartman {
753ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
754ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
755ab4382d2SGreg Kroah-Hartman 
7564e7decdaSCyrille Pitchen 	if (port->x_char &&
7574e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
758a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
759ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
760ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
761ab4382d2SGreg Kroah-Hartman 	}
762ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
763ab4382d2SGreg Kroah-Hartman 		return;
764ab4382d2SGreg Kroah-Hartman 
7654e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7664e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
767a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
768ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
769ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
770ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
771ab4382d2SGreg Kroah-Hartman 			break;
772ab4382d2SGreg Kroah-Hartman 	}
773ab4382d2SGreg Kroah-Hartman 
774ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
775ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
776ab4382d2SGreg Kroah-Hartman 
777ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
778ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7794e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7804e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
781ab4382d2SGreg Kroah-Hartman }
782ab4382d2SGreg Kroah-Hartman 
78308f738beSElen Song static void atmel_complete_tx_dma(void *arg)
78408f738beSElen Song {
78508f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
78608f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
78708f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
78808f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
78908f738beSElen Song 	unsigned long flags;
79008f738beSElen Song 
79108f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
79208f738beSElen Song 
79308f738beSElen Song 	if (chan)
79408f738beSElen Song 		dmaengine_terminate_all(chan);
7955f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
79608f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
79708f738beSElen Song 
7985f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
79908f738beSElen Song 
80008f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
80108f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
80208f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
80308f738beSElen Song 	atmel_port->desc_tx = NULL;
80408f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
80508f738beSElen Song 
80608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
80708f738beSElen Song 		uart_write_wakeup(port);
80808f738beSElen Song 
8091842dc2eSCyrille Pitchen 	/*
8101842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
8111842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
8121842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
8131842dc2eSCyrille Pitchen 	 */
81408f738beSElen Song 	if (!uart_circ_empty(xmit))
81598f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
816b389f173SRichard Genoud 	else if ((port->rs485.flags & SER_RS485_ENABLED) &&
817b389f173SRichard Genoud 		 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
818b389f173SRichard Genoud 		/* DMA done, stop TX, start RX for RS485 */
819b389f173SRichard Genoud 		atmel_start_rx(port);
820b389f173SRichard Genoud 	}
82108f738beSElen Song 
82208f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
82308f738beSElen Song }
82408f738beSElen Song 
82508f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
82608f738beSElen Song {
82708f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
82808f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
82908f738beSElen Song 
83008f738beSElen Song 	if (chan) {
83108f738beSElen Song 		dmaengine_terminate_all(chan);
83208f738beSElen Song 		dma_release_channel(chan);
83308f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
83448479148SWolfram Sang 				DMA_TO_DEVICE);
83508f738beSElen Song 	}
83608f738beSElen Song 
83708f738beSElen Song 	atmel_port->desc_tx = NULL;
83808f738beSElen Song 	atmel_port->chan_tx = NULL;
83908f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
84008f738beSElen Song }
84108f738beSElen Song 
84208f738beSElen Song /*
84308f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
84408f738beSElen Song  */
84508f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
84608f738beSElen Song {
84708f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
84808f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
84908f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
85008f738beSElen Song 	struct dma_async_tx_descriptor *desc;
8515f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
8525f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
8535f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
85408f738beSElen Song 
85508f738beSElen Song 	/* Make sure we have an idle channel */
85608f738beSElen Song 	if (atmel_port->desc_tx != NULL)
85708f738beSElen Song 		return;
85808f738beSElen Song 
85908f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
86008f738beSElen Song 		/*
86108f738beSElen Song 		 * DMA is idle now.
86208f738beSElen Song 		 * Port xmit buffer is already mapped,
86308f738beSElen Song 		 * and it is one page... Just adjust
86408f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
86508f738beSElen Song 		 * we have to transmit till the end, and then the rest.
86608f738beSElen Song 		 * Take the port lock to get a
86708f738beSElen Song 		 * consistent xmit buffer state.
86808f738beSElen Song 		 */
8695f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
87008f738beSElen Song 					 xmit->tail,
87108f738beSElen Song 					 UART_XMIT_SIZE);
8725f258b3eSCyrille Pitchen 
8735f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
8745f258b3eSCyrille Pitchen 			/* multi data mode */
8755f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
8765f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
8775f258b3eSCyrille Pitchen 		} else {
8785f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
8795f258b3eSCyrille Pitchen 			part1_len = 0;
8805f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
8815f258b3eSCyrille Pitchen 		}
8825f258b3eSCyrille Pitchen 
8835f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
8845f258b3eSCyrille Pitchen 		sg_len = 0;
8855f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
8865f258b3eSCyrille Pitchen 		if (part1_len) {
8875f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8885f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8895f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
8905f258b3eSCyrille Pitchen 
8915f258b3eSCyrille Pitchen 			phys_addr += part1_len;
8925f258b3eSCyrille Pitchen 		}
8935f258b3eSCyrille Pitchen 
8945f258b3eSCyrille Pitchen 		if (part2_len) {
8955f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8965f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8975f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
8985f258b3eSCyrille Pitchen 		}
8995f258b3eSCyrille Pitchen 
9005f258b3eSCyrille Pitchen 		/*
9015f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
9025f258b3eSCyrille Pitchen 		 * xmit->tail correctly
9035f258b3eSCyrille Pitchen 		 */
9045f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
90508f738beSElen Song 
90608f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
9075f258b3eSCyrille Pitchen 					       sgl,
9085f258b3eSCyrille Pitchen 					       sg_len,
90908f738beSElen Song 					       DMA_MEM_TO_DEV,
91008f738beSElen Song 					       DMA_PREP_INTERRUPT |
91108f738beSElen Song 					       DMA_CTRL_ACK);
91208f738beSElen Song 		if (!desc) {
91308f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
91408f738beSElen Song 			return;
91508f738beSElen Song 		}
91608f738beSElen Song 
9175f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
91808f738beSElen Song 
91908f738beSElen Song 		atmel_port->desc_tx = desc;
92008f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
92108f738beSElen Song 		desc->callback_param = atmel_port;
92208f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
92308f738beSElen Song 	}
92408f738beSElen Song 
92508f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
92608f738beSElen Song 		uart_write_wakeup(port);
92708f738beSElen Song }
92808f738beSElen Song 
92908f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
93008f738beSElen Song {
93108f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
93208f738beSElen Song 	dma_cap_mask_t		mask;
93308f738beSElen Song 	struct dma_slave_config config;
93408f738beSElen Song 	int ret, nent;
93508f738beSElen Song 
93608f738beSElen Song 	dma_cap_zero(mask);
93708f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
93808f738beSElen Song 
93908f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
94008f738beSElen Song 	if (atmel_port->chan_tx == NULL)
94108f738beSElen Song 		goto chan_err;
94208f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
94308f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
94408f738beSElen Song 
94508f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
94608f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
94708f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
9482c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
94908f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
95008f738beSElen Song 			virt_to_page(port->state->xmit.buf),
95108f738beSElen Song 			UART_XMIT_SIZE,
9522b5cf14bSGeliang Tang 			offset_in_page(port->state->xmit.buf));
95308f738beSElen Song 	nent = dma_map_sg(port->dev,
95408f738beSElen Song 				&atmel_port->sg_tx,
95508f738beSElen Song 				1,
95648479148SWolfram Sang 				DMA_TO_DEVICE);
95708f738beSElen Song 
95808f738beSElen Song 	if (!nent) {
95908f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
96008f738beSElen Song 		goto chan_err;
96108f738beSElen Song 	} else {
962c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
96308f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
96408f738beSElen Song 			port->state->xmit.buf,
965c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
96608f738beSElen Song 	}
96708f738beSElen Song 
96808f738beSElen Song 	/* Configure the slave DMA */
96908f738beSElen Song 	memset(&config, 0, sizeof(config));
97008f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
9715f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
9725f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
9735f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
97408f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
975a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
97608f738beSElen Song 
9775483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9785483c10eSMaxime Ripard 				     &config);
97908f738beSElen Song 	if (ret) {
98008f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
98108f738beSElen Song 		goto chan_err;
98208f738beSElen Song 	}
98308f738beSElen Song 
98408f738beSElen Song 	return 0;
98508f738beSElen Song 
98608f738beSElen Song chan_err:
98708f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
98808f738beSElen Song 	atmel_port->use_dma_tx = 0;
98908f738beSElen Song 	if (atmel_port->chan_tx)
99008f738beSElen Song 		atmel_release_tx_dma(port);
99108f738beSElen Song 	return -EINVAL;
99208f738beSElen Song }
99308f738beSElen Song 
99434df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
99534df42f5SElen Song {
99634df42f5SElen Song 	struct uart_port *port = arg;
99734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
99834df42f5SElen Song 
99998f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
100034df42f5SElen Song }
100134df42f5SElen Song 
100234df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
100334df42f5SElen Song {
100434df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
100534df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
100634df42f5SElen Song 
100734df42f5SElen Song 	if (chan) {
100834df42f5SElen Song 		dmaengine_terminate_all(chan);
100934df42f5SElen Song 		dma_release_channel(chan);
101034df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
101148479148SWolfram Sang 				DMA_FROM_DEVICE);
101234df42f5SElen Song 	}
101334df42f5SElen Song 
101434df42f5SElen Song 	atmel_port->desc_rx = NULL;
101534df42f5SElen Song 	atmel_port->chan_rx = NULL;
101634df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
101734df42f5SElen Song }
101834df42f5SElen Song 
101934df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
102034df42f5SElen Song {
102134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
102266f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
102334df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
102434df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
102534df42f5SElen Song 	struct dma_tx_state state;
102634df42f5SElen Song 	enum dma_status dmastat;
102766f37aafSCyrille Pitchen 	size_t count;
102834df42f5SElen Song 
102934df42f5SElen Song 
103034df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
10314e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
103234df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
103334df42f5SElen Song 				atmel_port->cookie_rx,
103434df42f5SElen Song 				&state);
103534df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
103634df42f5SElen Song 	if (dmastat == DMA_ERROR) {
103734df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
10384e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
103998f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
104034df42f5SElen Song 		return;
104134df42f5SElen Song 	}
104266f37aafSCyrille Pitchen 
104366f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
104466f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
104566f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
104666f37aafSCyrille Pitchen 			    1,
1047485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
104834df42f5SElen Song 
104934df42f5SElen Song 	/*
105066f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
105166f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
105266f37aafSCyrille Pitchen 	 * framework.
105366f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
105466f37aafSCyrille Pitchen 	 * length.
105534df42f5SElen Song 	 */
105666f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
105766f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
105866f37aafSCyrille Pitchen 	/*
105966f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
106066f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
106166f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
106266f37aafSCyrille Pitchen 	 *
106366f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
106466f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
106566f37aafSCyrille Pitchen 	 *
106666f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
106766f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
106866f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
106966f37aafSCyrille Pitchen 	 */
107066f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
107166f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
107234df42f5SElen Song 
107366f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
107466f37aafSCyrille Pitchen 		ring->tail = 0;
107534df42f5SElen Song 		port->icount.rx += count;
107634df42f5SElen Song 	}
107734df42f5SElen Song 
107866f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
107966f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
108066f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
108166f37aafSCyrille Pitchen 
108266f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
108366f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
108466f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
108566f37aafSCyrille Pitchen 			ring->head = 0;
108666f37aafSCyrille Pitchen 		ring->tail = ring->head;
108766f37aafSCyrille Pitchen 		port->icount.rx += count;
108866f37aafSCyrille Pitchen 	}
108966f37aafSCyrille Pitchen 
109066f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
109166f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
109266f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
109366f37aafSCyrille Pitchen 			       1,
1094485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
109566f37aafSCyrille Pitchen 
109666f37aafSCyrille Pitchen 	/*
109766f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
109866f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
109966f37aafSCyrille Pitchen 	 */
110066f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
110166f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
110266f37aafSCyrille Pitchen 	spin_lock(&port->lock);
110366f37aafSCyrille Pitchen 
11044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
110534df42f5SElen Song }
110634df42f5SElen Song 
110734df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
110834df42f5SElen Song {
110934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
111034df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
111134df42f5SElen Song 	dma_cap_mask_t		mask;
111234df42f5SElen Song 	struct dma_slave_config config;
111334df42f5SElen Song 	struct circ_buf		*ring;
111434df42f5SElen Song 	int ret, nent;
111534df42f5SElen Song 
111634df42f5SElen Song 	ring = &atmel_port->rx_ring;
111734df42f5SElen Song 
111834df42f5SElen Song 	dma_cap_zero(mask);
111934df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
112034df42f5SElen Song 
112134df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
112234df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
112334df42f5SElen Song 		goto chan_err;
112434df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
112534df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
112634df42f5SElen Song 
112734df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
112834df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
112934df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
11302c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
113134df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
113234df42f5SElen Song 		    virt_to_page(ring->buf),
1133a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
11342b5cf14bSGeliang Tang 		    offset_in_page(ring->buf));
113534df42f5SElen Song 	nent = dma_map_sg(port->dev,
113634df42f5SElen Song 			  &atmel_port->sg_rx,
113734df42f5SElen Song 			  1,
113848479148SWolfram Sang 			  DMA_FROM_DEVICE);
113934df42f5SElen Song 
114034df42f5SElen Song 	if (!nent) {
114134df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
114234df42f5SElen Song 		goto chan_err;
114334df42f5SElen Song 	} else {
1144c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
114534df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
114634df42f5SElen Song 			ring->buf,
1147c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
114834df42f5SElen Song 	}
114934df42f5SElen Song 
115034df42f5SElen Song 	/* Configure the slave DMA */
115134df42f5SElen Song 	memset(&config, 0, sizeof(config));
115234df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
115334df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
115434df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1155a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
115634df42f5SElen Song 
11575483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
11585483c10eSMaxime Ripard 				     &config);
115934df42f5SElen Song 	if (ret) {
116034df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
116134df42f5SElen Song 		goto chan_err;
116234df42f5SElen Song 	}
116334df42f5SElen Song 	/*
116434df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
116534df42f5SElen Song 	 * each one is half ring buffer size
116634df42f5SElen Song 	 */
116734df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
116834df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
116934df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
117034df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
117134df42f5SElen Song 					 DMA_DEV_TO_MEM,
117234df42f5SElen Song 					 DMA_PREP_INTERRUPT);
117334df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
117434df42f5SElen Song 	desc->callback_param = port;
117534df42f5SElen Song 	atmel_port->desc_rx = desc;
117634df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
117734df42f5SElen Song 
117834df42f5SElen Song 	return 0;
117934df42f5SElen Song 
118034df42f5SElen Song chan_err:
118134df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
118234df42f5SElen Song 	atmel_port->use_dma_rx = 0;
118334df42f5SElen Song 	if (atmel_port->chan_rx)
118434df42f5SElen Song 		atmel_release_rx_dma(port);
118534df42f5SElen Song 	return -EINVAL;
118634df42f5SElen Song }
118734df42f5SElen Song 
1188*026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t)
11892e68c22fSElen Song {
1190*026cb432SKees Cook 	struct atmel_uart_port *atmel_port = from_timer(atmel_port, t,
1191*026cb432SKees Cook 							uart_timer);
1192*026cb432SKees Cook 	struct uart_port *port = &atmel_port->uart;
11932e68c22fSElen Song 
119498f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown)) {
119500e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
119698f2082cSNicolas Ferre 		mod_timer(&atmel_port->uart_timer,
119798f2082cSNicolas Ferre 			  jiffies + uart_poll_timeout(port));
119898f2082cSNicolas Ferre 	}
11992e68c22fSElen Song }
12002e68c22fSElen Song 
1201ab4382d2SGreg Kroah-Hartman /*
1202ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1203ab4382d2SGreg Kroah-Hartman  */
1204ab4382d2SGreg Kroah-Hartman static void
1205ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1206ab4382d2SGreg Kroah-Hartman {
1207ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1208ab4382d2SGreg Kroah-Hartman 
120964e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1210ab4382d2SGreg Kroah-Hartman 		/*
1211ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1212ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1213ab4382d2SGreg Kroah-Hartman 		 *
1214ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1215ab4382d2SGreg Kroah-Hartman 		 * the moment.
1216ab4382d2SGreg Kroah-Hartman 		 */
1217ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
12184e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12194e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
122098f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
122198f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
1222ab4382d2SGreg Kroah-Hartman 		}
1223ab4382d2SGreg Kroah-Hartman 
1224ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1225ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1226ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1227ab4382d2SGreg Kroah-Hartman 	}
1228ab4382d2SGreg Kroah-Hartman 
122934df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
123034df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
12314e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12324e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
123398f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
123498f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
123534df42f5SElen Song 		}
123634df42f5SElen Song 	}
123734df42f5SElen Song 
1238ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1239ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1240ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1241ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1242ab4382d2SGreg Kroah-Hartman 		/*
1243ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1244ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1245ab4382d2SGreg Kroah-Hartman 		 */
12464e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
12474e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1248ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1249ab4382d2SGreg Kroah-Hartman 	}
1250ab4382d2SGreg Kroah-Hartman }
1251ab4382d2SGreg Kroah-Hartman 
1252ab4382d2SGreg Kroah-Hartman /*
1253ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1254ab4382d2SGreg Kroah-Hartman  */
1255ab4382d2SGreg Kroah-Hartman static void
1256ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1257ab4382d2SGreg Kroah-Hartman {
1258ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1259ab4382d2SGreg Kroah-Hartman 
1260ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1261ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
12624e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
12634e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
126498f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1265ab4382d2SGreg Kroah-Hartman 	}
1266ab4382d2SGreg Kroah-Hartman }
1267ab4382d2SGreg Kroah-Hartman 
1268ab4382d2SGreg Kroah-Hartman /*
1269ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1270ab4382d2SGreg Kroah-Hartman  */
1271ab4382d2SGreg Kroah-Hartman static void
1272ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1273ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1274ab4382d2SGreg Kroah-Hartman {
1275ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12769205218eSNicolas Ferre 	unsigned int status_change;
1277ab4382d2SGreg Kroah-Hartman 
1278ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1279ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
12809205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1281d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
12829205218eSNicolas Ferre 
12839205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
12849205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
12859205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
12869205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
12879205218eSNicolas Ferre 				port->icount.rng++;
12889205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
12899205218eSNicolas Ferre 				port->icount.dsr++;
12909205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
12919205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
12929205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
12939205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
12949205218eSNicolas Ferre 
12959205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
12969205218eSNicolas Ferre 		}
1297ab4382d2SGreg Kroah-Hartman 	}
1298ab4382d2SGreg Kroah-Hartman }
1299ab4382d2SGreg Kroah-Hartman 
1300ab4382d2SGreg Kroah-Hartman /*
1301ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1302ab4382d2SGreg Kroah-Hartman  */
1303ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1304ab4382d2SGreg Kroah-Hartman {
1305ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1306ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
13072c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1308ab4382d2SGreg Kroah-Hartman 
13092c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
13102c7af5baSBoris BREZILLON 
1311ab4382d2SGreg Kroah-Hartman 	do {
1312e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
13134e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
13142c7af5baSBoris BREZILLON 		pending = status & mask;
1315ab4382d2SGreg Kroah-Hartman 		if (!pending)
1316ab4382d2SGreg Kroah-Hartman 			break;
1317ab4382d2SGreg Kroah-Hartman 
13182c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
13192c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
13202c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
13214e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
13222c7af5baSBoris BREZILLON 			pm_system_wakeup();
13232c7af5baSBoris BREZILLON 			break;
13242c7af5baSBoris BREZILLON 		}
13252c7af5baSBoris BREZILLON 
1326ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1327ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1328ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1329ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1330ab4382d2SGreg Kroah-Hartman 
13312c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
13322c7af5baSBoris BREZILLON 
1333ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1334ab4382d2SGreg Kroah-Hartman }
1335ab4382d2SGreg Kroah-Hartman 
1336a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1337a930e528SElen Song {
1338a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1339a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1340a930e528SElen Song 
1341a930e528SElen Song 	dma_unmap_single(port->dev,
1342a930e528SElen Song 			 pdc->dma_addr,
1343a930e528SElen Song 			 pdc->dma_size,
1344a930e528SElen Song 			 DMA_TO_DEVICE);
1345a930e528SElen Song }
1346a930e528SElen Song 
1347ab4382d2SGreg Kroah-Hartman /*
1348ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1349ab4382d2SGreg Kroah-Hartman  */
135064e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1351ab4382d2SGreg Kroah-Hartman {
1352ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1353ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1354ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1355ab4382d2SGreg Kroah-Hartman 	int count;
1356ab4382d2SGreg Kroah-Hartman 
1357ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
13584e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1359ab4382d2SGreg Kroah-Hartman 		return;
1360ab4382d2SGreg Kroah-Hartman 
1361ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1362ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1363ab4382d2SGreg Kroah-Hartman 
1364ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1365ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1366ab4382d2SGreg Kroah-Hartman 
1367ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1368ab4382d2SGreg Kroah-Hartman 
1369ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
13704e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1371ab4382d2SGreg Kroah-Hartman 
1372ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1373ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1374ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1375ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1376ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1377ab4382d2SGreg Kroah-Hartman 
1378ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1379ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1380ab4382d2SGreg Kroah-Hartman 
13814e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13824e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13834e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1384ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13854e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1386ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13874e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13884e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1389ab4382d2SGreg Kroah-Hartman 	} else {
139013bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
139113bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1392ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1393ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1394ab4382d2SGreg Kroah-Hartman 		}
1395ab4382d2SGreg Kroah-Hartman 	}
1396ab4382d2SGreg Kroah-Hartman 
1397ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1398ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1399ab4382d2SGreg Kroah-Hartman }
1400ab4382d2SGreg Kroah-Hartman 
1401a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1402a930e528SElen Song {
1403a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1404a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1405a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1406a930e528SElen Song 
1407a930e528SElen Song 	pdc->buf = xmit->buf;
1408a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1409a930e528SElen Song 					pdc->buf,
1410a930e528SElen Song 					UART_XMIT_SIZE,
1411a930e528SElen Song 					DMA_TO_DEVICE);
1412a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1413a930e528SElen Song 	pdc->ofs = 0;
1414a930e528SElen Song 
1415a930e528SElen Song 	return 0;
1416a930e528SElen Song }
1417a930e528SElen Song 
1418ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1419ab4382d2SGreg Kroah-Hartman {
1420ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1421ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1422ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1423ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1424ab4382d2SGreg Kroah-Hartman 
1425ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1426ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1427ab4382d2SGreg Kroah-Hartman 
1428ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1429ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1430ab4382d2SGreg Kroah-Hartman 
1431ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1432ab4382d2SGreg Kroah-Hartman 
1433ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1434ab4382d2SGreg Kroah-Hartman 
1435ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1436ab4382d2SGreg Kroah-Hartman 		status = c.status;
1437ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1438ab4382d2SGreg Kroah-Hartman 
1439ab4382d2SGreg Kroah-Hartman 		/*
1440ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1441ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1442ab4382d2SGreg Kroah-Hartman 		 */
1443ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1444ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1445ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1446ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1447ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1448ab4382d2SGreg Kroah-Hartman 
1449ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1450ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1451ab4382d2SGreg Kroah-Hartman 					continue;
1452ab4382d2SGreg Kroah-Hartman 			}
1453ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1454ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1455ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1456ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1457ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1458ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1459ab4382d2SGreg Kroah-Hartman 
1460ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1461ab4382d2SGreg Kroah-Hartman 
1462ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1463ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1464ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1465ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1466ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1467ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1468ab4382d2SGreg Kroah-Hartman 		}
1469ab4382d2SGreg Kroah-Hartman 
1470ab4382d2SGreg Kroah-Hartman 
1471ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1472ab4382d2SGreg Kroah-Hartman 			continue;
1473ab4382d2SGreg Kroah-Hartman 
1474ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1475ab4382d2SGreg Kroah-Hartman 	}
1476ab4382d2SGreg Kroah-Hartman 
1477ab4382d2SGreg Kroah-Hartman 	/*
1478ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1479ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1480ab4382d2SGreg Kroah-Hartman 	 */
1481ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14822e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1483ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1484ab4382d2SGreg Kroah-Hartman }
1485ab4382d2SGreg Kroah-Hartman 
1486a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1487a930e528SElen Song {
1488a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1489a930e528SElen Song 	int i;
1490a930e528SElen Song 
1491a930e528SElen Song 	for (i = 0; i < 2; i++) {
1492a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1493a930e528SElen Song 
1494a930e528SElen Song 		dma_unmap_single(port->dev,
1495a930e528SElen Song 				 pdc->dma_addr,
1496a930e528SElen Song 				 pdc->dma_size,
1497a930e528SElen Song 				 DMA_FROM_DEVICE);
1498a930e528SElen Song 		kfree(pdc->buf);
1499a930e528SElen Song 	}
1500a930e528SElen Song }
1501a930e528SElen Song 
150264e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1503ab4382d2SGreg Kroah-Hartman {
1504ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
150505c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1506ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1507ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1508ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1509ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1510ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1511ab4382d2SGreg Kroah-Hartman 
1512ab4382d2SGreg Kroah-Hartman 	do {
1513ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
15144e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1515ab4382d2SGreg Kroah-Hartman 
1516ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
15174e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1518ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1519ab4382d2SGreg Kroah-Hartman 
1520ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1521ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1522ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1523ab4382d2SGreg Kroah-Hartman 		 * find out.
1524ab4382d2SGreg Kroah-Hartman 		 *
1525ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1526ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1527ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1528ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1529ab4382d2SGreg Kroah-Hartman 		 */
1530ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1531ab4382d2SGreg Kroah-Hartman 
1532ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1533ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1534ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1535ab4382d2SGreg Kroah-Hartman 
1536ab4382d2SGreg Kroah-Hartman 			/*
1537ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1538ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1539ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1540ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1541ab4382d2SGreg Kroah-Hartman 			 */
1542ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1543ab4382d2SGreg Kroah-Hartman 
154405c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
154505c7cd39SJiri Slaby 						count);
1546ab4382d2SGreg Kroah-Hartman 
1547ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1548ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1549ab4382d2SGreg Kroah-Hartman 
1550ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1551ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1552ab4382d2SGreg Kroah-Hartman 		}
1553ab4382d2SGreg Kroah-Hartman 
1554ab4382d2SGreg Kroah-Hartman 		/*
1555ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1556ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1557ab4382d2SGreg Kroah-Hartman 		 */
1558ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1559ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
15604e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
15614e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1562ab4382d2SGreg Kroah-Hartman 
1563ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1564ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1565ab4382d2SGreg Kroah-Hartman 		}
1566ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1567ab4382d2SGreg Kroah-Hartman 
1568ab4382d2SGreg Kroah-Hartman 	/*
1569ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1570ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1571ab4382d2SGreg Kroah-Hartman 	 */
1572ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15732e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1574ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1575ab4382d2SGreg Kroah-Hartman 
15764e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
15774e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1578ab4382d2SGreg Kroah-Hartman }
1579ab4382d2SGreg Kroah-Hartman 
1580a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1581a930e528SElen Song {
1582a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1583a930e528SElen Song 	int i;
1584a930e528SElen Song 
1585a930e528SElen Song 	for (i = 0; i < 2; i++) {
1586a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1587a930e528SElen Song 
1588a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1589a930e528SElen Song 		if (pdc->buf == NULL) {
1590a930e528SElen Song 			if (i != 0) {
1591a930e528SElen Song 				dma_unmap_single(port->dev,
1592a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1593a930e528SElen Song 					PDC_BUFFER_SIZE,
1594a930e528SElen Song 					DMA_FROM_DEVICE);
1595a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1596a930e528SElen Song 			}
1597a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1598a930e528SElen Song 			return -ENOMEM;
1599a930e528SElen Song 		}
1600a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1601a930e528SElen Song 						pdc->buf,
1602a930e528SElen Song 						PDC_BUFFER_SIZE,
1603a930e528SElen Song 						DMA_FROM_DEVICE);
1604a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1605a930e528SElen Song 		pdc->ofs = 0;
1606a930e528SElen Song 	}
1607a930e528SElen Song 
1608a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1609a930e528SElen Song 
16104e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
16114e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1612a930e528SElen Song 
16134e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
16144e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
16154e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1616a930e528SElen Song 
1617a930e528SElen Song 	return 0;
1618a930e528SElen Song }
1619a930e528SElen Song 
1620ab4382d2SGreg Kroah-Hartman /*
1621ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1622ab4382d2SGreg Kroah-Hartman  */
162300e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data)
1624ab4382d2SGreg Kroah-Hartman {
1625ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1626ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1627ab4382d2SGreg Kroah-Hartman 
1628ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1629ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1630a930e528SElen Song 	atmel_port->schedule_rx(port);
163100e8e658SNicolas Ferre 	spin_unlock(&port->lock);
163200e8e658SNicolas Ferre }
1633ab4382d2SGreg Kroah-Hartman 
163400e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data)
163500e8e658SNicolas Ferre {
163600e8e658SNicolas Ferre 	struct uart_port *port = (struct uart_port *)data;
163700e8e658SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
163800e8e658SNicolas Ferre 
163900e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
164000e8e658SNicolas Ferre 	spin_lock(&port->lock);
164100e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1642ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1643ab4382d2SGreg Kroah-Hartman }
1644ab4382d2SGreg Kroah-Hartman 
16454a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
164633d64c4fSElen Song 				struct platform_device *pdev)
164733d64c4fSElen Song {
164833d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
164933d64c4fSElen Song 
165033d64c4fSElen Song 	/* DMA/PDC usage specification */
1651490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1652490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
165333d64c4fSElen Song 			atmel_port->use_dma_rx  = true;
165433d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
165533d64c4fSElen Song 		} else {
165633d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
165733d64c4fSElen Song 			atmel_port->use_pdc_rx  = true;
165833d64c4fSElen Song 		}
165933d64c4fSElen Song 	} else {
166033d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
166133d64c4fSElen Song 		atmel_port->use_pdc_rx  = false;
166233d64c4fSElen Song 	}
166333d64c4fSElen Song 
1664490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1665490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
166633d64c4fSElen Song 			atmel_port->use_dma_tx  = true;
166733d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
166833d64c4fSElen Song 		} else {
166933d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
167033d64c4fSElen Song 			atmel_port->use_pdc_tx  = true;
167133d64c4fSElen Song 		}
167233d64c4fSElen Song 	} else {
167333d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
167433d64c4fSElen Song 		atmel_port->use_pdc_tx  = false;
167533d64c4fSElen Song 	}
167633d64c4fSElen Song }
167733d64c4fSElen Song 
1678a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1679a930e528SElen Song {
1680a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1681a930e528SElen Song 
168234df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
168334df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
168434df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
168534df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
168634df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1687a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1688a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1689a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1690a930e528SElen Song 	} else {
1691a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1692a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1693a930e528SElen Song 		atmel_port->release_rx = NULL;
1694a930e528SElen Song 	}
1695a930e528SElen Song 
169608f738beSElen Song 	if (atmel_use_dma_tx(port)) {
169708f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
169808f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
169908f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
170008f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1701a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1702a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1703a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1704a930e528SElen Song 	} else {
1705a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1706a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1707a930e528SElen Song 		atmel_port->release_tx = NULL;
1708a930e528SElen Song 	}
1709a930e528SElen Song }
1710a930e528SElen Song 
1711ab4382d2SGreg Kroah-Hartman /*
1712055560b0SElen Song  * Get ip name usart or uart
1713055560b0SElen Song  */
1714892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1715055560b0SElen Song {
1716055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
17174e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1718731d9caeSNicolas Ferre 	u32 version;
17191d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
17204b769371SNicolas Ferre 	/* ASCII decoding for IP version */
17214b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
17224b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
17231d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1724055560b0SElen Song 
17255bf5635aSLudovic Desroches 	/*
17265bf5635aSLudovic Desroches 	 * Only USART devices from at91sam9260 SOC implement fractional
17272867af2dSRomain Izard 	 * baudrate. It is available for all asynchronous modes, with the
17282867af2dSRomain Izard 	 * following restriction: the sampling clock's duty cycle is not
17292867af2dSRomain Izard 	 * constant.
17305bf5635aSLudovic Desroches 	 */
17315bf5635aSLudovic Desroches 	atmel_port->has_frac_baudrate = false;
17324b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
1733055560b0SElen Song 
17342958cceeSLudovic Desroches 	if (name == new_uart) {
17352958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
17364b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
17372958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
17382958cceeSLudovic Desroches 	} else if (name == usart) {
17392958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
17405bf5635aSLudovic Desroches 		atmel_port->has_frac_baudrate = true;
17412958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
17422958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
17434b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
17444b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1745055560b0SElen Song 	} else {
1746731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17474e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1748731d9caeSNicolas Ferre 		switch (version) {
1749731d9caeSNicolas Ferre 		case 0x302:
1750731d9caeSNicolas Ferre 		case 0x10213:
1751731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
17525bf5635aSLudovic Desroches 			atmel_port->has_frac_baudrate = true;
17534b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
17542958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1755731d9caeSNicolas Ferre 			break;
1756731d9caeSNicolas Ferre 		case 0x203:
1757731d9caeSNicolas Ferre 		case 0x10202:
1758731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1759731d9caeSNicolas Ferre 			break;
1760731d9caeSNicolas Ferre 		default:
1761731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1762731d9caeSNicolas Ferre 		}
1763055560b0SElen Song 	}
1764055560b0SElen Song }
1765055560b0SElen Song 
1766055560b0SElen Song /*
1767ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1768ab4382d2SGreg Kroah-Hartman  */
1769ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1770ab4382d2SGreg Kroah-Hartman {
177133d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1772ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1773ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1774ab4382d2SGreg Kroah-Hartman 	int retval;
1775ab4382d2SGreg Kroah-Hartman 
1776ab4382d2SGreg Kroah-Hartman 	/*
1777ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1778ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1779ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1780ab4382d2SGreg Kroah-Hartman 	 */
17814e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1782ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1783ab4382d2SGreg Kroah-Hartman 
1784ab4382d2SGreg Kroah-Hartman 	/*
1785ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1786ab4382d2SGreg Kroah-Hartman 	 */
17872c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
17882c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1789ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1790ab4382d2SGreg Kroah-Hartman 	if (retval) {
1791ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1792ab4382d2SGreg Kroah-Hartman 		return retval;
1793ab4382d2SGreg Kroah-Hartman 	}
1794ab4382d2SGreg Kroah-Hartman 
179598f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
179698f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
179798f2082cSNicolas Ferre 			(unsigned long)port);
179898f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
179998f2082cSNicolas Ferre 			(unsigned long)port);
18001e125786SLeilei Zhao 
1801ab5e4e41SRichard Genoud 	/*
1802ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1803ab4382d2SGreg Kroah-Hartman 	 */
180433d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
18054d9628a1SLeilei Zhao 	atmel_set_ops(port);
180633d64c4fSElen Song 
1807a930e528SElen Song 	if (atmel_port->prepare_rx) {
1808a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1809a930e528SElen Song 		if (retval < 0)
1810a930e528SElen Song 			atmel_set_ops(port);
1811ab4382d2SGreg Kroah-Hartman 	}
1812ab4382d2SGreg Kroah-Hartman 
1813a930e528SElen Song 	if (atmel_port->prepare_tx) {
1814a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1815a930e528SElen Song 		if (retval < 0)
1816a930e528SElen Song 			atmel_set_ops(port);
1817ab4382d2SGreg Kroah-Hartman 	}
1818ab4382d2SGreg Kroah-Hartman 
1819b5199d46SCyrille Pitchen 	/*
1820b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1821b5199d46SCyrille Pitchen 	 */
1822b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1823b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1824b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1825b5199d46SCyrille Pitchen 		unsigned int fmr;
1826b5199d46SCyrille Pitchen 
1827b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1828b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1829b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1830b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1831b5199d46SCyrille Pitchen 
18325f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
18335f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
18345f258b3eSCyrille Pitchen 
1835b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1836b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1837b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1838b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1839b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1840b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1841b5199d46SCyrille Pitchen 
1842b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1843b5199d46SCyrille Pitchen 	}
1844b5199d46SCyrille Pitchen 
1845ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1846e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1847ab4382d2SGreg Kroah-Hartman 
1848ab4382d2SGreg Kroah-Hartman 	/*
1849ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1850ab4382d2SGreg Kroah-Hartman 	 */
18514e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1852ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
18534e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1854ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
1855ab4382d2SGreg Kroah-Hartman 
1856*026cb432SKees Cook 	timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0);
18578bc661bfSMarek Roszko 
18588bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18598bc661bfSMarek Roszko 		/* set UART timeout */
18604b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18612e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18622e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18632e68c22fSElen Song 		/* set USART timeout */
18642e68c22fSElen Song 		} else {
18652958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18662958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18674e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1868ab4382d2SGreg Kroah-Hartman 
18694e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18704e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18712e68c22fSElen Song 		}
1872ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
18734e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
187434df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18752e68c22fSElen Song 		/* 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);
188434df42f5SElen Song 
18854e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18864e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
18872e68c22fSElen Song 		}
1888ab4382d2SGreg Kroah-Hartman 	} else {
1889ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
18904e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1891ab4382d2SGreg Kroah-Hartman 	}
1892ab4382d2SGreg Kroah-Hartman 
1893ab4382d2SGreg Kroah-Hartman 	return 0;
1894ab4382d2SGreg Kroah-Hartman }
1895ab4382d2SGreg Kroah-Hartman 
1896ab4382d2SGreg Kroah-Hartman /*
1897479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1898479e9b94SPeter Hurley  * buffer is reset.
1899479e9b94SPeter Hurley  */
1900479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1901479e9b94SPeter Hurley {
1902479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1903479e9b94SPeter Hurley 
1904479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
19054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1906479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1907479e9b94SPeter Hurley 	}
190831ca2c63SRichard Genoud 	/*
190931ca2c63SRichard Genoud 	 * in uart_flush_buffer(), the xmit circular buffer has just
191031ca2c63SRichard Genoud 	 * been cleared, so we have to reset tx_len accordingly.
191131ca2c63SRichard Genoud 	 */
191231ca2c63SRichard Genoud 	atmel_port->tx_len = 0;
1913479e9b94SPeter Hurley }
1914479e9b94SPeter Hurley 
1915479e9b94SPeter Hurley /*
1916ab4382d2SGreg Kroah-Hartman  * Disable the port
1917ab4382d2SGreg Kroah-Hartman  */
1918ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1919ab4382d2SGreg Kroah-Hartman {
1920ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19210cc7c6c7SMarek Roszko 
19220ae9fdefSRichard Genoud 	/* Disable modem control lines interrupts */
19230ae9fdefSRichard Genoud 	atmel_disable_ms(port);
19240ae9fdefSRichard Genoud 
192598f2082cSNicolas Ferre 	/* Disable interrupts at device level */
192698f2082cSNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
192798f2082cSNicolas Ferre 
192898f2082cSNicolas Ferre 	/* Prevent spurious interrupts from scheduling the tasklet */
192998f2082cSNicolas Ferre 	atomic_inc(&atmel_port->tasklet_shutdown);
193098f2082cSNicolas Ferre 
1931ab4382d2SGreg Kroah-Hartman 	/*
19328bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19338bc661bfSMarek Roszko 	 * cleanup
19348bc661bfSMarek Roszko 	 */
19358bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19368bc661bfSMarek Roszko 
193798f2082cSNicolas Ferre 	/* Make sure that no interrupt is on the fly */
193898f2082cSNicolas Ferre 	synchronize_irq(port->irq);
193998f2082cSNicolas Ferre 
19408bc661bfSMarek Roszko 	/*
19410cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19420cc7c6c7SMarek Roszko 	 * we destroy the buffers
19430cc7c6c7SMarek Roszko 	 */
194400e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
194500e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
19460cc7c6c7SMarek Roszko 
19470cc7c6c7SMarek Roszko 	/*
19480cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
194998f2082cSNicolas Ferre 	 * disable port and break condition.
1950ab4382d2SGreg Kroah-Hartman 	 */
1951ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1952ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1953ab4382d2SGreg Kroah-Hartman 
19544e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
19550cc7c6c7SMarek Roszko 
1956ab4382d2SGreg Kroah-Hartman 	/*
1957ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1958ab4382d2SGreg Kroah-Hartman 	 */
1959a930e528SElen Song 	if (atmel_port->release_rx)
1960a930e528SElen Song 		atmel_port->release_rx(port);
1961a930e528SElen Song 	if (atmel_port->release_tx)
1962a930e528SElen Song 		atmel_port->release_tx(port);
1963ab4382d2SGreg Kroah-Hartman 
1964ab4382d2SGreg Kroah-Hartman 	/*
1965bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1966bb7e73c5SMark Deneen 	 */
1967bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1968bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1969bb7e73c5SMark Deneen 
1970bb7e73c5SMark Deneen 	/*
1971ab5e4e41SRichard Genoud 	 * Free the interrupts
1972ab4382d2SGreg Kroah-Hartman 	 */
1973ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1974ab5e4e41SRichard Genoud 
1975479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1976ab4382d2SGreg Kroah-Hartman }
1977ab4382d2SGreg Kroah-Hartman 
1978ab4382d2SGreg Kroah-Hartman /*
1979ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1980ab4382d2SGreg Kroah-Hartman  */
1981ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1982ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1983ab4382d2SGreg Kroah-Hartman {
1984ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1985ab4382d2SGreg Kroah-Hartman 
1986ab4382d2SGreg Kroah-Hartman 	switch (state) {
1987ab4382d2SGreg Kroah-Hartman 	case 0:
1988ab4382d2SGreg Kroah-Hartman 		/*
1989ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1990ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1991ab4382d2SGreg Kroah-Hartman 		 */
199291f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1993ab4382d2SGreg Kroah-Hartman 
1994ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
19954e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
1996ab4382d2SGreg Kroah-Hartman 		break;
1997ab4382d2SGreg Kroah-Hartman 	case 3:
1998ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
19994e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
20004e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
2001ab4382d2SGreg Kroah-Hartman 
2002ab4382d2SGreg Kroah-Hartman 		/*
2003ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
2004ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
2005ab4382d2SGreg Kroah-Hartman 		 */
200691f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2007ab4382d2SGreg Kroah-Hartman 		break;
2008ab4382d2SGreg Kroah-Hartman 	default:
2009ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2010ab4382d2SGreg Kroah-Hartman 	}
2011ab4382d2SGreg Kroah-Hartman }
2012ab4382d2SGreg Kroah-Hartman 
2013ab4382d2SGreg Kroah-Hartman /*
2014ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2015ab4382d2SGreg Kroah-Hartman  */
2016ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2017ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2018ab4382d2SGreg Kroah-Hartman {
20195bf5635aSLudovic Desroches 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2020ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
20215bf5635aSLudovic Desroches 	unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
2022ab4382d2SGreg Kroah-Hartman 
20231cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20244e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20251cf6e8fcSCyrille Pitchen 
20261cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20271cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20281cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2029ab4382d2SGreg Kroah-Hartman 
2030ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2031ab4382d2SGreg Kroah-Hartman 
2032ab4382d2SGreg Kroah-Hartman 	/* byte size */
2033ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2034ab4382d2SGreg Kroah-Hartman 	case CS5:
2035ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2036ab4382d2SGreg Kroah-Hartman 		break;
2037ab4382d2SGreg Kroah-Hartman 	case CS6:
2038ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2039ab4382d2SGreg Kroah-Hartman 		break;
2040ab4382d2SGreg Kroah-Hartman 	case CS7:
2041ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2042ab4382d2SGreg Kroah-Hartman 		break;
2043ab4382d2SGreg Kroah-Hartman 	default:
2044ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2045ab4382d2SGreg Kroah-Hartman 		break;
2046ab4382d2SGreg Kroah-Hartman 	}
2047ab4382d2SGreg Kroah-Hartman 
2048ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2049ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2050ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2051ab4382d2SGreg Kroah-Hartman 
2052ab4382d2SGreg Kroah-Hartman 	/* parity */
2053ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2054ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2055ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2056ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2057ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2058ab4382d2SGreg Kroah-Hartman 			else
2059ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2060ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2061ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2062ab4382d2SGreg Kroah-Hartman 		else
2063ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2064ab4382d2SGreg Kroah-Hartman 	} else
2065ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2066ab4382d2SGreg Kroah-Hartman 
2067ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2068ab4382d2SGreg Kroah-Hartman 
2069ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2070ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2071ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2072ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2073ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2074ab4382d2SGreg Kroah-Hartman 
207564e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2076ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
20774e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2078ab4382d2SGreg Kroah-Hartman 
2079ab4382d2SGreg Kroah-Hartman 	/*
2080ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2081ab4382d2SGreg Kroah-Hartman 	 */
2082ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2083ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2084ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2085ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2086ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2087ab4382d2SGreg Kroah-Hartman 		/*
2088ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2089ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2090ab4382d2SGreg Kroah-Hartman 		 */
2091ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2092ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2093ab4382d2SGreg Kroah-Hartman 	}
2094ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2095ab4382d2SGreg Kroah-Hartman 
2096ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2097ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2098ab4382d2SGreg Kroah-Hartman 
2099ab4382d2SGreg Kroah-Hartman 	/*
2100ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2101ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2102ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2103ab4382d2SGreg Kroah-Hartman 	 */
21044e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
21054e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2106ab4382d2SGreg Kroah-Hartman 
2107ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
21084e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2109ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
2110ab4382d2SGreg Kroah-Hartman 
21111cf6e8fcSCyrille Pitchen 	/* mode */
211213bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
21134e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
21144e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2115ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
21161cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
21171cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
21189bcffe75SRichard Genoud 		if (atmel_use_fifo(port) &&
21199bcffe75SRichard Genoud 		    !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
21209bcffe75SRichard Genoud 			/*
21219bcffe75SRichard Genoud 			 * with ATMEL_US_USMODE_HWHS set, the controller will
21229bcffe75SRichard Genoud 			 * be able to drive the RTS pin high/low when the RX
21239bcffe75SRichard Genoud 			 * FIFO is above RXFTHRES/below RXFTHRES2.
21249bcffe75SRichard Genoud 			 * It will also disable the transmitter when the CTS
21259bcffe75SRichard Genoud 			 * pin is high.
21269bcffe75SRichard Genoud 			 * This mode is not activated if CTS pin is a GPIO
21279bcffe75SRichard Genoud 			 * because in this case, the transmitter is always
21289bcffe75SRichard Genoud 			 * disabled (there must be an internal pull-up
21299bcffe75SRichard Genoud 			 * responsible for this behaviour).
21309bcffe75SRichard Genoud 			 * If the RTS pin is a GPIO, the controller won't be
21319bcffe75SRichard Genoud 			 * able to drive it according to the FIFO thresholds,
21329bcffe75SRichard Genoud 			 * but it will be handled by the driver.
21339bcffe75SRichard Genoud 			 */
21341cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
21359bcffe75SRichard Genoud 		} else {
21369bcffe75SRichard Genoud 			/*
21379bcffe75SRichard Genoud 			 * For platforms without FIFO, the flow control is
21389bcffe75SRichard Genoud 			 * handled by the driver.
21399bcffe75SRichard Genoud 			 */
21409bcffe75SRichard Genoud 			mode |= ATMEL_US_USMODE_NORMAL;
21415be605acSAlexandre Belloni 		}
21421cf6e8fcSCyrille Pitchen 	} else {
21431cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21441cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2145ab4382d2SGreg Kroah-Hartman 	}
2146ab4382d2SGreg Kroah-Hartman 
21471cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21484e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2149ab4382d2SGreg Kroah-Hartman 
21501cf6e8fcSCyrille Pitchen 	/*
21511cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21521cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21531cf6e8fcSCyrille Pitchen 	 */
21541cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21551cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21561cf6e8fcSCyrille Pitchen 
21571cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21581cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21591cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21601cf6e8fcSCyrille Pitchen 		} else {
21611cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21621cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21631cf6e8fcSCyrille Pitchen 		}
21641cf6e8fcSCyrille Pitchen 
21654e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21661cf6e8fcSCyrille Pitchen 	}
21671cf6e8fcSCyrille Pitchen 
21685bf5635aSLudovic Desroches 	/*
21695bf5635aSLudovic Desroches 	 * Set the baud rate:
21705bf5635aSLudovic Desroches 	 * Fractional baudrate allows to setup output frequency more
21715bf5635aSLudovic Desroches 	 * accurately. This feature is enabled only when using normal mode.
21725bf5635aSLudovic Desroches 	 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
21735bf5635aSLudovic Desroches 	 * Currently, OVER is always set to 0 so we get
217436131cdfSAlexey Starikovskiy 	 * baudrate = selected clock / (16 * (CD + FP / 8))
217536131cdfSAlexey Starikovskiy 	 * then
217636131cdfSAlexey Starikovskiy 	 * 8 CD + FP = selected clock / (2 * baudrate)
21775bf5635aSLudovic Desroches 	 */
21782867af2dSRomain Izard 	if (atmel_port->has_frac_baudrate) {
217936131cdfSAlexey Starikovskiy 		div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
218036131cdfSAlexey Starikovskiy 		cd = div >> 3;
218136131cdfSAlexey Starikovskiy 		fp = div & ATMEL_US_FP_MASK;
21825bf5635aSLudovic Desroches 	} else {
21835bf5635aSLudovic Desroches 		cd = uart_get_divisor(port, baud);
21845bf5635aSLudovic Desroches 	}
21855bf5635aSLudovic Desroches 
21865bf5635aSLudovic Desroches 	if (cd > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
21875bf5635aSLudovic Desroches 		cd /= 8;
21885bf5635aSLudovic Desroches 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
21895bf5635aSLudovic Desroches 	}
21905bf5635aSLudovic Desroches 	quot = cd | fp << ATMEL_US_FP_OFFSET;
21915bf5635aSLudovic Desroches 
21924e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
21934e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
21944e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2195ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2196ab4382d2SGreg Kroah-Hartman 
2197ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
21984e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2199ab4382d2SGreg Kroah-Hartman 
2200ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2201ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
220235b675b9SRichard Genoud 		atmel_enable_ms(port);
220335b675b9SRichard Genoud 	else
220435b675b9SRichard Genoud 		atmel_disable_ms(port);
2205ab4382d2SGreg Kroah-Hartman 
2206ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2207ab4382d2SGreg Kroah-Hartman }
2208ab4382d2SGreg Kroah-Hartman 
2209732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
221042bd7a4fSViktar Palstsiuk {
2211732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
221242bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2213d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
221442bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2215d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
221642bd7a4fSViktar Palstsiuk 	} else {
221742bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2218cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2219cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2220cab68f89SPeter Hurley 			atmel_disable_ms(port);
2221cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2222cab68f89SPeter Hurley 		}
222342bd7a4fSViktar Palstsiuk 	}
222442bd7a4fSViktar Palstsiuk }
222542bd7a4fSViktar Palstsiuk 
2226ab4382d2SGreg Kroah-Hartman /*
2227ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2228ab4382d2SGreg Kroah-Hartman  */
2229ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2230ab4382d2SGreg Kroah-Hartman {
2231ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2232ab4382d2SGreg Kroah-Hartman }
2233ab4382d2SGreg Kroah-Hartman 
2234ab4382d2SGreg Kroah-Hartman /*
2235ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2236ab4382d2SGreg Kroah-Hartman  */
2237ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2238ab4382d2SGreg Kroah-Hartman {
2239ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2240ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2241ab4382d2SGreg Kroah-Hartman 
2242ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2243ab4382d2SGreg Kroah-Hartman 
2244ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2245ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2246ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2247ab4382d2SGreg Kroah-Hartman 	}
2248ab4382d2SGreg Kroah-Hartman }
2249ab4382d2SGreg Kroah-Hartman 
2250ab4382d2SGreg Kroah-Hartman /*
2251ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2252ab4382d2SGreg Kroah-Hartman  */
2253ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2254ab4382d2SGreg Kroah-Hartman {
2255ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2256ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2257ab4382d2SGreg Kroah-Hartman 
2258ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2259ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2260ab4382d2SGreg Kroah-Hartman 
2261ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2262ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2263ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2264ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2265ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2266ab4382d2SGreg Kroah-Hartman 		}
2267ab4382d2SGreg Kroah-Hartman 	}
2268ab4382d2SGreg Kroah-Hartman 
2269ab4382d2SGreg Kroah-Hartman 	return 0;
2270ab4382d2SGreg Kroah-Hartman }
2271ab4382d2SGreg Kroah-Hartman 
2272ab4382d2SGreg Kroah-Hartman /*
2273ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2274ab4382d2SGreg Kroah-Hartman  */
2275ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2276ab4382d2SGreg Kroah-Hartman {
2277ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2278ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2279ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2280ab4382d2SGreg Kroah-Hartman 	}
2281ab4382d2SGreg Kroah-Hartman }
2282ab4382d2SGreg Kroah-Hartman 
2283ab4382d2SGreg Kroah-Hartman /*
2284ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2285ab4382d2SGreg Kroah-Hartman  */
2286ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2287ab4382d2SGreg Kroah-Hartman {
2288ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2289ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2290ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2291ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2292ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2293ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2294ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2295ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2296ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2297270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2298ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2299ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2300ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2301ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2302ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2303ab4382d2SGreg Kroah-Hartman 	return ret;
2304ab4382d2SGreg Kroah-Hartman }
2305ab4382d2SGreg Kroah-Hartman 
2306ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2307ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2308ab4382d2SGreg Kroah-Hartman {
23094e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2310ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2311ab4382d2SGreg Kroah-Hartman 
2312a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2313ab4382d2SGreg Kroah-Hartman }
2314ab4382d2SGreg Kroah-Hartman 
2315ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2316ab4382d2SGreg Kroah-Hartman {
23174e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2318ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2319ab4382d2SGreg Kroah-Hartman 
2320a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2321ab4382d2SGreg Kroah-Hartman }
2322ab4382d2SGreg Kroah-Hartman #endif
2323ab4382d2SGreg Kroah-Hartman 
23245c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = {
2325ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2326ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2327ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2328ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2329ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2330ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2331ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2332ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2333ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2334ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2335ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2336ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
233742bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2338ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2339ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2340ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2341ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2342ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2343ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2344ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2345ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2346ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2347ab4382d2SGreg Kroah-Hartman #endif
2348ab4382d2SGreg Kroah-Hartman };
2349ab4382d2SGreg Kroah-Hartman 
2350ab4382d2SGreg Kroah-Hartman /*
2351ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2352ab4382d2SGreg Kroah-Hartman  */
235391f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2354ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2355ab4382d2SGreg Kroah-Hartman {
235691f8c2d8SBoris BREZILLON 	int ret;
2357ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2358ab4382d2SGreg Kroah-Hartman 
23594a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2360a930e528SElen Song 	atmel_set_ops(port);
2361a930e528SElen Song 
236279d9e95aSSascha Hauer 	of_get_rs485_mode(pdev->dev.of_node, &port->rs485);
236333d64c4fSElen Song 
2364ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
236592c8f7c0SAlexandre Belloni 	port->flags		= UPF_BOOT_AUTOCONF | UPF_IOREMAP;
2366ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2367ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2368ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2369ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2370ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
237113bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
237292c8f7c0SAlexandre Belloni 	port->membase	= NULL;
2373ab4382d2SGreg Kroah-Hartman 
2374ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2375ab4382d2SGreg Kroah-Hartman 
2376ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2377ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2378ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
237991f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
238091f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
238191f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
238291f8c2d8SBoris BREZILLON 			return ret;
238391f8c2d8SBoris BREZILLON 		}
238491f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
238591f8c2d8SBoris BREZILLON 		if (ret) {
238691f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
238791f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
238891f8c2d8SBoris BREZILLON 			return ret;
238991f8c2d8SBoris BREZILLON 		}
2390ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
239191f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2392ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2393ab4382d2SGreg Kroah-Hartman 	}
2394ab4382d2SGreg Kroah-Hartman 
2395ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
239613bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2397ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
239864e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2399ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2400ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2401ab4382d2SGreg Kroah-Hartman 	} else {
2402ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2403ab4382d2SGreg Kroah-Hartman 	}
240491f8c2d8SBoris BREZILLON 
240591f8c2d8SBoris BREZILLON 	return 0;
2406ab4382d2SGreg Kroah-Hartman }
2407ab4382d2SGreg Kroah-Hartman 
2408ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2409ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2410ab4382d2SGreg Kroah-Hartman {
24114e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2412ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2413a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2414ab4382d2SGreg Kroah-Hartman }
2415ab4382d2SGreg Kroah-Hartman 
2416ab4382d2SGreg Kroah-Hartman /*
2417ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2418ab4382d2SGreg Kroah-Hartman  */
2419ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2420ab4382d2SGreg Kroah-Hartman {
2421ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2422ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2423ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2424ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2425ab4382d2SGreg Kroah-Hartman 
2426ab4382d2SGreg Kroah-Hartman 	/*
2427ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2428ab4382d2SGreg Kroah-Hartman 	 */
24294e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
24304e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
24314e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2432ab4382d2SGreg Kroah-Hartman 
2433ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
24344e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
24354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2436ab4382d2SGreg Kroah-Hartman 
2437497e1e16SNicolas Ferre 	/* Make sure that tx path is actually able to send characters */
2438497e1e16SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2439ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2440497e1e16SNicolas Ferre 
2441ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman 	/*
2444ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2445ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2446ab4382d2SGreg Kroah-Hartman 	 */
2447ab4382d2SGreg Kroah-Hartman 	do {
24484e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2449ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2450ab4382d2SGreg Kroah-Hartman 
2451ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2452ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24534e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2454ab4382d2SGreg Kroah-Hartman 
2455ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24564e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2457ab4382d2SGreg Kroah-Hartman }
2458ab4382d2SGreg Kroah-Hartman 
2459ab4382d2SGreg Kroah-Hartman /*
2460ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2461ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2462ab4382d2SGreg Kroah-Hartman  */
2463ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2464ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2465ab4382d2SGreg Kroah-Hartman {
2466ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2467ab4382d2SGreg Kroah-Hartman 
2468ab4382d2SGreg Kroah-Hartman 	/*
2469ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2470ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2471ab4382d2SGreg Kroah-Hartman 	 */
24724e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2473ab4382d2SGreg Kroah-Hartman 	if (!quot)
2474ab4382d2SGreg Kroah-Hartman 		return;
2475ab4382d2SGreg Kroah-Hartman 
24764e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2477ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2478ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2479ab4382d2SGreg Kroah-Hartman 	else
2480ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2481ab4382d2SGreg Kroah-Hartman 
24824e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2483ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2484ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2485ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2486ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2487ab4382d2SGreg Kroah-Hartman 
2488ab4382d2SGreg Kroah-Hartman 	/*
2489ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2490ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2491ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2492ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2493ab4382d2SGreg Kroah-Hartman 	 */
2494ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2495ab4382d2SGreg Kroah-Hartman }
2496ab4382d2SGreg Kroah-Hartman 
2497ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2498ab4382d2SGreg Kroah-Hartman {
249991f8c2d8SBoris BREZILLON 	int ret;
2500ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2501ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2502ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2503ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2504ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2505ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2506ab4382d2SGreg Kroah-Hartman 
2507ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2508ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2509ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2510ab4382d2SGreg Kroah-Hartman 	}
2511ab4382d2SGreg Kroah-Hartman 
251291f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
251391f8c2d8SBoris BREZILLON 	if (ret)
251491f8c2d8SBoris BREZILLON 		return ret;
2515ab4382d2SGreg Kroah-Hartman 
25164e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
25174e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
25184e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2519ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2520ab4382d2SGreg Kroah-Hartman 
2521ab4382d2SGreg Kroah-Hartman 	if (options)
2522ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2523ab4382d2SGreg Kroah-Hartman 	else
2524ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2525ab4382d2SGreg Kroah-Hartman 
2526ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2527ab4382d2SGreg Kroah-Hartman }
2528ab4382d2SGreg Kroah-Hartman 
2529ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2530ab4382d2SGreg Kroah-Hartman 
2531ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2532ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2533ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2534ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2535ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2536ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2537ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2538ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2539ab4382d2SGreg Kroah-Hartman };
2540ab4382d2SGreg Kroah-Hartman 
2541ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2542ab4382d2SGreg Kroah-Hartman 
2543ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2544ab4382d2SGreg Kroah-Hartman {
2545ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2546ab4382d2SGreg Kroah-Hartman }
2547ab4382d2SGreg Kroah-Hartman 
2548ab4382d2SGreg Kroah-Hartman #else
2549ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2550ab4382d2SGreg Kroah-Hartman 
2551ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2552ab4382d2SGreg Kroah-Hartman {
2553ab4382d2SGreg Kroah-Hartman 	return false;
2554ab4382d2SGreg Kroah-Hartman }
2555ab4382d2SGreg Kroah-Hartman #endif
2556ab4382d2SGreg Kroah-Hartman 
2557ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2558ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2559ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2560ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2561ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2562ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2563ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2564ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2565ab4382d2SGreg Kroah-Hartman };
2566ab4382d2SGreg Kroah-Hartman 
2567ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2568ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2569ab4382d2SGreg Kroah-Hartman {
2570ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2571ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2572ab4382d2SGreg Kroah-Hartman #else
2573ab4382d2SGreg Kroah-Hartman 	return false;
2574ab4382d2SGreg Kroah-Hartman #endif
2575ab4382d2SGreg Kroah-Hartman }
2576ab4382d2SGreg Kroah-Hartman 
2577ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2578ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2579ab4382d2SGreg Kroah-Hartman {
2580ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2581ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2582ab4382d2SGreg Kroah-Hartman 
2583ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2584ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
25854e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
25864e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2587ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2588ab4382d2SGreg Kroah-Hartman 	}
2589ab4382d2SGreg Kroah-Hartman 
25906a5f0e2fSAlexandre Belloni 	if (atmel_is_console_port(port) && !console_suspend_enabled) {
25916a5f0e2fSAlexandre Belloni 		/* Cache register values as we won't get a full shutdown/startup
25926a5f0e2fSAlexandre Belloni 		 * cycle
25936a5f0e2fSAlexandre Belloni 		 */
25946a5f0e2fSAlexandre Belloni 		atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR);
25956a5f0e2fSAlexandre Belloni 		atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR);
25966a5f0e2fSAlexandre Belloni 		atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
25976a5f0e2fSAlexandre Belloni 		atmel_port->cache.rtor = atmel_uart_readl(port,
25986a5f0e2fSAlexandre Belloni 							  atmel_port->rtor);
25996a5f0e2fSAlexandre Belloni 		atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR);
26006a5f0e2fSAlexandre Belloni 		atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR);
26016a5f0e2fSAlexandre Belloni 		atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR);
26026a5f0e2fSAlexandre Belloni 	}
26036a5f0e2fSAlexandre Belloni 
2604ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2605ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
26062c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
26072c7af5baSBoris BREZILLON 		unsigned long flags;
26082c7af5baSBoris BREZILLON 
26092c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26102c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
26112c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2612ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
26132c7af5baSBoris BREZILLON 	}
2614ab4382d2SGreg Kroah-Hartman 
2615ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2616ab4382d2SGreg Kroah-Hartman 
2617ab4382d2SGreg Kroah-Hartman 	return 0;
2618ab4382d2SGreg Kroah-Hartman }
2619ab4382d2SGreg Kroah-Hartman 
2620ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2621ab4382d2SGreg Kroah-Hartman {
2622ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2623ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26242c7af5baSBoris BREZILLON 	unsigned long flags;
26252c7af5baSBoris BREZILLON 
26266a5f0e2fSAlexandre Belloni 	if (atmel_is_console_port(port) && !console_suspend_enabled) {
26276a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
26286a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
26296a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
26306a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, atmel_port->rtor,
26316a5f0e2fSAlexandre Belloni 				  atmel_port->cache.rtor);
26326a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr);
26336a5f0e2fSAlexandre Belloni 
26346a5f0e2fSAlexandre Belloni 		if (atmel_port->fifo_size) {
26356a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN |
26366a5f0e2fSAlexandre Belloni 					  ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR);
26376a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FMR,
26386a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fmr);
26396a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FIER,
26406a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fimr);
26416a5f0e2fSAlexandre Belloni 		}
26426a5f0e2fSAlexandre Belloni 		atmel_start_rx(port);
26436a5f0e2fSAlexandre Belloni 	}
26446a5f0e2fSAlexandre Belloni 
26452c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26462c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26472c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26482c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26492c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26502c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26512c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26522c7af5baSBoris BREZILLON 	}
26532c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26542c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2655ab4382d2SGreg Kroah-Hartman 
2656ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2657ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2658ab4382d2SGreg Kroah-Hartman 
2659ab4382d2SGreg Kroah-Hartman 	return 0;
2660ab4382d2SGreg Kroah-Hartman }
2661ab4382d2SGreg Kroah-Hartman #else
2662ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2663ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2664ab4382d2SGreg Kroah-Hartman #endif
2665ab4382d2SGreg Kroah-Hartman 
2666b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2667b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2668b5199d46SCyrille Pitchen {
2669b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2670b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2671b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2672b5199d46SCyrille Pitchen 
2673b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2674b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2675b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2676b5199d46SCyrille Pitchen 		return;
2677b5199d46SCyrille Pitchen 
2678b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2679b5199d46SCyrille Pitchen 		return;
2680b5199d46SCyrille Pitchen 
2681b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2682b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2683b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2684b5199d46SCyrille Pitchen 		return;
2685b5199d46SCyrille Pitchen 	}
2686b5199d46SCyrille Pitchen 
2687b5199d46SCyrille Pitchen 	/*
2688b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2689b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2690b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2691b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2692b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2693b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2694b5199d46SCyrille Pitchen 	 */
2695b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2696b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2697b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2698b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2699b5199d46SCyrille Pitchen 
2700b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2701b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2702b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2703b78cd169SJaeden Amero 		atmel_port->rts_high);
2704b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2705b78cd169SJaeden Amero 		atmel_port->rts_low);
2706b5199d46SCyrille Pitchen }
2707b5199d46SCyrille Pitchen 
27089671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2709ab4382d2SGreg Kroah-Hartman {
2710b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
27115fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2712ab4382d2SGreg Kroah-Hartman 	void *data;
27134cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2714bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2715ab4382d2SGreg Kroah-Hartman 
2716ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2717ab4382d2SGreg Kroah-Hartman 
27185fbe46b6SNicolas Ferre 	ret = of_alias_get_id(np, "serial");
27194cbf9f48SNicolas Ferre 	if (ret < 0)
27205fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
27214cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2722503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
27234cbf9f48SNicolas Ferre 
2724503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
27254cbf9f48SNicolas Ferre 		ret = -ENODEV;
27264cbf9f48SNicolas Ferre 		goto err;
27274cbf9f48SNicolas Ferre 	}
27284cbf9f48SNicolas Ferre 
2729503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
27304cbf9f48SNicolas Ferre 		/* port already in use */
27314cbf9f48SNicolas Ferre 		ret = -EBUSY;
27324cbf9f48SNicolas Ferre 		goto err;
27334cbf9f48SNicolas Ferre 	}
27344cbf9f48SNicolas Ferre 
2735b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2736b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2737b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2738b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2739354e57f3SLinus Walleij 
274098f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
2741b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
27422c7af5baSBoris BREZILLON 
2743b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
274491f8c2d8SBoris BREZILLON 	if (ret)
27456fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2746ab4382d2SGreg Kroah-Hartman 
2747b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2748b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2749b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
275018dfef9cSUwe Kleine-König 		goto err_clear_bit;
275118dfef9cSUwe Kleine-König 	}
275218dfef9cSUwe Kleine-König 
2753b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2754ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2755ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2756ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2757ab4382d2SGreg Kroah-Hartman 		if (!data)
2758ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2759b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2760ab4382d2SGreg Kroah-Hartman 	}
2761ab4382d2SGreg Kroah-Hartman 
2762b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2763bd737f87SRicardo Ribalda Delgado 
2764b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2765ab4382d2SGreg Kroah-Hartman 	if (ret)
2766ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2767ab4382d2SGreg Kroah-Hartman 
2768ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2769b78cd169SJaeden Amero 	if (atmel_is_console_port(&atmel_port->uart)
2770ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2771ab4382d2SGreg Kroah-Hartman 		/*
2772ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
277391f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2774ab4382d2SGreg Kroah-Hartman 		 */
2775b78cd169SJaeden Amero 		clk_disable_unprepare(atmel_port->clk);
2776ab4382d2SGreg Kroah-Hartman 	}
2777ab4382d2SGreg Kroah-Hartman #endif
2778ab4382d2SGreg Kroah-Hartman 
2779ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2780b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2781ab4382d2SGreg Kroah-Hartman 
2782d4f64187SCyrille Pitchen 	/*
2783d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2784d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2785d4f64187SCyrille Pitchen 	 */
2786b78cd169SJaeden Amero 	clk_prepare_enable(atmel_port->clk);
2787d4f64187SCyrille Pitchen 
2788bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2789b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
27904e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2791b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2792b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2793fc887b15SLinus Torvalds 	}
2794fc887b15SLinus Torvalds 
2795055560b0SElen Song 	/*
2796055560b0SElen Song 	 * Get port name of usart or uart
2797055560b0SElen Song 	 */
2798b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2799055560b0SElen Song 
2800d4f64187SCyrille Pitchen 	/*
2801d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2802d4f64187SCyrille Pitchen 	 * is used
2803d4f64187SCyrille Pitchen 	 */
2804b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2805d4f64187SCyrille Pitchen 
2806ab4382d2SGreg Kroah-Hartman 	return 0;
2807ab4382d2SGreg Kroah-Hartman 
2808ab4382d2SGreg Kroah-Hartman err_add_port:
2809b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2810b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
2811ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2812b78cd169SJaeden Amero 	if (!atmel_is_console_port(&atmel_port->uart)) {
2813b78cd169SJaeden Amero 		clk_put(atmel_port->clk);
2814b78cd169SJaeden Amero 		atmel_port->clk = NULL;
2815ab4382d2SGreg Kroah-Hartman 	}
28166fbb9bdfSCyrille Pitchen err_clear_bit:
2817b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
28184cbf9f48SNicolas Ferre err:
2819ab4382d2SGreg Kroah-Hartman 	return ret;
2820ab4382d2SGreg Kroah-Hartman }
2821ab4382d2SGreg Kroah-Hartman 
2822f4a8ab04SRomain Izard /*
2823f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2824f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2825f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2826f4a8ab04SRomain Izard  *
2827f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
2828f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
2829f4a8ab04SRomain Izard  * port in the normal case.
2830f4a8ab04SRomain Izard  */
2831f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
2832f4a8ab04SRomain Izard {
2833f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
2834f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2835f4a8ab04SRomain Izard 	int ret = 0;
2836f4a8ab04SRomain Izard 
283700e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
283800e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
2839f4a8ab04SRomain Izard 
2840f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
2841f4a8ab04SRomain Izard 
2842f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
2843f4a8ab04SRomain Izard 
2844f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
2845f4a8ab04SRomain Izard 
2846f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
2847f4a8ab04SRomain Izard 
2848f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
2849f4a8ab04SRomain Izard 
2850f4a8ab04SRomain Izard 	clk_put(atmel_port->clk);
2851f4a8ab04SRomain Izard 	atmel_port->clk = NULL;
2852f4a8ab04SRomain Izard 
2853f4a8ab04SRomain Izard 	return ret;
2854f4a8ab04SRomain Izard }
2855f4a8ab04SRomain Izard 
2856ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2857ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
2858f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
2859ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2860ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2861ab4382d2SGreg Kroah-Hartman 	.driver		= {
2862ab4382d2SGreg Kroah-Hartman 		.name			= "atmel_usart",
28635fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
2864ab4382d2SGreg Kroah-Hartman 	},
2865ab4382d2SGreg Kroah-Hartman };
2866ab4382d2SGreg Kroah-Hartman 
2867ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2868ab4382d2SGreg Kroah-Hartman {
2869ab4382d2SGreg Kroah-Hartman 	int ret;
2870ab4382d2SGreg Kroah-Hartman 
2871ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2872ab4382d2SGreg Kroah-Hartman 	if (ret)
2873ab4382d2SGreg Kroah-Hartman 		return ret;
2874ab4382d2SGreg Kroah-Hartman 
2875ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2876ab4382d2SGreg Kroah-Hartman 	if (ret)
2877ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2878ab4382d2SGreg Kroah-Hartman 
2879ab4382d2SGreg Kroah-Hartman 	return ret;
2880ab4382d2SGreg Kroah-Hartman }
2881c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
2882