xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 4e7decdaaa67b287d6a13de8dedced68f1d7d716)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  Driver for Atmel AT91 / AT32 Serial ports
3ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7ab4382d2SGreg Kroah-Hartman  *
8ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
11ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
12ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
13ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
14ab4382d2SGreg Kroah-Hartman  *
15ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
16ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
19ab4382d2SGreg Kroah-Hartman  *
20ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
21ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
22ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23ab4382d2SGreg Kroah-Hartman  *
24ab4382d2SGreg Kroah-Hartman  */
25ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
35ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
365fbe46b6SNicolas Ferre #include <linux/of.h>
375fbe46b6SNicolas Ferre #include <linux/of_device.h>
38354e57f3SLinus Walleij #include <linux/of_gpio.h>
39ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
406b997babSVinod Koul #include <linux/dmaengine.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
43ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
44bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
452e68c22fSElen Song #include <linux/timer.h>
46354e57f3SLinus Walleij #include <linux/gpio.h>
47e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
48e0b0baadSRichard Genoud #include <linux/err.h>
49ab5e4e41SRichard Genoud #include <linux/irq.h>
502c7af5baSBoris BREZILLON #include <linux/suspend.h>
51ab4382d2SGreg Kroah-Hartman 
52ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
53ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
54ab4382d2SGreg Kroah-Hartman 
55ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
56ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
57ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
58ab4382d2SGreg Kroah-Hartman 
59ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
60ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
61ab4382d2SGreg Kroah-Hartman #endif
62ab4382d2SGreg Kroah-Hartman 
63ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
64ab4382d2SGreg Kroah-Hartman 
65e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
66e0b0baadSRichard Genoud 
67ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
68ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
69ab4382d2SGreg Kroah-Hartman 
70ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
71ab4382d2SGreg Kroah-Hartman 
72ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
73ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
74ab4382d2SGreg Kroah-Hartman  * UART. */
75ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
76ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
77ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
78ab4382d2SGreg Kroah-Hartman 
79ab4382d2SGreg Kroah-Hartman #else
80ab4382d2SGreg Kroah-Hartman 
81ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
82ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
83ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
84ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
85ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
86ab4382d2SGreg Kroah-Hartman 
87ab4382d2SGreg Kroah-Hartman #endif
88ab4382d2SGreg Kroah-Hartman 
89ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
90ab4382d2SGreg Kroah-Hartman 
91ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
92ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
93ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
94ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
95ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
96ab4382d2SGreg Kroah-Hartman };
97ab4382d2SGreg Kroah-Hartman 
98ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
99ab4382d2SGreg Kroah-Hartman 	u16		status;
100ab4382d2SGreg Kroah-Hartman 	u16		ch;
101ab4382d2SGreg Kroah-Hartman };
102ab4382d2SGreg Kroah-Hartman 
103ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
104ab4382d2SGreg Kroah-Hartman 
105ab4382d2SGreg Kroah-Hartman /*
106ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
107ab4382d2SGreg Kroah-Hartman  */
108ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
109ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
110ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
111ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
112ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
113ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
114ab4382d2SGreg Kroah-Hartman 
11534df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
11664e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
117ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
118ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
119ab4382d2SGreg Kroah-Hartman 
12008f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
12164e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
122ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
123ab4382d2SGreg Kroah-Hartman 
12408f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
12534df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
12608f738beSElen Song 	struct dma_chan			*chan_tx;
12734df42f5SElen Song 	struct dma_chan			*chan_rx;
12808f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
12934df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
13008f738beSElen Song 	dma_cookie_t			cookie_tx;
13134df42f5SElen Song 	dma_cookie_t			cookie_rx;
13208f738beSElen Song 	struct scatterlist		sg_tx;
13334df42f5SElen Song 	struct scatterlist		sg_rx;
134ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
135ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
136ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
137d033e82dSLeilei Zhao 	unsigned int		status_change;
138ab4382d2SGreg Kroah-Hartman 
139ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
140ab4382d2SGreg Kroah-Hartman 
141e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
142ab5e4e41SRichard Genoud 	int			gpio_irq[UART_GPIO_MAX];
143ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
144ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
145055560b0SElen Song 	bool			is_usart;	/* usart or uart */
1462e68c22fSElen Song 	struct timer_list	uart_timer;	/* uart timer */
1472c7af5baSBoris BREZILLON 
1482c7af5baSBoris BREZILLON 	bool			suspended;
1492c7af5baSBoris BREZILLON 	unsigned int		pending;
1502c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1512c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1522c7af5baSBoris BREZILLON 
153a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
154a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
155a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
156a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
157a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
158a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
159ab4382d2SGreg Kroah-Hartman };
160ab4382d2SGreg Kroah-Hartman 
161ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
162503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
163ab4382d2SGreg Kroah-Hartman 
164ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
165ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
166ab4382d2SGreg Kroah-Hartman #endif
167ab4382d2SGreg Kroah-Hartman 
1685fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1695fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1705fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1715fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1725fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1735fbe46b6SNicolas Ferre };
1745fbe46b6SNicolas Ferre 
1755fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
1765fbe46b6SNicolas Ferre #endif
1775fbe46b6SNicolas Ferre 
178ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
179ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
180ab4382d2SGreg Kroah-Hartman {
181ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
182ab4382d2SGreg Kroah-Hartman }
183ab4382d2SGreg Kroah-Hartman 
184*4e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
185*4e7decdaSCyrille Pitchen {
186*4e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
187*4e7decdaSCyrille Pitchen }
188*4e7decdaSCyrille Pitchen 
189*4e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
190*4e7decdaSCyrille Pitchen {
191*4e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
192*4e7decdaSCyrille Pitchen }
193*4e7decdaSCyrille Pitchen 
194ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
19564e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
196ab4382d2SGreg Kroah-Hartman {
197ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
198ab4382d2SGreg Kroah-Hartman 
19964e22ebeSElen Song 	return atmel_port->use_pdc_rx;
200ab4382d2SGreg Kroah-Hartman }
201ab4382d2SGreg Kroah-Hartman 
20264e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
203ab4382d2SGreg Kroah-Hartman {
204ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
205ab4382d2SGreg Kroah-Hartman 
20664e22ebeSElen Song 	return atmel_port->use_pdc_tx;
207ab4382d2SGreg Kroah-Hartman }
208ab4382d2SGreg Kroah-Hartman #else
20964e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
210ab4382d2SGreg Kroah-Hartman {
211ab4382d2SGreg Kroah-Hartman 	return false;
212ab4382d2SGreg Kroah-Hartman }
213ab4382d2SGreg Kroah-Hartman 
21464e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
215ab4382d2SGreg Kroah-Hartman {
216ab4382d2SGreg Kroah-Hartman 	return false;
217ab4382d2SGreg Kroah-Hartman }
218ab4382d2SGreg Kroah-Hartman #endif
219ab4382d2SGreg Kroah-Hartman 
22008f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
22108f738beSElen Song {
22208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
22308f738beSElen Song 
22408f738beSElen Song 	return atmel_port->use_dma_tx;
22508f738beSElen Song }
22608f738beSElen Song 
22734df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
22834df42f5SElen Song {
22934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
23034df42f5SElen Song 
23134df42f5SElen Song 	return atmel_port->use_dma_rx;
23234df42f5SElen Song }
23334df42f5SElen Song 
234e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
235e0b0baadSRichard Genoud {
236e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
237e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
238e0b0baadSRichard Genoud 
239*4e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
240e0b0baadSRichard Genoud 
241e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
242e0b0baadSRichard Genoud 
243e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
244e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
245e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
246e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
247e0b0baadSRichard Genoud 		else
248e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
249e0b0baadSRichard Genoud 	}
250e0b0baadSRichard Genoud 
251e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
252e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
253e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
254e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
255e0b0baadSRichard Genoud 		else
256e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
257e0b0baadSRichard Genoud 	}
258e0b0baadSRichard Genoud 
259e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
260e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
261e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
262e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
263e0b0baadSRichard Genoud 		else
264e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
265e0b0baadSRichard Genoud 	}
266e0b0baadSRichard Genoud 
267e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
268e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
269e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
270e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
271e0b0baadSRichard Genoud 		else
272e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
273e0b0baadSRichard Genoud 	}
274e0b0baadSRichard Genoud 
275e0b0baadSRichard Genoud 	return status;
276e0b0baadSRichard Genoud }
277e0b0baadSRichard Genoud 
278ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
27913bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
28013bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
281ab4382d2SGreg Kroah-Hartman {
282ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
283ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
284ab4382d2SGreg Kroah-Hartman 
285ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
286*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
287ab4382d2SGreg Kroah-Hartman 
288*4e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
289ab4382d2SGreg Kroah-Hartman 
290ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
291ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
292ab4382d2SGreg Kroah-Hartman 
29313bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
294ab4382d2SGreg Kroah-Hartman 
295ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
296ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
297ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
298*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
299*4e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
300ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
301ab4382d2SGreg Kroah-Hartman 	} else {
302ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
30364e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
304ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
305ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
306ab4382d2SGreg Kroah-Hartman 		else
307ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
308ab4382d2SGreg Kroah-Hartman 	}
309*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
310ab4382d2SGreg Kroah-Hartman 
311ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
312*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
313ab4382d2SGreg Kroah-Hartman 
31413bd3e6fSRicardo Ribalda Delgado 	return 0;
315ab4382d2SGreg Kroah-Hartman }
316ab4382d2SGreg Kroah-Hartman 
317ab4382d2SGreg Kroah-Hartman /*
318ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
319ab4382d2SGreg Kroah-Hartman  */
320ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
321ab4382d2SGreg Kroah-Hartman {
322*4e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
323*4e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
324*4e7decdaSCyrille Pitchen 		0;
325ab4382d2SGreg Kroah-Hartman }
326ab4382d2SGreg Kroah-Hartman 
327ab4382d2SGreg Kroah-Hartman /*
328ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
329ab4382d2SGreg Kroah-Hartman  */
330ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
331ab4382d2SGreg Kroah-Hartman {
332ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
333*4e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3341cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
335ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
336ab4382d2SGreg Kroah-Hartman 
3371cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3381cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
339*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
340*4e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
3411cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3421cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3431cf6e8fcSCyrille Pitchen 	}
3441cf6e8fcSCyrille Pitchen 
3451cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
3461cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
3471cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3481cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
3491cf6e8fcSCyrille Pitchen 
3501cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
3511cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
3521cf6e8fcSCyrille Pitchen 	} else {
3531cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3541cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
3551cf6e8fcSCyrille Pitchen 
3561cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
3571cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
3581cf6e8fcSCyrille Pitchen 	}
3591cf6e8fcSCyrille Pitchen 
360ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
3611cf6e8fcSCyrille Pitchen 		control |= rts_ready;
362ab4382d2SGreg Kroah-Hartman 	else
3631cf6e8fcSCyrille Pitchen 		control |= rts_paused;
364ab4382d2SGreg Kroah-Hartman 
365ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
366ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
367ab4382d2SGreg Kroah-Hartman 	else
368ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
369ab4382d2SGreg Kroah-Hartman 
370*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
371ab4382d2SGreg Kroah-Hartman 
372e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
373e0b0baadSRichard Genoud 
374ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
3751cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
376ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
377ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
378ab4382d2SGreg Kroah-Hartman 	else
379ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
380ab4382d2SGreg Kroah-Hartman 
381*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
382ab4382d2SGreg Kroah-Hartman }
383ab4382d2SGreg Kroah-Hartman 
384ab4382d2SGreg Kroah-Hartman /*
385ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
386ab4382d2SGreg Kroah-Hartman  */
387ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
388ab4382d2SGreg Kroah-Hartman {
389e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
390e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
391ab4382d2SGreg Kroah-Hartman 
392*4e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
393ab4382d2SGreg Kroah-Hartman 
394ab4382d2SGreg Kroah-Hartman 	/*
395ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
396ab4382d2SGreg Kroah-Hartman 	 */
397ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
398ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
399ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
400ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
401ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
402ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
403ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
404ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
405ab4382d2SGreg Kroah-Hartman 
406e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
407ab4382d2SGreg Kroah-Hartman }
408ab4382d2SGreg Kroah-Hartman 
409ab4382d2SGreg Kroah-Hartman /*
410ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
411ab4382d2SGreg Kroah-Hartman  */
412ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
413ab4382d2SGreg Kroah-Hartman {
414ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
415ab4382d2SGreg Kroah-Hartman 
41664e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
417ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
418*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
419ab4382d2SGreg Kroah-Hartman 	}
420ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
421*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
422ab4382d2SGreg Kroah-Hartman 
42313bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
42413bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
425ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
426ab4382d2SGreg Kroah-Hartman }
427ab4382d2SGreg Kroah-Hartman 
428ab4382d2SGreg Kroah-Hartman /*
429ab4382d2SGreg Kroah-Hartman  * Start transmitting.
430ab4382d2SGreg Kroah-Hartman  */
431ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
432ab4382d2SGreg Kroah-Hartman {
433ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
434ab4382d2SGreg Kroah-Hartman 
43564e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
436*4e7decdaSCyrille Pitchen 		if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
437ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
438ab4382d2SGreg Kroah-Hartman 			   really need this.*/
439ab4382d2SGreg Kroah-Hartman 			return;
440ab4382d2SGreg Kroah-Hartman 
44113bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
44213bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
443ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
444ab4382d2SGreg Kroah-Hartman 
445ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
446*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
447ab4382d2SGreg Kroah-Hartman 	}
448ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
449*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
450ab4382d2SGreg Kroah-Hartman }
451ab4382d2SGreg Kroah-Hartman 
452ab4382d2SGreg Kroah-Hartman /*
453ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
454ab4382d2SGreg Kroah-Hartman  */
455ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
456ab4382d2SGreg Kroah-Hartman {
457*4e7decdaSCyrille Pitchen 	/* reset status and receiver */
458*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
459ab4382d2SGreg Kroah-Hartman 
460*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
46157c36868SSiftar, Gabe 
46264e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
463ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
464*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
465*4e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
466ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
467*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
468ab4382d2SGreg Kroah-Hartman 	} else {
469*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
470ab4382d2SGreg Kroah-Hartman 	}
471ab4382d2SGreg Kroah-Hartman }
472ab4382d2SGreg Kroah-Hartman 
473ab4382d2SGreg Kroah-Hartman /*
474ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
475ab4382d2SGreg Kroah-Hartman  */
476ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
477ab4382d2SGreg Kroah-Hartman {
478*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
47957c36868SSiftar, Gabe 
48064e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
481ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
482*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
483*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
484*4e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
485ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
486ab4382d2SGreg Kroah-Hartman 	} else {
487*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, 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 
527*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, 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 
566*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, 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)
575*4e7decdaSCyrille Pitchen 		/* start break */
576*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
577ab4382d2SGreg Kroah-Hartman 	else
578*4e7decdaSCyrille Pitchen 		/* stop break */
579*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
580ab4382d2SGreg Kroah-Hartman }
581ab4382d2SGreg Kroah-Hartman 
582ab4382d2SGreg Kroah-Hartman /*
583ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
584ab4382d2SGreg Kroah-Hartman  */
585ab4382d2SGreg Kroah-Hartman static void
586ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
587ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
588ab4382d2SGreg Kroah-Hartman {
589ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
590ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
591ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
592ab4382d2SGreg Kroah-Hartman 
593ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
594ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
595ab4382d2SGreg Kroah-Hartman 		return;
596ab4382d2SGreg Kroah-Hartman 
597ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
598ab4382d2SGreg Kroah-Hartman 	c->status	= status;
599ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
600ab4382d2SGreg Kroah-Hartman 
601ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
602ab4382d2SGreg Kroah-Hartman 	smp_wmb();
603ab4382d2SGreg Kroah-Hartman 
604ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
605ab4382d2SGreg Kroah-Hartman }
606ab4382d2SGreg Kroah-Hartman 
607ab4382d2SGreg Kroah-Hartman /*
608ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
609ab4382d2SGreg Kroah-Hartman  */
610ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
611ab4382d2SGreg Kroah-Hartman {
612ab4382d2SGreg Kroah-Hartman 	/* clear error */
613*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
614ab4382d2SGreg Kroah-Hartman 
615ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
616ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
617ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
618ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
619ab4382d2SGreg Kroah-Hartman 	}
620ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
621ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
622ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
623ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
624ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
625ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
626ab4382d2SGreg Kroah-Hartman }
627ab4382d2SGreg Kroah-Hartman 
628ab4382d2SGreg Kroah-Hartman /*
629ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
630ab4382d2SGreg Kroah-Hartman  */
631ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
632ab4382d2SGreg Kroah-Hartman {
633ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
634ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
635ab4382d2SGreg Kroah-Hartman 
636*4e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
637ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
638*4e7decdaSCyrille Pitchen 		ch = atmel_uart_readl(port, ATMEL_US_RHR);
639ab4382d2SGreg Kroah-Hartman 
640ab4382d2SGreg Kroah-Hartman 		/*
641ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
642ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
643ab4382d2SGreg Kroah-Hartman 		 */
644ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
645ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
646ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
647ab4382d2SGreg Kroah-Hartman 
648ab4382d2SGreg Kroah-Hartman 			/* clear error */
649*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
650ab4382d2SGreg Kroah-Hartman 
651ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
652ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
653ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
654*4e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
655*4e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
656ab4382d2SGreg Kroah-Hartman 			} else {
657ab4382d2SGreg Kroah-Hartman 				/*
658ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
659ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
660ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
661ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
662ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
663ab4382d2SGreg Kroah-Hartman 				 */
664*4e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
665*4e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
666ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
667ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
668ab4382d2SGreg Kroah-Hartman 			}
669ab4382d2SGreg Kroah-Hartman 		}
670ab4382d2SGreg Kroah-Hartman 
671ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
672*4e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
673ab4382d2SGreg Kroah-Hartman 	}
674ab4382d2SGreg Kroah-Hartman 
675ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
676ab4382d2SGreg Kroah-Hartman }
677ab4382d2SGreg Kroah-Hartman 
678ab4382d2SGreg Kroah-Hartman /*
679ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
680ab4382d2SGreg Kroah-Hartman  * disabled)
681ab4382d2SGreg Kroah-Hartman  */
682ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
683ab4382d2SGreg Kroah-Hartman {
684ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
685ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
686ab4382d2SGreg Kroah-Hartman 
687*4e7decdaSCyrille Pitchen 	if (port->x_char &&
688*4e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
689*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_THR, port->x_char);
690ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
691ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
692ab4382d2SGreg Kroah-Hartman 	}
693ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
694ab4382d2SGreg Kroah-Hartman 		return;
695ab4382d2SGreg Kroah-Hartman 
696*4e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
697*4e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
698*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_THR, xmit->buf[xmit->tail]);
699ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
700ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
701ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
702ab4382d2SGreg Kroah-Hartman 			break;
703ab4382d2SGreg Kroah-Hartman 	}
704ab4382d2SGreg Kroah-Hartman 
705ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
706ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
707ab4382d2SGreg Kroah-Hartman 
708ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
709ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
710*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
711*4e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
712ab4382d2SGreg Kroah-Hartman }
713ab4382d2SGreg Kroah-Hartman 
71408f738beSElen Song static void atmel_complete_tx_dma(void *arg)
71508f738beSElen Song {
71608f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
71708f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
71808f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
71908f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
72008f738beSElen Song 	unsigned long flags;
72108f738beSElen Song 
72208f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
72308f738beSElen Song 
72408f738beSElen Song 	if (chan)
72508f738beSElen Song 		dmaengine_terminate_all(chan);
72608f738beSElen Song 	xmit->tail += sg_dma_len(&atmel_port->sg_tx);
72708f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
72808f738beSElen Song 
72908f738beSElen Song 	port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
73008f738beSElen Song 
73108f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
73208f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
73308f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
73408f738beSElen Song 	atmel_port->desc_tx = NULL;
73508f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
73608f738beSElen Song 
73708f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
73808f738beSElen Song 		uart_write_wakeup(port);
73908f738beSElen Song 
7401842dc2eSCyrille Pitchen 	/*
7411842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7421842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7431842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7441842dc2eSCyrille Pitchen 	 */
74508f738beSElen Song 	if (!uart_circ_empty(xmit))
74608f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
74708f738beSElen Song 
74808f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
74908f738beSElen Song }
75008f738beSElen Song 
75108f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
75208f738beSElen Song {
75308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
75408f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
75508f738beSElen Song 
75608f738beSElen Song 	if (chan) {
75708f738beSElen Song 		dmaengine_terminate_all(chan);
75808f738beSElen Song 		dma_release_channel(chan);
75908f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
76048479148SWolfram Sang 				DMA_TO_DEVICE);
76108f738beSElen Song 	}
76208f738beSElen Song 
76308f738beSElen Song 	atmel_port->desc_tx = NULL;
76408f738beSElen Song 	atmel_port->chan_tx = NULL;
76508f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
76608f738beSElen Song }
76708f738beSElen Song 
76808f738beSElen Song /*
76908f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
77008f738beSElen Song  */
77108f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
77208f738beSElen Song {
77308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
77408f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
77508f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
77608f738beSElen Song 	struct dma_async_tx_descriptor *desc;
77708f738beSElen Song 	struct scatterlist *sg = &atmel_port->sg_tx;
77808f738beSElen Song 
77908f738beSElen Song 	/* Make sure we have an idle channel */
78008f738beSElen Song 	if (atmel_port->desc_tx != NULL)
78108f738beSElen Song 		return;
78208f738beSElen Song 
78308f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
78408f738beSElen Song 		/*
78508f738beSElen Song 		 * DMA is idle now.
78608f738beSElen Song 		 * Port xmit buffer is already mapped,
78708f738beSElen Song 		 * and it is one page... Just adjust
78808f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
78908f738beSElen Song 		 * we have to transmit till the end, and then the rest.
79008f738beSElen Song 		 * Take the port lock to get a
79108f738beSElen Song 		 * consistent xmit buffer state.
79208f738beSElen Song 		 */
79308f738beSElen Song 		sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
79408f738beSElen Song 		sg_dma_address(sg) = (sg_dma_address(sg) &
79508f738beSElen Song 					~(UART_XMIT_SIZE - 1))
79608f738beSElen Song 					+ sg->offset;
79708f738beSElen Song 		sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
79808f738beSElen Song 						xmit->tail,
79908f738beSElen Song 						UART_XMIT_SIZE);
80008f738beSElen Song 		BUG_ON(!sg_dma_len(sg));
80108f738beSElen Song 
80208f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
80308f738beSElen Song 					       sg,
80408f738beSElen Song 					       1,
80508f738beSElen Song 					       DMA_MEM_TO_DEV,
80608f738beSElen Song 					       DMA_PREP_INTERRUPT |
80708f738beSElen Song 					       DMA_CTRL_ACK);
80808f738beSElen Song 		if (!desc) {
80908f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
81008f738beSElen Song 			return;
81108f738beSElen Song 		}
81208f738beSElen Song 
813485819b5SCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
81408f738beSElen Song 
81508f738beSElen Song 		atmel_port->desc_tx = desc;
81608f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
81708f738beSElen Song 		desc->callback_param = atmel_port;
81808f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
81908f738beSElen Song 
82008f738beSElen Song 	} else {
82113bd3e6fSRicardo Ribalda Delgado 		if (port->rs485.flags & SER_RS485_ENABLED) {
82208f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
82308f738beSElen Song 			atmel_start_rx(port);
82408f738beSElen Song 		}
82508f738beSElen Song 	}
82608f738beSElen Song 
82708f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
82808f738beSElen Song 		uart_write_wakeup(port);
82908f738beSElen Song }
83008f738beSElen Song 
83108f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
83208f738beSElen Song {
83308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
83408f738beSElen Song 	dma_cap_mask_t		mask;
83508f738beSElen Song 	struct dma_slave_config config;
83608f738beSElen Song 	int ret, nent;
83708f738beSElen Song 
83808f738beSElen Song 	dma_cap_zero(mask);
83908f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
84008f738beSElen Song 
84108f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
84208f738beSElen Song 	if (atmel_port->chan_tx == NULL)
84308f738beSElen Song 		goto chan_err;
84408f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
84508f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
84608f738beSElen Song 
84708f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
84808f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
84908f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
8502c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
85108f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
85208f738beSElen Song 			virt_to_page(port->state->xmit.buf),
85308f738beSElen Song 			UART_XMIT_SIZE,
85408f738beSElen Song 			(int)port->state->xmit.buf & ~PAGE_MASK);
85508f738beSElen Song 	nent = dma_map_sg(port->dev,
85608f738beSElen Song 				&atmel_port->sg_tx,
85708f738beSElen Song 				1,
85848479148SWolfram Sang 				DMA_TO_DEVICE);
85908f738beSElen Song 
86008f738beSElen Song 	if (!nent) {
86108f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
86208f738beSElen Song 		goto chan_err;
86308f738beSElen Song 	} else {
86408f738beSElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
86508f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
86608f738beSElen Song 			port->state->xmit.buf,
86708f738beSElen Song 			sg_dma_address(&atmel_port->sg_tx));
86808f738beSElen Song 	}
86908f738beSElen Song 
87008f738beSElen Song 	/* Configure the slave DMA */
87108f738beSElen Song 	memset(&config, 0, sizeof(config));
87208f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
87308f738beSElen Song 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
87408f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
875a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
87608f738beSElen Song 
8775483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
8785483c10eSMaxime Ripard 				     &config);
87908f738beSElen Song 	if (ret) {
88008f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
88108f738beSElen Song 		goto chan_err;
88208f738beSElen Song 	}
88308f738beSElen Song 
88408f738beSElen Song 	return 0;
88508f738beSElen Song 
88608f738beSElen Song chan_err:
88708f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
88808f738beSElen Song 	atmel_port->use_dma_tx = 0;
88908f738beSElen Song 	if (atmel_port->chan_tx)
89008f738beSElen Song 		atmel_release_tx_dma(port);
89108f738beSElen Song 	return -EINVAL;
89208f738beSElen Song }
89308f738beSElen Song 
89434df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
89534df42f5SElen Song {
89634df42f5SElen Song 	struct uart_port *port = arg;
89734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
89834df42f5SElen Song 
89934df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
90034df42f5SElen Song }
90134df42f5SElen Song 
90234df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
90334df42f5SElen Song {
90434df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
90534df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
90634df42f5SElen Song 
90734df42f5SElen Song 	if (chan) {
90834df42f5SElen Song 		dmaengine_terminate_all(chan);
90934df42f5SElen Song 		dma_release_channel(chan);
91034df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
91148479148SWolfram Sang 				DMA_FROM_DEVICE);
91234df42f5SElen Song 	}
91334df42f5SElen Song 
91434df42f5SElen Song 	atmel_port->desc_rx = NULL;
91534df42f5SElen Song 	atmel_port->chan_rx = NULL;
91634df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
91734df42f5SElen Song }
91834df42f5SElen Song 
91934df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
92034df42f5SElen Song {
92134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
92266f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
92334df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
92434df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
92534df42f5SElen Song 	struct dma_tx_state state;
92634df42f5SElen Song 	enum dma_status dmastat;
92766f37aafSCyrille Pitchen 	size_t count;
92834df42f5SElen Song 
92934df42f5SElen Song 
93034df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
931*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
93234df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
93334df42f5SElen Song 				atmel_port->cookie_rx,
93434df42f5SElen Song 				&state);
93534df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
93634df42f5SElen Song 	if (dmastat == DMA_ERROR) {
93734df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
938*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
93934df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
94034df42f5SElen Song 		return;
94134df42f5SElen Song 	}
94266f37aafSCyrille Pitchen 
94366f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
94466f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
94566f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
94666f37aafSCyrille Pitchen 			    1,
947485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
94834df42f5SElen Song 
94934df42f5SElen Song 	/*
95066f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
95166f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
95266f37aafSCyrille Pitchen 	 * framework.
95366f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
95466f37aafSCyrille Pitchen 	 * length.
95534df42f5SElen Song 	 */
95666f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
95766f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
95866f37aafSCyrille Pitchen 	/*
95966f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
96066f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
96166f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
96266f37aafSCyrille Pitchen 	 *
96366f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
96466f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
96566f37aafSCyrille Pitchen 	 *
96666f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
96766f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
96866f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
96966f37aafSCyrille Pitchen 	 */
97066f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
97166f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
97234df42f5SElen Song 
97366f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
97466f37aafSCyrille Pitchen 		ring->tail = 0;
97534df42f5SElen Song 		port->icount.rx += count;
97634df42f5SElen Song 	}
97734df42f5SElen Song 
97866f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
97966f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
98066f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
98166f37aafSCyrille Pitchen 
98266f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
98366f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
98466f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
98566f37aafSCyrille Pitchen 			ring->head = 0;
98666f37aafSCyrille Pitchen 		ring->tail = ring->head;
98766f37aafSCyrille Pitchen 		port->icount.rx += count;
98866f37aafSCyrille Pitchen 	}
98966f37aafSCyrille Pitchen 
99066f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
99166f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
99266f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
99366f37aafSCyrille Pitchen 			       1,
994485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
99566f37aafSCyrille Pitchen 
99666f37aafSCyrille Pitchen 	/*
99766f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
99866f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
99966f37aafSCyrille Pitchen 	 */
100066f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
100166f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
100266f37aafSCyrille Pitchen 	spin_lock(&port->lock);
100366f37aafSCyrille Pitchen 
1004*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
100534df42f5SElen Song }
100634df42f5SElen Song 
100734df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
100834df42f5SElen Song {
100934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
101034df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
101134df42f5SElen Song 	dma_cap_mask_t		mask;
101234df42f5SElen Song 	struct dma_slave_config config;
101334df42f5SElen Song 	struct circ_buf		*ring;
101434df42f5SElen Song 	int ret, nent;
101534df42f5SElen Song 
101634df42f5SElen Song 	ring = &atmel_port->rx_ring;
101734df42f5SElen Song 
101834df42f5SElen Song 	dma_cap_zero(mask);
101934df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
102034df42f5SElen Song 
102134df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
102234df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
102334df42f5SElen Song 		goto chan_err;
102434df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
102534df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
102634df42f5SElen Song 
102734df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
102834df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
102934df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
10302c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
103134df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
103234df42f5SElen Song 		    virt_to_page(ring->buf),
1033a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
103434df42f5SElen Song 		    (int)ring->buf & ~PAGE_MASK);
103534df42f5SElen Song 	nent = dma_map_sg(port->dev,
103634df42f5SElen Song 			  &atmel_port->sg_rx,
103734df42f5SElen Song 			  1,
103848479148SWolfram Sang 			  DMA_FROM_DEVICE);
103934df42f5SElen Song 
104034df42f5SElen Song 	if (!nent) {
104134df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
104234df42f5SElen Song 		goto chan_err;
104334df42f5SElen Song 	} else {
104434df42f5SElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
104534df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
104634df42f5SElen Song 			ring->buf,
104734df42f5SElen Song 			sg_dma_address(&atmel_port->sg_rx));
104834df42f5SElen Song 	}
104934df42f5SElen Song 
105034df42f5SElen Song 	/* Configure the slave DMA */
105134df42f5SElen Song 	memset(&config, 0, sizeof(config));
105234df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
105334df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
105434df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1055a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
105634df42f5SElen Song 
10575483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
10585483c10eSMaxime Ripard 				     &config);
105934df42f5SElen Song 	if (ret) {
106034df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
106134df42f5SElen Song 		goto chan_err;
106234df42f5SElen Song 	}
106334df42f5SElen Song 	/*
106434df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
106534df42f5SElen Song 	 * each one is half ring buffer size
106634df42f5SElen Song 	 */
106734df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
106834df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
106934df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
107034df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
107134df42f5SElen Song 					 DMA_DEV_TO_MEM,
107234df42f5SElen Song 					 DMA_PREP_INTERRUPT);
107334df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
107434df42f5SElen Song 	desc->callback_param = port;
107534df42f5SElen Song 	atmel_port->desc_rx = desc;
107634df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
107734df42f5SElen Song 
107834df42f5SElen Song 	return 0;
107934df42f5SElen Song 
108034df42f5SElen Song chan_err:
108134df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
108234df42f5SElen Song 	atmel_port->use_dma_rx = 0;
108334df42f5SElen Song 	if (atmel_port->chan_rx)
108434df42f5SElen Song 		atmel_release_rx_dma(port);
108534df42f5SElen Song 	return -EINVAL;
108634df42f5SElen Song }
108734df42f5SElen Song 
10882e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
10892e68c22fSElen Song {
10902e68c22fSElen Song 	struct uart_port *port = (void *)data;
10912e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
10922e68c22fSElen Song 
10932e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
10942e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
10952e68c22fSElen Song }
10962e68c22fSElen Song 
1097ab4382d2SGreg Kroah-Hartman /*
1098ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1099ab4382d2SGreg Kroah-Hartman  */
1100ab4382d2SGreg Kroah-Hartman static void
1101ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1102ab4382d2SGreg Kroah-Hartman {
1103ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1104ab4382d2SGreg Kroah-Hartman 
110564e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1106ab4382d2SGreg Kroah-Hartman 		/*
1107ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1108ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1109ab4382d2SGreg Kroah-Hartman 		 *
1110ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1111ab4382d2SGreg Kroah-Hartman 		 * the moment.
1112ab4382d2SGreg Kroah-Hartman 		 */
1113ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
1114*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
1115*4e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
1116ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
1117ab4382d2SGreg Kroah-Hartman 		}
1118ab4382d2SGreg Kroah-Hartman 
1119ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1120ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1121ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1122ab4382d2SGreg Kroah-Hartman 	}
1123ab4382d2SGreg Kroah-Hartman 
112434df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
112534df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
1126*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
1127*4e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
112834df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
112934df42f5SElen Song 		}
113034df42f5SElen Song 	}
113134df42f5SElen Song 
1132ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1133ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1134ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1135ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1136ab4382d2SGreg Kroah-Hartman 		/*
1137ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1138ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1139ab4382d2SGreg Kroah-Hartman 		 */
1140*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1141*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1142ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1143ab4382d2SGreg Kroah-Hartman 	}
1144ab4382d2SGreg Kroah-Hartman }
1145ab4382d2SGreg Kroah-Hartman 
1146ab4382d2SGreg Kroah-Hartman /*
1147ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1148ab4382d2SGreg Kroah-Hartman  */
1149ab4382d2SGreg Kroah-Hartman static void
1150ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1151ab4382d2SGreg Kroah-Hartman {
1152ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1153ab4382d2SGreg Kroah-Hartman 
1154ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1155ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
1156*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
1157*4e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1158ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1159ab4382d2SGreg Kroah-Hartman 	}
1160ab4382d2SGreg Kroah-Hartman }
1161ab4382d2SGreg Kroah-Hartman 
1162ab4382d2SGreg Kroah-Hartman /*
1163ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1164ab4382d2SGreg Kroah-Hartman  */
1165ab4382d2SGreg Kroah-Hartman static void
1166ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1167ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1168ab4382d2SGreg Kroah-Hartman {
1169ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1170ab4382d2SGreg Kroah-Hartman 
1171ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1172ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1173ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1174d033e82dSLeilei Zhao 		atmel_port->status_change = atmel_port->irq_status ^
1175d033e82dSLeilei Zhao 					    atmel_port->irq_status_prev;
1176d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
1177ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1178ab4382d2SGreg Kroah-Hartman 	}
1179ab4382d2SGreg Kroah-Hartman }
1180ab4382d2SGreg Kroah-Hartman 
1181ab4382d2SGreg Kroah-Hartman /*
1182ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1183ab4382d2SGreg Kroah-Hartman  */
1184ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1185ab4382d2SGreg Kroah-Hartman {
1186ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1187ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11882c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1189ab5e4e41SRichard Genoud 	bool gpio_handled = false;
1190ab4382d2SGreg Kroah-Hartman 
11912c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
11922c7af5baSBoris BREZILLON 
1193ab4382d2SGreg Kroah-Hartman 	do {
1194e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
1195*4e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
11962c7af5baSBoris BREZILLON 		pending = status & mask;
1197ab5e4e41SRichard Genoud 		if (!gpio_handled) {
1198ab5e4e41SRichard Genoud 			/*
1199ab5e4e41SRichard Genoud 			 * Dealing with GPIO interrupt
1200ab5e4e41SRichard Genoud 			 */
1201ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1202ab5e4e41SRichard Genoud 				pending |= ATMEL_US_CTSIC;
1203ab5e4e41SRichard Genoud 
1204ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1205ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DSRIC;
1206ab5e4e41SRichard Genoud 
1207ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1208ab5e4e41SRichard Genoud 				pending |= ATMEL_US_RIIC;
1209ab5e4e41SRichard Genoud 
1210ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1211ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DCDIC;
1212ab5e4e41SRichard Genoud 
1213ab5e4e41SRichard Genoud 			gpio_handled = true;
1214ab5e4e41SRichard Genoud 		}
1215ab4382d2SGreg Kroah-Hartman 		if (!pending)
1216ab4382d2SGreg Kroah-Hartman 			break;
1217ab4382d2SGreg Kroah-Hartman 
12182c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
12192c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
12202c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
1221*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
12222c7af5baSBoris BREZILLON 			pm_system_wakeup();
12232c7af5baSBoris BREZILLON 			break;
12242c7af5baSBoris BREZILLON 		}
12252c7af5baSBoris BREZILLON 
1226ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1227ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1228ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1229ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1230ab4382d2SGreg Kroah-Hartman 
12312c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
12322c7af5baSBoris BREZILLON 
1233ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1234ab4382d2SGreg Kroah-Hartman }
1235ab4382d2SGreg Kroah-Hartman 
1236a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1237a930e528SElen Song {
1238a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1239a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1240a930e528SElen Song 
1241a930e528SElen Song 	dma_unmap_single(port->dev,
1242a930e528SElen Song 			 pdc->dma_addr,
1243a930e528SElen Song 			 pdc->dma_size,
1244a930e528SElen Song 			 DMA_TO_DEVICE);
1245a930e528SElen Song }
1246a930e528SElen Song 
1247ab4382d2SGreg Kroah-Hartman /*
1248ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1249ab4382d2SGreg Kroah-Hartman  */
125064e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1251ab4382d2SGreg Kroah-Hartman {
1252ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1253ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1254ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1255ab4382d2SGreg Kroah-Hartman 	int count;
1256ab4382d2SGreg Kroah-Hartman 
1257ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
1258*4e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1259ab4382d2SGreg Kroah-Hartman 		return;
1260ab4382d2SGreg Kroah-Hartman 
1261ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1262ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1263ab4382d2SGreg Kroah-Hartman 
1264ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1265ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1266ab4382d2SGreg Kroah-Hartman 
1267ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1268ab4382d2SGreg Kroah-Hartman 
1269ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
1270*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1271ab4382d2SGreg Kroah-Hartman 
1272ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1273ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1274ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1275ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1276ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1277ab4382d2SGreg Kroah-Hartman 
1278ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1279ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1280ab4382d2SGreg Kroah-Hartman 
1281*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
1282*4e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
1283*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1284ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
1285*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1286ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
1287*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
1288*4e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1289ab4382d2SGreg Kroah-Hartman 	} else {
129013bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
129113bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1292ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1293ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1294ab4382d2SGreg Kroah-Hartman 		}
1295ab4382d2SGreg Kroah-Hartman 	}
1296ab4382d2SGreg Kroah-Hartman 
1297ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1298ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1299ab4382d2SGreg Kroah-Hartman }
1300ab4382d2SGreg Kroah-Hartman 
1301a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1302a930e528SElen Song {
1303a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1304a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1305a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1306a930e528SElen Song 
1307a930e528SElen Song 	pdc->buf = xmit->buf;
1308a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1309a930e528SElen Song 					pdc->buf,
1310a930e528SElen Song 					UART_XMIT_SIZE,
1311a930e528SElen Song 					DMA_TO_DEVICE);
1312a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1313a930e528SElen Song 	pdc->ofs = 0;
1314a930e528SElen Song 
1315a930e528SElen Song 	return 0;
1316a930e528SElen Song }
1317a930e528SElen Song 
1318ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1319ab4382d2SGreg Kroah-Hartman {
1320ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1321ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1322ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1323ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1324ab4382d2SGreg Kroah-Hartman 
1325ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1326ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1327ab4382d2SGreg Kroah-Hartman 
1328ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1329ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1330ab4382d2SGreg Kroah-Hartman 
1331ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1332ab4382d2SGreg Kroah-Hartman 
1333ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1334ab4382d2SGreg Kroah-Hartman 
1335ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1336ab4382d2SGreg Kroah-Hartman 		status = c.status;
1337ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1338ab4382d2SGreg Kroah-Hartman 
1339ab4382d2SGreg Kroah-Hartman 		/*
1340ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1341ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1342ab4382d2SGreg Kroah-Hartman 		 */
1343ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1344ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1345ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1346ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1347ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1348ab4382d2SGreg Kroah-Hartman 
1349ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1350ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1351ab4382d2SGreg Kroah-Hartman 					continue;
1352ab4382d2SGreg Kroah-Hartman 			}
1353ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1354ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1355ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1356ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1357ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1358ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1359ab4382d2SGreg Kroah-Hartman 
1360ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1361ab4382d2SGreg Kroah-Hartman 
1362ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1363ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1364ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1365ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1366ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1367ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1368ab4382d2SGreg Kroah-Hartman 		}
1369ab4382d2SGreg Kroah-Hartman 
1370ab4382d2SGreg Kroah-Hartman 
1371ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1372ab4382d2SGreg Kroah-Hartman 			continue;
1373ab4382d2SGreg Kroah-Hartman 
1374ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1375ab4382d2SGreg Kroah-Hartman 	}
1376ab4382d2SGreg Kroah-Hartman 
1377ab4382d2SGreg Kroah-Hartman 	/*
1378ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1379ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1380ab4382d2SGreg Kroah-Hartman 	 */
1381ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
13822e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1383ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1384ab4382d2SGreg Kroah-Hartman }
1385ab4382d2SGreg Kroah-Hartman 
1386a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1387a930e528SElen Song {
1388a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1389a930e528SElen Song 	int i;
1390a930e528SElen Song 
1391a930e528SElen Song 	for (i = 0; i < 2; i++) {
1392a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1393a930e528SElen Song 
1394a930e528SElen Song 		dma_unmap_single(port->dev,
1395a930e528SElen Song 				 pdc->dma_addr,
1396a930e528SElen Song 				 pdc->dma_size,
1397a930e528SElen Song 				 DMA_FROM_DEVICE);
1398a930e528SElen Song 		kfree(pdc->buf);
1399a930e528SElen Song 	}
1400a930e528SElen Song }
1401a930e528SElen Song 
140264e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1403ab4382d2SGreg Kroah-Hartman {
1404ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
140505c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1406ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1407ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1408ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1409ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1410ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1411ab4382d2SGreg Kroah-Hartman 
1412ab4382d2SGreg Kroah-Hartman 	do {
1413ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
1414*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1415ab4382d2SGreg Kroah-Hartman 
1416ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
1417*4e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1418ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1419ab4382d2SGreg Kroah-Hartman 
1420ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1421ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1422ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1423ab4382d2SGreg Kroah-Hartman 		 * find out.
1424ab4382d2SGreg Kroah-Hartman 		 *
1425ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1426ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1427ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1428ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1429ab4382d2SGreg Kroah-Hartman 		 */
1430ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1431ab4382d2SGreg Kroah-Hartman 
1432ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1433ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1434ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1435ab4382d2SGreg Kroah-Hartman 
1436ab4382d2SGreg Kroah-Hartman 			/*
1437ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1438ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1439ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1440ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1441ab4382d2SGreg Kroah-Hartman 			 */
1442ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1443ab4382d2SGreg Kroah-Hartman 
144405c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
144505c7cd39SJiri Slaby 						count);
1446ab4382d2SGreg Kroah-Hartman 
1447ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1448ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1449ab4382d2SGreg Kroah-Hartman 
1450ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1451ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1452ab4382d2SGreg Kroah-Hartman 		}
1453ab4382d2SGreg Kroah-Hartman 
1454ab4382d2SGreg Kroah-Hartman 		/*
1455ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1456ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1457ab4382d2SGreg Kroah-Hartman 		 */
1458ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1459ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
1460*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1461*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1462ab4382d2SGreg Kroah-Hartman 
1463ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1464ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1465ab4382d2SGreg Kroah-Hartman 		}
1466ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1467ab4382d2SGreg Kroah-Hartman 
1468ab4382d2SGreg Kroah-Hartman 	/*
1469ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1470ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1471ab4382d2SGreg Kroah-Hartman 	 */
1472ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14732e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1474ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1475ab4382d2SGreg Kroah-Hartman 
1476*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
1477*4e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1478ab4382d2SGreg Kroah-Hartman }
1479ab4382d2SGreg Kroah-Hartman 
1480a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1481a930e528SElen Song {
1482a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1483a930e528SElen Song 	int i;
1484a930e528SElen Song 
1485a930e528SElen Song 	for (i = 0; i < 2; i++) {
1486a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1487a930e528SElen Song 
1488a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1489a930e528SElen Song 		if (pdc->buf == NULL) {
1490a930e528SElen Song 			if (i != 0) {
1491a930e528SElen Song 				dma_unmap_single(port->dev,
1492a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1493a930e528SElen Song 					PDC_BUFFER_SIZE,
1494a930e528SElen Song 					DMA_FROM_DEVICE);
1495a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1496a930e528SElen Song 			}
1497a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1498a930e528SElen Song 			return -ENOMEM;
1499a930e528SElen Song 		}
1500a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1501a930e528SElen Song 						pdc->buf,
1502a930e528SElen Song 						PDC_BUFFER_SIZE,
1503a930e528SElen Song 						DMA_FROM_DEVICE);
1504a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1505a930e528SElen Song 		pdc->ofs = 0;
1506a930e528SElen Song 	}
1507a930e528SElen Song 
1508a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1509a930e528SElen Song 
1510*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1511*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1512a930e528SElen Song 
1513*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
1514*4e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
1515*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1516a930e528SElen Song 
1517a930e528SElen Song 	return 0;
1518a930e528SElen Song }
1519a930e528SElen Song 
1520ab4382d2SGreg Kroah-Hartman /*
1521ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1522ab4382d2SGreg Kroah-Hartman  */
1523ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1524ab4382d2SGreg Kroah-Hartman {
1525ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1526ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1527d033e82dSLeilei Zhao 	unsigned int status = atmel_port->irq_status;
1528d033e82dSLeilei Zhao 	unsigned int status_change = atmel_port->status_change;
1529ab4382d2SGreg Kroah-Hartman 
1530ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1531ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1532ab4382d2SGreg Kroah-Hartman 
1533a930e528SElen Song 	atmel_port->schedule_tx(port);
1534ab4382d2SGreg Kroah-Hartman 
1535ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1536ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1537ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1538ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1539ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1540ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1541ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1542ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1543ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1544ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1545ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1546ab4382d2SGreg Kroah-Hartman 
1547ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1548ab4382d2SGreg Kroah-Hartman 
1549d033e82dSLeilei Zhao 		atmel_port->status_change = 0;
1550ab4382d2SGreg Kroah-Hartman 	}
1551ab4382d2SGreg Kroah-Hartman 
1552a930e528SElen Song 	atmel_port->schedule_rx(port);
1553ab4382d2SGreg Kroah-Hartman 
1554ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1555ab4382d2SGreg Kroah-Hartman }
1556ab4382d2SGreg Kroah-Hartman 
15574a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
155833d64c4fSElen Song 				struct platform_device *pdev)
155933d64c4fSElen Song {
156033d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1561574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
156233d64c4fSElen Song 
156333d64c4fSElen Song 	if (np) {
156433d64c4fSElen Song 		/* DMA/PDC usage specification */
156533d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
156633d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
156733d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
156833d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
156933d64c4fSElen Song 			} else {
157033d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
157133d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
157233d64c4fSElen Song 			}
157333d64c4fSElen Song 		} else {
157433d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
157533d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
157633d64c4fSElen Song 		}
157733d64c4fSElen Song 
157833d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
157933d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
158033d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
158133d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
158233d64c4fSElen Song 			} else {
158333d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
158433d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
158533d64c4fSElen Song 			}
158633d64c4fSElen Song 		} else {
158733d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
158833d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
158933d64c4fSElen Song 		}
159033d64c4fSElen Song 
159133d64c4fSElen Song 	} else {
159233d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
159333d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
159433d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
159533d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
159633d64c4fSElen Song 	}
159733d64c4fSElen Song 
159833d64c4fSElen Song }
159933d64c4fSElen Song 
160013bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
160133d64c4fSElen Song 				struct platform_device *pdev)
160233d64c4fSElen Song {
160333d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1604574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
160533d64c4fSElen Song 
160633d64c4fSElen Song 	if (np) {
160733d64c4fSElen Song 		u32 rs485_delay[2];
160833d64c4fSElen Song 		/* rs485 properties */
160933d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
161033d64c4fSElen Song 					rs485_delay, 2) == 0) {
161113bd3e6fSRicardo Ribalda Delgado 			struct serial_rs485 *rs485conf = &port->rs485;
161233d64c4fSElen Song 
161333d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
161433d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
161533d64c4fSElen Song 			rs485conf->flags = 0;
161633d64c4fSElen Song 
161733d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
161833d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
161933d64c4fSElen Song 
162033d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
162133d64c4fSElen Song 								NULL))
162233d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
162333d64c4fSElen Song 		}
162433d64c4fSElen Song 	} else {
162513bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
162633d64c4fSElen Song 	}
162733d64c4fSElen Song 
162833d64c4fSElen Song }
162933d64c4fSElen Song 
1630a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1631a930e528SElen Song {
1632a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1633a930e528SElen Song 
163434df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
163534df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
163634df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
163734df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
163834df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1639a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1640a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1641a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1642a930e528SElen Song 	} else {
1643a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1644a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1645a930e528SElen Song 		atmel_port->release_rx = NULL;
1646a930e528SElen Song 	}
1647a930e528SElen Song 
164808f738beSElen Song 	if (atmel_use_dma_tx(port)) {
164908f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
165008f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
165108f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
165208f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1653a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1654a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1655a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1656a930e528SElen Song 	} else {
1657a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1658a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1659a930e528SElen Song 		atmel_port->release_tx = NULL;
1660a930e528SElen Song 	}
1661a930e528SElen Song }
1662a930e528SElen Song 
1663ab4382d2SGreg Kroah-Hartman /*
1664055560b0SElen Song  * Get ip name usart or uart
1665055560b0SElen Song  */
1666892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1667055560b0SElen Song {
1668055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1669*4e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1670731d9caeSNicolas Ferre 	u32 version;
1671055560b0SElen Song 	int usart, uart;
1672055560b0SElen Song 	/* usart and uart ascii */
1673055560b0SElen Song 	usart = 0x55534152;
1674055560b0SElen Song 	uart = 0x44424755;
1675055560b0SElen Song 
1676055560b0SElen Song 	atmel_port->is_usart = false;
1677055560b0SElen Song 
1678055560b0SElen Song 	if (name == usart) {
1679055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1680055560b0SElen Song 		atmel_port->is_usart = true;
1681055560b0SElen Song 	} else if (name == uart) {
1682055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1683055560b0SElen Song 		atmel_port->is_usart = false;
1684055560b0SElen Song 	} else {
1685731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
1686*4e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1687731d9caeSNicolas Ferre 		switch (version) {
1688731d9caeSNicolas Ferre 		case 0x302:
1689731d9caeSNicolas Ferre 		case 0x10213:
1690731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1691731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1692731d9caeSNicolas Ferre 			break;
1693731d9caeSNicolas Ferre 		case 0x203:
1694731d9caeSNicolas Ferre 		case 0x10202:
1695731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1696731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1697731d9caeSNicolas Ferre 			break;
1698731d9caeSNicolas Ferre 		default:
1699731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1700731d9caeSNicolas Ferre 		}
1701055560b0SElen Song 	}
1702055560b0SElen Song }
1703055560b0SElen Song 
1704ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port)
1705ab5e4e41SRichard Genoud {
1706ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1707ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1708ab5e4e41SRichard Genoud 
1709ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++)
1710ab5e4e41SRichard Genoud 		if (atmel_port->gpio_irq[i] >= 0)
1711ab5e4e41SRichard Genoud 			free_irq(atmel_port->gpio_irq[i], port);
1712ab5e4e41SRichard Genoud }
1713ab5e4e41SRichard Genoud 
1714ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port)
1715ab5e4e41SRichard Genoud {
1716ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1717ab5e4e41SRichard Genoud 	int *irq = atmel_port->gpio_irq;
1718ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1719ab5e4e41SRichard Genoud 	int err = 0;
1720ab5e4e41SRichard Genoud 
1721ab5e4e41SRichard Genoud 	for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1722ab5e4e41SRichard Genoud 		if (irq[i] < 0)
1723ab5e4e41SRichard Genoud 			continue;
1724ab5e4e41SRichard Genoud 
1725ab5e4e41SRichard Genoud 		irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1726ab5e4e41SRichard Genoud 		err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1727ab5e4e41SRichard Genoud 				  "atmel_serial", port);
1728ab5e4e41SRichard Genoud 		if (err)
1729ab5e4e41SRichard Genoud 			dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1730ab5e4e41SRichard Genoud 				irq[i]);
1731ab5e4e41SRichard Genoud 	}
1732ab5e4e41SRichard Genoud 
1733ab5e4e41SRichard Genoud 	/*
1734ab5e4e41SRichard Genoud 	 * If something went wrong, rollback.
1735ab5e4e41SRichard Genoud 	 */
1736ab5e4e41SRichard Genoud 	while (err && (--i >= 0))
1737ab5e4e41SRichard Genoud 		if (irq[i] >= 0)
1738ab5e4e41SRichard Genoud 			free_irq(irq[i], port);
1739ab5e4e41SRichard Genoud 
1740ab5e4e41SRichard Genoud 	return err;
1741ab5e4e41SRichard Genoud }
1742ab5e4e41SRichard Genoud 
1743055560b0SElen Song /*
1744ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1745ab4382d2SGreg Kroah-Hartman  */
1746ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1747ab4382d2SGreg Kroah-Hartman {
174833d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1749ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1750ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1751ab4382d2SGreg Kroah-Hartman 	int retval;
1752ab4382d2SGreg Kroah-Hartman 
1753ab4382d2SGreg Kroah-Hartman 	/*
1754ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1755ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1756ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1757ab4382d2SGreg Kroah-Hartman 	 */
1758*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1759ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1760ab4382d2SGreg Kroah-Hartman 
1761ab4382d2SGreg Kroah-Hartman 	/*
1762ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1763ab4382d2SGreg Kroah-Hartman 	 */
17642c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
17652c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1766ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1767ab4382d2SGreg Kroah-Hartman 	if (retval) {
1768ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1769ab4382d2SGreg Kroah-Hartman 		return retval;
1770ab4382d2SGreg Kroah-Hartman 	}
1771ab4382d2SGreg Kroah-Hartman 
1772ab4382d2SGreg Kroah-Hartman 	/*
1773ab5e4e41SRichard Genoud 	 * Get the GPIO lines IRQ
1774ab5e4e41SRichard Genoud 	 */
1775ab5e4e41SRichard Genoud 	retval = atmel_request_gpio_irq(port);
1776ab5e4e41SRichard Genoud 	if (retval)
1777ab5e4e41SRichard Genoud 		goto free_irq;
1778ab5e4e41SRichard Genoud 
17791e125786SLeilei Zhao 	tasklet_enable(&atmel_port->tasklet);
17801e125786SLeilei Zhao 
1781ab5e4e41SRichard Genoud 	/*
1782ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1783ab4382d2SGreg Kroah-Hartman 	 */
178433d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
17854d9628a1SLeilei Zhao 	atmel_set_ops(port);
178633d64c4fSElen Song 
1787a930e528SElen Song 	if (atmel_port->prepare_rx) {
1788a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1789a930e528SElen Song 		if (retval < 0)
1790a930e528SElen Song 			atmel_set_ops(port);
1791ab4382d2SGreg Kroah-Hartman 	}
1792ab4382d2SGreg Kroah-Hartman 
1793a930e528SElen Song 	if (atmel_port->prepare_tx) {
1794a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1795a930e528SElen Song 		if (retval < 0)
1796a930e528SElen Song 			atmel_set_ops(port);
1797ab4382d2SGreg Kroah-Hartman 	}
1798ab4382d2SGreg Kroah-Hartman 
1799ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1800e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1801ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1802ab4382d2SGreg Kroah-Hartman 
1803ab4382d2SGreg Kroah-Hartman 	/*
1804ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1805ab4382d2SGreg Kroah-Hartman 	 */
1806*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1807ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
1808*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1809ab4382d2SGreg Kroah-Hartman 
18102e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
18112e68c22fSElen Song 			atmel_uart_timer_callback,
18122e68c22fSElen Song 			(unsigned long)port);
18138bc661bfSMarek Roszko 
18148bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18158bc661bfSMarek Roszko 		/* set UART timeout */
18168bc661bfSMarek Roszko 		if (!atmel_port->is_usart) {
18172e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18182e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18192e68c22fSElen Song 		/* set USART timeout */
18202e68c22fSElen Song 		} else {
1821*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1822*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1823ab4382d2SGreg Kroah-Hartman 
1824*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
1825*4e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18262e68c22fSElen Song 		}
1827ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
1828*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
182934df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18302e68c22fSElen Song 		/* set UART timeout */
18312e68c22fSElen Song 		if (!atmel_port->is_usart) {
18322e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18332e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18342e68c22fSElen Song 		/* set USART timeout */
18352e68c22fSElen Song 		} else {
1836*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
1837*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
183834df42f5SElen Song 
1839*4e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
1840*4e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
18412e68c22fSElen Song 		}
1842ab4382d2SGreg Kroah-Hartman 	} else {
1843ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
1844*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1845ab4382d2SGreg Kroah-Hartman 	}
1846ab4382d2SGreg Kroah-Hartman 
1847ab4382d2SGreg Kroah-Hartman 	return 0;
1848ab5e4e41SRichard Genoud 
1849ab5e4e41SRichard Genoud free_irq:
1850ab5e4e41SRichard Genoud 	free_irq(port->irq, port);
1851ab5e4e41SRichard Genoud 
1852ab5e4e41SRichard Genoud 	return retval;
1853ab4382d2SGreg Kroah-Hartman }
1854ab4382d2SGreg Kroah-Hartman 
1855ab4382d2SGreg Kroah-Hartman /*
1856479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1857479e9b94SPeter Hurley  * buffer is reset.
1858479e9b94SPeter Hurley  */
1859479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1860479e9b94SPeter Hurley {
1861479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1862479e9b94SPeter Hurley 
1863479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
1864*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1865479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1866479e9b94SPeter Hurley 	}
1867479e9b94SPeter Hurley }
1868479e9b94SPeter Hurley 
1869479e9b94SPeter Hurley /*
1870ab4382d2SGreg Kroah-Hartman  * Disable the port
1871ab4382d2SGreg Kroah-Hartman  */
1872ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1873ab4382d2SGreg Kroah-Hartman {
1874ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
18750cc7c6c7SMarek Roszko 
1876ab4382d2SGreg Kroah-Hartman 	/*
18778bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
18788bc661bfSMarek Roszko 	 * cleanup
18798bc661bfSMarek Roszko 	 */
18808bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
18818bc661bfSMarek Roszko 
18828bc661bfSMarek Roszko 	/*
18830cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
18840cc7c6c7SMarek Roszko 	 * we destroy the buffers
18850cc7c6c7SMarek Roszko 	 */
18861e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
18870cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
18880cc7c6c7SMarek Roszko 
18890cc7c6c7SMarek Roszko 	/*
18900cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
18910cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1892ab4382d2SGreg Kroah-Hartman 	 */
1893ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1894ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1895ab4382d2SGreg Kroah-Hartman 
1896*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1897*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
18980cc7c6c7SMarek Roszko 
18990cc7c6c7SMarek Roszko 
1900ab4382d2SGreg Kroah-Hartman 	/*
1901ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1902ab4382d2SGreg Kroah-Hartman 	 */
1903a930e528SElen Song 	if (atmel_port->release_rx)
1904a930e528SElen Song 		atmel_port->release_rx(port);
1905a930e528SElen Song 	if (atmel_port->release_tx)
1906a930e528SElen Song 		atmel_port->release_tx(port);
1907ab4382d2SGreg Kroah-Hartman 
1908ab4382d2SGreg Kroah-Hartman 	/*
1909bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1910bb7e73c5SMark Deneen 	 */
1911bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1912bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1913bb7e73c5SMark Deneen 
1914bb7e73c5SMark Deneen 	/*
1915ab5e4e41SRichard Genoud 	 * Free the interrupts
1916ab4382d2SGreg Kroah-Hartman 	 */
1917ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1918ab5e4e41SRichard Genoud 	atmel_free_gpio_irq(port);
1919ab5e4e41SRichard Genoud 
1920ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1921ab4382d2SGreg Kroah-Hartman 
1922479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1923ab4382d2SGreg Kroah-Hartman }
1924ab4382d2SGreg Kroah-Hartman 
1925ab4382d2SGreg Kroah-Hartman /*
1926ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1927ab4382d2SGreg Kroah-Hartman  */
1928ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1929ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1930ab4382d2SGreg Kroah-Hartman {
1931ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1932ab4382d2SGreg Kroah-Hartman 
1933ab4382d2SGreg Kroah-Hartman 	switch (state) {
1934ab4382d2SGreg Kroah-Hartman 	case 0:
1935ab4382d2SGreg Kroah-Hartman 		/*
1936ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1937ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1938ab4382d2SGreg Kroah-Hartman 		 */
193991f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1940ab4382d2SGreg Kroah-Hartman 
1941ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1942*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
1943ab4382d2SGreg Kroah-Hartman 		break;
1944ab4382d2SGreg Kroah-Hartman 	case 3:
1945ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1946*4e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
1947*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
1948ab4382d2SGreg Kroah-Hartman 
1949ab4382d2SGreg Kroah-Hartman 		/*
1950ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1951ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1952ab4382d2SGreg Kroah-Hartman 		 */
195391f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1954ab4382d2SGreg Kroah-Hartman 		break;
1955ab4382d2SGreg Kroah-Hartman 	default:
1956ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1957ab4382d2SGreg Kroah-Hartman 	}
1958ab4382d2SGreg Kroah-Hartman }
1959ab4382d2SGreg Kroah-Hartman 
1960ab4382d2SGreg Kroah-Hartman /*
1961ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1962ab4382d2SGreg Kroah-Hartman  */
1963ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1964ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1965ab4382d2SGreg Kroah-Hartman {
1966ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
19671cf6e8fcSCyrille Pitchen 	unsigned int old_mode, mode, imr, quot, baud;
1968ab4382d2SGreg Kroah-Hartman 
19691cf6e8fcSCyrille Pitchen 	/* save the current mode register */
1970*4e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
19711cf6e8fcSCyrille Pitchen 
19721cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
19731cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
19741cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
1975ab4382d2SGreg Kroah-Hartman 
1976ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1977ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1978ab4382d2SGreg Kroah-Hartman 
1979ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1980ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1981ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1982ab4382d2SGreg Kroah-Hartman 	}
1983ab4382d2SGreg Kroah-Hartman 
1984ab4382d2SGreg Kroah-Hartman 	/* byte size */
1985ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1986ab4382d2SGreg Kroah-Hartman 	case CS5:
1987ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1988ab4382d2SGreg Kroah-Hartman 		break;
1989ab4382d2SGreg Kroah-Hartman 	case CS6:
1990ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1991ab4382d2SGreg Kroah-Hartman 		break;
1992ab4382d2SGreg Kroah-Hartman 	case CS7:
1993ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1994ab4382d2SGreg Kroah-Hartman 		break;
1995ab4382d2SGreg Kroah-Hartman 	default:
1996ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1997ab4382d2SGreg Kroah-Hartman 		break;
1998ab4382d2SGreg Kroah-Hartman 	}
1999ab4382d2SGreg Kroah-Hartman 
2000ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2001ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2002ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2003ab4382d2SGreg Kroah-Hartman 
2004ab4382d2SGreg Kroah-Hartman 	/* parity */
2005ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2006ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2007ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2008ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2009ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2010ab4382d2SGreg Kroah-Hartman 			else
2011ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2012ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2013ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2014ab4382d2SGreg Kroah-Hartman 		else
2015ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2016ab4382d2SGreg Kroah-Hartman 	} else
2017ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2018ab4382d2SGreg Kroah-Hartman 
2019ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2020ab4382d2SGreg Kroah-Hartman 
2021ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2022ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2023ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2024ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2025ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2026ab4382d2SGreg Kroah-Hartman 
202764e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2028ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
2029*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2030ab4382d2SGreg Kroah-Hartman 
2031ab4382d2SGreg Kroah-Hartman 	/*
2032ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2033ab4382d2SGreg Kroah-Hartman 	 */
2034ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2035ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2036ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2037ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2038ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2039ab4382d2SGreg Kroah-Hartman 		/*
2040ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2041ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2042ab4382d2SGreg Kroah-Hartman 		 */
2043ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2044ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2045ab4382d2SGreg Kroah-Hartman 	}
2046ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2047ab4382d2SGreg Kroah-Hartman 
2048ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2049ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2050ab4382d2SGreg Kroah-Hartman 
2051ab4382d2SGreg Kroah-Hartman 	/*
2052ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2053ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2054ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2055ab4382d2SGreg Kroah-Hartman 	 */
2056*4e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
2057*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2058ab4382d2SGreg Kroah-Hartman 
2059ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
2060*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2061ab4382d2SGreg Kroah-Hartman 
20621cf6e8fcSCyrille Pitchen 	/* mode */
206313bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
2064*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
2065*4e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2066ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
20671cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
20681cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
20691cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_HWHS;
20701cf6e8fcSCyrille Pitchen 	} else {
20711cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
20721cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2073ab4382d2SGreg Kroah-Hartman 	}
2074ab4382d2SGreg Kroah-Hartman 
20751cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
2076*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2077ab4382d2SGreg Kroah-Hartman 
20781cf6e8fcSCyrille Pitchen 	/*
20791cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
20801cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
20811cf6e8fcSCyrille Pitchen 	 */
20821cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
20831cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
20841cf6e8fcSCyrille Pitchen 
20851cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
20861cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
20871cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
20881cf6e8fcSCyrille Pitchen 		} else {
20891cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
20901cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
20911cf6e8fcSCyrille Pitchen 		}
20921cf6e8fcSCyrille Pitchen 
2093*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
20941cf6e8fcSCyrille Pitchen 	}
20951cf6e8fcSCyrille Pitchen 
2096ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
2097*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2098*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2099*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2100ab4382d2SGreg Kroah-Hartman 
2101ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
2102*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2103ab4382d2SGreg Kroah-Hartman 
2104ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2105ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
210635b675b9SRichard Genoud 		atmel_enable_ms(port);
210735b675b9SRichard Genoud 	else
210835b675b9SRichard Genoud 		atmel_disable_ms(port);
2109ab4382d2SGreg Kroah-Hartman 
2110ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2111ab4382d2SGreg Kroah-Hartman }
2112ab4382d2SGreg Kroah-Hartman 
2113732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
211442bd7a4fSViktar Palstsiuk {
2115732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
211642bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2117d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
211842bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2119d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
212042bd7a4fSViktar Palstsiuk 	} else {
212142bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2122cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2123cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2124cab68f89SPeter Hurley 			atmel_disable_ms(port);
2125cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2126cab68f89SPeter Hurley 		}
212742bd7a4fSViktar Palstsiuk 	}
212842bd7a4fSViktar Palstsiuk }
212942bd7a4fSViktar Palstsiuk 
2130ab4382d2SGreg Kroah-Hartman /*
2131ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2132ab4382d2SGreg Kroah-Hartman  */
2133ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2134ab4382d2SGreg Kroah-Hartman {
2135ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2136ab4382d2SGreg Kroah-Hartman }
2137ab4382d2SGreg Kroah-Hartman 
2138ab4382d2SGreg Kroah-Hartman /*
2139ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2140ab4382d2SGreg Kroah-Hartman  */
2141ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2142ab4382d2SGreg Kroah-Hartman {
2143ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2144ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2145ab4382d2SGreg Kroah-Hartman 
2146ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2147ab4382d2SGreg Kroah-Hartman 
2148ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2149ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2150ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2151ab4382d2SGreg Kroah-Hartman 	}
2152ab4382d2SGreg Kroah-Hartman }
2153ab4382d2SGreg Kroah-Hartman 
2154ab4382d2SGreg Kroah-Hartman /*
2155ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2156ab4382d2SGreg Kroah-Hartman  */
2157ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2158ab4382d2SGreg Kroah-Hartman {
2159ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2160ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2161ab4382d2SGreg Kroah-Hartman 
2162ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2163ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2164ab4382d2SGreg Kroah-Hartman 
2165ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2166ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2167ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2168ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2169ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2170ab4382d2SGreg Kroah-Hartman 		}
2171ab4382d2SGreg Kroah-Hartman 	}
2172ab4382d2SGreg Kroah-Hartman 
2173ab4382d2SGreg Kroah-Hartman 	return 0;
2174ab4382d2SGreg Kroah-Hartman }
2175ab4382d2SGreg Kroah-Hartman 
2176ab4382d2SGreg Kroah-Hartman /*
2177ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2178ab4382d2SGreg Kroah-Hartman  */
2179ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2180ab4382d2SGreg Kroah-Hartman {
2181ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2182ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2183ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2184ab4382d2SGreg Kroah-Hartman 	}
2185ab4382d2SGreg Kroah-Hartman }
2186ab4382d2SGreg Kroah-Hartman 
2187ab4382d2SGreg Kroah-Hartman /*
2188ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2189ab4382d2SGreg Kroah-Hartman  */
2190ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2191ab4382d2SGreg Kroah-Hartman {
2192ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2193ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2194ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2195ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2196ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2197ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2198ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2199ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2200ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2201ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
2202ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2203ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2204ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2205ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2206ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2207ab4382d2SGreg Kroah-Hartman 	return ret;
2208ab4382d2SGreg Kroah-Hartman }
2209ab4382d2SGreg Kroah-Hartman 
2210ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2211ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2212ab4382d2SGreg Kroah-Hartman {
2213*4e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2214ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2215ab4382d2SGreg Kroah-Hartman 
2216*4e7decdaSCyrille Pitchen 	return atmel_uart_readl(port, ATMEL_US_RHR);
2217ab4382d2SGreg Kroah-Hartman }
2218ab4382d2SGreg Kroah-Hartman 
2219ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2220ab4382d2SGreg Kroah-Hartman {
2221*4e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2222ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2223ab4382d2SGreg Kroah-Hartman 
2224*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_THR, ch);
2225ab4382d2SGreg Kroah-Hartman }
2226ab4382d2SGreg Kroah-Hartman #endif
2227ab4382d2SGreg Kroah-Hartman 
2228ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2229ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2230ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2231ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2232ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2233ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2234ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2235ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2236ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2237ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2238ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2239ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2240ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
224142bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2242ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2243ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2244ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2245ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2246ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2247ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2248ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2249ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2250ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2251ab4382d2SGreg Kroah-Hartman #endif
2252ab4382d2SGreg Kroah-Hartman };
2253ab4382d2SGreg Kroah-Hartman 
2254ab4382d2SGreg Kroah-Hartman /*
2255ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2256ab4382d2SGreg Kroah-Hartman  */
225791f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2258ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2259ab4382d2SGreg Kroah-Hartman {
226091f8c2d8SBoris BREZILLON 	int ret;
2261ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2262574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2263ab4382d2SGreg Kroah-Hartman 
22644a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2265a930e528SElen Song 	atmel_set_ops(port);
2266a930e528SElen Song 
226713bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
226833d64c4fSElen Song 
2269ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2270ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2271ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2272ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2273ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2274ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2275ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
227613bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2277ab4382d2SGreg Kroah-Hartman 
2278ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2279ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
22801e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
2281ab4382d2SGreg Kroah-Hartman 
2282ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2283ab4382d2SGreg Kroah-Hartman 
22845fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2285ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
22861acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2287588edbf3SNicolas Ferre 	} else {
2288ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2289ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2290ab4382d2SGreg Kroah-Hartman 	}
2291ab4382d2SGreg Kroah-Hartman 
2292ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2293ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2294ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
229591f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
229691f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
229791f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
229891f8c2d8SBoris BREZILLON 			return ret;
229991f8c2d8SBoris BREZILLON 		}
230091f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
230191f8c2d8SBoris BREZILLON 		if (ret) {
230291f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
230391f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
230491f8c2d8SBoris BREZILLON 			return ret;
230591f8c2d8SBoris BREZILLON 		}
2306ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
230791f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2308ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2309ab4382d2SGreg Kroah-Hartman 	}
2310ab4382d2SGreg Kroah-Hartman 
2311ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
231213bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2313ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
231464e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2315ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2316ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2317ab4382d2SGreg Kroah-Hartman 	} else {
2318ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2319ab4382d2SGreg Kroah-Hartman 	}
232091f8c2d8SBoris BREZILLON 
232191f8c2d8SBoris BREZILLON 	return 0;
2322ab4382d2SGreg Kroah-Hartman }
2323ab4382d2SGreg Kroah-Hartman 
232469f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
232569f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2326ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2327ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2328ab4382d2SGreg Kroah-Hartman {
2329*4e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2330ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2331*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_THR, ch);
2332ab4382d2SGreg Kroah-Hartman }
2333ab4382d2SGreg Kroah-Hartman 
2334ab4382d2SGreg Kroah-Hartman /*
2335ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2336ab4382d2SGreg Kroah-Hartman  */
2337ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2338ab4382d2SGreg Kroah-Hartman {
2339ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2340ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2341ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2342ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2343ab4382d2SGreg Kroah-Hartman 
2344ab4382d2SGreg Kroah-Hartman 	/*
2345ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2346ab4382d2SGreg Kroah-Hartman 	 */
2347*4e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
2348*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
2349*4e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2350ab4382d2SGreg Kroah-Hartman 
2351ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
2352*4e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2353*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2354ab4382d2SGreg Kroah-Hartman 
2355ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2356ab4382d2SGreg Kroah-Hartman 
2357ab4382d2SGreg Kroah-Hartman 	/*
2358ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2359ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2360ab4382d2SGreg Kroah-Hartman 	 */
2361ab4382d2SGreg Kroah-Hartman 	do {
2362*4e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2363ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2364ab4382d2SGreg Kroah-Hartman 
2365ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2366ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
2367*4e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2368ab4382d2SGreg Kroah-Hartman 
2369ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
2370*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2371ab4382d2SGreg Kroah-Hartman }
2372ab4382d2SGreg Kroah-Hartman 
2373ab4382d2SGreg Kroah-Hartman /*
2374ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2375ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2376ab4382d2SGreg Kroah-Hartman  */
2377ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2378ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2379ab4382d2SGreg Kroah-Hartman {
2380ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2381ab4382d2SGreg Kroah-Hartman 
2382ab4382d2SGreg Kroah-Hartman 	/*
2383ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2384ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2385ab4382d2SGreg Kroah-Hartman 	 */
2386*4e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2387ab4382d2SGreg Kroah-Hartman 	if (!quot)
2388ab4382d2SGreg Kroah-Hartman 		return;
2389ab4382d2SGreg Kroah-Hartman 
2390*4e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2391ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2392ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2393ab4382d2SGreg Kroah-Hartman 	else
2394ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2395ab4382d2SGreg Kroah-Hartman 
2396*4e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2397ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2398ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2399ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2400ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2401ab4382d2SGreg Kroah-Hartman 
2402ab4382d2SGreg Kroah-Hartman 	/*
2403ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2404ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2405ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2406ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2407ab4382d2SGreg Kroah-Hartman 	 */
2408ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2409ab4382d2SGreg Kroah-Hartman }
2410ab4382d2SGreg Kroah-Hartman 
2411ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2412ab4382d2SGreg Kroah-Hartman {
241391f8c2d8SBoris BREZILLON 	int ret;
2414ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2415ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2416ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2417ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2418ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2419ab4382d2SGreg Kroah-Hartman 
2420ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2421ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2422ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2423ab4382d2SGreg Kroah-Hartman 	}
2424ab4382d2SGreg Kroah-Hartman 
242591f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
242691f8c2d8SBoris BREZILLON 	if (ret)
242791f8c2d8SBoris BREZILLON 		return ret;
2428ab4382d2SGreg Kroah-Hartman 
2429*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2430*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2431*4e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2432ab4382d2SGreg Kroah-Hartman 
2433ab4382d2SGreg Kroah-Hartman 	if (options)
2434ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2435ab4382d2SGreg Kroah-Hartman 	else
2436ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2437ab4382d2SGreg Kroah-Hartman 
2438ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2439ab4382d2SGreg Kroah-Hartman }
2440ab4382d2SGreg Kroah-Hartman 
2441ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2444ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2445ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2446ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2447ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2448ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2449ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2450ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2451ab4382d2SGreg Kroah-Hartman };
2452ab4382d2SGreg Kroah-Hartman 
2453ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2454ab4382d2SGreg Kroah-Hartman 
2455ab4382d2SGreg Kroah-Hartman /*
2456ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2457ab4382d2SGreg Kroah-Hartman  */
2458ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2459ab4382d2SGreg Kroah-Hartman {
246091f8c2d8SBoris BREZILLON 	int ret;
2461ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
24620d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2463574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2464efb8d21bSLinus Torvalds 		int id = pdata->num;
24654cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
24660d0a3cc1SVoss, Nikolaus 
24674cbf9f48SNicolas Ferre 		port->backup_imr = 0;
24684cbf9f48SNicolas Ferre 		port->uart.line = id;
24694cbf9f48SNicolas Ferre 
24704cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
247191f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
247291f8c2d8SBoris BREZILLON 		if (ret)
247391f8c2d8SBoris BREZILLON 			return ret;
2474ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2475ab4382d2SGreg Kroah-Hartman 	}
2476ab4382d2SGreg Kroah-Hartman 
2477ab4382d2SGreg Kroah-Hartman 	return 0;
2478ab4382d2SGreg Kroah-Hartman }
2479ab4382d2SGreg Kroah-Hartman 
2480ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2481ab4382d2SGreg Kroah-Hartman 
2482ab4382d2SGreg Kroah-Hartman /*
2483ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2484ab4382d2SGreg Kroah-Hartman  */
2485ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2486ab4382d2SGreg Kroah-Hartman {
2487ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2488ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2489ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2490ab4382d2SGreg Kroah-Hartman 
2491ab4382d2SGreg Kroah-Hartman 	return 0;
2492ab4382d2SGreg Kroah-Hartman }
2493ab4382d2SGreg Kroah-Hartman 
2494ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2495ab4382d2SGreg Kroah-Hartman 
2496ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2497ab4382d2SGreg Kroah-Hartman {
2498ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2499ab4382d2SGreg Kroah-Hartman }
2500ab4382d2SGreg Kroah-Hartman 
2501ab4382d2SGreg Kroah-Hartman #else
2502ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2503ab4382d2SGreg Kroah-Hartman 
2504ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2505ab4382d2SGreg Kroah-Hartman {
2506ab4382d2SGreg Kroah-Hartman 	return false;
2507ab4382d2SGreg Kroah-Hartman }
2508ab4382d2SGreg Kroah-Hartman #endif
2509ab4382d2SGreg Kroah-Hartman 
2510ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2511ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2512ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2513ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2514ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2515ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2516ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2517ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2518ab4382d2SGreg Kroah-Hartman };
2519ab4382d2SGreg Kroah-Hartman 
2520ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2521ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2522ab4382d2SGreg Kroah-Hartman {
2523ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2524ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2525ab4382d2SGreg Kroah-Hartman #else
2526ab4382d2SGreg Kroah-Hartman 	return false;
2527ab4382d2SGreg Kroah-Hartman #endif
2528ab4382d2SGreg Kroah-Hartman }
2529ab4382d2SGreg Kroah-Hartman 
2530ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2531ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2532ab4382d2SGreg Kroah-Hartman {
2533ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2534ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2535ab4382d2SGreg Kroah-Hartman 
2536ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2537ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
2538*4e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2539*4e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2540ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2541ab4382d2SGreg Kroah-Hartman 	}
2542ab4382d2SGreg Kroah-Hartman 
2543ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2544ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
25452c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
25462c7af5baSBoris BREZILLON 		unsigned long flags;
25472c7af5baSBoris BREZILLON 
25482c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25492c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
25502c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2551ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
25522c7af5baSBoris BREZILLON 	}
2553ab4382d2SGreg Kroah-Hartman 
2554ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2555ab4382d2SGreg Kroah-Hartman 
2556ab4382d2SGreg Kroah-Hartman 	return 0;
2557ab4382d2SGreg Kroah-Hartman }
2558ab4382d2SGreg Kroah-Hartman 
2559ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2560ab4382d2SGreg Kroah-Hartman {
2561ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2562ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
25632c7af5baSBoris BREZILLON 	unsigned long flags;
25642c7af5baSBoris BREZILLON 
25652c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25662c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
25672c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
25682c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
25692c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
25702c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
25712c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
25722c7af5baSBoris BREZILLON 	}
25732c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
25742c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2575ab4382d2SGreg Kroah-Hartman 
2576ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2577ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2578ab4382d2SGreg Kroah-Hartman 
2579ab4382d2SGreg Kroah-Hartman 	return 0;
2580ab4382d2SGreg Kroah-Hartman }
2581ab4382d2SGreg Kroah-Hartman #else
2582ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2583ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2584ab4382d2SGreg Kroah-Hartman #endif
2585ab4382d2SGreg Kroah-Hartman 
2586e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2587e0b0baadSRichard Genoud {
2588ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
2589ab5e4e41SRichard Genoud 	struct gpio_desc *gpiod;
2590ab5e4e41SRichard Genoud 
2591e0b0baadSRichard Genoud 	p->gpios = mctrl_gpio_init(dev, 0);
2592722ccf41SUwe Kleine-König 	if (IS_ERR(p->gpios))
2593722ccf41SUwe Kleine-König 		return PTR_ERR(p->gpios);
2594e0b0baadSRichard Genoud 
2595ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++) {
2596ab5e4e41SRichard Genoud 		gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2597ab5e4e41SRichard Genoud 		if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2598ab5e4e41SRichard Genoud 			p->gpio_irq[i] = gpiod_to_irq(gpiod);
2599ab5e4e41SRichard Genoud 		else
2600ab5e4e41SRichard Genoud 			p->gpio_irq[i] = -EINVAL;
2601ab5e4e41SRichard Genoud 	}
2602ab5e4e41SRichard Genoud 
2603e0b0baadSRichard Genoud 	return 0;
2604e0b0baadSRichard Genoud }
2605e0b0baadSRichard Genoud 
26069671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2607ab4382d2SGreg Kroah-Hartman {
2608ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
26095fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2610574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2611ab4382d2SGreg Kroah-Hartman 	void *data;
26124cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2613bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2614ab4382d2SGreg Kroah-Hartman 
2615ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2616ab4382d2SGreg Kroah-Hartman 
26175fbe46b6SNicolas Ferre 	if (np)
26185fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
26195fbe46b6SNicolas Ferre 	else
26204cbf9f48SNicolas Ferre 		if (pdata)
26214cbf9f48SNicolas Ferre 			ret = pdata->num;
26224cbf9f48SNicolas Ferre 
26234cbf9f48SNicolas Ferre 	if (ret < 0)
26245fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
26254cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2626503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
26274cbf9f48SNicolas Ferre 
2628503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
26294cbf9f48SNicolas Ferre 		ret = -ENODEV;
26304cbf9f48SNicolas Ferre 		goto err;
26314cbf9f48SNicolas Ferre 	}
26324cbf9f48SNicolas Ferre 
2633503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
26344cbf9f48SNicolas Ferre 		/* port already in use */
26354cbf9f48SNicolas Ferre 		ret = -EBUSY;
26364cbf9f48SNicolas Ferre 		goto err;
26374cbf9f48SNicolas Ferre 	}
26384cbf9f48SNicolas Ferre 
26394cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2640ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
26414cbf9f48SNicolas Ferre 	port->uart.line = ret;
2642354e57f3SLinus Walleij 
26432c7af5baSBoris BREZILLON 	spin_lock_init(&port->lock_suspended);
26442c7af5baSBoris BREZILLON 
2645e0b0baadSRichard Genoud 	ret = atmel_init_gpios(port, &pdev->dev);
2646722ccf41SUwe Kleine-König 	if (ret < 0) {
2647722ccf41SUwe Kleine-König 		dev_err(&pdev->dev, "Failed to initialize GPIOs.");
2648722ccf41SUwe Kleine-König 		goto err;
2649722ccf41SUwe Kleine-König 	}
2650ab4382d2SGreg Kroah-Hartman 
265191f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
265291f8c2d8SBoris BREZILLON 	if (ret)
26536fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2654ab4382d2SGreg Kroah-Hartman 
265564e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2656ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2657ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2658ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2659ab4382d2SGreg Kroah-Hartman 		if (!data)
2660ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2661ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2662ab4382d2SGreg Kroah-Hartman 	}
2663ab4382d2SGreg Kroah-Hartman 
2664bd737f87SRicardo Ribalda Delgado 	rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2665bd737f87SRicardo Ribalda Delgado 
2666ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2667ab4382d2SGreg Kroah-Hartman 	if (ret)
2668ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2669ab4382d2SGreg Kroah-Hartman 
2670ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2671ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2672ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2673ab4382d2SGreg Kroah-Hartman 		/*
2674ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
267591f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2676ab4382d2SGreg Kroah-Hartman 		 */
267791f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2678ab4382d2SGreg Kroah-Hartman 	}
2679ab4382d2SGreg Kroah-Hartman #endif
2680ab4382d2SGreg Kroah-Hartman 
2681ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2682ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2683ab4382d2SGreg Kroah-Hartman 
2684d4f64187SCyrille Pitchen 	/*
2685d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2686d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2687d4f64187SCyrille Pitchen 	 */
2688d4f64187SCyrille Pitchen 	clk_prepare_enable(port->clk);
2689d4f64187SCyrille Pitchen 
2690bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2691*4e7decdaSCyrille Pitchen 		atmel_uart_writel(&port->uart, ATMEL_US_MR,
2692*4e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2693*4e7decdaSCyrille Pitchen 		atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
2694fc887b15SLinus Torvalds 	}
2695fc887b15SLinus Torvalds 
2696055560b0SElen Song 	/*
2697055560b0SElen Song 	 * Get port name of usart or uart
2698055560b0SElen Song 	 */
2699892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2700055560b0SElen Song 
2701d4f64187SCyrille Pitchen 	/*
2702d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2703d4f64187SCyrille Pitchen 	 * is used
2704d4f64187SCyrille Pitchen 	 */
2705d4f64187SCyrille Pitchen 	clk_disable_unprepare(port->clk);
2706d4f64187SCyrille Pitchen 
2707ab4382d2SGreg Kroah-Hartman 	return 0;
2708ab4382d2SGreg Kroah-Hartman 
2709ab4382d2SGreg Kroah-Hartman err_add_port:
2710ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2711ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2712ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2713ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2714ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2715ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2716ab4382d2SGreg Kroah-Hartman 	}
27176fbb9bdfSCyrille Pitchen err_clear_bit:
27186fbb9bdfSCyrille Pitchen 	clear_bit(port->uart.line, atmel_ports_in_use);
27194cbf9f48SNicolas Ferre err:
2720ab4382d2SGreg Kroah-Hartman 	return ret;
2721ab4382d2SGreg Kroah-Hartman }
2722ab4382d2SGreg Kroah-Hartman 
2723ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2724ab4382d2SGreg Kroah-Hartman {
2725ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2726ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2727ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2728ab4382d2SGreg Kroah-Hartman 
2729f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2730f50c995fSMarek Roszko 
2731ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2732ab4382d2SGreg Kroah-Hartman 
2733ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2734ab4382d2SGreg Kroah-Hartman 
2735ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2736ab4382d2SGreg Kroah-Hartman 
2737ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2738ab4382d2SGreg Kroah-Hartman 
2739503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
27404cbf9f48SNicolas Ferre 
2741ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2742ab4382d2SGreg Kroah-Hartman 
2743ab4382d2SGreg Kroah-Hartman 	return ret;
2744ab4382d2SGreg Kroah-Hartman }
2745ab4382d2SGreg Kroah-Hartman 
2746ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2747ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
27482d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2749ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2750ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2751ab4382d2SGreg Kroah-Hartman 	.driver		= {
2752ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
27535fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2754ab4382d2SGreg Kroah-Hartman 	},
2755ab4382d2SGreg Kroah-Hartman };
2756ab4382d2SGreg Kroah-Hartman 
2757ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2758ab4382d2SGreg Kroah-Hartman {
2759ab4382d2SGreg Kroah-Hartman 	int ret;
2760ab4382d2SGreg Kroah-Hartman 
2761ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2762ab4382d2SGreg Kroah-Hartman 	if (ret)
2763ab4382d2SGreg Kroah-Hartman 		return ret;
2764ab4382d2SGreg Kroah-Hartman 
2765ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2766ab4382d2SGreg Kroah-Hartman 	if (ret)
2767ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2768ab4382d2SGreg Kroah-Hartman 
2769ab4382d2SGreg Kroah-Hartman 	return ret;
2770ab4382d2SGreg Kroah-Hartman }
2771ab4382d2SGreg Kroah-Hartman 
2772ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2773ab4382d2SGreg Kroah-Hartman {
2774ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2775ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2776ab4382d2SGreg Kroah-Hartman }
2777ab4382d2SGreg Kroah-Hartman 
2778ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2779ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2780ab4382d2SGreg Kroah-Hartman 
2781ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2782ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2783ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2784ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2785