xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision d033e82db9a5e9bc93224c8a058b9c03e4e3f8ad)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  Driver for Atmel AT91 / AT32 Serial ports
3ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7ab4382d2SGreg Kroah-Hartman  *
8ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
11ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
12ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
13ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
14ab4382d2SGreg Kroah-Hartman  *
15ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
16ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
19ab4382d2SGreg Kroah-Hartman  *
20ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
21ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
22ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23ab4382d2SGreg Kroah-Hartman  *
24ab4382d2SGreg Kroah-Hartman  */
25ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
35ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
365fbe46b6SNicolas Ferre #include <linux/of.h>
375fbe46b6SNicolas Ferre #include <linux/of_device.h>
38354e57f3SLinus Walleij #include <linux/of_gpio.h>
39ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
406b997babSVinod Koul #include <linux/dmaengine.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
43ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
44bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
452e68c22fSElen Song #include <linux/timer.h>
46354e57f3SLinus Walleij #include <linux/gpio.h>
47e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
48e0b0baadSRichard Genoud #include <linux/err.h>
49ab5e4e41SRichard Genoud #include <linux/irq.h>
502c7af5baSBoris BREZILLON #include <linux/suspend.h>
51ab4382d2SGreg Kroah-Hartman 
52ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
53ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
54ab4382d2SGreg Kroah-Hartman 
55ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
56ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
57ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
58ab4382d2SGreg Kroah-Hartman 
59ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
60ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
61ab4382d2SGreg Kroah-Hartman #endif
62ab4382d2SGreg Kroah-Hartman 
63ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
64ab4382d2SGreg Kroah-Hartman 
65e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
66e0b0baadSRichard Genoud 
67ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
68ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
69ab4382d2SGreg Kroah-Hartman 
70ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
71ab4382d2SGreg Kroah-Hartman 
72ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
73ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
74ab4382d2SGreg Kroah-Hartman  * UART. */
75ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
76ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
77ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
78ab4382d2SGreg Kroah-Hartman 
79ab4382d2SGreg Kroah-Hartman #else
80ab4382d2SGreg Kroah-Hartman 
81ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
82ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
83ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
84ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
85ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
86ab4382d2SGreg Kroah-Hartman 
87ab4382d2SGreg Kroah-Hartman #endif
88ab4382d2SGreg Kroah-Hartman 
89ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
90ab4382d2SGreg Kroah-Hartman 
91ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */
92ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_CR)
93ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port)	__raw_readl((port)->membase + ATMEL_US_MR)
94ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_MR)
95ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IER)
96ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IDR)
97ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port)	__raw_readl((port)->membase + ATMEL_US_IMR)
98ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port)	__raw_readl((port)->membase + ATMEL_US_CSR)
99ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port)	__raw_readl((port)->membase + ATMEL_US_RHR)
100ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_THR)
101ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port)	__raw_readl((port)->membase + ATMEL_US_BRGR)
102ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_BRGR)
103ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_RTOR)
104ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v)	__raw_writel(v, (port)->membase + ATMEL_US_TTGR)
105055560b0SElen Song #define UART_GET_IP_NAME(port)	__raw_readl((port)->membase + ATMEL_US_NAME)
106731d9caeSNicolas Ferre #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
107ab4382d2SGreg Kroah-Hartman 
108ab4382d2SGreg Kroah-Hartman  /* PDC registers */
109ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
110ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port)	__raw_readl((port)->membase + ATMEL_PDC_PTSR)
111ab4382d2SGreg Kroah-Hartman 
112ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
113ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port)	__raw_readl((port)->membase + ATMEL_PDC_RPR)
114ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
115ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
116ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
117ab4382d2SGreg Kroah-Hartman 
118ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
119ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
120ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port)	__raw_readl((port)->membase + ATMEL_PDC_TCR)
121ab4382d2SGreg Kroah-Hartman 
122ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
123ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
124ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
125ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
126ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
127ab4382d2SGreg Kroah-Hartman };
128ab4382d2SGreg Kroah-Hartman 
129ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
130ab4382d2SGreg Kroah-Hartman 	u16		status;
131ab4382d2SGreg Kroah-Hartman 	u16		ch;
132ab4382d2SGreg Kroah-Hartman };
133ab4382d2SGreg Kroah-Hartman 
134ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
135ab4382d2SGreg Kroah-Hartman 
136ab4382d2SGreg Kroah-Hartman /*
137ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
138ab4382d2SGreg Kroah-Hartman  */
139ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
140ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
141ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
142ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
143ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
144ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
145ab4382d2SGreg Kroah-Hartman 
14634df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
14764e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
148ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
149ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
150ab4382d2SGreg Kroah-Hartman 
15108f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
15264e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
153ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
154ab4382d2SGreg Kroah-Hartman 
15508f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
15634df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
15708f738beSElen Song 	struct dma_chan			*chan_tx;
15834df42f5SElen Song 	struct dma_chan			*chan_rx;
15908f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
16034df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
16108f738beSElen Song 	dma_cookie_t			cookie_tx;
16234df42f5SElen Song 	dma_cookie_t			cookie_rx;
16308f738beSElen Song 	struct scatterlist		sg_tx;
16434df42f5SElen Song 	struct scatterlist		sg_rx;
165ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
166ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
167ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
168*d033e82dSLeilei Zhao 	unsigned int		status_change;
169ab4382d2SGreg Kroah-Hartman 
170ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
171ab4382d2SGreg Kroah-Hartman 
172e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
173ab5e4e41SRichard Genoud 	int			gpio_irq[UART_GPIO_MAX];
174ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
175ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
176055560b0SElen Song 	bool			is_usart;	/* usart or uart */
1772e68c22fSElen Song 	struct timer_list	uart_timer;	/* uart timer */
1782c7af5baSBoris BREZILLON 
1792c7af5baSBoris BREZILLON 	bool			suspended;
1802c7af5baSBoris BREZILLON 	unsigned int		pending;
1812c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1822c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1832c7af5baSBoris BREZILLON 
184a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
185a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
186a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
187a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
188a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
189a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
190ab4382d2SGreg Kroah-Hartman };
191ab4382d2SGreg Kroah-Hartman 
192ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
193503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
194ab4382d2SGreg Kroah-Hartman 
195ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
196ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
197ab4382d2SGreg Kroah-Hartman #endif
198ab4382d2SGreg Kroah-Hartman 
1995fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
2005fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
2015fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
2025fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
2035fbe46b6SNicolas Ferre 	{ /* sentinel */ }
2045fbe46b6SNicolas Ferre };
2055fbe46b6SNicolas Ferre 
2065fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
2075fbe46b6SNicolas Ferre #endif
2085fbe46b6SNicolas Ferre 
209ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
210ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
211ab4382d2SGreg Kroah-Hartman {
212ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
213ab4382d2SGreg Kroah-Hartman }
214ab4382d2SGreg Kroah-Hartman 
215ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
21664e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
217ab4382d2SGreg Kroah-Hartman {
218ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
219ab4382d2SGreg Kroah-Hartman 
22064e22ebeSElen Song 	return atmel_port->use_pdc_rx;
221ab4382d2SGreg Kroah-Hartman }
222ab4382d2SGreg Kroah-Hartman 
22364e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
224ab4382d2SGreg Kroah-Hartman {
225ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
226ab4382d2SGreg Kroah-Hartman 
22764e22ebeSElen Song 	return atmel_port->use_pdc_tx;
228ab4382d2SGreg Kroah-Hartman }
229ab4382d2SGreg Kroah-Hartman #else
23064e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
231ab4382d2SGreg Kroah-Hartman {
232ab4382d2SGreg Kroah-Hartman 	return false;
233ab4382d2SGreg Kroah-Hartman }
234ab4382d2SGreg Kroah-Hartman 
23564e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
236ab4382d2SGreg Kroah-Hartman {
237ab4382d2SGreg Kroah-Hartman 	return false;
238ab4382d2SGreg Kroah-Hartman }
239ab4382d2SGreg Kroah-Hartman #endif
240ab4382d2SGreg Kroah-Hartman 
24108f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
24208f738beSElen Song {
24308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
24408f738beSElen Song 
24508f738beSElen Song 	return atmel_port->use_dma_tx;
24608f738beSElen Song }
24708f738beSElen Song 
24834df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
24934df42f5SElen Song {
25034df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
25134df42f5SElen Song 
25234df42f5SElen Song 	return atmel_port->use_dma_rx;
25334df42f5SElen Song }
25434df42f5SElen Song 
255e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
256e0b0baadSRichard Genoud {
257e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
258e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
259e0b0baadSRichard Genoud 
260e0b0baadSRichard Genoud 	status = UART_GET_CSR(port);
261e0b0baadSRichard Genoud 
262e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
263e0b0baadSRichard Genoud 
264e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
265e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
266e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
267e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
268e0b0baadSRichard Genoud 		else
269e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
270e0b0baadSRichard Genoud 	}
271e0b0baadSRichard Genoud 
272e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
273e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
274e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
275e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
276e0b0baadSRichard Genoud 		else
277e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
278e0b0baadSRichard Genoud 	}
279e0b0baadSRichard Genoud 
280e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
281e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
282e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
283e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
284e0b0baadSRichard Genoud 		else
285e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
286e0b0baadSRichard Genoud 	}
287e0b0baadSRichard Genoud 
288e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
289e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
290e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
291e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
292e0b0baadSRichard Genoud 		else
293e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
294e0b0baadSRichard Genoud 	}
295e0b0baadSRichard Genoud 
296e0b0baadSRichard Genoud 	return status;
297e0b0baadSRichard Genoud }
298e0b0baadSRichard Genoud 
299ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
30013bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
30113bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
302ab4382d2SGreg Kroah-Hartman {
303ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
304ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
305ab4382d2SGreg Kroah-Hartman 
306ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
307ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
308ab4382d2SGreg Kroah-Hartman 
309ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port);
310ab4382d2SGreg Kroah-Hartman 
311ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
312ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
313ab4382d2SGreg Kroah-Hartman 
31413bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
315ab4382d2SGreg Kroah-Hartman 
316ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
317ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
318ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
31993f3350cSClaudio Scordino 		if ((rs485conf->delay_rts_after_send) > 0)
320ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
321ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
322ab4382d2SGreg Kroah-Hartman 	} else {
323ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
32464e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
325ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
326ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
327ab4382d2SGreg Kroah-Hartman 		else
328ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
329ab4382d2SGreg Kroah-Hartman 	}
330ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
331ab4382d2SGreg Kroah-Hartman 
332ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
333ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
334ab4382d2SGreg Kroah-Hartman 
33513bd3e6fSRicardo Ribalda Delgado 	return 0;
336ab4382d2SGreg Kroah-Hartman }
337ab4382d2SGreg Kroah-Hartman 
338ab4382d2SGreg Kroah-Hartman /*
339ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
340ab4382d2SGreg Kroah-Hartman  */
341ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
342ab4382d2SGreg Kroah-Hartman {
343ab4382d2SGreg Kroah-Hartman 	return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
344ab4382d2SGreg Kroah-Hartman }
345ab4382d2SGreg Kroah-Hartman 
346ab4382d2SGreg Kroah-Hartman /*
347ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
348ab4382d2SGreg Kroah-Hartman  */
349ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
350ab4382d2SGreg Kroah-Hartman {
351ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3521cf6e8fcSCyrille Pitchen 	unsigned int mode = UART_GET_MR(port);
3531cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
354ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
355ab4382d2SGreg Kroah-Hartman 
3561cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3571cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
3581cf6e8fcSCyrille Pitchen 		if ((port->rs485.delay_rts_after_send) > 0)
3591cf6e8fcSCyrille Pitchen 			UART_PUT_TTGR(port, port->rs485.delay_rts_after_send);
3601cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3611cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3621cf6e8fcSCyrille Pitchen 	}
3631cf6e8fcSCyrille Pitchen 
3641cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
3651cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
3661cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3671cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
3681cf6e8fcSCyrille Pitchen 
3691cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
3701cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
3711cf6e8fcSCyrille Pitchen 	} else {
3721cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3731cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
3741cf6e8fcSCyrille Pitchen 
3751cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
3761cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
3771cf6e8fcSCyrille Pitchen 	}
3781cf6e8fcSCyrille Pitchen 
379ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
3801cf6e8fcSCyrille Pitchen 		control |= rts_ready;
381ab4382d2SGreg Kroah-Hartman 	else
3821cf6e8fcSCyrille Pitchen 		control |= rts_paused;
383ab4382d2SGreg Kroah-Hartman 
384ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
385ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
386ab4382d2SGreg Kroah-Hartman 	else
387ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
388ab4382d2SGreg Kroah-Hartman 
389ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, control);
390ab4382d2SGreg Kroah-Hartman 
391e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
392e0b0baadSRichard Genoud 
393ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
3941cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
395ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
396ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
397ab4382d2SGreg Kroah-Hartman 	else
398ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
399ab4382d2SGreg Kroah-Hartman 
400ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
401ab4382d2SGreg Kroah-Hartman }
402ab4382d2SGreg Kroah-Hartman 
403ab4382d2SGreg Kroah-Hartman /*
404ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
405ab4382d2SGreg Kroah-Hartman  */
406ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
407ab4382d2SGreg Kroah-Hartman {
408e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
409e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
410ab4382d2SGreg Kroah-Hartman 
411ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
412ab4382d2SGreg Kroah-Hartman 
413ab4382d2SGreg Kroah-Hartman 	/*
414ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
415ab4382d2SGreg Kroah-Hartman 	 */
416ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
417ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
418ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
419ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
420ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
421ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
422ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
423ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
424ab4382d2SGreg Kroah-Hartman 
425e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
426ab4382d2SGreg Kroah-Hartman }
427ab4382d2SGreg Kroah-Hartman 
428ab4382d2SGreg Kroah-Hartman /*
429ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
430ab4382d2SGreg Kroah-Hartman  */
431ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
432ab4382d2SGreg Kroah-Hartman {
433ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
434ab4382d2SGreg Kroah-Hartman 
43564e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
436ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
437ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
438ab4382d2SGreg Kroah-Hartman 	}
439ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
440ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
441ab4382d2SGreg Kroah-Hartman 
44213bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
44313bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
444ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
445ab4382d2SGreg Kroah-Hartman }
446ab4382d2SGreg Kroah-Hartman 
447ab4382d2SGreg Kroah-Hartman /*
448ab4382d2SGreg Kroah-Hartman  * Start transmitting.
449ab4382d2SGreg Kroah-Hartman  */
450ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
451ab4382d2SGreg Kroah-Hartman {
452ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
453ab4382d2SGreg Kroah-Hartman 
45464e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
455ab4382d2SGreg Kroah-Hartman 		if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
456ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
457ab4382d2SGreg Kroah-Hartman 			   really need this.*/
458ab4382d2SGreg Kroah-Hartman 			return;
459ab4382d2SGreg Kroah-Hartman 
46013bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
46113bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
462ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
463ab4382d2SGreg Kroah-Hartman 
464ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
465ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
466ab4382d2SGreg Kroah-Hartman 	}
467ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
468ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
469ab4382d2SGreg Kroah-Hartman }
470ab4382d2SGreg Kroah-Hartman 
471ab4382d2SGreg Kroah-Hartman /*
472ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
473ab4382d2SGreg Kroah-Hartman  */
474ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
475ab4382d2SGreg Kroah-Hartman {
476ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);  /* reset status and receiver */
477ab4382d2SGreg Kroah-Hartman 
47857c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXEN);
47957c36868SSiftar, Gabe 
48064e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
481ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
482ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
483ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
484ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
485ab4382d2SGreg Kroah-Hartman 	} else {
486ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
487ab4382d2SGreg Kroah-Hartman 	}
488ab4382d2SGreg Kroah-Hartman }
489ab4382d2SGreg Kroah-Hartman 
490ab4382d2SGreg Kroah-Hartman /*
491ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
492ab4382d2SGreg Kroah-Hartman  */
493ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
494ab4382d2SGreg Kroah-Hartman {
49557c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXDIS);
49657c36868SSiftar, Gabe 
49764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
498ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
499ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
500ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
501ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
502ab4382d2SGreg Kroah-Hartman 	} else {
503ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXRDY);
504ab4382d2SGreg Kroah-Hartman 	}
505ab4382d2SGreg Kroah-Hartman }
506ab4382d2SGreg Kroah-Hartman 
507ab4382d2SGreg Kroah-Hartman /*
508ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
509ab4382d2SGreg Kroah-Hartman  */
510ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
511ab4382d2SGreg Kroah-Hartman {
512ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
513ab5e4e41SRichard Genoud 	uint32_t ier = 0;
514ab5e4e41SRichard Genoud 
515ab5e4e41SRichard Genoud 	/*
516ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
517ab5e4e41SRichard Genoud 	 */
518ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
519ab5e4e41SRichard Genoud 		return;
520ab5e4e41SRichard Genoud 
521ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
522ab5e4e41SRichard Genoud 
523ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
524ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
525ab5e4e41SRichard Genoud 	else
526ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
527ab5e4e41SRichard Genoud 
528ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
529ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
530ab5e4e41SRichard Genoud 	else
531ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
532ab5e4e41SRichard Genoud 
533ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
534ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
535ab5e4e41SRichard Genoud 	else
536ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
537ab5e4e41SRichard Genoud 
538ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
539ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
540ab5e4e41SRichard Genoud 	else
541ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
542ab5e4e41SRichard Genoud 
543ab5e4e41SRichard Genoud 	UART_PUT_IER(port, ier);
544ab4382d2SGreg Kroah-Hartman }
545ab4382d2SGreg Kroah-Hartman 
546ab4382d2SGreg Kroah-Hartman /*
54735b675b9SRichard Genoud  * Disable modem status interrupts
54835b675b9SRichard Genoud  */
54935b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
55035b675b9SRichard Genoud {
55135b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
55235b675b9SRichard Genoud 	uint32_t idr = 0;
55335b675b9SRichard Genoud 
55435b675b9SRichard Genoud 	/*
55535b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
55635b675b9SRichard Genoud 	 */
55735b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
55835b675b9SRichard Genoud 		return;
55935b675b9SRichard Genoud 
56035b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
56135b675b9SRichard Genoud 
56235b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
56335b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
56435b675b9SRichard Genoud 	else
56535b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
56635b675b9SRichard Genoud 
56735b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
56835b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
56935b675b9SRichard Genoud 	else
57035b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
57135b675b9SRichard Genoud 
57235b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
57335b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
57435b675b9SRichard Genoud 	else
57535b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
57635b675b9SRichard Genoud 
57735b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
57835b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
57935b675b9SRichard Genoud 	else
58035b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
58135b675b9SRichard Genoud 
58235b675b9SRichard Genoud 	UART_PUT_IDR(port, idr);
58335b675b9SRichard Genoud }
58435b675b9SRichard Genoud 
58535b675b9SRichard Genoud /*
586ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
587ab4382d2SGreg Kroah-Hartman  */
588ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
589ab4382d2SGreg Kroah-Hartman {
590ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
591ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTBRK);	/* start break */
592ab4382d2SGreg Kroah-Hartman 	else
593ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STPBRK);	/* stop break */
594ab4382d2SGreg Kroah-Hartman }
595ab4382d2SGreg Kroah-Hartman 
596ab4382d2SGreg Kroah-Hartman /*
597ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
598ab4382d2SGreg Kroah-Hartman  */
599ab4382d2SGreg Kroah-Hartman static void
600ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
601ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
602ab4382d2SGreg Kroah-Hartman {
603ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
604ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
605ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
606ab4382d2SGreg Kroah-Hartman 
607ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
608ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
609ab4382d2SGreg Kroah-Hartman 		return;
610ab4382d2SGreg Kroah-Hartman 
611ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
612ab4382d2SGreg Kroah-Hartman 	c->status	= status;
613ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
614ab4382d2SGreg Kroah-Hartman 
615ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
616ab4382d2SGreg Kroah-Hartman 	smp_wmb();
617ab4382d2SGreg Kroah-Hartman 
618ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
619ab4382d2SGreg Kroah-Hartman }
620ab4382d2SGreg Kroah-Hartman 
621ab4382d2SGreg Kroah-Hartman /*
622ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
623ab4382d2SGreg Kroah-Hartman  */
624ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
625ab4382d2SGreg Kroah-Hartman {
626ab4382d2SGreg Kroah-Hartman 	/* clear error */
627ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
628ab4382d2SGreg Kroah-Hartman 
629ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
630ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
631ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
632ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
633ab4382d2SGreg Kroah-Hartman 	}
634ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
635ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
636ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
637ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
638ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
639ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
640ab4382d2SGreg Kroah-Hartman }
641ab4382d2SGreg Kroah-Hartman 
642ab4382d2SGreg Kroah-Hartman /*
643ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
644ab4382d2SGreg Kroah-Hartman  */
645ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
646ab4382d2SGreg Kroah-Hartman {
647ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
648ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
649ab4382d2SGreg Kroah-Hartman 
650ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
651ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
652ab4382d2SGreg Kroah-Hartman 		ch = UART_GET_CHAR(port);
653ab4382d2SGreg Kroah-Hartman 
654ab4382d2SGreg Kroah-Hartman 		/*
655ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
656ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
657ab4382d2SGreg Kroah-Hartman 		 */
658ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
659ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
660ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
661ab4382d2SGreg Kroah-Hartman 
662ab4382d2SGreg Kroah-Hartman 			/* clear error */
663ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_RSTSTA);
664ab4382d2SGreg Kroah-Hartman 
665ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
666ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
667ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
668ab4382d2SGreg Kroah-Hartman 				UART_PUT_IER(port, ATMEL_US_RXBRK);
669ab4382d2SGreg Kroah-Hartman 			} else {
670ab4382d2SGreg Kroah-Hartman 				/*
671ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
672ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
673ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
674ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
675ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
676ab4382d2SGreg Kroah-Hartman 				 */
677ab4382d2SGreg Kroah-Hartman 				UART_PUT_IDR(port, ATMEL_US_RXBRK);
678ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
679ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
680ab4382d2SGreg Kroah-Hartman 			}
681ab4382d2SGreg Kroah-Hartman 		}
682ab4382d2SGreg Kroah-Hartman 
683ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
684ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
685ab4382d2SGreg Kroah-Hartman 	}
686ab4382d2SGreg Kroah-Hartman 
687ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
688ab4382d2SGreg Kroah-Hartman }
689ab4382d2SGreg Kroah-Hartman 
690ab4382d2SGreg Kroah-Hartman /*
691ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
692ab4382d2SGreg Kroah-Hartman  * disabled)
693ab4382d2SGreg Kroah-Hartman  */
694ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
695ab4382d2SGreg Kroah-Hartman {
696ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
697ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
698ab4382d2SGreg Kroah-Hartman 
699ab4382d2SGreg Kroah-Hartman 	if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
700ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, port->x_char);
701ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
702ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
703ab4382d2SGreg Kroah-Hartman 	}
704ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
705ab4382d2SGreg Kroah-Hartman 		return;
706ab4382d2SGreg Kroah-Hartman 
707ab4382d2SGreg Kroah-Hartman 	while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
708ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
709ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
710ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
711ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
712ab4382d2SGreg Kroah-Hartman 			break;
713ab4382d2SGreg Kroah-Hartman 	}
714ab4382d2SGreg Kroah-Hartman 
715ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
716ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
717ab4382d2SGreg Kroah-Hartman 
718ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
719ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
720ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
721ab4382d2SGreg Kroah-Hartman }
722ab4382d2SGreg Kroah-Hartman 
72308f738beSElen Song static void atmel_complete_tx_dma(void *arg)
72408f738beSElen Song {
72508f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
72608f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
72708f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
72808f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
72908f738beSElen Song 	unsigned long flags;
73008f738beSElen Song 
73108f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
73208f738beSElen Song 
73308f738beSElen Song 	if (chan)
73408f738beSElen Song 		dmaengine_terminate_all(chan);
73508f738beSElen Song 	xmit->tail += sg_dma_len(&atmel_port->sg_tx);
73608f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
73708f738beSElen Song 
73808f738beSElen Song 	port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
73908f738beSElen Song 
74008f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
74108f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
74208f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
74308f738beSElen Song 	atmel_port->desc_tx = NULL;
74408f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
74508f738beSElen Song 
74608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
74708f738beSElen Song 		uart_write_wakeup(port);
74808f738beSElen Song 
7491842dc2eSCyrille Pitchen 	/*
7501842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7511842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7521842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7531842dc2eSCyrille Pitchen 	 */
75408f738beSElen Song 	if (!uart_circ_empty(xmit))
75508f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
75608f738beSElen Song 
75708f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
75808f738beSElen Song }
75908f738beSElen Song 
76008f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
76108f738beSElen Song {
76208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
76308f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
76408f738beSElen Song 
76508f738beSElen Song 	if (chan) {
76608f738beSElen Song 		dmaengine_terminate_all(chan);
76708f738beSElen Song 		dma_release_channel(chan);
76808f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
76948479148SWolfram Sang 				DMA_TO_DEVICE);
77008f738beSElen Song 	}
77108f738beSElen Song 
77208f738beSElen Song 	atmel_port->desc_tx = NULL;
77308f738beSElen Song 	atmel_port->chan_tx = NULL;
77408f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
77508f738beSElen Song }
77608f738beSElen Song 
77708f738beSElen Song /*
77808f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
77908f738beSElen Song  */
78008f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
78108f738beSElen Song {
78208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
78308f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
78408f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
78508f738beSElen Song 	struct dma_async_tx_descriptor *desc;
78608f738beSElen Song 	struct scatterlist *sg = &atmel_port->sg_tx;
78708f738beSElen Song 
78808f738beSElen Song 	/* Make sure we have an idle channel */
78908f738beSElen Song 	if (atmel_port->desc_tx != NULL)
79008f738beSElen Song 		return;
79108f738beSElen Song 
79208f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
79308f738beSElen Song 		/*
79408f738beSElen Song 		 * DMA is idle now.
79508f738beSElen Song 		 * Port xmit buffer is already mapped,
79608f738beSElen Song 		 * and it is one page... Just adjust
79708f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
79808f738beSElen Song 		 * we have to transmit till the end, and then the rest.
79908f738beSElen Song 		 * Take the port lock to get a
80008f738beSElen Song 		 * consistent xmit buffer state.
80108f738beSElen Song 		 */
80208f738beSElen Song 		sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
80308f738beSElen Song 		sg_dma_address(sg) = (sg_dma_address(sg) &
80408f738beSElen Song 					~(UART_XMIT_SIZE - 1))
80508f738beSElen Song 					+ sg->offset;
80608f738beSElen Song 		sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
80708f738beSElen Song 						xmit->tail,
80808f738beSElen Song 						UART_XMIT_SIZE);
80908f738beSElen Song 		BUG_ON(!sg_dma_len(sg));
81008f738beSElen Song 
81108f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
81208f738beSElen Song 					       sg,
81308f738beSElen Song 					       1,
81408f738beSElen Song 					       DMA_MEM_TO_DEV,
81508f738beSElen Song 					       DMA_PREP_INTERRUPT |
81608f738beSElen Song 					       DMA_CTRL_ACK);
81708f738beSElen Song 		if (!desc) {
81808f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
81908f738beSElen Song 			return;
82008f738beSElen Song 		}
82108f738beSElen Song 
822485819b5SCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
82308f738beSElen Song 
82408f738beSElen Song 		atmel_port->desc_tx = desc;
82508f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
82608f738beSElen Song 		desc->callback_param = atmel_port;
82708f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
82808f738beSElen Song 
82908f738beSElen Song 	} else {
83013bd3e6fSRicardo Ribalda Delgado 		if (port->rs485.flags & SER_RS485_ENABLED) {
83108f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
83208f738beSElen Song 			atmel_start_rx(port);
83308f738beSElen Song 		}
83408f738beSElen Song 	}
83508f738beSElen Song 
83608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
83708f738beSElen Song 		uart_write_wakeup(port);
83808f738beSElen Song }
83908f738beSElen Song 
84008f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
84108f738beSElen Song {
84208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
84308f738beSElen Song 	dma_cap_mask_t		mask;
84408f738beSElen Song 	struct dma_slave_config config;
84508f738beSElen Song 	int ret, nent;
84608f738beSElen Song 
84708f738beSElen Song 	dma_cap_zero(mask);
84808f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
84908f738beSElen Song 
85008f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
85108f738beSElen Song 	if (atmel_port->chan_tx == NULL)
85208f738beSElen Song 		goto chan_err;
85308f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
85408f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
85508f738beSElen Song 
85608f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
85708f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
85808f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
8592c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
86008f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
86108f738beSElen Song 			virt_to_page(port->state->xmit.buf),
86208f738beSElen Song 			UART_XMIT_SIZE,
86308f738beSElen Song 			(int)port->state->xmit.buf & ~PAGE_MASK);
86408f738beSElen Song 	nent = dma_map_sg(port->dev,
86508f738beSElen Song 				&atmel_port->sg_tx,
86608f738beSElen Song 				1,
86748479148SWolfram Sang 				DMA_TO_DEVICE);
86808f738beSElen Song 
86908f738beSElen Song 	if (!nent) {
87008f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
87108f738beSElen Song 		goto chan_err;
87208f738beSElen Song 	} else {
87308f738beSElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
87408f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
87508f738beSElen Song 			port->state->xmit.buf,
87608f738beSElen Song 			sg_dma_address(&atmel_port->sg_tx));
87708f738beSElen Song 	}
87808f738beSElen Song 
87908f738beSElen Song 	/* Configure the slave DMA */
88008f738beSElen Song 	memset(&config, 0, sizeof(config));
88108f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
88208f738beSElen Song 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
88308f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
884a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
88508f738beSElen Song 
8865483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
8875483c10eSMaxime Ripard 				     &config);
88808f738beSElen Song 	if (ret) {
88908f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
89008f738beSElen Song 		goto chan_err;
89108f738beSElen Song 	}
89208f738beSElen Song 
89308f738beSElen Song 	return 0;
89408f738beSElen Song 
89508f738beSElen Song chan_err:
89608f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
89708f738beSElen Song 	atmel_port->use_dma_tx = 0;
89808f738beSElen Song 	if (atmel_port->chan_tx)
89908f738beSElen Song 		atmel_release_tx_dma(port);
90008f738beSElen Song 	return -EINVAL;
90108f738beSElen Song }
90208f738beSElen Song 
90334df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
90434df42f5SElen Song {
90534df42f5SElen Song 	struct uart_port *port = arg;
90634df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
90734df42f5SElen Song 
90834df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
90934df42f5SElen Song }
91034df42f5SElen Song 
91134df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
91234df42f5SElen Song {
91334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
91434df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
91534df42f5SElen Song 
91634df42f5SElen Song 	if (chan) {
91734df42f5SElen Song 		dmaengine_terminate_all(chan);
91834df42f5SElen Song 		dma_release_channel(chan);
91934df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
92048479148SWolfram Sang 				DMA_FROM_DEVICE);
92134df42f5SElen Song 	}
92234df42f5SElen Song 
92334df42f5SElen Song 	atmel_port->desc_rx = NULL;
92434df42f5SElen Song 	atmel_port->chan_rx = NULL;
92534df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
92634df42f5SElen Song }
92734df42f5SElen Song 
92834df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
92934df42f5SElen Song {
93034df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
93166f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
93234df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
93334df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
93434df42f5SElen Song 	struct dma_tx_state state;
93534df42f5SElen Song 	enum dma_status dmastat;
93666f37aafSCyrille Pitchen 	size_t count;
93734df42f5SElen Song 
93834df42f5SElen Song 
93934df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
94034df42f5SElen Song 	UART_PUT_CR(port, ATMEL_US_STTTO);
94134df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
94234df42f5SElen Song 				atmel_port->cookie_rx,
94334df42f5SElen Song 				&state);
94434df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
94534df42f5SElen Song 	if (dmastat == DMA_ERROR) {
94634df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
94734df42f5SElen Song 		UART_PUT_IER(port, ATMEL_US_TIMEOUT);
94834df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
94934df42f5SElen Song 		return;
95034df42f5SElen Song 	}
95166f37aafSCyrille Pitchen 
95266f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
95366f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
95466f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
95566f37aafSCyrille Pitchen 			    1,
956485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
95734df42f5SElen Song 
95834df42f5SElen Song 	/*
95966f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
96066f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
96166f37aafSCyrille Pitchen 	 * framework.
96266f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
96366f37aafSCyrille Pitchen 	 * length.
96434df42f5SElen Song 	 */
96566f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
96666f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
96766f37aafSCyrille Pitchen 	/*
96866f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
96966f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
97066f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
97166f37aafSCyrille Pitchen 	 *
97266f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
97366f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
97466f37aafSCyrille Pitchen 	 *
97566f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
97666f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
97766f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
97866f37aafSCyrille Pitchen 	 */
97966f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
98066f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
98134df42f5SElen Song 
98266f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
98366f37aafSCyrille Pitchen 		ring->tail = 0;
98434df42f5SElen Song 		port->icount.rx += count;
98534df42f5SElen Song 	}
98634df42f5SElen Song 
98766f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
98866f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
98966f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
99066f37aafSCyrille Pitchen 
99166f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
99266f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
99366f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
99466f37aafSCyrille Pitchen 			ring->head = 0;
99566f37aafSCyrille Pitchen 		ring->tail = ring->head;
99666f37aafSCyrille Pitchen 		port->icount.rx += count;
99766f37aafSCyrille Pitchen 	}
99866f37aafSCyrille Pitchen 
99966f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
100066f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
100166f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
100266f37aafSCyrille Pitchen 			       1,
1003485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
100466f37aafSCyrille Pitchen 
100566f37aafSCyrille Pitchen 	/*
100666f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
100766f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
100866f37aafSCyrille Pitchen 	 */
100966f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
101066f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
101166f37aafSCyrille Pitchen 	spin_lock(&port->lock);
101266f37aafSCyrille Pitchen 
101334df42f5SElen Song 	UART_PUT_IER(port, ATMEL_US_TIMEOUT);
101434df42f5SElen Song }
101534df42f5SElen Song 
101634df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
101734df42f5SElen Song {
101834df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
101934df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
102034df42f5SElen Song 	dma_cap_mask_t		mask;
102134df42f5SElen Song 	struct dma_slave_config config;
102234df42f5SElen Song 	struct circ_buf		*ring;
102334df42f5SElen Song 	int ret, nent;
102434df42f5SElen Song 
102534df42f5SElen Song 	ring = &atmel_port->rx_ring;
102634df42f5SElen Song 
102734df42f5SElen Song 	dma_cap_zero(mask);
102834df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
102934df42f5SElen Song 
103034df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
103134df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
103234df42f5SElen Song 		goto chan_err;
103334df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
103434df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
103534df42f5SElen Song 
103634df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
103734df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
103834df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
10392c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
104034df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
104134df42f5SElen Song 		    virt_to_page(ring->buf),
1042a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
104334df42f5SElen Song 		    (int)ring->buf & ~PAGE_MASK);
104434df42f5SElen Song 	nent = dma_map_sg(port->dev,
104534df42f5SElen Song 			  &atmel_port->sg_rx,
104634df42f5SElen Song 			  1,
104748479148SWolfram Sang 			  DMA_FROM_DEVICE);
104834df42f5SElen Song 
104934df42f5SElen Song 	if (!nent) {
105034df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
105134df42f5SElen Song 		goto chan_err;
105234df42f5SElen Song 	} else {
105334df42f5SElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
105434df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
105534df42f5SElen Song 			ring->buf,
105634df42f5SElen Song 			sg_dma_address(&atmel_port->sg_rx));
105734df42f5SElen Song 	}
105834df42f5SElen Song 
105934df42f5SElen Song 	/* Configure the slave DMA */
106034df42f5SElen Song 	memset(&config, 0, sizeof(config));
106134df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
106234df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
106334df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1064a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
106534df42f5SElen Song 
10665483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
10675483c10eSMaxime Ripard 				     &config);
106834df42f5SElen Song 	if (ret) {
106934df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
107034df42f5SElen Song 		goto chan_err;
107134df42f5SElen Song 	}
107234df42f5SElen Song 	/*
107334df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
107434df42f5SElen Song 	 * each one is half ring buffer size
107534df42f5SElen Song 	 */
107634df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
107734df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
107834df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
107934df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
108034df42f5SElen Song 					 DMA_DEV_TO_MEM,
108134df42f5SElen Song 					 DMA_PREP_INTERRUPT);
108234df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
108334df42f5SElen Song 	desc->callback_param = port;
108434df42f5SElen Song 	atmel_port->desc_rx = desc;
108534df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
108634df42f5SElen Song 
108734df42f5SElen Song 	return 0;
108834df42f5SElen Song 
108934df42f5SElen Song chan_err:
109034df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
109134df42f5SElen Song 	atmel_port->use_dma_rx = 0;
109234df42f5SElen Song 	if (atmel_port->chan_rx)
109334df42f5SElen Song 		atmel_release_rx_dma(port);
109434df42f5SElen Song 	return -EINVAL;
109534df42f5SElen Song }
109634df42f5SElen Song 
10972e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
10982e68c22fSElen Song {
10992e68c22fSElen Song 	struct uart_port *port = (void *)data;
11002e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11012e68c22fSElen Song 
11022e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
11032e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
11042e68c22fSElen Song }
11052e68c22fSElen Song 
1106ab4382d2SGreg Kroah-Hartman /*
1107ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1108ab4382d2SGreg Kroah-Hartman  */
1109ab4382d2SGreg Kroah-Hartman static void
1110ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1111ab4382d2SGreg Kroah-Hartman {
1112ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1113ab4382d2SGreg Kroah-Hartman 
111464e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1115ab4382d2SGreg Kroah-Hartman 		/*
1116ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1117ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1118ab4382d2SGreg Kroah-Hartman 		 *
1119ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1120ab4382d2SGreg Kroah-Hartman 		 * the moment.
1121ab4382d2SGreg Kroah-Hartman 		 */
1122ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
1123ab4382d2SGreg Kroah-Hartman 			UART_PUT_IDR(port, (ATMEL_US_ENDRX
1124ab4382d2SGreg Kroah-Hartman 						| ATMEL_US_TIMEOUT));
1125ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
1126ab4382d2SGreg Kroah-Hartman 		}
1127ab4382d2SGreg Kroah-Hartman 
1128ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1129ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1130ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1131ab4382d2SGreg Kroah-Hartman 	}
1132ab4382d2SGreg Kroah-Hartman 
113334df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
113434df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
113534df42f5SElen Song 			UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
113634df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
113734df42f5SElen Song 		}
113834df42f5SElen Song 	}
113934df42f5SElen Song 
1140ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1141ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1142ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1143ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1144ab4382d2SGreg Kroah-Hartman 		/*
1145ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1146ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1147ab4382d2SGreg Kroah-Hartman 		 */
1148ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_RSTSTA);
1149ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXBRK);
1150ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1151ab4382d2SGreg Kroah-Hartman 	}
1152ab4382d2SGreg Kroah-Hartman }
1153ab4382d2SGreg Kroah-Hartman 
1154ab4382d2SGreg Kroah-Hartman /*
1155ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1156ab4382d2SGreg Kroah-Hartman  */
1157ab4382d2SGreg Kroah-Hartman static void
1158ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1159ab4382d2SGreg Kroah-Hartman {
1160ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1161ab4382d2SGreg Kroah-Hartman 
1162ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1163ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
1164ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, atmel_port->tx_done_mask);
1165ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1166ab4382d2SGreg Kroah-Hartman 	}
1167ab4382d2SGreg Kroah-Hartman }
1168ab4382d2SGreg Kroah-Hartman 
1169ab4382d2SGreg Kroah-Hartman /*
1170ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1171ab4382d2SGreg Kroah-Hartman  */
1172ab4382d2SGreg Kroah-Hartman static void
1173ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1174ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1175ab4382d2SGreg Kroah-Hartman {
1176ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1177ab4382d2SGreg Kroah-Hartman 
1178ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1179ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1180ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1181*d033e82dSLeilei Zhao 		atmel_port->status_change = atmel_port->irq_status ^
1182*d033e82dSLeilei Zhao 					    atmel_port->irq_status_prev;
1183*d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
1184ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1185ab4382d2SGreg Kroah-Hartman 	}
1186ab4382d2SGreg Kroah-Hartman }
1187ab4382d2SGreg Kroah-Hartman 
1188ab4382d2SGreg Kroah-Hartman /*
1189ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1190ab4382d2SGreg Kroah-Hartman  */
1191ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1192ab4382d2SGreg Kroah-Hartman {
1193ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1194ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11952c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1196ab5e4e41SRichard Genoud 	bool gpio_handled = false;
1197ab4382d2SGreg Kroah-Hartman 
11982c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
11992c7af5baSBoris BREZILLON 
1200ab4382d2SGreg Kroah-Hartman 	do {
1201e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
12022c7af5baSBoris BREZILLON 		mask = UART_GET_IMR(port);
12032c7af5baSBoris BREZILLON 		pending = status & mask;
1204ab5e4e41SRichard Genoud 		if (!gpio_handled) {
1205ab5e4e41SRichard Genoud 			/*
1206ab5e4e41SRichard Genoud 			 * Dealing with GPIO interrupt
1207ab5e4e41SRichard Genoud 			 */
1208ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1209ab5e4e41SRichard Genoud 				pending |= ATMEL_US_CTSIC;
1210ab5e4e41SRichard Genoud 
1211ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1212ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DSRIC;
1213ab5e4e41SRichard Genoud 
1214ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1215ab5e4e41SRichard Genoud 				pending |= ATMEL_US_RIIC;
1216ab5e4e41SRichard Genoud 
1217ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1218ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DCDIC;
1219ab5e4e41SRichard Genoud 
1220ab5e4e41SRichard Genoud 			gpio_handled = true;
1221ab5e4e41SRichard Genoud 		}
1222ab4382d2SGreg Kroah-Hartman 		if (!pending)
1223ab4382d2SGreg Kroah-Hartman 			break;
1224ab4382d2SGreg Kroah-Hartman 
12252c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
12262c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
12272c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
12282c7af5baSBoris BREZILLON 			UART_PUT_IDR(port, mask);
12292c7af5baSBoris BREZILLON 			pm_system_wakeup();
12302c7af5baSBoris BREZILLON 			break;
12312c7af5baSBoris BREZILLON 		}
12322c7af5baSBoris BREZILLON 
1233ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1234ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1235ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1236ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1237ab4382d2SGreg Kroah-Hartman 
12382c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
12392c7af5baSBoris BREZILLON 
1240ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1241ab4382d2SGreg Kroah-Hartman }
1242ab4382d2SGreg Kroah-Hartman 
1243a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1244a930e528SElen Song {
1245a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1246a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1247a930e528SElen Song 
1248a930e528SElen Song 	dma_unmap_single(port->dev,
1249a930e528SElen Song 			 pdc->dma_addr,
1250a930e528SElen Song 			 pdc->dma_size,
1251a930e528SElen Song 			 DMA_TO_DEVICE);
1252a930e528SElen Song }
1253a930e528SElen Song 
1254ab4382d2SGreg Kroah-Hartman /*
1255ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1256ab4382d2SGreg Kroah-Hartman  */
125764e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1258ab4382d2SGreg Kroah-Hartman {
1259ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1260ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1261ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1262ab4382d2SGreg Kroah-Hartman 	int count;
1263ab4382d2SGreg Kroah-Hartman 
1264ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
1265ab4382d2SGreg Kroah-Hartman 	if (UART_GET_TCR(port))
1266ab4382d2SGreg Kroah-Hartman 		return;
1267ab4382d2SGreg Kroah-Hartman 
1268ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1269ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1270ab4382d2SGreg Kroah-Hartman 
1271ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1272ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1273ab4382d2SGreg Kroah-Hartman 
1274ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1275ab4382d2SGreg Kroah-Hartman 
1276ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
1277ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1278ab4382d2SGreg Kroah-Hartman 
1279ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1280ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1281ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1282ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1283ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1284ab4382d2SGreg Kroah-Hartman 
1285ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1286ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1287ab4382d2SGreg Kroah-Hartman 
1288ab4382d2SGreg Kroah-Hartman 		UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1289ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, count);
1290ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
1291ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1292ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
1293ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
1294ab4382d2SGreg Kroah-Hartman 	} else {
129513bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
129613bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1297ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1298ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1299ab4382d2SGreg Kroah-Hartman 		}
1300ab4382d2SGreg Kroah-Hartman 	}
1301ab4382d2SGreg Kroah-Hartman 
1302ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1303ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1304ab4382d2SGreg Kroah-Hartman }
1305ab4382d2SGreg Kroah-Hartman 
1306a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1307a930e528SElen Song {
1308a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1309a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1310a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1311a930e528SElen Song 
1312a930e528SElen Song 	pdc->buf = xmit->buf;
1313a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1314a930e528SElen Song 					pdc->buf,
1315a930e528SElen Song 					UART_XMIT_SIZE,
1316a930e528SElen Song 					DMA_TO_DEVICE);
1317a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1318a930e528SElen Song 	pdc->ofs = 0;
1319a930e528SElen Song 
1320a930e528SElen Song 	return 0;
1321a930e528SElen Song }
1322a930e528SElen Song 
1323ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1324ab4382d2SGreg Kroah-Hartman {
1325ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1326ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1327ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1328ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1329ab4382d2SGreg Kroah-Hartman 
1330ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1331ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1332ab4382d2SGreg Kroah-Hartman 
1333ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1334ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1335ab4382d2SGreg Kroah-Hartman 
1336ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1337ab4382d2SGreg Kroah-Hartman 
1338ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1339ab4382d2SGreg Kroah-Hartman 
1340ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1341ab4382d2SGreg Kroah-Hartman 		status = c.status;
1342ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1343ab4382d2SGreg Kroah-Hartman 
1344ab4382d2SGreg Kroah-Hartman 		/*
1345ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1346ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1347ab4382d2SGreg Kroah-Hartman 		 */
1348ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1349ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1350ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1351ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1352ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1353ab4382d2SGreg Kroah-Hartman 
1354ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1355ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1356ab4382d2SGreg Kroah-Hartman 					continue;
1357ab4382d2SGreg Kroah-Hartman 			}
1358ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1359ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1360ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1361ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1362ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1363ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1364ab4382d2SGreg Kroah-Hartman 
1365ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1366ab4382d2SGreg Kroah-Hartman 
1367ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1368ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1369ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1370ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1371ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1372ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1373ab4382d2SGreg Kroah-Hartman 		}
1374ab4382d2SGreg Kroah-Hartman 
1375ab4382d2SGreg Kroah-Hartman 
1376ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1377ab4382d2SGreg Kroah-Hartman 			continue;
1378ab4382d2SGreg Kroah-Hartman 
1379ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1380ab4382d2SGreg Kroah-Hartman 	}
1381ab4382d2SGreg Kroah-Hartman 
1382ab4382d2SGreg Kroah-Hartman 	/*
1383ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1384ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1385ab4382d2SGreg Kroah-Hartman 	 */
1386ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
13872e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1388ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1389ab4382d2SGreg Kroah-Hartman }
1390ab4382d2SGreg Kroah-Hartman 
1391a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1392a930e528SElen Song {
1393a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1394a930e528SElen Song 	int i;
1395a930e528SElen Song 
1396a930e528SElen Song 	for (i = 0; i < 2; i++) {
1397a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1398a930e528SElen Song 
1399a930e528SElen Song 		dma_unmap_single(port->dev,
1400a930e528SElen Song 				 pdc->dma_addr,
1401a930e528SElen Song 				 pdc->dma_size,
1402a930e528SElen Song 				 DMA_FROM_DEVICE);
1403a930e528SElen Song 		kfree(pdc->buf);
1404a930e528SElen Song 	}
1405a930e528SElen Song }
1406a930e528SElen Song 
140764e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1408ab4382d2SGreg Kroah-Hartman {
1409ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
141005c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1411ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1412ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1413ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1414ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1415ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1416ab4382d2SGreg Kroah-Hartman 
1417ab4382d2SGreg Kroah-Hartman 	do {
1418ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
1419ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
1420ab4382d2SGreg Kroah-Hartman 
1421ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
1422ab4382d2SGreg Kroah-Hartman 		head = UART_GET_RPR(port) - pdc->dma_addr;
1423ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1424ab4382d2SGreg Kroah-Hartman 
1425ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1426ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1427ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1428ab4382d2SGreg Kroah-Hartman 		 * find out.
1429ab4382d2SGreg Kroah-Hartman 		 *
1430ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1431ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1432ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1433ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1434ab4382d2SGreg Kroah-Hartman 		 */
1435ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1436ab4382d2SGreg Kroah-Hartman 
1437ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1438ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1439ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1440ab4382d2SGreg Kroah-Hartman 
1441ab4382d2SGreg Kroah-Hartman 			/*
1442ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1443ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1444ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1445ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1446ab4382d2SGreg Kroah-Hartman 			 */
1447ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1448ab4382d2SGreg Kroah-Hartman 
144905c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
145005c7cd39SJiri Slaby 						count);
1451ab4382d2SGreg Kroah-Hartman 
1452ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1453ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1454ab4382d2SGreg Kroah-Hartman 
1455ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1456ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1457ab4382d2SGreg Kroah-Hartman 		}
1458ab4382d2SGreg Kroah-Hartman 
1459ab4382d2SGreg Kroah-Hartman 		/*
1460ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1461ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1462ab4382d2SGreg Kroah-Hartman 		 */
1463ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1464ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
1465ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNPR(port, pdc->dma_addr);
1466ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNCR(port, pdc->dma_size);
1467ab4382d2SGreg Kroah-Hartman 
1468ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1469ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1470ab4382d2SGreg Kroah-Hartman 		}
1471ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1472ab4382d2SGreg Kroah-Hartman 
1473ab4382d2SGreg Kroah-Hartman 	/*
1474ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1475ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1476ab4382d2SGreg Kroah-Hartman 	 */
1477ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14782e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1479ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1480ab4382d2SGreg Kroah-Hartman 
1481ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1482ab4382d2SGreg Kroah-Hartman }
1483ab4382d2SGreg Kroah-Hartman 
1484a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1485a930e528SElen Song {
1486a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1487a930e528SElen Song 	int i;
1488a930e528SElen Song 
1489a930e528SElen Song 	for (i = 0; i < 2; i++) {
1490a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1491a930e528SElen Song 
1492a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1493a930e528SElen Song 		if (pdc->buf == NULL) {
1494a930e528SElen Song 			if (i != 0) {
1495a930e528SElen Song 				dma_unmap_single(port->dev,
1496a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1497a930e528SElen Song 					PDC_BUFFER_SIZE,
1498a930e528SElen Song 					DMA_FROM_DEVICE);
1499a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1500a930e528SElen Song 			}
1501a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1502a930e528SElen Song 			return -ENOMEM;
1503a930e528SElen Song 		}
1504a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1505a930e528SElen Song 						pdc->buf,
1506a930e528SElen Song 						PDC_BUFFER_SIZE,
1507a930e528SElen Song 						DMA_FROM_DEVICE);
1508a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1509a930e528SElen Song 		pdc->ofs = 0;
1510a930e528SElen Song 	}
1511a930e528SElen Song 
1512a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1513a930e528SElen Song 
1514a930e528SElen Song 	UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1515a930e528SElen Song 	UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1516a930e528SElen Song 
1517a930e528SElen Song 	UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1518a930e528SElen Song 	UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1519a930e528SElen Song 
1520a930e528SElen Song 	return 0;
1521a930e528SElen Song }
1522a930e528SElen Song 
1523ab4382d2SGreg Kroah-Hartman /*
1524ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1525ab4382d2SGreg Kroah-Hartman  */
1526ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1527ab4382d2SGreg Kroah-Hartman {
1528ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1529ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1530*d033e82dSLeilei Zhao 	unsigned int status = atmel_port->irq_status;
1531*d033e82dSLeilei Zhao 	unsigned int status_change = atmel_port->status_change;
1532ab4382d2SGreg Kroah-Hartman 
1533ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1534ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1535ab4382d2SGreg Kroah-Hartman 
1536a930e528SElen Song 	atmel_port->schedule_tx(port);
1537ab4382d2SGreg Kroah-Hartman 
1538ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1539ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1540ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1541ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1542ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1543ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1544ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1545ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1546ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1547ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1548ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1549ab4382d2SGreg Kroah-Hartman 
1550ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1551ab4382d2SGreg Kroah-Hartman 
1552*d033e82dSLeilei Zhao 		atmel_port->status_change = 0;
1553ab4382d2SGreg Kroah-Hartman 	}
1554ab4382d2SGreg Kroah-Hartman 
1555a930e528SElen Song 	atmel_port->schedule_rx(port);
1556ab4382d2SGreg Kroah-Hartman 
1557ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1558ab4382d2SGreg Kroah-Hartman }
1559ab4382d2SGreg Kroah-Hartman 
15604a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
156133d64c4fSElen Song 				struct platform_device *pdev)
156233d64c4fSElen Song {
156333d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1564574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
156533d64c4fSElen Song 
156633d64c4fSElen Song 	if (np) {
156733d64c4fSElen Song 		/* DMA/PDC usage specification */
156833d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
156933d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
157033d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
157133d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
157233d64c4fSElen Song 			} else {
157333d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
157433d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
157533d64c4fSElen Song 			}
157633d64c4fSElen Song 		} else {
157733d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
157833d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
157933d64c4fSElen Song 		}
158033d64c4fSElen Song 
158133d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
158233d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
158333d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
158433d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
158533d64c4fSElen Song 			} else {
158633d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
158733d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
158833d64c4fSElen Song 			}
158933d64c4fSElen Song 		} else {
159033d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
159133d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
159233d64c4fSElen Song 		}
159333d64c4fSElen Song 
159433d64c4fSElen Song 	} else {
159533d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
159633d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
159733d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
159833d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
159933d64c4fSElen Song 	}
160033d64c4fSElen Song 
160133d64c4fSElen Song }
160233d64c4fSElen Song 
160313bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
160433d64c4fSElen Song 				struct platform_device *pdev)
160533d64c4fSElen Song {
160633d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1607574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
160833d64c4fSElen Song 
160933d64c4fSElen Song 	if (np) {
161033d64c4fSElen Song 		u32 rs485_delay[2];
161133d64c4fSElen Song 		/* rs485 properties */
161233d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
161333d64c4fSElen Song 					rs485_delay, 2) == 0) {
161413bd3e6fSRicardo Ribalda Delgado 			struct serial_rs485 *rs485conf = &port->rs485;
161533d64c4fSElen Song 
161633d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
161733d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
161833d64c4fSElen Song 			rs485conf->flags = 0;
161933d64c4fSElen Song 
162033d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
162133d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
162233d64c4fSElen Song 
162333d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
162433d64c4fSElen Song 								NULL))
162533d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
162633d64c4fSElen Song 		}
162733d64c4fSElen Song 	} else {
162813bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
162933d64c4fSElen Song 	}
163033d64c4fSElen Song 
163133d64c4fSElen Song }
163233d64c4fSElen Song 
1633a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1634a930e528SElen Song {
1635a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1636a930e528SElen Song 
163734df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
163834df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
163934df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
164034df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
164134df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1642a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1643a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1644a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1645a930e528SElen Song 	} else {
1646a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1647a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1648a930e528SElen Song 		atmel_port->release_rx = NULL;
1649a930e528SElen Song 	}
1650a930e528SElen Song 
165108f738beSElen Song 	if (atmel_use_dma_tx(port)) {
165208f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
165308f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
165408f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
165508f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1656a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1657a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1658a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1659a930e528SElen Song 	} else {
1660a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1661a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1662a930e528SElen Song 		atmel_port->release_tx = NULL;
1663a930e528SElen Song 	}
1664a930e528SElen Song }
1665a930e528SElen Song 
1666ab4382d2SGreg Kroah-Hartman /*
1667055560b0SElen Song  * Get ip name usart or uart
1668055560b0SElen Song  */
1669892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1670055560b0SElen Song {
1671055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1672055560b0SElen Song 	int name = UART_GET_IP_NAME(port);
1673731d9caeSNicolas Ferre 	u32 version;
1674055560b0SElen Song 	int usart, uart;
1675055560b0SElen Song 	/* usart and uart ascii */
1676055560b0SElen Song 	usart = 0x55534152;
1677055560b0SElen Song 	uart = 0x44424755;
1678055560b0SElen Song 
1679055560b0SElen Song 	atmel_port->is_usart = false;
1680055560b0SElen Song 
1681055560b0SElen Song 	if (name == usart) {
1682055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1683055560b0SElen Song 		atmel_port->is_usart = true;
1684055560b0SElen Song 	} else if (name == uart) {
1685055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1686055560b0SElen Song 		atmel_port->is_usart = false;
1687055560b0SElen Song 	} else {
1688731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
1689731d9caeSNicolas Ferre 		version = UART_GET_IP_VERSION(port);
1690731d9caeSNicolas Ferre 		switch (version) {
1691731d9caeSNicolas Ferre 		case 0x302:
1692731d9caeSNicolas Ferre 		case 0x10213:
1693731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1694731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1695731d9caeSNicolas Ferre 			break;
1696731d9caeSNicolas Ferre 		case 0x203:
1697731d9caeSNicolas Ferre 		case 0x10202:
1698731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1699731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1700731d9caeSNicolas Ferre 			break;
1701731d9caeSNicolas Ferre 		default:
1702731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1703731d9caeSNicolas Ferre 		}
1704055560b0SElen Song 	}
1705055560b0SElen Song }
1706055560b0SElen Song 
1707ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port)
1708ab5e4e41SRichard Genoud {
1709ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1710ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1711ab5e4e41SRichard Genoud 
1712ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++)
1713ab5e4e41SRichard Genoud 		if (atmel_port->gpio_irq[i] >= 0)
1714ab5e4e41SRichard Genoud 			free_irq(atmel_port->gpio_irq[i], port);
1715ab5e4e41SRichard Genoud }
1716ab5e4e41SRichard Genoud 
1717ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port)
1718ab5e4e41SRichard Genoud {
1719ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1720ab5e4e41SRichard Genoud 	int *irq = atmel_port->gpio_irq;
1721ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1722ab5e4e41SRichard Genoud 	int err = 0;
1723ab5e4e41SRichard Genoud 
1724ab5e4e41SRichard Genoud 	for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1725ab5e4e41SRichard Genoud 		if (irq[i] < 0)
1726ab5e4e41SRichard Genoud 			continue;
1727ab5e4e41SRichard Genoud 
1728ab5e4e41SRichard Genoud 		irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1729ab5e4e41SRichard Genoud 		err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1730ab5e4e41SRichard Genoud 				  "atmel_serial", port);
1731ab5e4e41SRichard Genoud 		if (err)
1732ab5e4e41SRichard Genoud 			dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1733ab5e4e41SRichard Genoud 				irq[i]);
1734ab5e4e41SRichard Genoud 	}
1735ab5e4e41SRichard Genoud 
1736ab5e4e41SRichard Genoud 	/*
1737ab5e4e41SRichard Genoud 	 * If something went wrong, rollback.
1738ab5e4e41SRichard Genoud 	 */
1739ab5e4e41SRichard Genoud 	while (err && (--i >= 0))
1740ab5e4e41SRichard Genoud 		if (irq[i] >= 0)
1741ab5e4e41SRichard Genoud 			free_irq(irq[i], port);
1742ab5e4e41SRichard Genoud 
1743ab5e4e41SRichard Genoud 	return err;
1744ab5e4e41SRichard Genoud }
1745ab5e4e41SRichard Genoud 
1746055560b0SElen Song /*
1747ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1748ab4382d2SGreg Kroah-Hartman  */
1749ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1750ab4382d2SGreg Kroah-Hartman {
175133d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1752ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1753ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1754ab4382d2SGreg Kroah-Hartman 	int retval;
1755ab4382d2SGreg Kroah-Hartman 
1756ab4382d2SGreg Kroah-Hartman 	/*
1757ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1758ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1759ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1760ab4382d2SGreg Kroah-Hartman 	 */
1761ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1762ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1763ab4382d2SGreg Kroah-Hartman 
1764ab4382d2SGreg Kroah-Hartman 	/*
1765ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1766ab4382d2SGreg Kroah-Hartman 	 */
17672c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
17682c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1769ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1770ab4382d2SGreg Kroah-Hartman 	if (retval) {
1771ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1772ab4382d2SGreg Kroah-Hartman 		return retval;
1773ab4382d2SGreg Kroah-Hartman 	}
1774ab4382d2SGreg Kroah-Hartman 
1775ab4382d2SGreg Kroah-Hartman 	/*
1776ab5e4e41SRichard Genoud 	 * Get the GPIO lines IRQ
1777ab5e4e41SRichard Genoud 	 */
1778ab5e4e41SRichard Genoud 	retval = atmel_request_gpio_irq(port);
1779ab5e4e41SRichard Genoud 	if (retval)
1780ab5e4e41SRichard Genoud 		goto free_irq;
1781ab5e4e41SRichard Genoud 
17821e125786SLeilei Zhao 	tasklet_enable(&atmel_port->tasklet);
17831e125786SLeilei Zhao 
1784ab5e4e41SRichard Genoud 	/*
1785ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1786ab4382d2SGreg Kroah-Hartman 	 */
178733d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
17884d9628a1SLeilei Zhao 	atmel_set_ops(port);
178933d64c4fSElen Song 
1790a930e528SElen Song 	if (atmel_port->prepare_rx) {
1791a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1792a930e528SElen Song 		if (retval < 0)
1793a930e528SElen Song 			atmel_set_ops(port);
1794ab4382d2SGreg Kroah-Hartman 	}
1795ab4382d2SGreg Kroah-Hartman 
1796a930e528SElen Song 	if (atmel_port->prepare_tx) {
1797a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1798a930e528SElen Song 		if (retval < 0)
1799a930e528SElen Song 			atmel_set_ops(port);
1800ab4382d2SGreg Kroah-Hartman 	}
1801ab4382d2SGreg Kroah-Hartman 
1802ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1803e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1804ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1805ab4382d2SGreg Kroah-Hartman 
1806ab4382d2SGreg Kroah-Hartman 	/*
1807ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1808ab4382d2SGreg Kroah-Hartman 	 */
1809ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1810ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
1811ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1812ab4382d2SGreg Kroah-Hartman 
18132e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
18142e68c22fSElen Song 			atmel_uart_timer_callback,
18152e68c22fSElen Song 			(unsigned long)port);
18168bc661bfSMarek Roszko 
18178bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18188bc661bfSMarek Roszko 		/* set UART timeout */
18198bc661bfSMarek Roszko 		if (!atmel_port->is_usart) {
18202e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18212e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18222e68c22fSElen Song 		/* set USART timeout */
18232e68c22fSElen Song 		} else {
1824ab4382d2SGreg Kroah-Hartman 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1825ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_STTTO);
1826ab4382d2SGreg Kroah-Hartman 
1827ab4382d2SGreg Kroah-Hartman 			UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18282e68c22fSElen Song 		}
1829ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
1830ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
183134df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18322e68c22fSElen Song 		/* set UART timeout */
18332e68c22fSElen Song 		if (!atmel_port->is_usart) {
18342e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18352e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18362e68c22fSElen Song 		/* set USART timeout */
18372e68c22fSElen Song 		} else {
183834df42f5SElen Song 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
183934df42f5SElen Song 			UART_PUT_CR(port, ATMEL_US_STTTO);
184034df42f5SElen Song 
184134df42f5SElen Song 			UART_PUT_IER(port, ATMEL_US_TIMEOUT);
18422e68c22fSElen Song 		}
1843ab4382d2SGreg Kroah-Hartman 	} else {
1844ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
1845ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
1846ab4382d2SGreg Kroah-Hartman 	}
1847ab4382d2SGreg Kroah-Hartman 
1848ab4382d2SGreg Kroah-Hartman 	return 0;
1849ab5e4e41SRichard Genoud 
1850ab5e4e41SRichard Genoud free_irq:
1851ab5e4e41SRichard Genoud 	free_irq(port->irq, port);
1852ab5e4e41SRichard Genoud 
1853ab5e4e41SRichard Genoud 	return retval;
1854ab4382d2SGreg Kroah-Hartman }
1855ab4382d2SGreg Kroah-Hartman 
1856ab4382d2SGreg Kroah-Hartman /*
1857479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1858479e9b94SPeter Hurley  * buffer is reset.
1859479e9b94SPeter Hurley  */
1860479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1861479e9b94SPeter Hurley {
1862479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1863479e9b94SPeter Hurley 
1864479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
1865479e9b94SPeter Hurley 		UART_PUT_TCR(port, 0);
1866479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1867479e9b94SPeter Hurley 	}
1868479e9b94SPeter Hurley }
1869479e9b94SPeter Hurley 
1870479e9b94SPeter Hurley /*
1871ab4382d2SGreg Kroah-Hartman  * Disable the port
1872ab4382d2SGreg Kroah-Hartman  */
1873ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1874ab4382d2SGreg Kroah-Hartman {
1875ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
18760cc7c6c7SMarek Roszko 
1877ab4382d2SGreg Kroah-Hartman 	/*
18788bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
18798bc661bfSMarek Roszko 	 * cleanup
18808bc661bfSMarek Roszko 	 */
18818bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
18828bc661bfSMarek Roszko 
18838bc661bfSMarek Roszko 	/*
18840cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
18850cc7c6c7SMarek Roszko 	 * we destroy the buffers
18860cc7c6c7SMarek Roszko 	 */
18871e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
18880cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
18890cc7c6c7SMarek Roszko 
18900cc7c6c7SMarek Roszko 	/*
18910cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
18920cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1893ab4382d2SGreg Kroah-Hartman 	 */
1894ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1895ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1896ab4382d2SGreg Kroah-Hartman 
18970cc7c6c7SMarek Roszko 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
18980cc7c6c7SMarek Roszko 	UART_PUT_IDR(port, -1);
18990cc7c6c7SMarek Roszko 
19000cc7c6c7SMarek Roszko 
1901ab4382d2SGreg Kroah-Hartman 	/*
1902ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1903ab4382d2SGreg Kroah-Hartman 	 */
1904a930e528SElen Song 	if (atmel_port->release_rx)
1905a930e528SElen Song 		atmel_port->release_rx(port);
1906a930e528SElen Song 	if (atmel_port->release_tx)
1907a930e528SElen Song 		atmel_port->release_tx(port);
1908ab4382d2SGreg Kroah-Hartman 
1909ab4382d2SGreg Kroah-Hartman 	/*
1910bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1911bb7e73c5SMark Deneen 	 */
1912bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1913bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1914bb7e73c5SMark Deneen 
1915bb7e73c5SMark Deneen 	/*
1916ab5e4e41SRichard Genoud 	 * Free the interrupts
1917ab4382d2SGreg Kroah-Hartman 	 */
1918ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1919ab5e4e41SRichard Genoud 	atmel_free_gpio_irq(port);
1920ab5e4e41SRichard Genoud 
1921ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1922ab4382d2SGreg Kroah-Hartman 
1923479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1924ab4382d2SGreg Kroah-Hartman }
1925ab4382d2SGreg Kroah-Hartman 
1926ab4382d2SGreg Kroah-Hartman /*
1927ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1928ab4382d2SGreg Kroah-Hartman  */
1929ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1930ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1931ab4382d2SGreg Kroah-Hartman {
1932ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1933ab4382d2SGreg Kroah-Hartman 
1934ab4382d2SGreg Kroah-Hartman 	switch (state) {
1935ab4382d2SGreg Kroah-Hartman 	case 0:
1936ab4382d2SGreg Kroah-Hartman 		/*
1937ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1938ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1939ab4382d2SGreg Kroah-Hartman 		 */
194091f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1941ab4382d2SGreg Kroah-Hartman 
1942ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1943ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->backup_imr);
1944ab4382d2SGreg Kroah-Hartman 		break;
1945ab4382d2SGreg Kroah-Hartman 	case 3:
1946ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1947ab4382d2SGreg Kroah-Hartman 		atmel_port->backup_imr = UART_GET_IMR(port);
1948ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, -1);
1949ab4382d2SGreg Kroah-Hartman 
1950ab4382d2SGreg Kroah-Hartman 		/*
1951ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1952ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1953ab4382d2SGreg Kroah-Hartman 		 */
195491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1955ab4382d2SGreg Kroah-Hartman 		break;
1956ab4382d2SGreg Kroah-Hartman 	default:
1957ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1958ab4382d2SGreg Kroah-Hartman 	}
1959ab4382d2SGreg Kroah-Hartman }
1960ab4382d2SGreg Kroah-Hartman 
1961ab4382d2SGreg Kroah-Hartman /*
1962ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1963ab4382d2SGreg Kroah-Hartman  */
1964ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1965ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1966ab4382d2SGreg Kroah-Hartman {
1967ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
19681cf6e8fcSCyrille Pitchen 	unsigned int old_mode, mode, imr, quot, baud;
1969ab4382d2SGreg Kroah-Hartman 
19701cf6e8fcSCyrille Pitchen 	/* save the current mode register */
19711cf6e8fcSCyrille Pitchen 	mode = old_mode = UART_GET_MR(port);
19721cf6e8fcSCyrille Pitchen 
19731cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
19741cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
19751cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
1976ab4382d2SGreg Kroah-Hartman 
1977ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1978ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1979ab4382d2SGreg Kroah-Hartman 
1980ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1981ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1982ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1983ab4382d2SGreg Kroah-Hartman 	}
1984ab4382d2SGreg Kroah-Hartman 
1985ab4382d2SGreg Kroah-Hartman 	/* byte size */
1986ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1987ab4382d2SGreg Kroah-Hartman 	case CS5:
1988ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1989ab4382d2SGreg Kroah-Hartman 		break;
1990ab4382d2SGreg Kroah-Hartman 	case CS6:
1991ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1992ab4382d2SGreg Kroah-Hartman 		break;
1993ab4382d2SGreg Kroah-Hartman 	case CS7:
1994ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1995ab4382d2SGreg Kroah-Hartman 		break;
1996ab4382d2SGreg Kroah-Hartman 	default:
1997ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1998ab4382d2SGreg Kroah-Hartman 		break;
1999ab4382d2SGreg Kroah-Hartman 	}
2000ab4382d2SGreg Kroah-Hartman 
2001ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2002ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2003ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2004ab4382d2SGreg Kroah-Hartman 
2005ab4382d2SGreg Kroah-Hartman 	/* parity */
2006ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2007ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2008ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2009ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2010ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2011ab4382d2SGreg Kroah-Hartman 			else
2012ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2013ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2014ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2015ab4382d2SGreg Kroah-Hartman 		else
2016ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2017ab4382d2SGreg Kroah-Hartman 	} else
2018ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2019ab4382d2SGreg Kroah-Hartman 
2020ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2021ab4382d2SGreg Kroah-Hartman 
2022ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2023ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2024ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2025ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2026ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2027ab4382d2SGreg Kroah-Hartman 
202864e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2029ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
2030ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, port->read_status_mask);
2031ab4382d2SGreg Kroah-Hartman 
2032ab4382d2SGreg Kroah-Hartman 	/*
2033ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2034ab4382d2SGreg Kroah-Hartman 	 */
2035ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2036ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2037ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2038ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2039ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2040ab4382d2SGreg Kroah-Hartman 		/*
2041ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2042ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2043ab4382d2SGreg Kroah-Hartman 		 */
2044ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2045ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2046ab4382d2SGreg Kroah-Hartman 	}
2047ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2048ab4382d2SGreg Kroah-Hartman 
2049ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2050ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2051ab4382d2SGreg Kroah-Hartman 
2052ab4382d2SGreg Kroah-Hartman 	/*
2053ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2054ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2055ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2056ab4382d2SGreg Kroah-Hartman 	 */
2057ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
2058ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
2059ab4382d2SGreg Kroah-Hartman 
2060ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
2061ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2062ab4382d2SGreg Kroah-Hartman 
20631cf6e8fcSCyrille Pitchen 	/* mode */
206413bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
206513bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.delay_rts_after_send) > 0)
206613bd3e6fSRicardo Ribalda Delgado 			UART_PUT_TTGR(port, port->rs485.delay_rts_after_send);
2067ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
20681cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
20691cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
20701cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_HWHS;
20711cf6e8fcSCyrille Pitchen 	} else {
20721cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
20731cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2074ab4382d2SGreg Kroah-Hartman 	}
2075ab4382d2SGreg Kroah-Hartman 
20761cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
2077ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
2078ab4382d2SGreg Kroah-Hartman 
20791cf6e8fcSCyrille Pitchen 	/*
20801cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
20811cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
20821cf6e8fcSCyrille Pitchen 	 */
20831cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
20841cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
20851cf6e8fcSCyrille Pitchen 
20861cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
20871cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
20881cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
20891cf6e8fcSCyrille Pitchen 		} else {
20901cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
20911cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
20921cf6e8fcSCyrille Pitchen 		}
20931cf6e8fcSCyrille Pitchen 
20941cf6e8fcSCyrille Pitchen 		UART_PUT_CR(port, rts_state);
20951cf6e8fcSCyrille Pitchen 	}
20961cf6e8fcSCyrille Pitchen 
2097ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
2098ab4382d2SGreg Kroah-Hartman 	UART_PUT_BRGR(port, quot);
2099ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2100ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2101ab4382d2SGreg Kroah-Hartman 
2102ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
2103ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
2104ab4382d2SGreg Kroah-Hartman 
2105ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2106ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
210735b675b9SRichard Genoud 		atmel_enable_ms(port);
210835b675b9SRichard Genoud 	else
210935b675b9SRichard Genoud 		atmel_disable_ms(port);
2110ab4382d2SGreg Kroah-Hartman 
2111ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2112ab4382d2SGreg Kroah-Hartman }
2113ab4382d2SGreg Kroah-Hartman 
2114732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
211542bd7a4fSViktar Palstsiuk {
2116732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
211742bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2118d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
211942bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2120d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
212142bd7a4fSViktar Palstsiuk 	} else {
212242bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2123cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2124cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2125cab68f89SPeter Hurley 			atmel_disable_ms(port);
2126cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2127cab68f89SPeter Hurley 		}
212842bd7a4fSViktar Palstsiuk 	}
212942bd7a4fSViktar Palstsiuk }
213042bd7a4fSViktar Palstsiuk 
2131ab4382d2SGreg Kroah-Hartman /*
2132ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2133ab4382d2SGreg Kroah-Hartman  */
2134ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2135ab4382d2SGreg Kroah-Hartman {
2136ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2137ab4382d2SGreg Kroah-Hartman }
2138ab4382d2SGreg Kroah-Hartman 
2139ab4382d2SGreg Kroah-Hartman /*
2140ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2141ab4382d2SGreg Kroah-Hartman  */
2142ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2143ab4382d2SGreg Kroah-Hartman {
2144ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2145ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2146ab4382d2SGreg Kroah-Hartman 
2147ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2148ab4382d2SGreg Kroah-Hartman 
2149ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2150ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2151ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2152ab4382d2SGreg Kroah-Hartman 	}
2153ab4382d2SGreg Kroah-Hartman }
2154ab4382d2SGreg Kroah-Hartman 
2155ab4382d2SGreg Kroah-Hartman /*
2156ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2157ab4382d2SGreg Kroah-Hartman  */
2158ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2159ab4382d2SGreg Kroah-Hartman {
2160ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2161ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2162ab4382d2SGreg Kroah-Hartman 
2163ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2164ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2165ab4382d2SGreg Kroah-Hartman 
2166ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2167ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2168ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2169ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2170ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2171ab4382d2SGreg Kroah-Hartman 		}
2172ab4382d2SGreg Kroah-Hartman 	}
2173ab4382d2SGreg Kroah-Hartman 
2174ab4382d2SGreg Kroah-Hartman 	return 0;
2175ab4382d2SGreg Kroah-Hartman }
2176ab4382d2SGreg Kroah-Hartman 
2177ab4382d2SGreg Kroah-Hartman /*
2178ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2179ab4382d2SGreg Kroah-Hartman  */
2180ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2181ab4382d2SGreg Kroah-Hartman {
2182ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2183ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2184ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2185ab4382d2SGreg Kroah-Hartman 	}
2186ab4382d2SGreg Kroah-Hartman }
2187ab4382d2SGreg Kroah-Hartman 
2188ab4382d2SGreg Kroah-Hartman /*
2189ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2190ab4382d2SGreg Kroah-Hartman  */
2191ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2192ab4382d2SGreg Kroah-Hartman {
2193ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2194ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2195ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2196ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2197ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2198ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2199ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2200ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2201ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2202ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
2203ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2204ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2205ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2206ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2207ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2208ab4382d2SGreg Kroah-Hartman 	return ret;
2209ab4382d2SGreg Kroah-Hartman }
2210ab4382d2SGreg Kroah-Hartman 
2211ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2212ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2213ab4382d2SGreg Kroah-Hartman {
2214ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
2215ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2216ab4382d2SGreg Kroah-Hartman 
2217ab4382d2SGreg Kroah-Hartman 	return UART_GET_CHAR(port);
2218ab4382d2SGreg Kroah-Hartman }
2219ab4382d2SGreg Kroah-Hartman 
2220ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2221ab4382d2SGreg Kroah-Hartman {
2222ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2223ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2224ab4382d2SGreg Kroah-Hartman 
2225ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
2226ab4382d2SGreg Kroah-Hartman }
2227ab4382d2SGreg Kroah-Hartman #endif
2228ab4382d2SGreg Kroah-Hartman 
2229ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2230ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2231ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2232ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2233ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2234ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2235ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2236ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2237ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2238ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2239ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2240ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2241ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
224242bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2243ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2244ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2245ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2246ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2247ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2248ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2249ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2250ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2251ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2252ab4382d2SGreg Kroah-Hartman #endif
2253ab4382d2SGreg Kroah-Hartman };
2254ab4382d2SGreg Kroah-Hartman 
2255ab4382d2SGreg Kroah-Hartman /*
2256ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2257ab4382d2SGreg Kroah-Hartman  */
225891f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2259ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2260ab4382d2SGreg Kroah-Hartman {
226191f8c2d8SBoris BREZILLON 	int ret;
2262ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2263574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2264ab4382d2SGreg Kroah-Hartman 
22654a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2266a930e528SElen Song 	atmel_set_ops(port);
2267a930e528SElen Song 
226813bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
226933d64c4fSElen Song 
2270ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2271ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2272ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2273ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2274ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2275ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2276ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
227713bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2278ab4382d2SGreg Kroah-Hartman 
2279ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2280ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
22811e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
2282ab4382d2SGreg Kroah-Hartman 
2283ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2284ab4382d2SGreg Kroah-Hartman 
22855fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2286ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
22871acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2288588edbf3SNicolas Ferre 	} else {
2289ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2290ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2291ab4382d2SGreg Kroah-Hartman 	}
2292ab4382d2SGreg Kroah-Hartman 
2293ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2294ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2295ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
229691f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
229791f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
229891f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
229991f8c2d8SBoris BREZILLON 			return ret;
230091f8c2d8SBoris BREZILLON 		}
230191f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
230291f8c2d8SBoris BREZILLON 		if (ret) {
230391f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
230491f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
230591f8c2d8SBoris BREZILLON 			return ret;
230691f8c2d8SBoris BREZILLON 		}
2307ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
230891f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2309ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2310ab4382d2SGreg Kroah-Hartman 	}
2311ab4382d2SGreg Kroah-Hartman 
2312ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
231313bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2314ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
231564e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2316ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2317ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2318ab4382d2SGreg Kroah-Hartman 	} else {
2319ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2320ab4382d2SGreg Kroah-Hartman 	}
232191f8c2d8SBoris BREZILLON 
232291f8c2d8SBoris BREZILLON 	return 0;
2323ab4382d2SGreg Kroah-Hartman }
2324ab4382d2SGreg Kroah-Hartman 
232569f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
232669f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2327ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2328ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2329ab4382d2SGreg Kroah-Hartman {
2330ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2331ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2332ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
2333ab4382d2SGreg Kroah-Hartman }
2334ab4382d2SGreg Kroah-Hartman 
2335ab4382d2SGreg Kroah-Hartman /*
2336ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2337ab4382d2SGreg Kroah-Hartman  */
2338ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2339ab4382d2SGreg Kroah-Hartman {
2340ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2341ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2342ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2343ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2344ab4382d2SGreg Kroah-Hartman 
2345ab4382d2SGreg Kroah-Hartman 	/*
2346ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2347ab4382d2SGreg Kroah-Hartman 	 */
2348ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
2349ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2350ab4382d2SGreg Kroah-Hartman 
2351ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
2352ab4382d2SGreg Kroah-Hartman 	pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2353ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2354ab4382d2SGreg Kroah-Hartman 
2355ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2356ab4382d2SGreg Kroah-Hartman 
2357ab4382d2SGreg Kroah-Hartman 	/*
2358ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2359ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2360ab4382d2SGreg Kroah-Hartman 	 */
2361ab4382d2SGreg Kroah-Hartman 	do {
2362ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
2363ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2364ab4382d2SGreg Kroah-Hartman 
2365ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2366ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
2367ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2368ab4382d2SGreg Kroah-Hartman 
2369ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
2370ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
2371ab4382d2SGreg Kroah-Hartman }
2372ab4382d2SGreg Kroah-Hartman 
2373ab4382d2SGreg Kroah-Hartman /*
2374ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2375ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2376ab4382d2SGreg Kroah-Hartman  */
2377ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2378ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2379ab4382d2SGreg Kroah-Hartman {
2380ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2381ab4382d2SGreg Kroah-Hartman 
2382ab4382d2SGreg Kroah-Hartman 	/*
2383ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2384ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2385ab4382d2SGreg Kroah-Hartman 	 */
2386ab4382d2SGreg Kroah-Hartman 	quot = UART_GET_BRGR(port) & ATMEL_US_CD;
2387ab4382d2SGreg Kroah-Hartman 	if (!quot)
2388ab4382d2SGreg Kroah-Hartman 		return;
2389ab4382d2SGreg Kroah-Hartman 
2390ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2391ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2392ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2393ab4382d2SGreg Kroah-Hartman 	else
2394ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2395ab4382d2SGreg Kroah-Hartman 
2396ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_PAR;
2397ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2398ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2399ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2400ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2401ab4382d2SGreg Kroah-Hartman 
2402ab4382d2SGreg Kroah-Hartman 	/*
2403ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2404ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2405ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2406ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2407ab4382d2SGreg Kroah-Hartman 	 */
2408ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2409ab4382d2SGreg Kroah-Hartman }
2410ab4382d2SGreg Kroah-Hartman 
2411ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2412ab4382d2SGreg Kroah-Hartman {
241391f8c2d8SBoris BREZILLON 	int ret;
2414ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2415ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2416ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2417ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2418ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2419ab4382d2SGreg Kroah-Hartman 
2420ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2421ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2422ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2423ab4382d2SGreg Kroah-Hartman 	}
2424ab4382d2SGreg Kroah-Hartman 
242591f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
242691f8c2d8SBoris BREZILLON 	if (ret)
242791f8c2d8SBoris BREZILLON 		return ret;
2428ab4382d2SGreg Kroah-Hartman 
2429ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
2430ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2431ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2432ab4382d2SGreg Kroah-Hartman 
2433ab4382d2SGreg Kroah-Hartman 	if (options)
2434ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2435ab4382d2SGreg Kroah-Hartman 	else
2436ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2437ab4382d2SGreg Kroah-Hartman 
2438ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2439ab4382d2SGreg Kroah-Hartman }
2440ab4382d2SGreg Kroah-Hartman 
2441ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2444ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2445ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2446ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2447ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2448ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2449ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2450ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2451ab4382d2SGreg Kroah-Hartman };
2452ab4382d2SGreg Kroah-Hartman 
2453ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2454ab4382d2SGreg Kroah-Hartman 
2455ab4382d2SGreg Kroah-Hartman /*
2456ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2457ab4382d2SGreg Kroah-Hartman  */
2458ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2459ab4382d2SGreg Kroah-Hartman {
246091f8c2d8SBoris BREZILLON 	int ret;
2461ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
24620d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2463574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2464efb8d21bSLinus Torvalds 		int id = pdata->num;
24654cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
24660d0a3cc1SVoss, Nikolaus 
24674cbf9f48SNicolas Ferre 		port->backup_imr = 0;
24684cbf9f48SNicolas Ferre 		port->uart.line = id;
24694cbf9f48SNicolas Ferre 
24704cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
247191f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
247291f8c2d8SBoris BREZILLON 		if (ret)
247391f8c2d8SBoris BREZILLON 			return ret;
2474ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2475ab4382d2SGreg Kroah-Hartman 	}
2476ab4382d2SGreg Kroah-Hartman 
2477ab4382d2SGreg Kroah-Hartman 	return 0;
2478ab4382d2SGreg Kroah-Hartman }
2479ab4382d2SGreg Kroah-Hartman 
2480ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2481ab4382d2SGreg Kroah-Hartman 
2482ab4382d2SGreg Kroah-Hartman /*
2483ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2484ab4382d2SGreg Kroah-Hartman  */
2485ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2486ab4382d2SGreg Kroah-Hartman {
2487ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2488ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2489ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2490ab4382d2SGreg Kroah-Hartman 
2491ab4382d2SGreg Kroah-Hartman 	return 0;
2492ab4382d2SGreg Kroah-Hartman }
2493ab4382d2SGreg Kroah-Hartman 
2494ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2495ab4382d2SGreg Kroah-Hartman 
2496ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2497ab4382d2SGreg Kroah-Hartman {
2498ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2499ab4382d2SGreg Kroah-Hartman }
2500ab4382d2SGreg Kroah-Hartman 
2501ab4382d2SGreg Kroah-Hartman #else
2502ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2503ab4382d2SGreg Kroah-Hartman 
2504ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2505ab4382d2SGreg Kroah-Hartman {
2506ab4382d2SGreg Kroah-Hartman 	return false;
2507ab4382d2SGreg Kroah-Hartman }
2508ab4382d2SGreg Kroah-Hartman #endif
2509ab4382d2SGreg Kroah-Hartman 
2510ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2511ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2512ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2513ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2514ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2515ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2516ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2517ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2518ab4382d2SGreg Kroah-Hartman };
2519ab4382d2SGreg Kroah-Hartman 
2520ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2521ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2522ab4382d2SGreg Kroah-Hartman {
2523ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2524ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2525ab4382d2SGreg Kroah-Hartman #else
2526ab4382d2SGreg Kroah-Hartman 	return false;
2527ab4382d2SGreg Kroah-Hartman #endif
2528ab4382d2SGreg Kroah-Hartman }
2529ab4382d2SGreg Kroah-Hartman 
2530ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2531ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2532ab4382d2SGreg Kroah-Hartman {
2533ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2534ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2535ab4382d2SGreg Kroah-Hartman 
2536ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2537ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
2538ab4382d2SGreg Kroah-Hartman 		while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2539ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2540ab4382d2SGreg Kroah-Hartman 	}
2541ab4382d2SGreg Kroah-Hartman 
2542ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2543ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
25442c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
25452c7af5baSBoris BREZILLON 		unsigned long flags;
25462c7af5baSBoris BREZILLON 
25472c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25482c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
25492c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2550ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
25512c7af5baSBoris BREZILLON 	}
2552ab4382d2SGreg Kroah-Hartman 
2553ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2554ab4382d2SGreg Kroah-Hartman 
2555ab4382d2SGreg Kroah-Hartman 	return 0;
2556ab4382d2SGreg Kroah-Hartman }
2557ab4382d2SGreg Kroah-Hartman 
2558ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2559ab4382d2SGreg Kroah-Hartman {
2560ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2561ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
25622c7af5baSBoris BREZILLON 	unsigned long flags;
25632c7af5baSBoris BREZILLON 
25642c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25652c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
25662c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
25672c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
25682c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
25692c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
25702c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
25712c7af5baSBoris BREZILLON 	}
25722c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
25732c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2574ab4382d2SGreg Kroah-Hartman 
2575ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2576ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2577ab4382d2SGreg Kroah-Hartman 
2578ab4382d2SGreg Kroah-Hartman 	return 0;
2579ab4382d2SGreg Kroah-Hartman }
2580ab4382d2SGreg Kroah-Hartman #else
2581ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2582ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2583ab4382d2SGreg Kroah-Hartman #endif
2584ab4382d2SGreg Kroah-Hartman 
2585e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2586e0b0baadSRichard Genoud {
2587ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
2588ab5e4e41SRichard Genoud 	struct gpio_desc *gpiod;
2589ab5e4e41SRichard Genoud 
2590e0b0baadSRichard Genoud 	p->gpios = mctrl_gpio_init(dev, 0);
2591722ccf41SUwe Kleine-König 	if (IS_ERR(p->gpios))
2592722ccf41SUwe Kleine-König 		return PTR_ERR(p->gpios);
2593e0b0baadSRichard Genoud 
2594ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++) {
2595ab5e4e41SRichard Genoud 		gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2596ab5e4e41SRichard Genoud 		if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2597ab5e4e41SRichard Genoud 			p->gpio_irq[i] = gpiod_to_irq(gpiod);
2598ab5e4e41SRichard Genoud 		else
2599ab5e4e41SRichard Genoud 			p->gpio_irq[i] = -EINVAL;
2600ab5e4e41SRichard Genoud 	}
2601ab5e4e41SRichard Genoud 
2602e0b0baadSRichard Genoud 	return 0;
2603e0b0baadSRichard Genoud }
2604e0b0baadSRichard Genoud 
26059671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2606ab4382d2SGreg Kroah-Hartman {
2607ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
26085fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2609574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2610ab4382d2SGreg Kroah-Hartman 	void *data;
26114cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2612bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2613ab4382d2SGreg Kroah-Hartman 
2614ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2615ab4382d2SGreg Kroah-Hartman 
26165fbe46b6SNicolas Ferre 	if (np)
26175fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
26185fbe46b6SNicolas Ferre 	else
26194cbf9f48SNicolas Ferre 		if (pdata)
26204cbf9f48SNicolas Ferre 			ret = pdata->num;
26214cbf9f48SNicolas Ferre 
26224cbf9f48SNicolas Ferre 	if (ret < 0)
26235fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
26244cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2625503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
26264cbf9f48SNicolas Ferre 
2627503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
26284cbf9f48SNicolas Ferre 		ret = -ENODEV;
26294cbf9f48SNicolas Ferre 		goto err;
26304cbf9f48SNicolas Ferre 	}
26314cbf9f48SNicolas Ferre 
2632503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
26334cbf9f48SNicolas Ferre 		/* port already in use */
26344cbf9f48SNicolas Ferre 		ret = -EBUSY;
26354cbf9f48SNicolas Ferre 		goto err;
26364cbf9f48SNicolas Ferre 	}
26374cbf9f48SNicolas Ferre 
26384cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2639ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
26404cbf9f48SNicolas Ferre 	port->uart.line = ret;
2641354e57f3SLinus Walleij 
26422c7af5baSBoris BREZILLON 	spin_lock_init(&port->lock_suspended);
26432c7af5baSBoris BREZILLON 
2644e0b0baadSRichard Genoud 	ret = atmel_init_gpios(port, &pdev->dev);
2645722ccf41SUwe Kleine-König 	if (ret < 0) {
2646722ccf41SUwe Kleine-König 		dev_err(&pdev->dev, "Failed to initialize GPIOs.");
2647722ccf41SUwe Kleine-König 		goto err;
2648722ccf41SUwe Kleine-König 	}
2649ab4382d2SGreg Kroah-Hartman 
265091f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
265191f8c2d8SBoris BREZILLON 	if (ret)
26526fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2653ab4382d2SGreg Kroah-Hartman 
265464e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2655ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2656ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2657ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2658ab4382d2SGreg Kroah-Hartman 		if (!data)
2659ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2660ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2661ab4382d2SGreg Kroah-Hartman 	}
2662ab4382d2SGreg Kroah-Hartman 
2663bd737f87SRicardo Ribalda Delgado 	rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2664bd737f87SRicardo Ribalda Delgado 
2665ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2666ab4382d2SGreg Kroah-Hartman 	if (ret)
2667ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2668ab4382d2SGreg Kroah-Hartman 
2669ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2670ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2671ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2672ab4382d2SGreg Kroah-Hartman 		/*
2673ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
267491f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2675ab4382d2SGreg Kroah-Hartman 		 */
267691f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2677ab4382d2SGreg Kroah-Hartman 	}
2678ab4382d2SGreg Kroah-Hartman #endif
2679ab4382d2SGreg Kroah-Hartman 
2680ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2681ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2682ab4382d2SGreg Kroah-Hartman 
2683d4f64187SCyrille Pitchen 	/*
2684d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2685d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2686d4f64187SCyrille Pitchen 	 */
2687d4f64187SCyrille Pitchen 	clk_prepare_enable(port->clk);
2688d4f64187SCyrille Pitchen 
2689bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2690fc887b15SLinus Torvalds 		UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2691fc887b15SLinus Torvalds 		UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2692fc887b15SLinus Torvalds 	}
2693fc887b15SLinus Torvalds 
2694055560b0SElen Song 	/*
2695055560b0SElen Song 	 * Get port name of usart or uart
2696055560b0SElen Song 	 */
2697892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2698055560b0SElen Song 
2699d4f64187SCyrille Pitchen 	/*
2700d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2701d4f64187SCyrille Pitchen 	 * is used
2702d4f64187SCyrille Pitchen 	 */
2703d4f64187SCyrille Pitchen 	clk_disable_unprepare(port->clk);
2704d4f64187SCyrille Pitchen 
2705ab4382d2SGreg Kroah-Hartman 	return 0;
2706ab4382d2SGreg Kroah-Hartman 
2707ab4382d2SGreg Kroah-Hartman err_add_port:
2708ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2709ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2710ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2711ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2712ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2713ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2714ab4382d2SGreg Kroah-Hartman 	}
27156fbb9bdfSCyrille Pitchen err_clear_bit:
27166fbb9bdfSCyrille Pitchen 	clear_bit(port->uart.line, atmel_ports_in_use);
27174cbf9f48SNicolas Ferre err:
2718ab4382d2SGreg Kroah-Hartman 	return ret;
2719ab4382d2SGreg Kroah-Hartman }
2720ab4382d2SGreg Kroah-Hartman 
2721ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2722ab4382d2SGreg Kroah-Hartman {
2723ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2724ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2725ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2726ab4382d2SGreg Kroah-Hartman 
2727f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2728f50c995fSMarek Roszko 
2729ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2730ab4382d2SGreg Kroah-Hartman 
2731ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2732ab4382d2SGreg Kroah-Hartman 
2733ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2734ab4382d2SGreg Kroah-Hartman 
2735ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2736ab4382d2SGreg Kroah-Hartman 
2737503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
27384cbf9f48SNicolas Ferre 
2739ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2740ab4382d2SGreg Kroah-Hartman 
2741ab4382d2SGreg Kroah-Hartman 	return ret;
2742ab4382d2SGreg Kroah-Hartman }
2743ab4382d2SGreg Kroah-Hartman 
2744ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2745ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
27462d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2747ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2748ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2749ab4382d2SGreg Kroah-Hartman 	.driver		= {
2750ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
27515fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2752ab4382d2SGreg Kroah-Hartman 	},
2753ab4382d2SGreg Kroah-Hartman };
2754ab4382d2SGreg Kroah-Hartman 
2755ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2756ab4382d2SGreg Kroah-Hartman {
2757ab4382d2SGreg Kroah-Hartman 	int ret;
2758ab4382d2SGreg Kroah-Hartman 
2759ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2760ab4382d2SGreg Kroah-Hartman 	if (ret)
2761ab4382d2SGreg Kroah-Hartman 		return ret;
2762ab4382d2SGreg Kroah-Hartman 
2763ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2764ab4382d2SGreg Kroah-Hartman 	if (ret)
2765ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2766ab4382d2SGreg Kroah-Hartman 
2767ab4382d2SGreg Kroah-Hartman 	return ret;
2768ab4382d2SGreg Kroah-Hartman }
2769ab4382d2SGreg Kroah-Hartman 
2770ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2771ab4382d2SGreg Kroah-Hartman {
2772ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2773ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2774ab4382d2SGreg Kroah-Hartman }
2775ab4382d2SGreg Kroah-Hartman 
2776ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2777ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2778ab4382d2SGreg Kroah-Hartman 
2779ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2780ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2781ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2782ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2783