xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 732a84a037a4de29b54e0b4e6cb6f9b3813e29e5)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  Driver for Atmel AT91 / AT32 Serial ports
3ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7ab4382d2SGreg Kroah-Hartman  *
8ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
11ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
12ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
13ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
14ab4382d2SGreg Kroah-Hartman  *
15ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
16ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
19ab4382d2SGreg Kroah-Hartman  *
20ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
21ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
22ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23ab4382d2SGreg Kroah-Hartman  *
24ab4382d2SGreg Kroah-Hartman  */
25ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
35ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
365fbe46b6SNicolas Ferre #include <linux/of.h>
375fbe46b6SNicolas Ferre #include <linux/of_device.h>
38354e57f3SLinus Walleij #include <linux/of_gpio.h>
39ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
406b997babSVinod Koul #include <linux/dmaengine.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
43ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
44bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
452e68c22fSElen Song #include <linux/timer.h>
46354e57f3SLinus Walleij #include <linux/gpio.h>
47e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
48e0b0baadSRichard Genoud #include <linux/err.h>
49ab5e4e41SRichard Genoud #include <linux/irq.h>
50ab4382d2SGreg Kroah-Hartman 
51ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
52ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
53ab4382d2SGreg Kroah-Hartman 
54ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
55ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
56ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
57ab4382d2SGreg Kroah-Hartman 
58ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
59ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
60ab4382d2SGreg Kroah-Hartman #endif
61ab4382d2SGreg Kroah-Hartman 
62ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
63ab4382d2SGreg Kroah-Hartman 
64e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
65e0b0baadSRichard Genoud 
66ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
67ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
68ab4382d2SGreg Kroah-Hartman 
69ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
72ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
73ab4382d2SGreg Kroah-Hartman  * UART. */
74ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
75ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
76ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
77ab4382d2SGreg Kroah-Hartman 
78ab4382d2SGreg Kroah-Hartman #else
79ab4382d2SGreg Kroah-Hartman 
80ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
81ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
82ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
83ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
84ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
85ab4382d2SGreg Kroah-Hartman 
86ab4382d2SGreg Kroah-Hartman #endif
87ab4382d2SGreg Kroah-Hartman 
88ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
89ab4382d2SGreg Kroah-Hartman 
90ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */
91ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_CR)
92ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port)	__raw_readl((port)->membase + ATMEL_US_MR)
93ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_MR)
94ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IER)
95ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IDR)
96ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port)	__raw_readl((port)->membase + ATMEL_US_IMR)
97ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port)	__raw_readl((port)->membase + ATMEL_US_CSR)
98ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port)	__raw_readl((port)->membase + ATMEL_US_RHR)
99ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_THR)
100ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port)	__raw_readl((port)->membase + ATMEL_US_BRGR)
101ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_BRGR)
102ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_RTOR)
103ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v)	__raw_writel(v, (port)->membase + ATMEL_US_TTGR)
104055560b0SElen Song #define UART_GET_IP_NAME(port)	__raw_readl((port)->membase + ATMEL_US_NAME)
105731d9caeSNicolas Ferre #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
106ab4382d2SGreg Kroah-Hartman 
107ab4382d2SGreg Kroah-Hartman  /* PDC registers */
108ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
109ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port)	__raw_readl((port)->membase + ATMEL_PDC_PTSR)
110ab4382d2SGreg Kroah-Hartman 
111ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
112ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port)	__raw_readl((port)->membase + ATMEL_PDC_RPR)
113ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
114ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
115ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
116ab4382d2SGreg Kroah-Hartman 
117ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
118ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
119ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port)	__raw_readl((port)->membase + ATMEL_PDC_TCR)
120ab4382d2SGreg Kroah-Hartman 
121ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
122ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
123ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
124ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
125ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
126ab4382d2SGreg Kroah-Hartman };
127ab4382d2SGreg Kroah-Hartman 
128ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
129ab4382d2SGreg Kroah-Hartman 	u16		status;
130ab4382d2SGreg Kroah-Hartman 	u16		ch;
131ab4382d2SGreg Kroah-Hartman };
132ab4382d2SGreg Kroah-Hartman 
133ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
134ab4382d2SGreg Kroah-Hartman 
135ab4382d2SGreg Kroah-Hartman /*
136ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
137ab4382d2SGreg Kroah-Hartman  */
138ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
139ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
140ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
141ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
142ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
143ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
144ab4382d2SGreg Kroah-Hartman 
14534df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
14664e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
147ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
148ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
149ab4382d2SGreg Kroah-Hartman 
15008f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
15164e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
152ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
153ab4382d2SGreg Kroah-Hartman 
15408f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
15534df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
15608f738beSElen Song 	struct dma_chan			*chan_tx;
15734df42f5SElen Song 	struct dma_chan			*chan_rx;
15808f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
15934df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
16008f738beSElen Song 	dma_cookie_t			cookie_tx;
16134df42f5SElen Song 	dma_cookie_t			cookie_rx;
16208f738beSElen Song 	struct scatterlist		sg_tx;
16334df42f5SElen Song 	struct scatterlist		sg_rx;
164ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
165ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
166ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
167ab4382d2SGreg Kroah-Hartman 
168ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
169ab4382d2SGreg Kroah-Hartman 
170ab4382d2SGreg Kroah-Hartman 	struct serial_rs485	rs485;		/* rs485 settings */
171e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
172ab5e4e41SRichard Genoud 	int			gpio_irq[UART_GPIO_MAX];
173ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
174ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
175055560b0SElen Song 	bool			is_usart;	/* usart or uart */
1762e68c22fSElen Song 	struct timer_list	uart_timer;	/* uart timer */
177a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
178a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
179a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
180a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
181a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
182a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
183ab4382d2SGreg Kroah-Hartman };
184ab4382d2SGreg Kroah-Hartman 
185ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
186503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
187ab4382d2SGreg Kroah-Hartman 
188ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
189ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
190ab4382d2SGreg Kroah-Hartman #endif
191ab4382d2SGreg Kroah-Hartman 
1925fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1935fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1945fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1955fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1965fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1975fbe46b6SNicolas Ferre };
1985fbe46b6SNicolas Ferre 
1995fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
2005fbe46b6SNicolas Ferre #endif
2015fbe46b6SNicolas Ferre 
202ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
203ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
204ab4382d2SGreg Kroah-Hartman {
205ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
206ab4382d2SGreg Kroah-Hartman }
207ab4382d2SGreg Kroah-Hartman 
208ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
20964e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
210ab4382d2SGreg Kroah-Hartman {
211ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
212ab4382d2SGreg Kroah-Hartman 
21364e22ebeSElen Song 	return atmel_port->use_pdc_rx;
214ab4382d2SGreg Kroah-Hartman }
215ab4382d2SGreg Kroah-Hartman 
21664e22ebeSElen Song static bool atmel_use_pdc_tx(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_tx;
221ab4382d2SGreg Kroah-Hartman }
222ab4382d2SGreg Kroah-Hartman #else
22364e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
224ab4382d2SGreg Kroah-Hartman {
225ab4382d2SGreg Kroah-Hartman 	return false;
226ab4382d2SGreg Kroah-Hartman }
227ab4382d2SGreg Kroah-Hartman 
22864e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
229ab4382d2SGreg Kroah-Hartman {
230ab4382d2SGreg Kroah-Hartman 	return false;
231ab4382d2SGreg Kroah-Hartman }
232ab4382d2SGreg Kroah-Hartman #endif
233ab4382d2SGreg Kroah-Hartman 
23408f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
23508f738beSElen Song {
23608f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
23708f738beSElen Song 
23808f738beSElen Song 	return atmel_port->use_dma_tx;
23908f738beSElen Song }
24008f738beSElen Song 
24134df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
24234df42f5SElen Song {
24334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
24434df42f5SElen Song 
24534df42f5SElen Song 	return atmel_port->use_dma_rx;
24634df42f5SElen Song }
24734df42f5SElen Song 
248e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
249e0b0baadSRichard Genoud {
250e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
251e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
252e0b0baadSRichard Genoud 
253e0b0baadSRichard Genoud 	status = UART_GET_CSR(port);
254e0b0baadSRichard Genoud 
255e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
256e0b0baadSRichard Genoud 
257e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
258e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
259e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
260e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
261e0b0baadSRichard Genoud 		else
262e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
263e0b0baadSRichard Genoud 	}
264e0b0baadSRichard Genoud 
265e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
266e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
267e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
268e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
269e0b0baadSRichard Genoud 		else
270e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
271e0b0baadSRichard Genoud 	}
272e0b0baadSRichard Genoud 
273e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
274e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
275e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
276e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
277e0b0baadSRichard Genoud 		else
278e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
279e0b0baadSRichard Genoud 	}
280e0b0baadSRichard Genoud 
281e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
282e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
283e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
284e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
285e0b0baadSRichard Genoud 		else
286e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
287e0b0baadSRichard Genoud 	}
288e0b0baadSRichard Genoud 
289e0b0baadSRichard Genoud 	return status;
290e0b0baadSRichard Genoud }
291e0b0baadSRichard Genoud 
292ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
293ab4382d2SGreg Kroah-Hartman void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
294ab4382d2SGreg Kroah-Hartman {
295ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
296ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
297dbf1115dSClaudio Scordino 	unsigned long flags;
298ab4382d2SGreg Kroah-Hartman 
299dbf1115dSClaudio Scordino 	spin_lock_irqsave(&port->lock, flags);
300ab4382d2SGreg Kroah-Hartman 
301ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
302ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
303ab4382d2SGreg Kroah-Hartman 
304ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port);
305ab4382d2SGreg Kroah-Hartman 
306ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
307ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
308ab4382d2SGreg Kroah-Hartman 
309ab4382d2SGreg Kroah-Hartman 	atmel_port->rs485 = *rs485conf;
310ab4382d2SGreg Kroah-Hartman 
311ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
312ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
313ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
31493f3350cSClaudio Scordino 		if ((rs485conf->delay_rts_after_send) > 0)
315ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
316ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
317ab4382d2SGreg Kroah-Hartman 	} else {
318ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
31964e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
320ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
321ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
322ab4382d2SGreg Kroah-Hartman 		else
323ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
324ab4382d2SGreg Kroah-Hartman 	}
325ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
326ab4382d2SGreg Kroah-Hartman 
327ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
328ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
329ab4382d2SGreg Kroah-Hartman 
330dbf1115dSClaudio Scordino 	spin_unlock_irqrestore(&port->lock, flags);
331ab4382d2SGreg Kroah-Hartman 
332ab4382d2SGreg Kroah-Hartman }
333ab4382d2SGreg Kroah-Hartman 
334ab4382d2SGreg Kroah-Hartman /*
335ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
336ab4382d2SGreg Kroah-Hartman  */
337ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
338ab4382d2SGreg Kroah-Hartman {
339ab4382d2SGreg Kroah-Hartman 	return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
340ab4382d2SGreg Kroah-Hartman }
341ab4382d2SGreg Kroah-Hartman 
342ab4382d2SGreg Kroah-Hartman /*
343ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
344ab4382d2SGreg Kroah-Hartman  */
345ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
346ab4382d2SGreg Kroah-Hartman {
347ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
348ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
349ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
350ab4382d2SGreg Kroah-Hartman 
351ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
352ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSEN;
353ab4382d2SGreg Kroah-Hartman 	else
354ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSDIS;
355ab4382d2SGreg Kroah-Hartman 
356ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
357ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
358ab4382d2SGreg Kroah-Hartman 	else
359ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
360ab4382d2SGreg Kroah-Hartman 
361ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, control);
362ab4382d2SGreg Kroah-Hartman 
363e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
364e0b0baadSRichard Genoud 
365ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
366ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
367ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
368ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
369ab4382d2SGreg Kroah-Hartman 	else
370ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
371ab4382d2SGreg Kroah-Hartman 
372ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
373ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
374ab4382d2SGreg Kroah-Hartman 
375ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
376ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
37793f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
378ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
379ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
380ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
381ab4382d2SGreg Kroah-Hartman 	} else {
382ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
383ab4382d2SGreg Kroah-Hartman 	}
384ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
385ab4382d2SGreg Kroah-Hartman }
386ab4382d2SGreg Kroah-Hartman 
387ab4382d2SGreg Kroah-Hartman /*
388ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
389ab4382d2SGreg Kroah-Hartman  */
390ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
391ab4382d2SGreg Kroah-Hartman {
392e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
393e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
394ab4382d2SGreg Kroah-Hartman 
395ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
396ab4382d2SGreg Kroah-Hartman 
397ab4382d2SGreg Kroah-Hartman 	/*
398ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
399ab4382d2SGreg Kroah-Hartman 	 */
400ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
401ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
402ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
403ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
404ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
405ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
406ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
407ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
408ab4382d2SGreg Kroah-Hartman 
409e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
410ab4382d2SGreg Kroah-Hartman }
411ab4382d2SGreg Kroah-Hartman 
412ab4382d2SGreg Kroah-Hartman /*
413ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
414ab4382d2SGreg Kroah-Hartman  */
415ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
416ab4382d2SGreg Kroah-Hartman {
417ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
418ab4382d2SGreg Kroah-Hartman 
41964e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
420ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
421ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
422ab4382d2SGreg Kroah-Hartman 	}
423ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
424ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
425ab4382d2SGreg Kroah-Hartman 
42683cac9f3SBernhard Roth 	if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
42783cac9f3SBernhard Roth 	    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
428ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
429ab4382d2SGreg Kroah-Hartman }
430ab4382d2SGreg Kroah-Hartman 
431ab4382d2SGreg Kroah-Hartman /*
432ab4382d2SGreg Kroah-Hartman  * Start transmitting.
433ab4382d2SGreg Kroah-Hartman  */
434ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
435ab4382d2SGreg Kroah-Hartman {
436ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
437ab4382d2SGreg Kroah-Hartman 
43864e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
439ab4382d2SGreg Kroah-Hartman 		if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
440ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
441ab4382d2SGreg Kroah-Hartman 			   really need this.*/
442ab4382d2SGreg Kroah-Hartman 			return;
443ab4382d2SGreg Kroah-Hartman 
44483cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
44583cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
446ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
447ab4382d2SGreg Kroah-Hartman 
448ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
449ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
450ab4382d2SGreg Kroah-Hartman 	}
451ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
452ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
453ab4382d2SGreg Kroah-Hartman }
454ab4382d2SGreg Kroah-Hartman 
455ab4382d2SGreg Kroah-Hartman /*
456ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
457ab4382d2SGreg Kroah-Hartman  */
458ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
459ab4382d2SGreg Kroah-Hartman {
460ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);  /* reset status and receiver */
461ab4382d2SGreg Kroah-Hartman 
46257c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXEN);
46357c36868SSiftar, Gabe 
46464e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
465ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
466ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
467ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
468ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
469ab4382d2SGreg Kroah-Hartman 	} else {
470ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
471ab4382d2SGreg Kroah-Hartman 	}
472ab4382d2SGreg Kroah-Hartman }
473ab4382d2SGreg Kroah-Hartman 
474ab4382d2SGreg Kroah-Hartman /*
475ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
476ab4382d2SGreg Kroah-Hartman  */
477ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
478ab4382d2SGreg Kroah-Hartman {
47957c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXDIS);
48057c36868SSiftar, Gabe 
48164e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
482ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
483ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
484ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
485ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
486ab4382d2SGreg Kroah-Hartman 	} else {
487ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXRDY);
488ab4382d2SGreg Kroah-Hartman 	}
489ab4382d2SGreg Kroah-Hartman }
490ab4382d2SGreg Kroah-Hartman 
491ab4382d2SGreg Kroah-Hartman /*
492ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
493ab4382d2SGreg Kroah-Hartman  */
494ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
495ab4382d2SGreg Kroah-Hartman {
496ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
497ab5e4e41SRichard Genoud 	uint32_t ier = 0;
498ab5e4e41SRichard Genoud 
499ab5e4e41SRichard Genoud 	/*
500ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
501ab5e4e41SRichard Genoud 	 */
502ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
503ab5e4e41SRichard Genoud 		return;
504ab5e4e41SRichard Genoud 
505ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
506ab5e4e41SRichard Genoud 
507ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
508ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
509ab5e4e41SRichard Genoud 	else
510ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
511ab5e4e41SRichard Genoud 
512ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
513ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
514ab5e4e41SRichard Genoud 	else
515ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
516ab5e4e41SRichard Genoud 
517ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
518ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
519ab5e4e41SRichard Genoud 	else
520ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
521ab5e4e41SRichard Genoud 
522ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
523ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
524ab5e4e41SRichard Genoud 	else
525ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
526ab5e4e41SRichard Genoud 
527ab5e4e41SRichard Genoud 	UART_PUT_IER(port, ier);
528ab4382d2SGreg Kroah-Hartman }
529ab4382d2SGreg Kroah-Hartman 
530ab4382d2SGreg Kroah-Hartman /*
53135b675b9SRichard Genoud  * Disable modem status interrupts
53235b675b9SRichard Genoud  */
53335b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
53435b675b9SRichard Genoud {
53535b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
53635b675b9SRichard Genoud 	uint32_t idr = 0;
53735b675b9SRichard Genoud 
53835b675b9SRichard Genoud 	/*
53935b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
54035b675b9SRichard Genoud 	 */
54135b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
54235b675b9SRichard Genoud 		return;
54335b675b9SRichard Genoud 
54435b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
54535b675b9SRichard Genoud 
54635b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
54735b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
54835b675b9SRichard Genoud 	else
54935b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
55035b675b9SRichard Genoud 
55135b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
55235b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
55335b675b9SRichard Genoud 	else
55435b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
55535b675b9SRichard Genoud 
55635b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
55735b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
55835b675b9SRichard Genoud 	else
55935b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
56035b675b9SRichard Genoud 
56135b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
56235b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
56335b675b9SRichard Genoud 	else
56435b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
56535b675b9SRichard Genoud 
56635b675b9SRichard Genoud 	UART_PUT_IDR(port, idr);
56735b675b9SRichard Genoud }
56835b675b9SRichard Genoud 
56935b675b9SRichard Genoud /*
570ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
571ab4382d2SGreg Kroah-Hartman  */
572ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
573ab4382d2SGreg Kroah-Hartman {
574ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
575ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTBRK);	/* start break */
576ab4382d2SGreg Kroah-Hartman 	else
577ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STPBRK);	/* stop break */
578ab4382d2SGreg Kroah-Hartman }
579ab4382d2SGreg Kroah-Hartman 
580ab4382d2SGreg Kroah-Hartman /*
581ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
582ab4382d2SGreg Kroah-Hartman  */
583ab4382d2SGreg Kroah-Hartman static void
584ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
585ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
586ab4382d2SGreg Kroah-Hartman {
587ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
588ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
589ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
590ab4382d2SGreg Kroah-Hartman 
591ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
592ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
593ab4382d2SGreg Kroah-Hartman 		return;
594ab4382d2SGreg Kroah-Hartman 
595ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
596ab4382d2SGreg Kroah-Hartman 	c->status	= status;
597ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
598ab4382d2SGreg Kroah-Hartman 
599ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
600ab4382d2SGreg Kroah-Hartman 	smp_wmb();
601ab4382d2SGreg Kroah-Hartman 
602ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
603ab4382d2SGreg Kroah-Hartman }
604ab4382d2SGreg Kroah-Hartman 
605ab4382d2SGreg Kroah-Hartman /*
606ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
607ab4382d2SGreg Kroah-Hartman  */
608ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
609ab4382d2SGreg Kroah-Hartman {
610ab4382d2SGreg Kroah-Hartman 	/* clear error */
611ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
612ab4382d2SGreg Kroah-Hartman 
613ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
614ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
615ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
616ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
617ab4382d2SGreg Kroah-Hartman 	}
618ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
619ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
620ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
621ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
622ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
623ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
624ab4382d2SGreg Kroah-Hartman }
625ab4382d2SGreg Kroah-Hartman 
626ab4382d2SGreg Kroah-Hartman /*
627ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
628ab4382d2SGreg Kroah-Hartman  */
629ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
630ab4382d2SGreg Kroah-Hartman {
631ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
632ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
633ab4382d2SGreg Kroah-Hartman 
634ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
635ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
636ab4382d2SGreg Kroah-Hartman 		ch = UART_GET_CHAR(port);
637ab4382d2SGreg Kroah-Hartman 
638ab4382d2SGreg Kroah-Hartman 		/*
639ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
640ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
641ab4382d2SGreg Kroah-Hartman 		 */
642ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
643ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
644ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
645ab4382d2SGreg Kroah-Hartman 
646ab4382d2SGreg Kroah-Hartman 			/* clear error */
647ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_RSTSTA);
648ab4382d2SGreg Kroah-Hartman 
649ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
650ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
651ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
652ab4382d2SGreg Kroah-Hartman 				UART_PUT_IER(port, ATMEL_US_RXBRK);
653ab4382d2SGreg Kroah-Hartman 			} else {
654ab4382d2SGreg Kroah-Hartman 				/*
655ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
656ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
657ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
658ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
659ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
660ab4382d2SGreg Kroah-Hartman 				 */
661ab4382d2SGreg Kroah-Hartman 				UART_PUT_IDR(port, ATMEL_US_RXBRK);
662ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
663ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
664ab4382d2SGreg Kroah-Hartman 			}
665ab4382d2SGreg Kroah-Hartman 		}
666ab4382d2SGreg Kroah-Hartman 
667ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
668ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
669ab4382d2SGreg Kroah-Hartman 	}
670ab4382d2SGreg Kroah-Hartman 
671ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
672ab4382d2SGreg Kroah-Hartman }
673ab4382d2SGreg Kroah-Hartman 
674ab4382d2SGreg Kroah-Hartman /*
675ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
676ab4382d2SGreg Kroah-Hartman  * disabled)
677ab4382d2SGreg Kroah-Hartman  */
678ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
679ab4382d2SGreg Kroah-Hartman {
680ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
681ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
682ab4382d2SGreg Kroah-Hartman 
683ab4382d2SGreg Kroah-Hartman 	if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
684ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, port->x_char);
685ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
686ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
687ab4382d2SGreg Kroah-Hartman 	}
688ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
689ab4382d2SGreg Kroah-Hartman 		return;
690ab4382d2SGreg Kroah-Hartman 
691ab4382d2SGreg Kroah-Hartman 	while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
692ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
693ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
694ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
695ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
696ab4382d2SGreg Kroah-Hartman 			break;
697ab4382d2SGreg Kroah-Hartman 	}
698ab4382d2SGreg Kroah-Hartman 
699ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
700ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
701ab4382d2SGreg Kroah-Hartman 
702ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
703ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
704ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
705ab4382d2SGreg Kroah-Hartman }
706ab4382d2SGreg Kroah-Hartman 
70708f738beSElen Song static void atmel_complete_tx_dma(void *arg)
70808f738beSElen Song {
70908f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
71008f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
71108f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
71208f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
71308f738beSElen Song 	unsigned long flags;
71408f738beSElen Song 
71508f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
71608f738beSElen Song 
71708f738beSElen Song 	if (chan)
71808f738beSElen Song 		dmaengine_terminate_all(chan);
71908f738beSElen Song 	xmit->tail += sg_dma_len(&atmel_port->sg_tx);
72008f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
72108f738beSElen Song 
72208f738beSElen Song 	port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
72308f738beSElen Song 
72408f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
72508f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
72608f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
72708f738beSElen Song 	atmel_port->desc_tx = NULL;
72808f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
72908f738beSElen Song 
73008f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
73108f738beSElen Song 		uart_write_wakeup(port);
73208f738beSElen Song 
73308f738beSElen Song 	/* Do we really need this? */
73408f738beSElen Song 	if (!uart_circ_empty(xmit))
73508f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
73608f738beSElen Song 
73708f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
73808f738beSElen Song }
73908f738beSElen Song 
74008f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
74108f738beSElen Song {
74208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
74308f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
74408f738beSElen Song 
74508f738beSElen Song 	if (chan) {
74608f738beSElen Song 		dmaengine_terminate_all(chan);
74708f738beSElen Song 		dma_release_channel(chan);
74808f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
74948479148SWolfram Sang 				DMA_TO_DEVICE);
75008f738beSElen Song 	}
75108f738beSElen Song 
75208f738beSElen Song 	atmel_port->desc_tx = NULL;
75308f738beSElen Song 	atmel_port->chan_tx = NULL;
75408f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
75508f738beSElen Song }
75608f738beSElen Song 
75708f738beSElen Song /*
75808f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
75908f738beSElen Song  */
76008f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
76108f738beSElen Song {
76208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
76308f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
76408f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
76508f738beSElen Song 	struct dma_async_tx_descriptor *desc;
76608f738beSElen Song 	struct scatterlist *sg = &atmel_port->sg_tx;
76708f738beSElen Song 
76808f738beSElen Song 	/* Make sure we have an idle channel */
76908f738beSElen Song 	if (atmel_port->desc_tx != NULL)
77008f738beSElen Song 		return;
77108f738beSElen Song 
77208f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
77308f738beSElen Song 		/*
77408f738beSElen Song 		 * DMA is idle now.
77508f738beSElen Song 		 * Port xmit buffer is already mapped,
77608f738beSElen Song 		 * and it is one page... Just adjust
77708f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
77808f738beSElen Song 		 * we have to transmit till the end, and then the rest.
77908f738beSElen Song 		 * Take the port lock to get a
78008f738beSElen Song 		 * consistent xmit buffer state.
78108f738beSElen Song 		 */
78208f738beSElen Song 		sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
78308f738beSElen Song 		sg_dma_address(sg) = (sg_dma_address(sg) &
78408f738beSElen Song 					~(UART_XMIT_SIZE - 1))
78508f738beSElen Song 					+ sg->offset;
78608f738beSElen Song 		sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
78708f738beSElen Song 						xmit->tail,
78808f738beSElen Song 						UART_XMIT_SIZE);
78908f738beSElen Song 		BUG_ON(!sg_dma_len(sg));
79008f738beSElen Song 
79108f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
79208f738beSElen Song 						sg,
79308f738beSElen Song 						1,
79408f738beSElen Song 						DMA_MEM_TO_DEV,
79508f738beSElen Song 						DMA_PREP_INTERRUPT |
79608f738beSElen Song 						DMA_CTRL_ACK);
79708f738beSElen Song 		if (!desc) {
79808f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
79908f738beSElen Song 			return;
80008f738beSElen Song 		}
80108f738beSElen Song 
80208f738beSElen Song 		dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
80308f738beSElen Song 
80408f738beSElen Song 		atmel_port->desc_tx = desc;
80508f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
80608f738beSElen Song 		desc->callback_param = atmel_port;
80708f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
80808f738beSElen Song 
80908f738beSElen Song 	} else {
81008f738beSElen Song 		if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
81108f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
81208f738beSElen Song 			atmel_start_rx(port);
81308f738beSElen Song 		}
81408f738beSElen Song 	}
81508f738beSElen Song 
81608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
81708f738beSElen Song 		uart_write_wakeup(port);
81808f738beSElen Song }
81908f738beSElen Song 
82008f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
82108f738beSElen Song {
82208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
82308f738beSElen Song 	dma_cap_mask_t		mask;
82408f738beSElen Song 	struct dma_slave_config config;
82508f738beSElen Song 	int ret, nent;
82608f738beSElen Song 
82708f738beSElen Song 	dma_cap_zero(mask);
82808f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
82908f738beSElen Song 
83008f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
83108f738beSElen Song 	if (atmel_port->chan_tx == NULL)
83208f738beSElen Song 		goto chan_err;
83308f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
83408f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
83508f738beSElen Song 
83608f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
83708f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
83808f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
83908f738beSElen Song 	BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
84008f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
84108f738beSElen Song 			virt_to_page(port->state->xmit.buf),
84208f738beSElen Song 			UART_XMIT_SIZE,
84308f738beSElen Song 			(int)port->state->xmit.buf & ~PAGE_MASK);
84408f738beSElen Song 	nent = dma_map_sg(port->dev,
84508f738beSElen Song 				&atmel_port->sg_tx,
84608f738beSElen Song 				1,
84748479148SWolfram Sang 				DMA_TO_DEVICE);
84808f738beSElen Song 
84908f738beSElen Song 	if (!nent) {
85008f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
85108f738beSElen Song 		goto chan_err;
85208f738beSElen Song 	} else {
85308f738beSElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
85408f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
85508f738beSElen Song 			port->state->xmit.buf,
85608f738beSElen Song 			sg_dma_address(&atmel_port->sg_tx));
85708f738beSElen Song 	}
85808f738beSElen Song 
85908f738beSElen Song 	/* Configure the slave DMA */
86008f738beSElen Song 	memset(&config, 0, sizeof(config));
86108f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
86208f738beSElen Song 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
86308f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
86408f738beSElen Song 
8655483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
8665483c10eSMaxime Ripard 				     &config);
86708f738beSElen Song 	if (ret) {
86808f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
86908f738beSElen Song 		goto chan_err;
87008f738beSElen Song 	}
87108f738beSElen Song 
87208f738beSElen Song 	return 0;
87308f738beSElen Song 
87408f738beSElen Song chan_err:
87508f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
87608f738beSElen Song 	atmel_port->use_dma_tx = 0;
87708f738beSElen Song 	if (atmel_port->chan_tx)
87808f738beSElen Song 		atmel_release_tx_dma(port);
87908f738beSElen Song 	return -EINVAL;
88008f738beSElen Song }
88108f738beSElen Song 
88234df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
88334df42f5SElen Song {
88434df42f5SElen Song 	struct uart_port *port = arg;
88534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
88634df42f5SElen Song 
88734df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
88834df42f5SElen Song }
88934df42f5SElen Song 
89034df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
89134df42f5SElen Song {
89234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
89334df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
89434df42f5SElen Song 
89534df42f5SElen Song 	if (chan) {
89634df42f5SElen Song 		dmaengine_terminate_all(chan);
89734df42f5SElen Song 		dma_release_channel(chan);
89834df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
89948479148SWolfram Sang 				DMA_FROM_DEVICE);
90034df42f5SElen Song 	}
90134df42f5SElen Song 
90234df42f5SElen Song 	atmel_port->desc_rx = NULL;
90334df42f5SElen Song 	atmel_port->chan_rx = NULL;
90434df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
90534df42f5SElen Song }
90634df42f5SElen Song 
90734df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
90834df42f5SElen Song {
90934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
91066f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
91134df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
91234df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
91334df42f5SElen Song 	struct dma_tx_state state;
91434df42f5SElen Song 	enum dma_status dmastat;
91566f37aafSCyrille Pitchen 	size_t count;
91634df42f5SElen Song 
91734df42f5SElen Song 
91834df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
91934df42f5SElen Song 	UART_PUT_CR(port, ATMEL_US_STTTO);
92034df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
92134df42f5SElen Song 				atmel_port->cookie_rx,
92234df42f5SElen Song 				&state);
92334df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
92434df42f5SElen Song 	if (dmastat == DMA_ERROR) {
92534df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
92634df42f5SElen Song 		UART_PUT_IER(port, ATMEL_US_TIMEOUT);
92734df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
92834df42f5SElen Song 		return;
92934df42f5SElen Song 	}
93066f37aafSCyrille Pitchen 
93166f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
93266f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
93366f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
93466f37aafSCyrille Pitchen 			    1,
93566f37aafSCyrille Pitchen 			    DMA_DEV_TO_MEM);
93634df42f5SElen Song 
93734df42f5SElen Song 	/*
93866f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
93966f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
94066f37aafSCyrille Pitchen 	 * framework.
94166f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
94266f37aafSCyrille Pitchen 	 * length.
94334df42f5SElen Song 	 */
94466f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
94566f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
94666f37aafSCyrille Pitchen 	/*
94766f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
94866f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
94966f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
95066f37aafSCyrille Pitchen 	 *
95166f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
95266f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
95366f37aafSCyrille Pitchen 	 *
95466f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
95566f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
95666f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
95766f37aafSCyrille Pitchen 	 */
95866f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
95966f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
96034df42f5SElen Song 
96166f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
96266f37aafSCyrille Pitchen 		ring->tail = 0;
96334df42f5SElen Song 		port->icount.rx += count;
96434df42f5SElen Song 	}
96534df42f5SElen Song 
96666f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
96766f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
96866f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
96966f37aafSCyrille Pitchen 
97066f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
97166f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
97266f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
97366f37aafSCyrille Pitchen 			ring->head = 0;
97466f37aafSCyrille Pitchen 		ring->tail = ring->head;
97566f37aafSCyrille Pitchen 		port->icount.rx += count;
97666f37aafSCyrille Pitchen 	}
97766f37aafSCyrille Pitchen 
97866f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
97966f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
98066f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
98166f37aafSCyrille Pitchen 			       1,
98266f37aafSCyrille Pitchen 			       DMA_DEV_TO_MEM);
98366f37aafSCyrille Pitchen 
98466f37aafSCyrille Pitchen 	/*
98566f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
98666f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
98766f37aafSCyrille Pitchen 	 */
98866f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
98966f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
99066f37aafSCyrille Pitchen 	spin_lock(&port->lock);
99166f37aafSCyrille Pitchen 
99234df42f5SElen Song 	UART_PUT_IER(port, ATMEL_US_TIMEOUT);
99334df42f5SElen Song }
99434df42f5SElen Song 
99534df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
99634df42f5SElen Song {
99734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
99834df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
99934df42f5SElen Song 	dma_cap_mask_t		mask;
100034df42f5SElen Song 	struct dma_slave_config config;
100134df42f5SElen Song 	struct circ_buf		*ring;
100234df42f5SElen Song 	int ret, nent;
100334df42f5SElen Song 
100434df42f5SElen Song 	ring = &atmel_port->rx_ring;
100534df42f5SElen Song 
100634df42f5SElen Song 	dma_cap_zero(mask);
100734df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
100834df42f5SElen Song 
100934df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
101034df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
101134df42f5SElen Song 		goto chan_err;
101234df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
101334df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
101434df42f5SElen Song 
101534df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
101634df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
101734df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
101834df42f5SElen Song 	BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
101934df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
102034df42f5SElen Song 			virt_to_page(ring->buf),
102134df42f5SElen Song 			ATMEL_SERIAL_RINGSIZE,
102234df42f5SElen Song 			(int)ring->buf & ~PAGE_MASK);
102334df42f5SElen Song 			nent = dma_map_sg(port->dev,
102434df42f5SElen Song 					&atmel_port->sg_rx,
102534df42f5SElen Song 					1,
102648479148SWolfram Sang 					DMA_FROM_DEVICE);
102734df42f5SElen Song 
102834df42f5SElen Song 	if (!nent) {
102934df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
103034df42f5SElen Song 		goto chan_err;
103134df42f5SElen Song 	} else {
103234df42f5SElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
103334df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
103434df42f5SElen Song 			ring->buf,
103534df42f5SElen Song 			sg_dma_address(&atmel_port->sg_rx));
103634df42f5SElen Song 	}
103734df42f5SElen Song 
103834df42f5SElen Song 	/* Configure the slave DMA */
103934df42f5SElen Song 	memset(&config, 0, sizeof(config));
104034df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
104134df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
104234df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
104334df42f5SElen Song 
10445483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
10455483c10eSMaxime Ripard 				     &config);
104634df42f5SElen Song 	if (ret) {
104734df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
104834df42f5SElen Song 		goto chan_err;
104934df42f5SElen Song 	}
105034df42f5SElen Song 	/*
105134df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
105234df42f5SElen Song 	 * each one is half ring buffer size
105334df42f5SElen Song 	 */
105434df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
105534df42f5SElen Song 				sg_dma_address(&atmel_port->sg_rx),
105634df42f5SElen Song 				sg_dma_len(&atmel_port->sg_rx),
105734df42f5SElen Song 				sg_dma_len(&atmel_port->sg_rx)/2,
105834df42f5SElen Song 				DMA_DEV_TO_MEM,
105934df42f5SElen Song 				DMA_PREP_INTERRUPT);
106034df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
106134df42f5SElen Song 	desc->callback_param = port;
106234df42f5SElen Song 	atmel_port->desc_rx = desc;
106334df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
106434df42f5SElen Song 
106534df42f5SElen Song 	return 0;
106634df42f5SElen Song 
106734df42f5SElen Song chan_err:
106834df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
106934df42f5SElen Song 	atmel_port->use_dma_rx = 0;
107034df42f5SElen Song 	if (atmel_port->chan_rx)
107134df42f5SElen Song 		atmel_release_rx_dma(port);
107234df42f5SElen Song 	return -EINVAL;
107334df42f5SElen Song }
107434df42f5SElen Song 
10752e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
10762e68c22fSElen Song {
10772e68c22fSElen Song 	struct uart_port *port = (void *)data;
10782e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
10792e68c22fSElen Song 
10802e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
10812e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
10822e68c22fSElen Song }
10832e68c22fSElen Song 
1084ab4382d2SGreg Kroah-Hartman /*
1085ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1086ab4382d2SGreg Kroah-Hartman  */
1087ab4382d2SGreg Kroah-Hartman static void
1088ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1089ab4382d2SGreg Kroah-Hartman {
1090ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1091ab4382d2SGreg Kroah-Hartman 
109264e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1093ab4382d2SGreg Kroah-Hartman 		/*
1094ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1095ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1096ab4382d2SGreg Kroah-Hartman 		 *
1097ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1098ab4382d2SGreg Kroah-Hartman 		 * the moment.
1099ab4382d2SGreg Kroah-Hartman 		 */
1100ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
1101ab4382d2SGreg Kroah-Hartman 			UART_PUT_IDR(port, (ATMEL_US_ENDRX
1102ab4382d2SGreg Kroah-Hartman 						| ATMEL_US_TIMEOUT));
1103ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
1104ab4382d2SGreg Kroah-Hartman 		}
1105ab4382d2SGreg Kroah-Hartman 
1106ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1107ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1108ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1109ab4382d2SGreg Kroah-Hartman 	}
1110ab4382d2SGreg Kroah-Hartman 
111134df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
111234df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
111334df42f5SElen Song 			UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
111434df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
111534df42f5SElen Song 		}
111634df42f5SElen Song 	}
111734df42f5SElen Song 
1118ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1119ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1120ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1121ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1122ab4382d2SGreg Kroah-Hartman 		/*
1123ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1124ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1125ab4382d2SGreg Kroah-Hartman 		 */
1126ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_RSTSTA);
1127ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXBRK);
1128ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1129ab4382d2SGreg Kroah-Hartman 	}
1130ab4382d2SGreg Kroah-Hartman }
1131ab4382d2SGreg Kroah-Hartman 
1132ab4382d2SGreg Kroah-Hartman /*
1133ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1134ab4382d2SGreg Kroah-Hartman  */
1135ab4382d2SGreg Kroah-Hartman static void
1136ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1137ab4382d2SGreg Kroah-Hartman {
1138ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1139ab4382d2SGreg Kroah-Hartman 
1140ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1141ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
1142ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, atmel_port->tx_done_mask);
1143ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1144ab4382d2SGreg Kroah-Hartman 	}
1145ab4382d2SGreg Kroah-Hartman }
1146ab4382d2SGreg Kroah-Hartman 
1147ab4382d2SGreg Kroah-Hartman /*
1148ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1149ab4382d2SGreg Kroah-Hartman  */
1150ab4382d2SGreg Kroah-Hartman static void
1151ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1152ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1153ab4382d2SGreg Kroah-Hartman {
1154ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1155ab4382d2SGreg Kroah-Hartman 
1156ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1157ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1158ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1159ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1160ab4382d2SGreg Kroah-Hartman 	}
1161ab4382d2SGreg Kroah-Hartman }
1162ab4382d2SGreg Kroah-Hartman 
1163ab4382d2SGreg Kroah-Hartman /*
1164ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1165ab4382d2SGreg Kroah-Hartman  */
1166ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1167ab4382d2SGreg Kroah-Hartman {
1168ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1169ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1170ab4382d2SGreg Kroah-Hartman 	unsigned int status, pending, pass_counter = 0;
1171ab5e4e41SRichard Genoud 	bool gpio_handled = false;
1172ab4382d2SGreg Kroah-Hartman 
1173ab4382d2SGreg Kroah-Hartman 	do {
1174e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
1175ab4382d2SGreg Kroah-Hartman 		pending = status & UART_GET_IMR(port);
1176ab5e4e41SRichard Genoud 		if (!gpio_handled) {
1177ab5e4e41SRichard Genoud 			/*
1178ab5e4e41SRichard Genoud 			 * Dealing with GPIO interrupt
1179ab5e4e41SRichard Genoud 			 */
1180ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1181ab5e4e41SRichard Genoud 				pending |= ATMEL_US_CTSIC;
1182ab5e4e41SRichard Genoud 
1183ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1184ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DSRIC;
1185ab5e4e41SRichard Genoud 
1186ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1187ab5e4e41SRichard Genoud 				pending |= ATMEL_US_RIIC;
1188ab5e4e41SRichard Genoud 
1189ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1190ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DCDIC;
1191ab5e4e41SRichard Genoud 
1192ab5e4e41SRichard Genoud 			gpio_handled = true;
1193ab5e4e41SRichard Genoud 		}
1194ab4382d2SGreg Kroah-Hartman 		if (!pending)
1195ab4382d2SGreg Kroah-Hartman 			break;
1196ab4382d2SGreg Kroah-Hartman 
1197ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1198ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1199ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1200ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1201ab4382d2SGreg Kroah-Hartman 
1202ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1203ab4382d2SGreg Kroah-Hartman }
1204ab4382d2SGreg Kroah-Hartman 
1205a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1206a930e528SElen Song {
1207a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1208a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1209a930e528SElen Song 
1210a930e528SElen Song 	dma_unmap_single(port->dev,
1211a930e528SElen Song 			 pdc->dma_addr,
1212a930e528SElen Song 			 pdc->dma_size,
1213a930e528SElen Song 			 DMA_TO_DEVICE);
1214a930e528SElen Song }
1215a930e528SElen Song 
1216ab4382d2SGreg Kroah-Hartman /*
1217ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1218ab4382d2SGreg Kroah-Hartman  */
121964e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1220ab4382d2SGreg Kroah-Hartman {
1221ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1222ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1223ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1224ab4382d2SGreg Kroah-Hartman 	int count;
1225ab4382d2SGreg Kroah-Hartman 
1226ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
1227ab4382d2SGreg Kroah-Hartman 	if (UART_GET_TCR(port))
1228ab4382d2SGreg Kroah-Hartman 		return;
1229ab4382d2SGreg Kroah-Hartman 
1230ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1231ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1232ab4382d2SGreg Kroah-Hartman 
1233ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1234ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1235ab4382d2SGreg Kroah-Hartman 
1236ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1237ab4382d2SGreg Kroah-Hartman 
1238ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
1239ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1240ab4382d2SGreg Kroah-Hartman 
1241ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1242ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1243ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1244ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1245ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1246ab4382d2SGreg Kroah-Hartman 
1247ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1248ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1249ab4382d2SGreg Kroah-Hartman 
1250ab4382d2SGreg Kroah-Hartman 		UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1251ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, count);
1252ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
1253ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1254ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
1255ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
1256ab4382d2SGreg Kroah-Hartman 	} else {
125783cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
125883cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1259ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1260ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1261ab4382d2SGreg Kroah-Hartman 		}
1262ab4382d2SGreg Kroah-Hartman 	}
1263ab4382d2SGreg Kroah-Hartman 
1264ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1265ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1266ab4382d2SGreg Kroah-Hartman }
1267ab4382d2SGreg Kroah-Hartman 
1268a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1269a930e528SElen Song {
1270a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1271a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1272a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1273a930e528SElen Song 
1274a930e528SElen Song 	pdc->buf = xmit->buf;
1275a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1276a930e528SElen Song 					pdc->buf,
1277a930e528SElen Song 					UART_XMIT_SIZE,
1278a930e528SElen Song 					DMA_TO_DEVICE);
1279a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1280a930e528SElen Song 	pdc->ofs = 0;
1281a930e528SElen Song 
1282a930e528SElen Song 	return 0;
1283a930e528SElen Song }
1284a930e528SElen Song 
1285ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1286ab4382d2SGreg Kroah-Hartman {
1287ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1288ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1289ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1290ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1291ab4382d2SGreg Kroah-Hartman 
1292ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1293ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1294ab4382d2SGreg Kroah-Hartman 
1295ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1296ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1297ab4382d2SGreg Kroah-Hartman 
1298ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1299ab4382d2SGreg Kroah-Hartman 
1300ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1301ab4382d2SGreg Kroah-Hartman 
1302ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1303ab4382d2SGreg Kroah-Hartman 		status = c.status;
1304ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1305ab4382d2SGreg Kroah-Hartman 
1306ab4382d2SGreg Kroah-Hartman 		/*
1307ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1308ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1309ab4382d2SGreg Kroah-Hartman 		 */
1310ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1311ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1312ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1313ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1314ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1315ab4382d2SGreg Kroah-Hartman 
1316ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1317ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1318ab4382d2SGreg Kroah-Hartman 					continue;
1319ab4382d2SGreg Kroah-Hartman 			}
1320ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1321ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1322ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1323ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1324ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1325ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1326ab4382d2SGreg Kroah-Hartman 
1327ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1328ab4382d2SGreg Kroah-Hartman 
1329ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1330ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1331ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1332ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1333ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1334ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1335ab4382d2SGreg Kroah-Hartman 		}
1336ab4382d2SGreg Kroah-Hartman 
1337ab4382d2SGreg Kroah-Hartman 
1338ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1339ab4382d2SGreg Kroah-Hartman 			continue;
1340ab4382d2SGreg Kroah-Hartman 
1341ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1342ab4382d2SGreg Kroah-Hartman 	}
1343ab4382d2SGreg Kroah-Hartman 
1344ab4382d2SGreg Kroah-Hartman 	/*
1345ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1346ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1347ab4382d2SGreg Kroah-Hartman 	 */
1348ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
13492e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1350ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1351ab4382d2SGreg Kroah-Hartman }
1352ab4382d2SGreg Kroah-Hartman 
1353a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1354a930e528SElen Song {
1355a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1356a930e528SElen Song 	int i;
1357a930e528SElen Song 
1358a930e528SElen Song 	for (i = 0; i < 2; i++) {
1359a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1360a930e528SElen Song 
1361a930e528SElen Song 		dma_unmap_single(port->dev,
1362a930e528SElen Song 				 pdc->dma_addr,
1363a930e528SElen Song 				 pdc->dma_size,
1364a930e528SElen Song 				 DMA_FROM_DEVICE);
1365a930e528SElen Song 		kfree(pdc->buf);
1366a930e528SElen Song 	}
1367a930e528SElen Song }
1368a930e528SElen Song 
136964e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1370ab4382d2SGreg Kroah-Hartman {
1371ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
137205c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1373ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1374ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1375ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1376ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1377ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1378ab4382d2SGreg Kroah-Hartman 
1379ab4382d2SGreg Kroah-Hartman 	do {
1380ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
1381ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
1382ab4382d2SGreg Kroah-Hartman 
1383ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
1384ab4382d2SGreg Kroah-Hartman 		head = UART_GET_RPR(port) - pdc->dma_addr;
1385ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1386ab4382d2SGreg Kroah-Hartman 
1387ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1388ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1389ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1390ab4382d2SGreg Kroah-Hartman 		 * find out.
1391ab4382d2SGreg Kroah-Hartman 		 *
1392ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1393ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1394ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1395ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1396ab4382d2SGreg Kroah-Hartman 		 */
1397ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1398ab4382d2SGreg Kroah-Hartman 
1399ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1400ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1401ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1402ab4382d2SGreg Kroah-Hartman 
1403ab4382d2SGreg Kroah-Hartman 			/*
1404ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1405ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1406ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1407ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1408ab4382d2SGreg Kroah-Hartman 			 */
1409ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1410ab4382d2SGreg Kroah-Hartman 
141105c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
141205c7cd39SJiri Slaby 						count);
1413ab4382d2SGreg Kroah-Hartman 
1414ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1415ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1416ab4382d2SGreg Kroah-Hartman 
1417ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1418ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1419ab4382d2SGreg Kroah-Hartman 		}
1420ab4382d2SGreg Kroah-Hartman 
1421ab4382d2SGreg Kroah-Hartman 		/*
1422ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1423ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1424ab4382d2SGreg Kroah-Hartman 		 */
1425ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1426ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
1427ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNPR(port, pdc->dma_addr);
1428ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNCR(port, pdc->dma_size);
1429ab4382d2SGreg Kroah-Hartman 
1430ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1431ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1432ab4382d2SGreg Kroah-Hartman 		}
1433ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1434ab4382d2SGreg Kroah-Hartman 
1435ab4382d2SGreg Kroah-Hartman 	/*
1436ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1437ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1438ab4382d2SGreg Kroah-Hartman 	 */
1439ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14402e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1441ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1442ab4382d2SGreg Kroah-Hartman 
1443ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1444ab4382d2SGreg Kroah-Hartman }
1445ab4382d2SGreg Kroah-Hartman 
1446a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1447a930e528SElen Song {
1448a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1449a930e528SElen Song 	int i;
1450a930e528SElen Song 
1451a930e528SElen Song 	for (i = 0; i < 2; i++) {
1452a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1453a930e528SElen Song 
1454a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1455a930e528SElen Song 		if (pdc->buf == NULL) {
1456a930e528SElen Song 			if (i != 0) {
1457a930e528SElen Song 				dma_unmap_single(port->dev,
1458a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1459a930e528SElen Song 					PDC_BUFFER_SIZE,
1460a930e528SElen Song 					DMA_FROM_DEVICE);
1461a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1462a930e528SElen Song 			}
1463a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1464a930e528SElen Song 			return -ENOMEM;
1465a930e528SElen Song 		}
1466a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1467a930e528SElen Song 						pdc->buf,
1468a930e528SElen Song 						PDC_BUFFER_SIZE,
1469a930e528SElen Song 						DMA_FROM_DEVICE);
1470a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1471a930e528SElen Song 		pdc->ofs = 0;
1472a930e528SElen Song 	}
1473a930e528SElen Song 
1474a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1475a930e528SElen Song 
1476a930e528SElen Song 	UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1477a930e528SElen Song 	UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1478a930e528SElen Song 
1479a930e528SElen Song 	UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1480a930e528SElen Song 	UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1481a930e528SElen Song 
1482a930e528SElen Song 	return 0;
1483a930e528SElen Song }
1484a930e528SElen Song 
1485ab4382d2SGreg Kroah-Hartman /*
1486ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1487ab4382d2SGreg Kroah-Hartman  */
1488ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1489ab4382d2SGreg Kroah-Hartman {
1490ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1491ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1492ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1493ab4382d2SGreg Kroah-Hartman 	unsigned int status_change;
1494ab4382d2SGreg Kroah-Hartman 
1495ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1496ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1497ab4382d2SGreg Kroah-Hartman 
1498a930e528SElen Song 	atmel_port->schedule_tx(port);
1499ab4382d2SGreg Kroah-Hartman 
1500ab4382d2SGreg Kroah-Hartman 	status = atmel_port->irq_status;
1501ab4382d2SGreg Kroah-Hartman 	status_change = status ^ atmel_port->irq_status_prev;
1502ab4382d2SGreg Kroah-Hartman 
1503ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1504ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1505ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1506ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1507ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1508ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1509ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1510ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1511ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1512ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1513ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1514ab4382d2SGreg Kroah-Hartman 
1515ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1516ab4382d2SGreg Kroah-Hartman 
1517ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status_prev = status;
1518ab4382d2SGreg Kroah-Hartman 	}
1519ab4382d2SGreg Kroah-Hartman 
1520a930e528SElen Song 	atmel_port->schedule_rx(port);
1521ab4382d2SGreg Kroah-Hartman 
1522ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1523ab4382d2SGreg Kroah-Hartman }
1524ab4382d2SGreg Kroah-Hartman 
152533d64c4fSElen Song static int atmel_init_property(struct atmel_uart_port *atmel_port,
152633d64c4fSElen Song 				struct platform_device *pdev)
152733d64c4fSElen Song {
152833d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1529574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
153033d64c4fSElen Song 
153133d64c4fSElen Song 	if (np) {
153233d64c4fSElen Song 		/* DMA/PDC usage specification */
153333d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
153433d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
153533d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
153633d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
153733d64c4fSElen Song 			} else {
153833d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
153933d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
154033d64c4fSElen Song 			}
154133d64c4fSElen Song 		} else {
154233d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
154333d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
154433d64c4fSElen Song 		}
154533d64c4fSElen Song 
154633d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
154733d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
154833d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
154933d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
155033d64c4fSElen Song 			} else {
155133d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
155233d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
155333d64c4fSElen Song 			}
155433d64c4fSElen Song 		} else {
155533d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
155633d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
155733d64c4fSElen Song 		}
155833d64c4fSElen Song 
155933d64c4fSElen Song 	} else {
156033d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
156133d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
156233d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
156333d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
156433d64c4fSElen Song 	}
156533d64c4fSElen Song 
156633d64c4fSElen Song 	return 0;
156733d64c4fSElen Song }
156833d64c4fSElen Song 
156933d64c4fSElen Song static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
157033d64c4fSElen Song 				struct platform_device *pdev)
157133d64c4fSElen Song {
157233d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1573574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
157433d64c4fSElen Song 
157533d64c4fSElen Song 	if (np) {
157633d64c4fSElen Song 		u32 rs485_delay[2];
157733d64c4fSElen Song 		/* rs485 properties */
157833d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
157933d64c4fSElen Song 					rs485_delay, 2) == 0) {
158033d64c4fSElen Song 			struct serial_rs485 *rs485conf = &atmel_port->rs485;
158133d64c4fSElen Song 
158233d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
158333d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
158433d64c4fSElen Song 			rs485conf->flags = 0;
158533d64c4fSElen Song 
158633d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
158733d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
158833d64c4fSElen Song 
158933d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
159033d64c4fSElen Song 								NULL))
159133d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
159233d64c4fSElen Song 		}
159333d64c4fSElen Song 	} else {
159433d64c4fSElen Song 		atmel_port->rs485       = pdata->rs485;
159533d64c4fSElen Song 	}
159633d64c4fSElen Song 
159733d64c4fSElen Song }
159833d64c4fSElen Song 
1599a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1600a930e528SElen Song {
1601a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1602a930e528SElen Song 
160334df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
160434df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
160534df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
160634df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
160734df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1608a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1609a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1610a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1611a930e528SElen Song 	} else {
1612a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1613a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1614a930e528SElen Song 		atmel_port->release_rx = NULL;
1615a930e528SElen Song 	}
1616a930e528SElen Song 
161708f738beSElen Song 	if (atmel_use_dma_tx(port)) {
161808f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
161908f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
162008f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
162108f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1622a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1623a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1624a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1625a930e528SElen Song 	} else {
1626a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1627a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1628a930e528SElen Song 		atmel_port->release_tx = NULL;
1629a930e528SElen Song 	}
1630a930e528SElen Song }
1631a930e528SElen Song 
1632ab4382d2SGreg Kroah-Hartman /*
1633055560b0SElen Song  * Get ip name usart or uart
1634055560b0SElen Song  */
1635892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1636055560b0SElen Song {
1637055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1638055560b0SElen Song 	int name = UART_GET_IP_NAME(port);
1639731d9caeSNicolas Ferre 	u32 version;
1640055560b0SElen Song 	int usart, uart;
1641055560b0SElen Song 	/* usart and uart ascii */
1642055560b0SElen Song 	usart = 0x55534152;
1643055560b0SElen Song 	uart = 0x44424755;
1644055560b0SElen Song 
1645055560b0SElen Song 	atmel_port->is_usart = false;
1646055560b0SElen Song 
1647055560b0SElen Song 	if (name == usart) {
1648055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1649055560b0SElen Song 		atmel_port->is_usart = true;
1650055560b0SElen Song 	} else if (name == uart) {
1651055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1652055560b0SElen Song 		atmel_port->is_usart = false;
1653055560b0SElen Song 	} else {
1654731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
1655731d9caeSNicolas Ferre 		version = UART_GET_IP_VERSION(port);
1656731d9caeSNicolas Ferre 		switch (version) {
1657731d9caeSNicolas Ferre 		case 0x302:
1658731d9caeSNicolas Ferre 		case 0x10213:
1659731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1660731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1661731d9caeSNicolas Ferre 			break;
1662731d9caeSNicolas Ferre 		case 0x203:
1663731d9caeSNicolas Ferre 		case 0x10202:
1664731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1665731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1666731d9caeSNicolas Ferre 			break;
1667731d9caeSNicolas Ferre 		default:
1668731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1669731d9caeSNicolas Ferre 		}
1670055560b0SElen Song 	}
1671055560b0SElen Song }
1672055560b0SElen Song 
1673ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port)
1674ab5e4e41SRichard Genoud {
1675ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1676ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1677ab5e4e41SRichard Genoud 
1678ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++)
1679ab5e4e41SRichard Genoud 		if (atmel_port->gpio_irq[i] >= 0)
1680ab5e4e41SRichard Genoud 			free_irq(atmel_port->gpio_irq[i], port);
1681ab5e4e41SRichard Genoud }
1682ab5e4e41SRichard Genoud 
1683ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port)
1684ab5e4e41SRichard Genoud {
1685ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1686ab5e4e41SRichard Genoud 	int *irq = atmel_port->gpio_irq;
1687ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1688ab5e4e41SRichard Genoud 	int err = 0;
1689ab5e4e41SRichard Genoud 
1690ab5e4e41SRichard Genoud 	for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1691ab5e4e41SRichard Genoud 		if (irq[i] < 0)
1692ab5e4e41SRichard Genoud 			continue;
1693ab5e4e41SRichard Genoud 
1694ab5e4e41SRichard Genoud 		irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1695ab5e4e41SRichard Genoud 		err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1696ab5e4e41SRichard Genoud 				  "atmel_serial", port);
1697ab5e4e41SRichard Genoud 		if (err)
1698ab5e4e41SRichard Genoud 			dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1699ab5e4e41SRichard Genoud 				irq[i]);
1700ab5e4e41SRichard Genoud 	}
1701ab5e4e41SRichard Genoud 
1702ab5e4e41SRichard Genoud 	/*
1703ab5e4e41SRichard Genoud 	 * If something went wrong, rollback.
1704ab5e4e41SRichard Genoud 	 */
1705ab5e4e41SRichard Genoud 	while (err && (--i >= 0))
1706ab5e4e41SRichard Genoud 		if (irq[i] >= 0)
1707ab5e4e41SRichard Genoud 			free_irq(irq[i], port);
1708ab5e4e41SRichard Genoud 
1709ab5e4e41SRichard Genoud 	return err;
1710ab5e4e41SRichard Genoud }
1711ab5e4e41SRichard Genoud 
1712055560b0SElen Song /*
1713ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1714ab4382d2SGreg Kroah-Hartman  */
1715ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1716ab4382d2SGreg Kroah-Hartman {
171733d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1718ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1719ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1720ab4382d2SGreg Kroah-Hartman 	int retval;
1721ab4382d2SGreg Kroah-Hartman 
1722ab4382d2SGreg Kroah-Hartman 	/*
1723ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1724ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1725ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1726ab4382d2SGreg Kroah-Hartman 	 */
1727ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1728ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1729ab4382d2SGreg Kroah-Hartman 
1730ab4382d2SGreg Kroah-Hartman 	/*
1731ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1732ab4382d2SGreg Kroah-Hartman 	 */
1733ab4382d2SGreg Kroah-Hartman 	retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
1734ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1735ab4382d2SGreg Kroah-Hartman 	if (retval) {
1736ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1737ab4382d2SGreg Kroah-Hartman 		return retval;
1738ab4382d2SGreg Kroah-Hartman 	}
1739ab4382d2SGreg Kroah-Hartman 
1740ab4382d2SGreg Kroah-Hartman 	/*
1741ab5e4e41SRichard Genoud 	 * Get the GPIO lines IRQ
1742ab5e4e41SRichard Genoud 	 */
1743ab5e4e41SRichard Genoud 	retval = atmel_request_gpio_irq(port);
1744ab5e4e41SRichard Genoud 	if (retval)
1745ab5e4e41SRichard Genoud 		goto free_irq;
1746ab5e4e41SRichard Genoud 
1747ab5e4e41SRichard Genoud 	/*
1748ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1749ab4382d2SGreg Kroah-Hartman 	 */
175033d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
175133d64c4fSElen Song 
1752a930e528SElen Song 	if (atmel_port->prepare_rx) {
1753a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1754a930e528SElen Song 		if (retval < 0)
1755a930e528SElen Song 			atmel_set_ops(port);
1756ab4382d2SGreg Kroah-Hartman 	}
1757ab4382d2SGreg Kroah-Hartman 
1758a930e528SElen Song 	if (atmel_port->prepare_tx) {
1759a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1760a930e528SElen Song 		if (retval < 0)
1761a930e528SElen Song 			atmel_set_ops(port);
1762ab4382d2SGreg Kroah-Hartman 	}
1763ab4382d2SGreg Kroah-Hartman 
1764ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1765e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1766ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1767ab4382d2SGreg Kroah-Hartman 
1768ab4382d2SGreg Kroah-Hartman 	/*
1769ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1770ab4382d2SGreg Kroah-Hartman 	 */
1771ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1772ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
1773ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1774ab4382d2SGreg Kroah-Hartman 
17752e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
17762e68c22fSElen Song 			atmel_uart_timer_callback,
17772e68c22fSElen Song 			(unsigned long)port);
17788bc661bfSMarek Roszko 
17798bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
17808bc661bfSMarek Roszko 		/* set UART timeout */
17818bc661bfSMarek Roszko 		if (!atmel_port->is_usart) {
17822e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
17832e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
17842e68c22fSElen Song 		/* set USART timeout */
17852e68c22fSElen Song 		} else {
1786ab4382d2SGreg Kroah-Hartman 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1787ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_STTTO);
1788ab4382d2SGreg Kroah-Hartman 
1789ab4382d2SGreg Kroah-Hartman 			UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
17902e68c22fSElen Song 		}
1791ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
1792ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
179334df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
17942e68c22fSElen Song 		/* set UART timeout */
17952e68c22fSElen Song 		if (!atmel_port->is_usart) {
17962e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
17972e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
17982e68c22fSElen Song 		/* set USART timeout */
17992e68c22fSElen Song 		} else {
180034df42f5SElen Song 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
180134df42f5SElen Song 			UART_PUT_CR(port, ATMEL_US_STTTO);
180234df42f5SElen Song 
180334df42f5SElen Song 			UART_PUT_IER(port, ATMEL_US_TIMEOUT);
18042e68c22fSElen Song 		}
1805ab4382d2SGreg Kroah-Hartman 	} else {
1806ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
1807ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
1808ab4382d2SGreg Kroah-Hartman 	}
1809ab4382d2SGreg Kroah-Hartman 
1810ab4382d2SGreg Kroah-Hartman 	return 0;
1811ab5e4e41SRichard Genoud 
1812ab5e4e41SRichard Genoud free_irq:
1813ab5e4e41SRichard Genoud 	free_irq(port->irq, port);
1814ab5e4e41SRichard Genoud 
1815ab5e4e41SRichard Genoud 	return retval;
1816ab4382d2SGreg Kroah-Hartman }
1817ab4382d2SGreg Kroah-Hartman 
1818ab4382d2SGreg Kroah-Hartman /*
1819479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1820479e9b94SPeter Hurley  * buffer is reset.
1821479e9b94SPeter Hurley  */
1822479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1823479e9b94SPeter Hurley {
1824479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1825479e9b94SPeter Hurley 
1826479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
1827479e9b94SPeter Hurley 		UART_PUT_TCR(port, 0);
1828479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1829479e9b94SPeter Hurley 	}
1830479e9b94SPeter Hurley }
1831479e9b94SPeter Hurley 
1832479e9b94SPeter Hurley /*
1833ab4382d2SGreg Kroah-Hartman  * Disable the port
1834ab4382d2SGreg Kroah-Hartman  */
1835ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1836ab4382d2SGreg Kroah-Hartman {
1837ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
18380cc7c6c7SMarek Roszko 
1839ab4382d2SGreg Kroah-Hartman 	/*
18408bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
18418bc661bfSMarek Roszko 	 * cleanup
18428bc661bfSMarek Roszko 	 */
18438bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
18448bc661bfSMarek Roszko 
18458bc661bfSMarek Roszko 	/*
18460cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
18470cc7c6c7SMarek Roszko 	 * we destroy the buffers
18480cc7c6c7SMarek Roszko 	 */
18490cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
18500cc7c6c7SMarek Roszko 
18510cc7c6c7SMarek Roszko 	/*
18520cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
18530cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1854ab4382d2SGreg Kroah-Hartman 	 */
1855ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1856ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1857ab4382d2SGreg Kroah-Hartman 
18580cc7c6c7SMarek Roszko 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
18590cc7c6c7SMarek Roszko 	UART_PUT_IDR(port, -1);
18600cc7c6c7SMarek Roszko 
18610cc7c6c7SMarek Roszko 
1862ab4382d2SGreg Kroah-Hartman 	/*
1863ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1864ab4382d2SGreg Kroah-Hartman 	 */
1865a930e528SElen Song 	if (atmel_port->release_rx)
1866a930e528SElen Song 		atmel_port->release_rx(port);
1867a930e528SElen Song 	if (atmel_port->release_tx)
1868a930e528SElen Song 		atmel_port->release_tx(port);
1869ab4382d2SGreg Kroah-Hartman 
1870ab4382d2SGreg Kroah-Hartman 	/*
1871bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1872bb7e73c5SMark Deneen 	 */
1873bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1874bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1875bb7e73c5SMark Deneen 
1876bb7e73c5SMark Deneen 	/*
1877ab5e4e41SRichard Genoud 	 * Free the interrupts
1878ab4382d2SGreg Kroah-Hartman 	 */
1879ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1880ab5e4e41SRichard Genoud 	atmel_free_gpio_irq(port);
1881ab5e4e41SRichard Genoud 
1882ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1883ab4382d2SGreg Kroah-Hartman 
1884479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1885ab4382d2SGreg Kroah-Hartman }
1886ab4382d2SGreg Kroah-Hartman 
1887ab4382d2SGreg Kroah-Hartman /*
1888ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1889ab4382d2SGreg Kroah-Hartman  */
1890ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1891ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1892ab4382d2SGreg Kroah-Hartman {
1893ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1894ab4382d2SGreg Kroah-Hartman 
1895ab4382d2SGreg Kroah-Hartman 	switch (state) {
1896ab4382d2SGreg Kroah-Hartman 	case 0:
1897ab4382d2SGreg Kroah-Hartman 		/*
1898ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1899ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1900ab4382d2SGreg Kroah-Hartman 		 */
190191f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1902ab4382d2SGreg Kroah-Hartman 
1903ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1904ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->backup_imr);
1905ab4382d2SGreg Kroah-Hartman 		break;
1906ab4382d2SGreg Kroah-Hartman 	case 3:
1907ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1908ab4382d2SGreg Kroah-Hartman 		atmel_port->backup_imr = UART_GET_IMR(port);
1909ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, -1);
1910ab4382d2SGreg Kroah-Hartman 
1911ab4382d2SGreg Kroah-Hartman 		/*
1912ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1913ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1914ab4382d2SGreg Kroah-Hartman 		 */
191591f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1916ab4382d2SGreg Kroah-Hartman 		break;
1917ab4382d2SGreg Kroah-Hartman 	default:
1918ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1919ab4382d2SGreg Kroah-Hartman 	}
1920ab4382d2SGreg Kroah-Hartman }
1921ab4382d2SGreg Kroah-Hartman 
1922ab4382d2SGreg Kroah-Hartman /*
1923ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1924ab4382d2SGreg Kroah-Hartman  */
1925ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1926ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1927ab4382d2SGreg Kroah-Hartman {
1928ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1929ab4382d2SGreg Kroah-Hartman 	unsigned int mode, imr, quot, baud;
1930ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1931ab4382d2SGreg Kroah-Hartman 
1932ab4382d2SGreg Kroah-Hartman 	/* Get current mode register */
1933ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1934ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_NBSTOP | ATMEL_US_PAR
1935ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_USMODE);
1936ab4382d2SGreg Kroah-Hartman 
1937ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1938ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1939ab4382d2SGreg Kroah-Hartman 
1940ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1941ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1942ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1943ab4382d2SGreg Kroah-Hartman 	}
1944ab4382d2SGreg Kroah-Hartman 
1945ab4382d2SGreg Kroah-Hartman 	/* byte size */
1946ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1947ab4382d2SGreg Kroah-Hartman 	case CS5:
1948ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1949ab4382d2SGreg Kroah-Hartman 		break;
1950ab4382d2SGreg Kroah-Hartman 	case CS6:
1951ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1952ab4382d2SGreg Kroah-Hartman 		break;
1953ab4382d2SGreg Kroah-Hartman 	case CS7:
1954ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1955ab4382d2SGreg Kroah-Hartman 		break;
1956ab4382d2SGreg Kroah-Hartman 	default:
1957ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1958ab4382d2SGreg Kroah-Hartman 		break;
1959ab4382d2SGreg Kroah-Hartman 	}
1960ab4382d2SGreg Kroah-Hartman 
1961ab4382d2SGreg Kroah-Hartman 	/* stop bits */
1962ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
1963ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
1964ab4382d2SGreg Kroah-Hartman 
1965ab4382d2SGreg Kroah-Hartman 	/* parity */
1966ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
1967ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
1968ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
1969ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
1970ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
1971ab4382d2SGreg Kroah-Hartman 			else
1972ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
1973ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
1974ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
1975ab4382d2SGreg Kroah-Hartman 		else
1976ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
1977ab4382d2SGreg Kroah-Hartman 	} else
1978ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
1979ab4382d2SGreg Kroah-Hartman 
1980ab4382d2SGreg Kroah-Hartman 	/* hardware handshake (RTS/CTS) */
1981ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
1982ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_HWHS;
1983ab4382d2SGreg Kroah-Hartman 	else
1984ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_NORMAL;
1985ab4382d2SGreg Kroah-Hartman 
1986ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
1987ab4382d2SGreg Kroah-Hartman 
1988ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
1989ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
1990ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1991ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
1992ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
1993ab4382d2SGreg Kroah-Hartman 
199464e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
1995ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
1996ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, port->read_status_mask);
1997ab4382d2SGreg Kroah-Hartman 
1998ab4382d2SGreg Kroah-Hartman 	/*
1999ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2000ab4382d2SGreg Kroah-Hartman 	 */
2001ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2002ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2003ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2004ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2005ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2006ab4382d2SGreg Kroah-Hartman 		/*
2007ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2008ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2009ab4382d2SGreg Kroah-Hartman 		 */
2010ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2011ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2012ab4382d2SGreg Kroah-Hartman 	}
2013ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2014ab4382d2SGreg Kroah-Hartman 
2015ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2016ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2017ab4382d2SGreg Kroah-Hartman 
2018ab4382d2SGreg Kroah-Hartman 	/*
2019ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2020ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2021ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2022ab4382d2SGreg Kroah-Hartman 	 */
2023ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
2024ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
2025ab4382d2SGreg Kroah-Hartman 
2026ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
2027ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2028ab4382d2SGreg Kroah-Hartman 
2029ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
2030ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
2031ab4382d2SGreg Kroah-Hartman 
2032ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
203393f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
2034ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
2035ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
2036ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
2037ab4382d2SGreg Kroah-Hartman 	}
2038ab4382d2SGreg Kroah-Hartman 
2039ab4382d2SGreg Kroah-Hartman 	/* set the parity, stop bits and data size */
2040ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
2041ab4382d2SGreg Kroah-Hartman 
2042ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
2043ab4382d2SGreg Kroah-Hartman 	UART_PUT_BRGR(port, quot);
2044ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2045ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2046ab4382d2SGreg Kroah-Hartman 
2047ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
2048ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
2049ab4382d2SGreg Kroah-Hartman 
2050ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2051ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
205235b675b9SRichard Genoud 		atmel_enable_ms(port);
205335b675b9SRichard Genoud 	else
205435b675b9SRichard Genoud 		atmel_disable_ms(port);
2055ab4382d2SGreg Kroah-Hartman 
2056ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2057ab4382d2SGreg Kroah-Hartman }
2058ab4382d2SGreg Kroah-Hartman 
2059*732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
206042bd7a4fSViktar Palstsiuk {
2061*732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
206242bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
206342bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
206442bd7a4fSViktar Palstsiuk 	} else {
206542bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
206642bd7a4fSViktar Palstsiuk 	}
206742bd7a4fSViktar Palstsiuk }
206842bd7a4fSViktar Palstsiuk 
2069ab4382d2SGreg Kroah-Hartman /*
2070ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2071ab4382d2SGreg Kroah-Hartman  */
2072ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2073ab4382d2SGreg Kroah-Hartman {
2074ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2075ab4382d2SGreg Kroah-Hartman }
2076ab4382d2SGreg Kroah-Hartman 
2077ab4382d2SGreg Kroah-Hartman /*
2078ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2079ab4382d2SGreg Kroah-Hartman  */
2080ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2081ab4382d2SGreg Kroah-Hartman {
2082ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2083ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2084ab4382d2SGreg Kroah-Hartman 
2085ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2086ab4382d2SGreg Kroah-Hartman 
2087ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2088ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2089ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2090ab4382d2SGreg Kroah-Hartman 	}
2091ab4382d2SGreg Kroah-Hartman }
2092ab4382d2SGreg Kroah-Hartman 
2093ab4382d2SGreg Kroah-Hartman /*
2094ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2095ab4382d2SGreg Kroah-Hartman  */
2096ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2097ab4382d2SGreg Kroah-Hartman {
2098ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2099ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2100ab4382d2SGreg Kroah-Hartman 
2101ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2102ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2103ab4382d2SGreg Kroah-Hartman 
2104ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2105ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2106ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2107ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2108ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2109ab4382d2SGreg Kroah-Hartman 		}
2110ab4382d2SGreg Kroah-Hartman 	}
2111ab4382d2SGreg Kroah-Hartman 
2112ab4382d2SGreg Kroah-Hartman 	return 0;
2113ab4382d2SGreg Kroah-Hartman }
2114ab4382d2SGreg Kroah-Hartman 
2115ab4382d2SGreg Kroah-Hartman /*
2116ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2117ab4382d2SGreg Kroah-Hartman  */
2118ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2119ab4382d2SGreg Kroah-Hartman {
2120ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2121ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2122ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2123ab4382d2SGreg Kroah-Hartman 	}
2124ab4382d2SGreg Kroah-Hartman }
2125ab4382d2SGreg Kroah-Hartman 
2126ab4382d2SGreg Kroah-Hartman /*
2127ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2128ab4382d2SGreg Kroah-Hartman  */
2129ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2130ab4382d2SGreg Kroah-Hartman {
2131ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2132ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2133ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2134ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2135ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2136ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2137ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2138ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2139ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2140ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
2141ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2142ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2143ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2144ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2145ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2146ab4382d2SGreg Kroah-Hartman 	return ret;
2147ab4382d2SGreg Kroah-Hartman }
2148ab4382d2SGreg Kroah-Hartman 
2149ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2150ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2151ab4382d2SGreg Kroah-Hartman {
2152ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
2153ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2154ab4382d2SGreg Kroah-Hartman 
2155ab4382d2SGreg Kroah-Hartman 	return UART_GET_CHAR(port);
2156ab4382d2SGreg Kroah-Hartman }
2157ab4382d2SGreg Kroah-Hartman 
2158ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2159ab4382d2SGreg Kroah-Hartman {
2160ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2161ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2162ab4382d2SGreg Kroah-Hartman 
2163ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
2164ab4382d2SGreg Kroah-Hartman }
2165ab4382d2SGreg Kroah-Hartman #endif
2166ab4382d2SGreg Kroah-Hartman 
2167ab4382d2SGreg Kroah-Hartman static int
2168ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
2169ab4382d2SGreg Kroah-Hartman {
2170ab4382d2SGreg Kroah-Hartman 	struct serial_rs485 rs485conf;
2171ab4382d2SGreg Kroah-Hartman 
2172ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
2173ab4382d2SGreg Kroah-Hartman 	case TIOCSRS485:
2174ab4382d2SGreg Kroah-Hartman 		if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
2175ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
2176ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
2177ab4382d2SGreg Kroah-Hartman 
2178ab4382d2SGreg Kroah-Hartman 		atmel_config_rs485(port, &rs485conf);
2179ab4382d2SGreg Kroah-Hartman 		break;
2180ab4382d2SGreg Kroah-Hartman 
2181ab4382d2SGreg Kroah-Hartman 	case TIOCGRS485:
2182ab4382d2SGreg Kroah-Hartman 		if (copy_to_user((struct serial_rs485 *) arg,
2183ab4382d2SGreg Kroah-Hartman 					&(to_atmel_uart_port(port)->rs485),
2184ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
2185ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
2186ab4382d2SGreg Kroah-Hartman 		break;
2187ab4382d2SGreg Kroah-Hartman 
2188ab4382d2SGreg Kroah-Hartman 	default:
2189ab4382d2SGreg Kroah-Hartman 		return -ENOIOCTLCMD;
2190ab4382d2SGreg Kroah-Hartman 	}
2191ab4382d2SGreg Kroah-Hartman 	return 0;
2192ab4382d2SGreg Kroah-Hartman }
2193ab4382d2SGreg Kroah-Hartman 
2194ab4382d2SGreg Kroah-Hartman 
2195ab4382d2SGreg Kroah-Hartman 
2196ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2197ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2198ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2199ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2200ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2201ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2202ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2203ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2204ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2205ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2206ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2207ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2208ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
220942bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2210ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2211ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2212ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2213ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2214ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2215ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2216ab4382d2SGreg Kroah-Hartman 	.ioctl		= atmel_ioctl,
2217ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2218ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2219ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2220ab4382d2SGreg Kroah-Hartman #endif
2221ab4382d2SGreg Kroah-Hartman };
2222ab4382d2SGreg Kroah-Hartman 
2223ab4382d2SGreg Kroah-Hartman /*
2224ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2225ab4382d2SGreg Kroah-Hartman  */
222691f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2227ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2228ab4382d2SGreg Kroah-Hartman {
222991f8c2d8SBoris BREZILLON 	int ret;
2230ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2231574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2232ab4382d2SGreg Kroah-Hartman 
223333d64c4fSElen Song 	if (!atmel_init_property(atmel_port, pdev))
2234a930e528SElen Song 		atmel_set_ops(port);
2235a930e528SElen Song 
223633d64c4fSElen Song 	atmel_init_rs485(atmel_port, pdev);
223733d64c4fSElen Song 
2238ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2239ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2240ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2241ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2242ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2243ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2244ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
2245ab4382d2SGreg Kroah-Hartman 
2246ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2247ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
2248ab4382d2SGreg Kroah-Hartman 
2249ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2250ab4382d2SGreg Kroah-Hartman 
22515fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2252ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
22531acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2254588edbf3SNicolas Ferre 	} else {
2255ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2256ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2257ab4382d2SGreg Kroah-Hartman 	}
2258ab4382d2SGreg Kroah-Hartman 
2259ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2260ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2261ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
226291f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
226391f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
226491f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
226591f8c2d8SBoris BREZILLON 			return ret;
226691f8c2d8SBoris BREZILLON 		}
226791f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
226891f8c2d8SBoris BREZILLON 		if (ret) {
226991f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
227091f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
227191f8c2d8SBoris BREZILLON 			return ret;
227291f8c2d8SBoris BREZILLON 		}
2273ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
227491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2275ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2276ab4382d2SGreg Kroah-Hartman 	}
2277ab4382d2SGreg Kroah-Hartman 
2278ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2279ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2280ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
228164e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2282ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2283ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2284ab4382d2SGreg Kroah-Hartman 	} else {
2285ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2286ab4382d2SGreg Kroah-Hartman 	}
228791f8c2d8SBoris BREZILLON 
228891f8c2d8SBoris BREZILLON 	return 0;
2289ab4382d2SGreg Kroah-Hartman }
2290ab4382d2SGreg Kroah-Hartman 
229169f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
229269f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2293ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2294ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2295ab4382d2SGreg Kroah-Hartman {
2296ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2297ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2298ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
2299ab4382d2SGreg Kroah-Hartman }
2300ab4382d2SGreg Kroah-Hartman 
2301ab4382d2SGreg Kroah-Hartman /*
2302ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2303ab4382d2SGreg Kroah-Hartman  */
2304ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2305ab4382d2SGreg Kroah-Hartman {
2306ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2307ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2308ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2309ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2310ab4382d2SGreg Kroah-Hartman 
2311ab4382d2SGreg Kroah-Hartman 	/*
2312ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2313ab4382d2SGreg Kroah-Hartman 	 */
2314ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
2315ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2316ab4382d2SGreg Kroah-Hartman 
2317ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
2318ab4382d2SGreg Kroah-Hartman 	pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2319ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2320ab4382d2SGreg Kroah-Hartman 
2321ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2322ab4382d2SGreg Kroah-Hartman 
2323ab4382d2SGreg Kroah-Hartman 	/*
2324ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2325ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2326ab4382d2SGreg Kroah-Hartman 	 */
2327ab4382d2SGreg Kroah-Hartman 	do {
2328ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
2329ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2330ab4382d2SGreg Kroah-Hartman 
2331ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2332ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
2333ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2334ab4382d2SGreg Kroah-Hartman 
2335ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
2336ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
2337ab4382d2SGreg Kroah-Hartman }
2338ab4382d2SGreg Kroah-Hartman 
2339ab4382d2SGreg Kroah-Hartman /*
2340ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2341ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2342ab4382d2SGreg Kroah-Hartman  */
2343ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2344ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2345ab4382d2SGreg Kroah-Hartman {
2346ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2347ab4382d2SGreg Kroah-Hartman 
2348ab4382d2SGreg Kroah-Hartman 	/*
2349ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2350ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2351ab4382d2SGreg Kroah-Hartman 	 */
2352ab4382d2SGreg Kroah-Hartman 	quot = UART_GET_BRGR(port) & ATMEL_US_CD;
2353ab4382d2SGreg Kroah-Hartman 	if (!quot)
2354ab4382d2SGreg Kroah-Hartman 		return;
2355ab4382d2SGreg Kroah-Hartman 
2356ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2357ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2358ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2359ab4382d2SGreg Kroah-Hartman 	else
2360ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2361ab4382d2SGreg Kroah-Hartman 
2362ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_PAR;
2363ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2364ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2365ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2366ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2367ab4382d2SGreg Kroah-Hartman 
2368ab4382d2SGreg Kroah-Hartman 	/*
2369ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2370ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2371ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2372ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2373ab4382d2SGreg Kroah-Hartman 	 */
2374ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2375ab4382d2SGreg Kroah-Hartman }
2376ab4382d2SGreg Kroah-Hartman 
2377ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2378ab4382d2SGreg Kroah-Hartman {
237991f8c2d8SBoris BREZILLON 	int ret;
2380ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2381ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2382ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2383ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2384ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2385ab4382d2SGreg Kroah-Hartman 
2386ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2387ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2388ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2389ab4382d2SGreg Kroah-Hartman 	}
2390ab4382d2SGreg Kroah-Hartman 
239191f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
239291f8c2d8SBoris BREZILLON 	if (ret)
239391f8c2d8SBoris BREZILLON 		return ret;
2394ab4382d2SGreg Kroah-Hartman 
2395ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
2396ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2397ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2398ab4382d2SGreg Kroah-Hartman 
2399ab4382d2SGreg Kroah-Hartman 	if (options)
2400ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2401ab4382d2SGreg Kroah-Hartman 	else
2402ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2403ab4382d2SGreg Kroah-Hartman 
2404ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2405ab4382d2SGreg Kroah-Hartman }
2406ab4382d2SGreg Kroah-Hartman 
2407ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2408ab4382d2SGreg Kroah-Hartman 
2409ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2410ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2411ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2412ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2413ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2414ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2415ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2416ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2417ab4382d2SGreg Kroah-Hartman };
2418ab4382d2SGreg Kroah-Hartman 
2419ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2420ab4382d2SGreg Kroah-Hartman 
2421ab4382d2SGreg Kroah-Hartman /*
2422ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2423ab4382d2SGreg Kroah-Hartman  */
2424ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2425ab4382d2SGreg Kroah-Hartman {
242691f8c2d8SBoris BREZILLON 	int ret;
2427ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
24280d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2429574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2430efb8d21bSLinus Torvalds 		int id = pdata->num;
24314cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
24320d0a3cc1SVoss, Nikolaus 
24334cbf9f48SNicolas Ferre 		port->backup_imr = 0;
24344cbf9f48SNicolas Ferre 		port->uart.line = id;
24354cbf9f48SNicolas Ferre 
24364cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
243791f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
243891f8c2d8SBoris BREZILLON 		if (ret)
243991f8c2d8SBoris BREZILLON 			return ret;
2440ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2441ab4382d2SGreg Kroah-Hartman 	}
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman 	return 0;
2444ab4382d2SGreg Kroah-Hartman }
2445ab4382d2SGreg Kroah-Hartman 
2446ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2447ab4382d2SGreg Kroah-Hartman 
2448ab4382d2SGreg Kroah-Hartman /*
2449ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2450ab4382d2SGreg Kroah-Hartman  */
2451ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2452ab4382d2SGreg Kroah-Hartman {
2453ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2454ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2455ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2456ab4382d2SGreg Kroah-Hartman 
2457ab4382d2SGreg Kroah-Hartman 	return 0;
2458ab4382d2SGreg Kroah-Hartman }
2459ab4382d2SGreg Kroah-Hartman 
2460ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2461ab4382d2SGreg Kroah-Hartman 
2462ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2463ab4382d2SGreg Kroah-Hartman {
2464ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2465ab4382d2SGreg Kroah-Hartman }
2466ab4382d2SGreg Kroah-Hartman 
2467ab4382d2SGreg Kroah-Hartman #else
2468ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2469ab4382d2SGreg Kroah-Hartman 
2470ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2471ab4382d2SGreg Kroah-Hartman {
2472ab4382d2SGreg Kroah-Hartman 	return false;
2473ab4382d2SGreg Kroah-Hartman }
2474ab4382d2SGreg Kroah-Hartman #endif
2475ab4382d2SGreg Kroah-Hartman 
2476ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2477ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2478ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2479ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2480ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2481ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2482ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2483ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2484ab4382d2SGreg Kroah-Hartman };
2485ab4382d2SGreg Kroah-Hartman 
2486ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2487ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2488ab4382d2SGreg Kroah-Hartman {
2489ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2490ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2491ab4382d2SGreg Kroah-Hartman #else
2492ab4382d2SGreg Kroah-Hartman 	return false;
2493ab4382d2SGreg Kroah-Hartman #endif
2494ab4382d2SGreg Kroah-Hartman }
2495ab4382d2SGreg Kroah-Hartman 
2496ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2497ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2498ab4382d2SGreg Kroah-Hartman {
2499ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2500ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2501ab4382d2SGreg Kroah-Hartman 
2502ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2503ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
2504ab4382d2SGreg Kroah-Hartman 		while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2505ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2506ab4382d2SGreg Kroah-Hartman 	}
2507ab4382d2SGreg Kroah-Hartman 
2508ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2509ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2510ab4382d2SGreg Kroah-Hartman 	if (atmel_serial_clk_will_stop())
2511ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
2512ab4382d2SGreg Kroah-Hartman 
2513ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2514ab4382d2SGreg Kroah-Hartman 
2515ab4382d2SGreg Kroah-Hartman 	return 0;
2516ab4382d2SGreg Kroah-Hartman }
2517ab4382d2SGreg Kroah-Hartman 
2518ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2519ab4382d2SGreg Kroah-Hartman {
2520ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2521ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2522ab4382d2SGreg Kroah-Hartman 
2523ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2524ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2525ab4382d2SGreg Kroah-Hartman 
2526ab4382d2SGreg Kroah-Hartman 	return 0;
2527ab4382d2SGreg Kroah-Hartman }
2528ab4382d2SGreg Kroah-Hartman #else
2529ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2530ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2531ab4382d2SGreg Kroah-Hartman #endif
2532ab4382d2SGreg Kroah-Hartman 
2533e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2534e0b0baadSRichard Genoud {
2535ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
2536ab5e4e41SRichard Genoud 	struct gpio_desc *gpiod;
2537ab5e4e41SRichard Genoud 
2538e0b0baadSRichard Genoud 	p->gpios = mctrl_gpio_init(dev, 0);
2539e0b0baadSRichard Genoud 	if (IS_ERR_OR_NULL(p->gpios))
2540e0b0baadSRichard Genoud 		return -1;
2541e0b0baadSRichard Genoud 
2542ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++) {
2543ab5e4e41SRichard Genoud 		gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2544ab5e4e41SRichard Genoud 		if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2545ab5e4e41SRichard Genoud 			p->gpio_irq[i] = gpiod_to_irq(gpiod);
2546ab5e4e41SRichard Genoud 		else
2547ab5e4e41SRichard Genoud 			p->gpio_irq[i] = -EINVAL;
2548ab5e4e41SRichard Genoud 	}
2549ab5e4e41SRichard Genoud 
2550e0b0baadSRichard Genoud 	return 0;
2551e0b0baadSRichard Genoud }
2552e0b0baadSRichard Genoud 
25539671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2554ab4382d2SGreg Kroah-Hartman {
2555ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
25565fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2557574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2558ab4382d2SGreg Kroah-Hartman 	void *data;
25594cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2560ab4382d2SGreg Kroah-Hartman 
2561ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2562ab4382d2SGreg Kroah-Hartman 
25635fbe46b6SNicolas Ferre 	if (np)
25645fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
25655fbe46b6SNicolas Ferre 	else
25664cbf9f48SNicolas Ferre 		if (pdata)
25674cbf9f48SNicolas Ferre 			ret = pdata->num;
25684cbf9f48SNicolas Ferre 
25694cbf9f48SNicolas Ferre 	if (ret < 0)
25705fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
25714cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2572503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
25734cbf9f48SNicolas Ferre 
2574503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
25754cbf9f48SNicolas Ferre 		ret = -ENODEV;
25764cbf9f48SNicolas Ferre 		goto err;
25774cbf9f48SNicolas Ferre 	}
25784cbf9f48SNicolas Ferre 
2579503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
25804cbf9f48SNicolas Ferre 		/* port already in use */
25814cbf9f48SNicolas Ferre 		ret = -EBUSY;
25824cbf9f48SNicolas Ferre 		goto err;
25834cbf9f48SNicolas Ferre 	}
25844cbf9f48SNicolas Ferre 
25854cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2586ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
25874cbf9f48SNicolas Ferre 	port->uart.line = ret;
2588354e57f3SLinus Walleij 
2589e0b0baadSRichard Genoud 	ret = atmel_init_gpios(port, &pdev->dev);
2590e0b0baadSRichard Genoud 	if (ret < 0)
2591e0b0baadSRichard Genoud 		dev_err(&pdev->dev, "%s",
2592e0b0baadSRichard Genoud 			"Failed to initialize GPIOs. The serial port may not work as expected");
2593ab4382d2SGreg Kroah-Hartman 
259491f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
259591f8c2d8SBoris BREZILLON 	if (ret)
259691f8c2d8SBoris BREZILLON 		goto err;
2597ab4382d2SGreg Kroah-Hartman 
259864e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2599ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2600ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2601ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2602ab4382d2SGreg Kroah-Hartman 		if (!data)
2603ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2604ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2605ab4382d2SGreg Kroah-Hartman 	}
2606ab4382d2SGreg Kroah-Hartman 
2607ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2608ab4382d2SGreg Kroah-Hartman 	if (ret)
2609ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2610ab4382d2SGreg Kroah-Hartman 
2611ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2612ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2613ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2614ab4382d2SGreg Kroah-Hartman 		/*
2615ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
261691f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2617ab4382d2SGreg Kroah-Hartman 		 */
261891f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2619ab4382d2SGreg Kroah-Hartman 	}
2620ab4382d2SGreg Kroah-Hartman #endif
2621ab4382d2SGreg Kroah-Hartman 
2622ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2623ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2624ab4382d2SGreg Kroah-Hartman 
2625fc887b15SLinus Torvalds 	if (port->rs485.flags & SER_RS485_ENABLED) {
2626fc887b15SLinus Torvalds 		UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2627fc887b15SLinus Torvalds 		UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2628fc887b15SLinus Torvalds 	}
2629fc887b15SLinus Torvalds 
2630055560b0SElen Song 	/*
2631055560b0SElen Song 	 * Get port name of usart or uart
2632055560b0SElen Song 	 */
2633892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2634055560b0SElen Song 
2635ab4382d2SGreg Kroah-Hartman 	return 0;
2636ab4382d2SGreg Kroah-Hartman 
2637ab4382d2SGreg Kroah-Hartman err_add_port:
2638ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2639ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2640ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2641ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2642ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2643ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2644ab4382d2SGreg Kroah-Hartman 	}
26454cbf9f48SNicolas Ferre err:
2646ab4382d2SGreg Kroah-Hartman 	return ret;
2647ab4382d2SGreg Kroah-Hartman }
2648ab4382d2SGreg Kroah-Hartman 
2649ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2650ab4382d2SGreg Kroah-Hartman {
2651ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2652ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2653ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2654ab4382d2SGreg Kroah-Hartman 
2655f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2656f50c995fSMarek Roszko 
2657ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2658ab4382d2SGreg Kroah-Hartman 
2659ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2660ab4382d2SGreg Kroah-Hartman 
2661ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2662ab4382d2SGreg Kroah-Hartman 
2663ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2664ab4382d2SGreg Kroah-Hartman 
2665503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
26664cbf9f48SNicolas Ferre 
2667ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2668ab4382d2SGreg Kroah-Hartman 
2669ab4382d2SGreg Kroah-Hartman 	return ret;
2670ab4382d2SGreg Kroah-Hartman }
2671ab4382d2SGreg Kroah-Hartman 
2672ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2673ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
26742d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2675ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2676ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2677ab4382d2SGreg Kroah-Hartman 	.driver		= {
2678ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
2679ab4382d2SGreg Kroah-Hartman 		.owner	= THIS_MODULE,
26805fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2681ab4382d2SGreg Kroah-Hartman 	},
2682ab4382d2SGreg Kroah-Hartman };
2683ab4382d2SGreg Kroah-Hartman 
2684ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2685ab4382d2SGreg Kroah-Hartman {
2686ab4382d2SGreg Kroah-Hartman 	int ret;
2687ab4382d2SGreg Kroah-Hartman 
2688ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2689ab4382d2SGreg Kroah-Hartman 	if (ret)
2690ab4382d2SGreg Kroah-Hartman 		return ret;
2691ab4382d2SGreg Kroah-Hartman 
2692ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2693ab4382d2SGreg Kroah-Hartman 	if (ret)
2694ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2695ab4382d2SGreg Kroah-Hartman 
2696ab4382d2SGreg Kroah-Hartman 	return ret;
2697ab4382d2SGreg Kroah-Hartman }
2698ab4382d2SGreg Kroah-Hartman 
2699ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2700ab4382d2SGreg Kroah-Hartman {
2701ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2702ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2703ab4382d2SGreg Kroah-Hartman }
2704ab4382d2SGreg Kroah-Hartman 
2705ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2706ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2707ab4382d2SGreg Kroah-Hartman 
2708ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2709ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2710ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2711ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2712