xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 270c2adef30e1912002ba014c16c08b86706d01f)
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 
59b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
60b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
61b5199d46SCyrille Pitchen /*
62b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
63b5199d46SCyrille Pitchen  * high and low thresholds
64b5199d46SCyrille Pitchen  */
65b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
66b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
67b5199d46SCyrille Pitchen 
68ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
69ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
70ab4382d2SGreg Kroah-Hartman #endif
71ab4382d2SGreg Kroah-Hartman 
72ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
73ab4382d2SGreg Kroah-Hartman 
74e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
75e0b0baadSRichard Genoud 
76ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
77ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
78ab4382d2SGreg Kroah-Hartman 
79ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
80ab4382d2SGreg Kroah-Hartman 
81ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
82ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
83ab4382d2SGreg Kroah-Hartman  * UART. */
84ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
85ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
86ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
87ab4382d2SGreg Kroah-Hartman 
88ab4382d2SGreg Kroah-Hartman #else
89ab4382d2SGreg Kroah-Hartman 
90ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
91ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
92ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
93ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
94ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
95ab4382d2SGreg Kroah-Hartman 
96ab4382d2SGreg Kroah-Hartman #endif
97ab4382d2SGreg Kroah-Hartman 
98ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
99ab4382d2SGreg Kroah-Hartman 
100ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
101ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
102ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
103ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
104ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
105ab4382d2SGreg Kroah-Hartman };
106ab4382d2SGreg Kroah-Hartman 
107ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
108ab4382d2SGreg Kroah-Hartman 	u16		status;
109ab4382d2SGreg Kroah-Hartman 	u16		ch;
110ab4382d2SGreg Kroah-Hartman };
111ab4382d2SGreg Kroah-Hartman 
112ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
113ab4382d2SGreg Kroah-Hartman 
114ab4382d2SGreg Kroah-Hartman /*
1159af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
1169af92fbfSAlexandre Belloni  * avr32: 4
1179af92fbfSAlexandre Belloni  */
1189af92fbfSAlexandre Belloni #define ATMEL_MAX_UART		7
1199af92fbfSAlexandre Belloni 
1209af92fbfSAlexandre Belloni /*
121ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
122ab4382d2SGreg Kroah-Hartman  */
123ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
124ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
125ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
126ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
127ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
128ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
129ab4382d2SGreg Kroah-Hartman 
13034df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
13164e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
132ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
133ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
134ab4382d2SGreg Kroah-Hartman 
13508f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
13664e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
137ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
138ab4382d2SGreg Kroah-Hartman 
13908f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
14034df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
14108f738beSElen Song 	struct dma_chan			*chan_tx;
14234df42f5SElen Song 	struct dma_chan			*chan_rx;
14308f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
14434df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
14508f738beSElen Song 	dma_cookie_t			cookie_tx;
14634df42f5SElen Song 	dma_cookie_t			cookie_rx;
14708f738beSElen Song 	struct scatterlist		sg_tx;
14834df42f5SElen Song 	struct scatterlist		sg_rx;
149ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
150ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
151ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
152d033e82dSLeilei Zhao 	unsigned int		status_change;
1535f258b3eSCyrille Pitchen 	unsigned int		tx_len;
154ab4382d2SGreg Kroah-Hartman 
155ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
156ab4382d2SGreg Kroah-Hartman 
157e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
158ab5e4e41SRichard Genoud 	int			gpio_irq[UART_GPIO_MAX];
159ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
160b5199d46SCyrille Pitchen 	u32			fifo_size;
161b5199d46SCyrille Pitchen 	u32			rts_high;
162b5199d46SCyrille Pitchen 	u32			rts_low;
163ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
164055560b0SElen Song 	bool			is_usart;	/* usart or uart */
1652e68c22fSElen Song 	struct timer_list	uart_timer;	/* uart timer */
1662c7af5baSBoris BREZILLON 
1672c7af5baSBoris BREZILLON 	bool			suspended;
1682c7af5baSBoris BREZILLON 	unsigned int		pending;
1692c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1702c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1712c7af5baSBoris BREZILLON 
172a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
173a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
174a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
175a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
176a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
177a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
178ab4382d2SGreg Kroah-Hartman };
179ab4382d2SGreg Kroah-Hartman 
180ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
181503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
182ab4382d2SGreg Kroah-Hartman 
183ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
184ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
185ab4382d2SGreg Kroah-Hartman #endif
186ab4382d2SGreg Kroah-Hartman 
1875fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1885fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1895fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1905fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1915fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1925fbe46b6SNicolas Ferre };
1935fbe46b6SNicolas Ferre 
1945fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
1955fbe46b6SNicolas Ferre #endif
1965fbe46b6SNicolas Ferre 
197ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
198ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
199ab4382d2SGreg Kroah-Hartman {
200ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
201ab4382d2SGreg Kroah-Hartman }
202ab4382d2SGreg Kroah-Hartman 
2034e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2044e7decdaSCyrille Pitchen {
2054e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2064e7decdaSCyrille Pitchen }
2074e7decdaSCyrille Pitchen 
2084e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2094e7decdaSCyrille Pitchen {
2104e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2114e7decdaSCyrille Pitchen }
2124e7decdaSCyrille Pitchen 
213a6499435SCyrille Pitchen #ifdef CONFIG_AVR32
214a6499435SCyrille Pitchen 
215a6499435SCyrille Pitchen /* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
216a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
217b5199d46SCyrille Pitchen {
218a6499435SCyrille Pitchen 	return __raw_readl(port->membase + ATMEL_US_RHR);
219b5199d46SCyrille Pitchen }
220b5199d46SCyrille Pitchen 
221a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
222b5199d46SCyrille Pitchen {
223a6499435SCyrille Pitchen 	__raw_writel(value, port->membase + ATMEL_US_THR);
224b5199d46SCyrille Pitchen }
225b5199d46SCyrille Pitchen 
226a6499435SCyrille Pitchen #else
227a6499435SCyrille Pitchen 
228a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
229a6499435SCyrille Pitchen {
230a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
231a6499435SCyrille Pitchen }
232a6499435SCyrille Pitchen 
233a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
234a6499435SCyrille Pitchen {
235a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
236a6499435SCyrille Pitchen }
237a6499435SCyrille Pitchen 
238a6499435SCyrille Pitchen #endif
239a6499435SCyrille Pitchen 
240ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
24164e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
242ab4382d2SGreg Kroah-Hartman {
243ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
244ab4382d2SGreg Kroah-Hartman 
24564e22ebeSElen Song 	return atmel_port->use_pdc_rx;
246ab4382d2SGreg Kroah-Hartman }
247ab4382d2SGreg Kroah-Hartman 
24864e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
249ab4382d2SGreg Kroah-Hartman {
250ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
251ab4382d2SGreg Kroah-Hartman 
25264e22ebeSElen Song 	return atmel_port->use_pdc_tx;
253ab4382d2SGreg Kroah-Hartman }
254ab4382d2SGreg Kroah-Hartman #else
25564e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
256ab4382d2SGreg Kroah-Hartman {
257ab4382d2SGreg Kroah-Hartman 	return false;
258ab4382d2SGreg Kroah-Hartman }
259ab4382d2SGreg Kroah-Hartman 
26064e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
261ab4382d2SGreg Kroah-Hartman {
262ab4382d2SGreg Kroah-Hartman 	return false;
263ab4382d2SGreg Kroah-Hartman }
264ab4382d2SGreg Kroah-Hartman #endif
265ab4382d2SGreg Kroah-Hartman 
26608f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26708f738beSElen Song {
26808f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26908f738beSElen Song 
27008f738beSElen Song 	return atmel_port->use_dma_tx;
27108f738beSElen Song }
27208f738beSElen Song 
27334df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27434df42f5SElen Song {
27534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27634df42f5SElen Song 
27734df42f5SElen Song 	return atmel_port->use_dma_rx;
27834df42f5SElen Song }
27934df42f5SElen Song 
280e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
281e0b0baadSRichard Genoud {
282e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
283e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
284e0b0baadSRichard Genoud 
2854e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
286e0b0baadSRichard Genoud 
287e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
288e0b0baadSRichard Genoud 
289e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
290e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
291e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
292e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
293e0b0baadSRichard Genoud 		else
294e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
295e0b0baadSRichard Genoud 	}
296e0b0baadSRichard Genoud 
297e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
298e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
299e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
300e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
301e0b0baadSRichard Genoud 		else
302e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
303e0b0baadSRichard Genoud 	}
304e0b0baadSRichard Genoud 
305e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
306e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
307e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
308e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
309e0b0baadSRichard Genoud 		else
310e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
311e0b0baadSRichard Genoud 	}
312e0b0baadSRichard Genoud 
313e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
314e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
315e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
316e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
317e0b0baadSRichard Genoud 		else
318e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
319e0b0baadSRichard Genoud 	}
320e0b0baadSRichard Genoud 
321e0b0baadSRichard Genoud 	return status;
322e0b0baadSRichard Genoud }
323e0b0baadSRichard Genoud 
324ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
32513bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
32613bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
327ab4382d2SGreg Kroah-Hartman {
328ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
329ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
330ab4382d2SGreg Kroah-Hartman 
331ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3324e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
333ab4382d2SGreg Kroah-Hartman 
3344e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
335ab4382d2SGreg Kroah-Hartman 
336ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
337ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
338ab4382d2SGreg Kroah-Hartman 
33913bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
340ab4382d2SGreg Kroah-Hartman 
341ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
342ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
343ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3444e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3454e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
346ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
347ab4382d2SGreg Kroah-Hartman 	} else {
348ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
34964e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
350ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
351ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
352ab4382d2SGreg Kroah-Hartman 		else
353ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
354ab4382d2SGreg Kroah-Hartman 	}
3554e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
356ab4382d2SGreg Kroah-Hartman 
357ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3584e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
359ab4382d2SGreg Kroah-Hartman 
36013bd3e6fSRicardo Ribalda Delgado 	return 0;
361ab4382d2SGreg Kroah-Hartman }
362ab4382d2SGreg Kroah-Hartman 
363ab4382d2SGreg Kroah-Hartman /*
364ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
365ab4382d2SGreg Kroah-Hartman  */
366ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
367ab4382d2SGreg Kroah-Hartman {
3684e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3694e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3704e7decdaSCyrille Pitchen 		0;
371ab4382d2SGreg Kroah-Hartman }
372ab4382d2SGreg Kroah-Hartman 
373ab4382d2SGreg Kroah-Hartman /*
374ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
375ab4382d2SGreg Kroah-Hartman  */
376ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
377ab4382d2SGreg Kroah-Hartman {
378ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3794e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3801cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
381ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
382ab4382d2SGreg Kroah-Hartman 
3831cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3841cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
3854e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3864e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
3871cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3881cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3891cf6e8fcSCyrille Pitchen 	}
3901cf6e8fcSCyrille Pitchen 
3911cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
3921cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
3931cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3941cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
3951cf6e8fcSCyrille Pitchen 
3961cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
3971cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
3981cf6e8fcSCyrille Pitchen 	} else {
3991cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4001cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4011cf6e8fcSCyrille Pitchen 
4021cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4031cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4041cf6e8fcSCyrille Pitchen 	}
4051cf6e8fcSCyrille Pitchen 
406ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4071cf6e8fcSCyrille Pitchen 		control |= rts_ready;
408ab4382d2SGreg Kroah-Hartman 	else
4091cf6e8fcSCyrille Pitchen 		control |= rts_paused;
410ab4382d2SGreg Kroah-Hartman 
411ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
412ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
413ab4382d2SGreg Kroah-Hartman 	else
414ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
415ab4382d2SGreg Kroah-Hartman 
4164e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
417ab4382d2SGreg Kroah-Hartman 
418e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
419e0b0baadSRichard Genoud 
420ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
4211cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
422ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
423ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
424ab4382d2SGreg Kroah-Hartman 	else
425ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
426ab4382d2SGreg Kroah-Hartman 
4274e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
428ab4382d2SGreg Kroah-Hartman }
429ab4382d2SGreg Kroah-Hartman 
430ab4382d2SGreg Kroah-Hartman /*
431ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
432ab4382d2SGreg Kroah-Hartman  */
433ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
434ab4382d2SGreg Kroah-Hartman {
435e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
436e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
437ab4382d2SGreg Kroah-Hartman 
4384e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
439ab4382d2SGreg Kroah-Hartman 
440ab4382d2SGreg Kroah-Hartman 	/*
441ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
442ab4382d2SGreg Kroah-Hartman 	 */
443ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
444ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
445ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
446ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
447ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
448ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
449ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
450ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
451ab4382d2SGreg Kroah-Hartman 
452e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
453ab4382d2SGreg Kroah-Hartman }
454ab4382d2SGreg Kroah-Hartman 
455ab4382d2SGreg Kroah-Hartman /*
456ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
457ab4382d2SGreg Kroah-Hartman  */
458ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
459ab4382d2SGreg Kroah-Hartman {
460ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
461ab4382d2SGreg Kroah-Hartman 
46264e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
463ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4644e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
465ab4382d2SGreg Kroah-Hartman 	}
466ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
468ab4382d2SGreg Kroah-Hartman 
46913bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
47013bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
471ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
472ab4382d2SGreg Kroah-Hartman }
473ab4382d2SGreg Kroah-Hartman 
474ab4382d2SGreg Kroah-Hartman /*
475ab4382d2SGreg Kroah-Hartman  * Start transmitting.
476ab4382d2SGreg Kroah-Hartman  */
477ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
478ab4382d2SGreg Kroah-Hartman {
479ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
480ab4382d2SGreg Kroah-Hartman 
48164e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
4824e7decdaSCyrille Pitchen 		if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
483ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
484ab4382d2SGreg Kroah-Hartman 			   really need this.*/
485ab4382d2SGreg Kroah-Hartman 			return;
486ab4382d2SGreg Kroah-Hartman 
48713bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
48813bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
489ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
490ab4382d2SGreg Kroah-Hartman 
491ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
4924e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
493ab4382d2SGreg Kroah-Hartman 	}
494ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
4954e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
496ab4382d2SGreg Kroah-Hartman }
497ab4382d2SGreg Kroah-Hartman 
498ab4382d2SGreg Kroah-Hartman /*
499ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
500ab4382d2SGreg Kroah-Hartman  */
501ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
502ab4382d2SGreg Kroah-Hartman {
5034e7decdaSCyrille Pitchen 	/* reset status and receiver */
5044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
505ab4382d2SGreg Kroah-Hartman 
5064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
50757c36868SSiftar, Gabe 
50864e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
509ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
5104e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
5114e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
512ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
5134e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
514ab4382d2SGreg Kroah-Hartman 	} else {
5154e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
516ab4382d2SGreg Kroah-Hartman 	}
517ab4382d2SGreg Kroah-Hartman }
518ab4382d2SGreg Kroah-Hartman 
519ab4382d2SGreg Kroah-Hartman /*
520ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
521ab4382d2SGreg Kroah-Hartman  */
522ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
523ab4382d2SGreg Kroah-Hartman {
5244e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
52557c36868SSiftar, Gabe 
52664e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
527ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5284e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5294e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5304e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
531ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
532ab4382d2SGreg Kroah-Hartman 	} else {
5334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
534ab4382d2SGreg Kroah-Hartman 	}
535ab4382d2SGreg Kroah-Hartman }
536ab4382d2SGreg Kroah-Hartman 
537ab4382d2SGreg Kroah-Hartman /*
538ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
539ab4382d2SGreg Kroah-Hartman  */
540ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
541ab4382d2SGreg Kroah-Hartman {
542ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
543ab5e4e41SRichard Genoud 	uint32_t ier = 0;
544ab5e4e41SRichard Genoud 
545ab5e4e41SRichard Genoud 	/*
546ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
547ab5e4e41SRichard Genoud 	 */
548ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
549ab5e4e41SRichard Genoud 		return;
550ab5e4e41SRichard Genoud 
551ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
552ab5e4e41SRichard Genoud 
553ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
554ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
555ab5e4e41SRichard Genoud 	else
556ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
557ab5e4e41SRichard Genoud 
558ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
559ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
560ab5e4e41SRichard Genoud 	else
561ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
562ab5e4e41SRichard Genoud 
563ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
564ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
565ab5e4e41SRichard Genoud 	else
566ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
567ab5e4e41SRichard Genoud 
568ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
569ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
570ab5e4e41SRichard Genoud 	else
571ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
572ab5e4e41SRichard Genoud 
5734e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
574ab4382d2SGreg Kroah-Hartman }
575ab4382d2SGreg Kroah-Hartman 
576ab4382d2SGreg Kroah-Hartman /*
57735b675b9SRichard Genoud  * Disable modem status interrupts
57835b675b9SRichard Genoud  */
57935b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
58035b675b9SRichard Genoud {
58135b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
58235b675b9SRichard Genoud 	uint32_t idr = 0;
58335b675b9SRichard Genoud 
58435b675b9SRichard Genoud 	/*
58535b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
58635b675b9SRichard Genoud 	 */
58735b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
58835b675b9SRichard Genoud 		return;
58935b675b9SRichard Genoud 
59035b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
59135b675b9SRichard Genoud 
59235b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
59335b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
59435b675b9SRichard Genoud 	else
59535b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
59635b675b9SRichard Genoud 
59735b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
59835b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
59935b675b9SRichard Genoud 	else
60035b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
60135b675b9SRichard Genoud 
60235b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
60335b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
60435b675b9SRichard Genoud 	else
60535b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
60635b675b9SRichard Genoud 
60735b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
60835b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
60935b675b9SRichard Genoud 	else
61035b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
61135b675b9SRichard Genoud 
6124e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
61335b675b9SRichard Genoud }
61435b675b9SRichard Genoud 
61535b675b9SRichard Genoud /*
616ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
617ab4382d2SGreg Kroah-Hartman  */
618ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
619ab4382d2SGreg Kroah-Hartman {
620ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
6214e7decdaSCyrille Pitchen 		/* start break */
6224e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
623ab4382d2SGreg Kroah-Hartman 	else
6244e7decdaSCyrille Pitchen 		/* stop break */
6254e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
626ab4382d2SGreg Kroah-Hartman }
627ab4382d2SGreg Kroah-Hartman 
628ab4382d2SGreg Kroah-Hartman /*
629ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
630ab4382d2SGreg Kroah-Hartman  */
631ab4382d2SGreg Kroah-Hartman static void
632ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
633ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
634ab4382d2SGreg Kroah-Hartman {
635ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
636ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
637ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
638ab4382d2SGreg Kroah-Hartman 
639ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
640ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
641ab4382d2SGreg Kroah-Hartman 		return;
642ab4382d2SGreg Kroah-Hartman 
643ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
644ab4382d2SGreg Kroah-Hartman 	c->status	= status;
645ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
648ab4382d2SGreg Kroah-Hartman 	smp_wmb();
649ab4382d2SGreg Kroah-Hartman 
650ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
651ab4382d2SGreg Kroah-Hartman }
652ab4382d2SGreg Kroah-Hartman 
653ab4382d2SGreg Kroah-Hartman /*
654ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
655ab4382d2SGreg Kroah-Hartman  */
656ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
657ab4382d2SGreg Kroah-Hartman {
658ab4382d2SGreg Kroah-Hartman 	/* clear error */
6594e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
660ab4382d2SGreg Kroah-Hartman 
661ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
662ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
663ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
664ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
665ab4382d2SGreg Kroah-Hartman 	}
666ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
667ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
668ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
669ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
670ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
671ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
672ab4382d2SGreg Kroah-Hartman }
673ab4382d2SGreg Kroah-Hartman 
674ab4382d2SGreg Kroah-Hartman /*
675ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
676ab4382d2SGreg Kroah-Hartman  */
677ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
678ab4382d2SGreg Kroah-Hartman {
679ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
680ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
681ab4382d2SGreg Kroah-Hartman 
6824e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
683ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
684a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
685ab4382d2SGreg Kroah-Hartman 
686ab4382d2SGreg Kroah-Hartman 		/*
687ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
688ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
689ab4382d2SGreg Kroah-Hartman 		 */
690ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
691ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
692ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
693ab4382d2SGreg Kroah-Hartman 
694ab4382d2SGreg Kroah-Hartman 			/* clear error */
6954e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
696ab4382d2SGreg Kroah-Hartman 
697ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
698ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
699ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
7004e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
7014e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
702ab4382d2SGreg Kroah-Hartman 			} else {
703ab4382d2SGreg Kroah-Hartman 				/*
704ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
705ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
706ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
707ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
708ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
709ab4382d2SGreg Kroah-Hartman 				 */
7104e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
7114e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
712ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
713ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
714ab4382d2SGreg Kroah-Hartman 			}
715ab4382d2SGreg Kroah-Hartman 		}
716ab4382d2SGreg Kroah-Hartman 
717ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
7184e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
719ab4382d2SGreg Kroah-Hartman 	}
720ab4382d2SGreg Kroah-Hartman 
721ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
722ab4382d2SGreg Kroah-Hartman }
723ab4382d2SGreg Kroah-Hartman 
724ab4382d2SGreg Kroah-Hartman /*
725ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
726ab4382d2SGreg Kroah-Hartman  * disabled)
727ab4382d2SGreg Kroah-Hartman  */
728ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
729ab4382d2SGreg Kroah-Hartman {
730ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
731ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
732ab4382d2SGreg Kroah-Hartman 
7334e7decdaSCyrille Pitchen 	if (port->x_char &&
7344e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
735a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
736ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
737ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
738ab4382d2SGreg Kroah-Hartman 	}
739ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
740ab4382d2SGreg Kroah-Hartman 		return;
741ab4382d2SGreg Kroah-Hartman 
7424e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7434e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
744a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
745ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
746ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
747ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
748ab4382d2SGreg Kroah-Hartman 			break;
749ab4382d2SGreg Kroah-Hartman 	}
750ab4382d2SGreg Kroah-Hartman 
751ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
752ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
753ab4382d2SGreg Kroah-Hartman 
754ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
755ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7564e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7574e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
758ab4382d2SGreg Kroah-Hartman }
759ab4382d2SGreg Kroah-Hartman 
76008f738beSElen Song static void atmel_complete_tx_dma(void *arg)
76108f738beSElen Song {
76208f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
76308f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
76408f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
76508f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
76608f738beSElen Song 	unsigned long flags;
76708f738beSElen Song 
76808f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
76908f738beSElen Song 
77008f738beSElen Song 	if (chan)
77108f738beSElen Song 		dmaengine_terminate_all(chan);
7725f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
77308f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
77408f738beSElen Song 
7755f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
77608f738beSElen Song 
77708f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
77808f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
77908f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
78008f738beSElen Song 	atmel_port->desc_tx = NULL;
78108f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
78208f738beSElen Song 
78308f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
78408f738beSElen Song 		uart_write_wakeup(port);
78508f738beSElen Song 
7861842dc2eSCyrille Pitchen 	/*
7871842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7881842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7891842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7901842dc2eSCyrille Pitchen 	 */
79108f738beSElen Song 	if (!uart_circ_empty(xmit))
79208f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
79308f738beSElen Song 
79408f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
79508f738beSElen Song }
79608f738beSElen Song 
79708f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
79808f738beSElen Song {
79908f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
80008f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
80108f738beSElen Song 
80208f738beSElen Song 	if (chan) {
80308f738beSElen Song 		dmaengine_terminate_all(chan);
80408f738beSElen Song 		dma_release_channel(chan);
80508f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
80648479148SWolfram Sang 				DMA_TO_DEVICE);
80708f738beSElen Song 	}
80808f738beSElen Song 
80908f738beSElen Song 	atmel_port->desc_tx = NULL;
81008f738beSElen Song 	atmel_port->chan_tx = NULL;
81108f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
81208f738beSElen Song }
81308f738beSElen Song 
81408f738beSElen Song /*
81508f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
81608f738beSElen Song  */
81708f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
81808f738beSElen Song {
81908f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
82008f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
82108f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
82208f738beSElen Song 	struct dma_async_tx_descriptor *desc;
8235f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
8245f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
8255f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
82608f738beSElen Song 
82708f738beSElen Song 	/* Make sure we have an idle channel */
82808f738beSElen Song 	if (atmel_port->desc_tx != NULL)
82908f738beSElen Song 		return;
83008f738beSElen Song 
83108f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
83208f738beSElen Song 		/*
83308f738beSElen Song 		 * DMA is idle now.
83408f738beSElen Song 		 * Port xmit buffer is already mapped,
83508f738beSElen Song 		 * and it is one page... Just adjust
83608f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
83708f738beSElen Song 		 * we have to transmit till the end, and then the rest.
83808f738beSElen Song 		 * Take the port lock to get a
83908f738beSElen Song 		 * consistent xmit buffer state.
84008f738beSElen Song 		 */
8415f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
84208f738beSElen Song 					 xmit->tail,
84308f738beSElen Song 					 UART_XMIT_SIZE);
8445f258b3eSCyrille Pitchen 
8455f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
8465f258b3eSCyrille Pitchen 			/* multi data mode */
8475f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
8485f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
8495f258b3eSCyrille Pitchen 		} else {
8505f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
8515f258b3eSCyrille Pitchen 			part1_len = 0;
8525f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
8535f258b3eSCyrille Pitchen 		}
8545f258b3eSCyrille Pitchen 
8555f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
8565f258b3eSCyrille Pitchen 		sg_len = 0;
8575f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
8585f258b3eSCyrille Pitchen 		if (part1_len) {
8595f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8605f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8615f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
8625f258b3eSCyrille Pitchen 
8635f258b3eSCyrille Pitchen 			phys_addr += part1_len;
8645f258b3eSCyrille Pitchen 		}
8655f258b3eSCyrille Pitchen 
8665f258b3eSCyrille Pitchen 		if (part2_len) {
8675f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8685f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8695f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
8705f258b3eSCyrille Pitchen 		}
8715f258b3eSCyrille Pitchen 
8725f258b3eSCyrille Pitchen 		/*
8735f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
8745f258b3eSCyrille Pitchen 		 * xmit->tail correctly
8755f258b3eSCyrille Pitchen 		 */
8765f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
87708f738beSElen Song 
87808f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
8795f258b3eSCyrille Pitchen 					       sgl,
8805f258b3eSCyrille Pitchen 					       sg_len,
88108f738beSElen Song 					       DMA_MEM_TO_DEV,
88208f738beSElen Song 					       DMA_PREP_INTERRUPT |
88308f738beSElen Song 					       DMA_CTRL_ACK);
88408f738beSElen Song 		if (!desc) {
88508f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
88608f738beSElen Song 			return;
88708f738beSElen Song 		}
88808f738beSElen Song 
8895f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
89008f738beSElen Song 
89108f738beSElen Song 		atmel_port->desc_tx = desc;
89208f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
89308f738beSElen Song 		desc->callback_param = atmel_port;
89408f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
89508f738beSElen Song 
89608f738beSElen Song 	} else {
89713bd3e6fSRicardo Ribalda Delgado 		if (port->rs485.flags & SER_RS485_ENABLED) {
89808f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
89908f738beSElen Song 			atmel_start_rx(port);
90008f738beSElen Song 		}
90108f738beSElen Song 	}
90208f738beSElen Song 
90308f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
90408f738beSElen Song 		uart_write_wakeup(port);
90508f738beSElen Song }
90608f738beSElen Song 
90708f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
90808f738beSElen Song {
90908f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
91008f738beSElen Song 	dma_cap_mask_t		mask;
91108f738beSElen Song 	struct dma_slave_config config;
91208f738beSElen Song 	int ret, nent;
91308f738beSElen Song 
91408f738beSElen Song 	dma_cap_zero(mask);
91508f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
91608f738beSElen Song 
91708f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
91808f738beSElen Song 	if (atmel_port->chan_tx == NULL)
91908f738beSElen Song 		goto chan_err;
92008f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
92108f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
92208f738beSElen Song 
92308f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
92408f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
92508f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
9262c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
92708f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
92808f738beSElen Song 			virt_to_page(port->state->xmit.buf),
92908f738beSElen Song 			UART_XMIT_SIZE,
930c8d1f022SUwe Kleine-König 			(unsigned long)port->state->xmit.buf & ~PAGE_MASK);
93108f738beSElen Song 	nent = dma_map_sg(port->dev,
93208f738beSElen Song 				&atmel_port->sg_tx,
93308f738beSElen Song 				1,
93448479148SWolfram Sang 				DMA_TO_DEVICE);
93508f738beSElen Song 
93608f738beSElen Song 	if (!nent) {
93708f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
93808f738beSElen Song 		goto chan_err;
93908f738beSElen Song 	} else {
940c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
94108f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
94208f738beSElen Song 			port->state->xmit.buf,
943c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
94408f738beSElen Song 	}
94508f738beSElen Song 
94608f738beSElen Song 	/* Configure the slave DMA */
94708f738beSElen Song 	memset(&config, 0, sizeof(config));
94808f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
9495f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
9505f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
9515f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
95208f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
953a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
95408f738beSElen Song 
9555483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9565483c10eSMaxime Ripard 				     &config);
95708f738beSElen Song 	if (ret) {
95808f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
95908f738beSElen Song 		goto chan_err;
96008f738beSElen Song 	}
96108f738beSElen Song 
96208f738beSElen Song 	return 0;
96308f738beSElen Song 
96408f738beSElen Song chan_err:
96508f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
96608f738beSElen Song 	atmel_port->use_dma_tx = 0;
96708f738beSElen Song 	if (atmel_port->chan_tx)
96808f738beSElen Song 		atmel_release_tx_dma(port);
96908f738beSElen Song 	return -EINVAL;
97008f738beSElen Song }
97108f738beSElen Song 
97234df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
97334df42f5SElen Song {
97434df42f5SElen Song 	struct uart_port *port = arg;
97534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
97634df42f5SElen Song 
97734df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
97834df42f5SElen Song }
97934df42f5SElen Song 
98034df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
98134df42f5SElen Song {
98234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
98334df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
98434df42f5SElen Song 
98534df42f5SElen Song 	if (chan) {
98634df42f5SElen Song 		dmaengine_terminate_all(chan);
98734df42f5SElen Song 		dma_release_channel(chan);
98834df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
98948479148SWolfram Sang 				DMA_FROM_DEVICE);
99034df42f5SElen Song 	}
99134df42f5SElen Song 
99234df42f5SElen Song 	atmel_port->desc_rx = NULL;
99334df42f5SElen Song 	atmel_port->chan_rx = NULL;
99434df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
99534df42f5SElen Song }
99634df42f5SElen Song 
99734df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
99834df42f5SElen Song {
99934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
100066f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
100134df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
100234df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
100334df42f5SElen Song 	struct dma_tx_state state;
100434df42f5SElen Song 	enum dma_status dmastat;
100566f37aafSCyrille Pitchen 	size_t count;
100634df42f5SElen Song 
100734df42f5SElen Song 
100834df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
10094e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
101034df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
101134df42f5SElen Song 				atmel_port->cookie_rx,
101234df42f5SElen Song 				&state);
101334df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
101434df42f5SElen Song 	if (dmastat == DMA_ERROR) {
101534df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
10164e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
101734df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
101834df42f5SElen Song 		return;
101934df42f5SElen Song 	}
102066f37aafSCyrille Pitchen 
102166f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
102266f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
102366f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
102466f37aafSCyrille Pitchen 			    1,
1025485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
102634df42f5SElen Song 
102734df42f5SElen Song 	/*
102866f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
102966f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
103066f37aafSCyrille Pitchen 	 * framework.
103166f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
103266f37aafSCyrille Pitchen 	 * length.
103334df42f5SElen Song 	 */
103466f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
103566f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
103666f37aafSCyrille Pitchen 	/*
103766f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
103866f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
103966f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
104066f37aafSCyrille Pitchen 	 *
104166f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
104266f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
104366f37aafSCyrille Pitchen 	 *
104466f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
104566f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
104666f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
104766f37aafSCyrille Pitchen 	 */
104866f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
104966f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
105034df42f5SElen Song 
105166f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
105266f37aafSCyrille Pitchen 		ring->tail = 0;
105334df42f5SElen Song 		port->icount.rx += count;
105434df42f5SElen Song 	}
105534df42f5SElen Song 
105666f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
105766f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
105866f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
105966f37aafSCyrille Pitchen 
106066f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
106166f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
106266f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
106366f37aafSCyrille Pitchen 			ring->head = 0;
106466f37aafSCyrille Pitchen 		ring->tail = ring->head;
106566f37aafSCyrille Pitchen 		port->icount.rx += count;
106666f37aafSCyrille Pitchen 	}
106766f37aafSCyrille Pitchen 
106866f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
106966f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
107066f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
107166f37aafSCyrille Pitchen 			       1,
1072485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
107366f37aafSCyrille Pitchen 
107466f37aafSCyrille Pitchen 	/*
107566f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
107666f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
107766f37aafSCyrille Pitchen 	 */
107866f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
107966f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
108066f37aafSCyrille Pitchen 	spin_lock(&port->lock);
108166f37aafSCyrille Pitchen 
10824e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
108334df42f5SElen Song }
108434df42f5SElen Song 
108534df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
108634df42f5SElen Song {
108734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
108834df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
108934df42f5SElen Song 	dma_cap_mask_t		mask;
109034df42f5SElen Song 	struct dma_slave_config config;
109134df42f5SElen Song 	struct circ_buf		*ring;
109234df42f5SElen Song 	int ret, nent;
109334df42f5SElen Song 
109434df42f5SElen Song 	ring = &atmel_port->rx_ring;
109534df42f5SElen Song 
109634df42f5SElen Song 	dma_cap_zero(mask);
109734df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
109834df42f5SElen Song 
109934df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
110034df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
110134df42f5SElen Song 		goto chan_err;
110234df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
110334df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
110434df42f5SElen Song 
110534df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
110634df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
110734df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
11082c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
110934df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
111034df42f5SElen Song 		    virt_to_page(ring->buf),
1111a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
1112c8d1f022SUwe Kleine-König 		    (unsigned long)ring->buf & ~PAGE_MASK);
111334df42f5SElen Song 	nent = dma_map_sg(port->dev,
111434df42f5SElen Song 			  &atmel_port->sg_rx,
111534df42f5SElen Song 			  1,
111648479148SWolfram Sang 			  DMA_FROM_DEVICE);
111734df42f5SElen Song 
111834df42f5SElen Song 	if (!nent) {
111934df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
112034df42f5SElen Song 		goto chan_err;
112134df42f5SElen Song 	} else {
1122c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
112334df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
112434df42f5SElen Song 			ring->buf,
1125c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
112634df42f5SElen Song 	}
112734df42f5SElen Song 
112834df42f5SElen Song 	/* Configure the slave DMA */
112934df42f5SElen Song 	memset(&config, 0, sizeof(config));
113034df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
113134df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
113234df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1133a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
113434df42f5SElen Song 
11355483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
11365483c10eSMaxime Ripard 				     &config);
113734df42f5SElen Song 	if (ret) {
113834df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
113934df42f5SElen Song 		goto chan_err;
114034df42f5SElen Song 	}
114134df42f5SElen Song 	/*
114234df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
114334df42f5SElen Song 	 * each one is half ring buffer size
114434df42f5SElen Song 	 */
114534df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
114634df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
114734df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
114834df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
114934df42f5SElen Song 					 DMA_DEV_TO_MEM,
115034df42f5SElen Song 					 DMA_PREP_INTERRUPT);
115134df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
115234df42f5SElen Song 	desc->callback_param = port;
115334df42f5SElen Song 	atmel_port->desc_rx = desc;
115434df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
115534df42f5SElen Song 
115634df42f5SElen Song 	return 0;
115734df42f5SElen Song 
115834df42f5SElen Song chan_err:
115934df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
116034df42f5SElen Song 	atmel_port->use_dma_rx = 0;
116134df42f5SElen Song 	if (atmel_port->chan_rx)
116234df42f5SElen Song 		atmel_release_rx_dma(port);
116334df42f5SElen Song 	return -EINVAL;
116434df42f5SElen Song }
116534df42f5SElen Song 
11662e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
11672e68c22fSElen Song {
11682e68c22fSElen Song 	struct uart_port *port = (void *)data;
11692e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11702e68c22fSElen Song 
11712e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
11722e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
11732e68c22fSElen Song }
11742e68c22fSElen Song 
1175ab4382d2SGreg Kroah-Hartman /*
1176ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1177ab4382d2SGreg Kroah-Hartman  */
1178ab4382d2SGreg Kroah-Hartman static void
1179ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1180ab4382d2SGreg Kroah-Hartman {
1181ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1182ab4382d2SGreg Kroah-Hartman 
118364e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1184ab4382d2SGreg Kroah-Hartman 		/*
1185ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1186ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1187ab4382d2SGreg Kroah-Hartman 		 *
1188ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1189ab4382d2SGreg Kroah-Hartman 		 * the moment.
1190ab4382d2SGreg Kroah-Hartman 		 */
1191ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
11924e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
11934e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
1194ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
1195ab4382d2SGreg Kroah-Hartman 		}
1196ab4382d2SGreg Kroah-Hartman 
1197ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1198ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1199ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1200ab4382d2SGreg Kroah-Hartman 	}
1201ab4382d2SGreg Kroah-Hartman 
120234df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
120334df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
12044e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12054e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
120634df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
120734df42f5SElen Song 		}
120834df42f5SElen Song 	}
120934df42f5SElen Song 
1210ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1211ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1212ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1213ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1214ab4382d2SGreg Kroah-Hartman 		/*
1215ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1216ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1217ab4382d2SGreg Kroah-Hartman 		 */
12184e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
12194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1220ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1221ab4382d2SGreg Kroah-Hartman 	}
1222ab4382d2SGreg Kroah-Hartman }
1223ab4382d2SGreg Kroah-Hartman 
1224ab4382d2SGreg Kroah-Hartman /*
1225ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1226ab4382d2SGreg Kroah-Hartman  */
1227ab4382d2SGreg Kroah-Hartman static void
1228ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1229ab4382d2SGreg Kroah-Hartman {
1230ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1231ab4382d2SGreg Kroah-Hartman 
1232ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1233ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
12344e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
12354e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1236ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1237ab4382d2SGreg Kroah-Hartman 	}
1238ab4382d2SGreg Kroah-Hartman }
1239ab4382d2SGreg Kroah-Hartman 
1240ab4382d2SGreg Kroah-Hartman /*
1241ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1242ab4382d2SGreg Kroah-Hartman  */
1243ab4382d2SGreg Kroah-Hartman static void
1244ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1245ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1246ab4382d2SGreg Kroah-Hartman {
1247ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1248ab4382d2SGreg Kroah-Hartman 
1249ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1250ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1251ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1252d033e82dSLeilei Zhao 		atmel_port->status_change = atmel_port->irq_status ^
1253d033e82dSLeilei Zhao 					    atmel_port->irq_status_prev;
1254d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
1255ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1256ab4382d2SGreg Kroah-Hartman 	}
1257ab4382d2SGreg Kroah-Hartman }
1258ab4382d2SGreg Kroah-Hartman 
1259ab4382d2SGreg Kroah-Hartman /*
1260ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1261ab4382d2SGreg Kroah-Hartman  */
1262ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1263ab4382d2SGreg Kroah-Hartman {
1264ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1265ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12662c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1267ab5e4e41SRichard Genoud 	bool gpio_handled = false;
1268ab4382d2SGreg Kroah-Hartman 
12692c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
12702c7af5baSBoris BREZILLON 
1271ab4382d2SGreg Kroah-Hartman 	do {
1272e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
12734e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
12742c7af5baSBoris BREZILLON 		pending = status & mask;
1275ab5e4e41SRichard Genoud 		if (!gpio_handled) {
1276ab5e4e41SRichard Genoud 			/*
1277ab5e4e41SRichard Genoud 			 * Dealing with GPIO interrupt
1278ab5e4e41SRichard Genoud 			 */
1279ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1280ab5e4e41SRichard Genoud 				pending |= ATMEL_US_CTSIC;
1281ab5e4e41SRichard Genoud 
1282ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1283ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DSRIC;
1284ab5e4e41SRichard Genoud 
1285ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1286ab5e4e41SRichard Genoud 				pending |= ATMEL_US_RIIC;
1287ab5e4e41SRichard Genoud 
1288ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1289ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DCDIC;
1290ab5e4e41SRichard Genoud 
1291ab5e4e41SRichard Genoud 			gpio_handled = true;
1292ab5e4e41SRichard Genoud 		}
1293ab4382d2SGreg Kroah-Hartman 		if (!pending)
1294ab4382d2SGreg Kroah-Hartman 			break;
1295ab4382d2SGreg Kroah-Hartman 
12962c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
12972c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
12982c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
12994e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
13002c7af5baSBoris BREZILLON 			pm_system_wakeup();
13012c7af5baSBoris BREZILLON 			break;
13022c7af5baSBoris BREZILLON 		}
13032c7af5baSBoris BREZILLON 
1304ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1305ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1306ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1307ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1308ab4382d2SGreg Kroah-Hartman 
13092c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
13102c7af5baSBoris BREZILLON 
1311ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1312ab4382d2SGreg Kroah-Hartman }
1313ab4382d2SGreg Kroah-Hartman 
1314a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1315a930e528SElen Song {
1316a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1317a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1318a930e528SElen Song 
1319a930e528SElen Song 	dma_unmap_single(port->dev,
1320a930e528SElen Song 			 pdc->dma_addr,
1321a930e528SElen Song 			 pdc->dma_size,
1322a930e528SElen Song 			 DMA_TO_DEVICE);
1323a930e528SElen Song }
1324a930e528SElen Song 
1325ab4382d2SGreg Kroah-Hartman /*
1326ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1327ab4382d2SGreg Kroah-Hartman  */
132864e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1329ab4382d2SGreg Kroah-Hartman {
1330ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1331ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1332ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1333ab4382d2SGreg Kroah-Hartman 	int count;
1334ab4382d2SGreg Kroah-Hartman 
1335ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
13364e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1337ab4382d2SGreg Kroah-Hartman 		return;
1338ab4382d2SGreg Kroah-Hartman 
1339ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1340ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1341ab4382d2SGreg Kroah-Hartman 
1342ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1343ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1344ab4382d2SGreg Kroah-Hartman 
1345ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1346ab4382d2SGreg Kroah-Hartman 
1347ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
13484e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1349ab4382d2SGreg Kroah-Hartman 
1350ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1351ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1352ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1353ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1354ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1355ab4382d2SGreg Kroah-Hartman 
1356ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1357ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1358ab4382d2SGreg Kroah-Hartman 
13594e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13604e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13614e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1362ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13634e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1364ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13654e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13664e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1367ab4382d2SGreg Kroah-Hartman 	} else {
136813bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
136913bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1370ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1371ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1372ab4382d2SGreg Kroah-Hartman 		}
1373ab4382d2SGreg Kroah-Hartman 	}
1374ab4382d2SGreg Kroah-Hartman 
1375ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1376ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1377ab4382d2SGreg Kroah-Hartman }
1378ab4382d2SGreg Kroah-Hartman 
1379a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1380a930e528SElen Song {
1381a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1382a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1383a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1384a930e528SElen Song 
1385a930e528SElen Song 	pdc->buf = xmit->buf;
1386a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1387a930e528SElen Song 					pdc->buf,
1388a930e528SElen Song 					UART_XMIT_SIZE,
1389a930e528SElen Song 					DMA_TO_DEVICE);
1390a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1391a930e528SElen Song 	pdc->ofs = 0;
1392a930e528SElen Song 
1393a930e528SElen Song 	return 0;
1394a930e528SElen Song }
1395a930e528SElen Song 
1396ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1397ab4382d2SGreg Kroah-Hartman {
1398ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1399ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1400ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1401ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1402ab4382d2SGreg Kroah-Hartman 
1403ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1404ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1405ab4382d2SGreg Kroah-Hartman 
1406ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1407ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1408ab4382d2SGreg Kroah-Hartman 
1409ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1410ab4382d2SGreg Kroah-Hartman 
1411ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1412ab4382d2SGreg Kroah-Hartman 
1413ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1414ab4382d2SGreg Kroah-Hartman 		status = c.status;
1415ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1416ab4382d2SGreg Kroah-Hartman 
1417ab4382d2SGreg Kroah-Hartman 		/*
1418ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1419ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1420ab4382d2SGreg Kroah-Hartman 		 */
1421ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1422ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1423ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1424ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1425ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1426ab4382d2SGreg Kroah-Hartman 
1427ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1428ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1429ab4382d2SGreg Kroah-Hartman 					continue;
1430ab4382d2SGreg Kroah-Hartman 			}
1431ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1432ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1433ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1434ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1435ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1436ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1437ab4382d2SGreg Kroah-Hartman 
1438ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1439ab4382d2SGreg Kroah-Hartman 
1440ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1441ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1442ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1443ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1444ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1445ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1446ab4382d2SGreg Kroah-Hartman 		}
1447ab4382d2SGreg Kroah-Hartman 
1448ab4382d2SGreg Kroah-Hartman 
1449ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1450ab4382d2SGreg Kroah-Hartman 			continue;
1451ab4382d2SGreg Kroah-Hartman 
1452ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1453ab4382d2SGreg Kroah-Hartman 	}
1454ab4382d2SGreg Kroah-Hartman 
1455ab4382d2SGreg Kroah-Hartman 	/*
1456ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1457ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1458ab4382d2SGreg Kroah-Hartman 	 */
1459ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14602e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1461ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1462ab4382d2SGreg Kroah-Hartman }
1463ab4382d2SGreg Kroah-Hartman 
1464a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1465a930e528SElen Song {
1466a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1467a930e528SElen Song 	int i;
1468a930e528SElen Song 
1469a930e528SElen Song 	for (i = 0; i < 2; i++) {
1470a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1471a930e528SElen Song 
1472a930e528SElen Song 		dma_unmap_single(port->dev,
1473a930e528SElen Song 				 pdc->dma_addr,
1474a930e528SElen Song 				 pdc->dma_size,
1475a930e528SElen Song 				 DMA_FROM_DEVICE);
1476a930e528SElen Song 		kfree(pdc->buf);
1477a930e528SElen Song 	}
1478a930e528SElen Song }
1479a930e528SElen Song 
148064e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1481ab4382d2SGreg Kroah-Hartman {
1482ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
148305c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1484ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1485ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1486ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1487ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1488ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1489ab4382d2SGreg Kroah-Hartman 
1490ab4382d2SGreg Kroah-Hartman 	do {
1491ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
14924e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1493ab4382d2SGreg Kroah-Hartman 
1494ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
14954e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1496ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1497ab4382d2SGreg Kroah-Hartman 
1498ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1499ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1500ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1501ab4382d2SGreg Kroah-Hartman 		 * find out.
1502ab4382d2SGreg Kroah-Hartman 		 *
1503ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1504ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1505ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1506ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1507ab4382d2SGreg Kroah-Hartman 		 */
1508ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1509ab4382d2SGreg Kroah-Hartman 
1510ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1511ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1512ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1513ab4382d2SGreg Kroah-Hartman 
1514ab4382d2SGreg Kroah-Hartman 			/*
1515ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1516ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1517ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1518ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1519ab4382d2SGreg Kroah-Hartman 			 */
1520ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1521ab4382d2SGreg Kroah-Hartman 
152205c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
152305c7cd39SJiri Slaby 						count);
1524ab4382d2SGreg Kroah-Hartman 
1525ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1526ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1527ab4382d2SGreg Kroah-Hartman 
1528ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1529ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1530ab4382d2SGreg Kroah-Hartman 		}
1531ab4382d2SGreg Kroah-Hartman 
1532ab4382d2SGreg Kroah-Hartman 		/*
1533ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1534ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1535ab4382d2SGreg Kroah-Hartman 		 */
1536ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1537ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
15384e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
15394e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1540ab4382d2SGreg Kroah-Hartman 
1541ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1542ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1543ab4382d2SGreg Kroah-Hartman 		}
1544ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1545ab4382d2SGreg Kroah-Hartman 
1546ab4382d2SGreg Kroah-Hartman 	/*
1547ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1548ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1549ab4382d2SGreg Kroah-Hartman 	 */
1550ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15512e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1552ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1553ab4382d2SGreg Kroah-Hartman 
15544e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
15554e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1556ab4382d2SGreg Kroah-Hartman }
1557ab4382d2SGreg Kroah-Hartman 
1558a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1559a930e528SElen Song {
1560a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1561a930e528SElen Song 	int i;
1562a930e528SElen Song 
1563a930e528SElen Song 	for (i = 0; i < 2; i++) {
1564a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1565a930e528SElen Song 
1566a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1567a930e528SElen Song 		if (pdc->buf == NULL) {
1568a930e528SElen Song 			if (i != 0) {
1569a930e528SElen Song 				dma_unmap_single(port->dev,
1570a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1571a930e528SElen Song 					PDC_BUFFER_SIZE,
1572a930e528SElen Song 					DMA_FROM_DEVICE);
1573a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1574a930e528SElen Song 			}
1575a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1576a930e528SElen Song 			return -ENOMEM;
1577a930e528SElen Song 		}
1578a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1579a930e528SElen Song 						pdc->buf,
1580a930e528SElen Song 						PDC_BUFFER_SIZE,
1581a930e528SElen Song 						DMA_FROM_DEVICE);
1582a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1583a930e528SElen Song 		pdc->ofs = 0;
1584a930e528SElen Song 	}
1585a930e528SElen Song 
1586a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1587a930e528SElen Song 
15884e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
15894e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1590a930e528SElen Song 
15914e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
15924e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
15934e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1594a930e528SElen Song 
1595a930e528SElen Song 	return 0;
1596a930e528SElen Song }
1597a930e528SElen Song 
1598ab4382d2SGreg Kroah-Hartman /*
1599ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1600ab4382d2SGreg Kroah-Hartman  */
1601ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1602ab4382d2SGreg Kroah-Hartman {
1603ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1604ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1605d033e82dSLeilei Zhao 	unsigned int status = atmel_port->irq_status;
1606d033e82dSLeilei Zhao 	unsigned int status_change = atmel_port->status_change;
1607ab4382d2SGreg Kroah-Hartman 
1608ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1609ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1610ab4382d2SGreg Kroah-Hartman 
1611a930e528SElen Song 	atmel_port->schedule_tx(port);
1612ab4382d2SGreg Kroah-Hartman 
1613ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1614ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1615ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1616ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1617ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1618ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1619ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1620ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1621ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1622ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1623ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1624ab4382d2SGreg Kroah-Hartman 
1625ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1626ab4382d2SGreg Kroah-Hartman 
1627d033e82dSLeilei Zhao 		atmel_port->status_change = 0;
1628ab4382d2SGreg Kroah-Hartman 	}
1629ab4382d2SGreg Kroah-Hartman 
1630a930e528SElen Song 	atmel_port->schedule_rx(port);
1631ab4382d2SGreg Kroah-Hartman 
1632ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1633ab4382d2SGreg Kroah-Hartman }
1634ab4382d2SGreg Kroah-Hartman 
16354a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
163633d64c4fSElen Song 				struct platform_device *pdev)
163733d64c4fSElen Song {
163833d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1639574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
164033d64c4fSElen Song 
164133d64c4fSElen Song 	if (np) {
164233d64c4fSElen Song 		/* DMA/PDC usage specification */
164333d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
164433d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
164533d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
164633d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
164733d64c4fSElen Song 			} else {
164833d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
164933d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
165033d64c4fSElen Song 			}
165133d64c4fSElen Song 		} else {
165233d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
165333d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
165433d64c4fSElen Song 		}
165533d64c4fSElen Song 
165633d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
165733d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
165833d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
165933d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
166033d64c4fSElen Song 			} else {
166133d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
166233d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
166333d64c4fSElen Song 			}
166433d64c4fSElen Song 		} else {
166533d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
166633d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
166733d64c4fSElen Song 		}
166833d64c4fSElen Song 
166933d64c4fSElen Song 	} else {
167033d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
167133d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
167233d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
167333d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
167433d64c4fSElen Song 	}
167533d64c4fSElen Song 
167633d64c4fSElen Song }
167733d64c4fSElen Song 
167813bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
167933d64c4fSElen Song 				struct platform_device *pdev)
168033d64c4fSElen Song {
168133d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1682574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
168333d64c4fSElen Song 
168433d64c4fSElen Song 	if (np) {
168533d64c4fSElen Song 		u32 rs485_delay[2];
168633d64c4fSElen Song 		/* rs485 properties */
168733d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
168833d64c4fSElen Song 					rs485_delay, 2) == 0) {
168913bd3e6fSRicardo Ribalda Delgado 			struct serial_rs485 *rs485conf = &port->rs485;
169033d64c4fSElen Song 
169133d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
169233d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
169333d64c4fSElen Song 			rs485conf->flags = 0;
169433d64c4fSElen Song 
169533d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
169633d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
169733d64c4fSElen Song 
169833d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
169933d64c4fSElen Song 								NULL))
170033d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
170133d64c4fSElen Song 		}
170233d64c4fSElen Song 	} else {
170313bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
170433d64c4fSElen Song 	}
170533d64c4fSElen Song 
170633d64c4fSElen Song }
170733d64c4fSElen Song 
1708a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1709a930e528SElen Song {
1710a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1711a930e528SElen Song 
171234df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
171334df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
171434df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
171534df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
171634df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1717a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1718a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1719a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1720a930e528SElen Song 	} else {
1721a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1722a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1723a930e528SElen Song 		atmel_port->release_rx = NULL;
1724a930e528SElen Song 	}
1725a930e528SElen Song 
172608f738beSElen Song 	if (atmel_use_dma_tx(port)) {
172708f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
172808f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
172908f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
173008f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1731a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1732a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1733a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1734a930e528SElen Song 	} else {
1735a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1736a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1737a930e528SElen Song 		atmel_port->release_tx = NULL;
1738a930e528SElen Song 	}
1739a930e528SElen Song }
1740a930e528SElen Song 
1741ab4382d2SGreg Kroah-Hartman /*
1742055560b0SElen Song  * Get ip name usart or uart
1743055560b0SElen Song  */
1744892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1745055560b0SElen Song {
1746055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
17474e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1748731d9caeSNicolas Ferre 	u32 version;
1749055560b0SElen Song 	int usart, uart;
1750055560b0SElen Song 	/* usart and uart ascii */
1751055560b0SElen Song 	usart = 0x55534152;
1752055560b0SElen Song 	uart = 0x44424755;
1753055560b0SElen Song 
1754055560b0SElen Song 	atmel_port->is_usart = false;
1755055560b0SElen Song 
1756055560b0SElen Song 	if (name == usart) {
1757055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1758055560b0SElen Song 		atmel_port->is_usart = true;
1759055560b0SElen Song 	} else if (name == uart) {
1760055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1761055560b0SElen Song 		atmel_port->is_usart = false;
1762055560b0SElen Song 	} else {
1763731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17644e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1765731d9caeSNicolas Ferre 		switch (version) {
1766731d9caeSNicolas Ferre 		case 0x302:
1767731d9caeSNicolas Ferre 		case 0x10213:
1768731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1769731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1770731d9caeSNicolas Ferre 			break;
1771731d9caeSNicolas Ferre 		case 0x203:
1772731d9caeSNicolas Ferre 		case 0x10202:
1773731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1774731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1775731d9caeSNicolas Ferre 			break;
1776731d9caeSNicolas Ferre 		default:
1777731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1778731d9caeSNicolas Ferre 		}
1779055560b0SElen Song 	}
1780055560b0SElen Song }
1781055560b0SElen Song 
1782ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port)
1783ab5e4e41SRichard Genoud {
1784ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1785ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1786ab5e4e41SRichard Genoud 
1787ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++)
1788ab5e4e41SRichard Genoud 		if (atmel_port->gpio_irq[i] >= 0)
1789ab5e4e41SRichard Genoud 			free_irq(atmel_port->gpio_irq[i], port);
1790ab5e4e41SRichard Genoud }
1791ab5e4e41SRichard Genoud 
1792ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port)
1793ab5e4e41SRichard Genoud {
1794ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1795ab5e4e41SRichard Genoud 	int *irq = atmel_port->gpio_irq;
1796ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1797ab5e4e41SRichard Genoud 	int err = 0;
1798ab5e4e41SRichard Genoud 
1799ab5e4e41SRichard Genoud 	for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1800ab5e4e41SRichard Genoud 		if (irq[i] < 0)
1801ab5e4e41SRichard Genoud 			continue;
1802ab5e4e41SRichard Genoud 
1803ab5e4e41SRichard Genoud 		irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1804ab5e4e41SRichard Genoud 		err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1805ab5e4e41SRichard Genoud 				  "atmel_serial", port);
1806ab5e4e41SRichard Genoud 		if (err)
1807ab5e4e41SRichard Genoud 			dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1808ab5e4e41SRichard Genoud 				irq[i]);
1809ab5e4e41SRichard Genoud 	}
1810ab5e4e41SRichard Genoud 
1811ab5e4e41SRichard Genoud 	/*
1812ab5e4e41SRichard Genoud 	 * If something went wrong, rollback.
1813ab5e4e41SRichard Genoud 	 */
1814ab5e4e41SRichard Genoud 	while (err && (--i >= 0))
1815ab5e4e41SRichard Genoud 		if (irq[i] >= 0)
1816ab5e4e41SRichard Genoud 			free_irq(irq[i], port);
1817ab5e4e41SRichard Genoud 
1818ab5e4e41SRichard Genoud 	return err;
1819ab5e4e41SRichard Genoud }
1820ab5e4e41SRichard Genoud 
1821055560b0SElen Song /*
1822ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1823ab4382d2SGreg Kroah-Hartman  */
1824ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1825ab4382d2SGreg Kroah-Hartman {
182633d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1827ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1828ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1829ab4382d2SGreg Kroah-Hartman 	int retval;
1830ab4382d2SGreg Kroah-Hartman 
1831ab4382d2SGreg Kroah-Hartman 	/*
1832ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1833ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1834ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1835ab4382d2SGreg Kroah-Hartman 	 */
18364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1837ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1838ab4382d2SGreg Kroah-Hartman 
1839ab4382d2SGreg Kroah-Hartman 	/*
1840ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1841ab4382d2SGreg Kroah-Hartman 	 */
18422c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
18432c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1844ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1845ab4382d2SGreg Kroah-Hartman 	if (retval) {
1846ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1847ab4382d2SGreg Kroah-Hartman 		return retval;
1848ab4382d2SGreg Kroah-Hartman 	}
1849ab4382d2SGreg Kroah-Hartman 
1850ab4382d2SGreg Kroah-Hartman 	/*
1851ab5e4e41SRichard Genoud 	 * Get the GPIO lines IRQ
1852ab5e4e41SRichard Genoud 	 */
1853ab5e4e41SRichard Genoud 	retval = atmel_request_gpio_irq(port);
1854ab5e4e41SRichard Genoud 	if (retval)
1855ab5e4e41SRichard Genoud 		goto free_irq;
1856ab5e4e41SRichard Genoud 
18571e125786SLeilei Zhao 	tasklet_enable(&atmel_port->tasklet);
18581e125786SLeilei Zhao 
1859ab5e4e41SRichard Genoud 	/*
1860ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1861ab4382d2SGreg Kroah-Hartman 	 */
186233d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
18634d9628a1SLeilei Zhao 	atmel_set_ops(port);
186433d64c4fSElen Song 
1865a930e528SElen Song 	if (atmel_port->prepare_rx) {
1866a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1867a930e528SElen Song 		if (retval < 0)
1868a930e528SElen Song 			atmel_set_ops(port);
1869ab4382d2SGreg Kroah-Hartman 	}
1870ab4382d2SGreg Kroah-Hartman 
1871a930e528SElen Song 	if (atmel_port->prepare_tx) {
1872a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1873a930e528SElen Song 		if (retval < 0)
1874a930e528SElen Song 			atmel_set_ops(port);
1875ab4382d2SGreg Kroah-Hartman 	}
1876ab4382d2SGreg Kroah-Hartman 
1877b5199d46SCyrille Pitchen 	/*
1878b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1879b5199d46SCyrille Pitchen 	 */
1880b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1881b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1882b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1883b5199d46SCyrille Pitchen 		unsigned int fmr;
1884b5199d46SCyrille Pitchen 
1885b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1886b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1887b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1888b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1889b5199d46SCyrille Pitchen 
18905f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
18915f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
18925f258b3eSCyrille Pitchen 
1893b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1894b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1895b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1896b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1897b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1898b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1899b5199d46SCyrille Pitchen 
1900b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1901b5199d46SCyrille Pitchen 	}
1902b5199d46SCyrille Pitchen 
1903ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1904e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1905ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1906ab4382d2SGreg Kroah-Hartman 
1907ab4382d2SGreg Kroah-Hartman 	/*
1908ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1909ab4382d2SGreg Kroah-Hartman 	 */
19104e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1911ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
19124e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1913ab4382d2SGreg Kroah-Hartman 
19142e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
19152e68c22fSElen Song 			atmel_uart_timer_callback,
19162e68c22fSElen Song 			(unsigned long)port);
19178bc661bfSMarek Roszko 
19188bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
19198bc661bfSMarek Roszko 		/* set UART timeout */
19208bc661bfSMarek Roszko 		if (!atmel_port->is_usart) {
19212e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19222e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19232e68c22fSElen Song 		/* set USART timeout */
19242e68c22fSElen Song 		} else {
19254e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
19264e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1927ab4382d2SGreg Kroah-Hartman 
19284e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19294e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
19302e68c22fSElen Song 		}
1931ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
19324e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
193334df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
19342e68c22fSElen Song 		/* set UART timeout */
19352e68c22fSElen Song 		if (!atmel_port->is_usart) {
19362e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19372e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19382e68c22fSElen Song 		/* set USART timeout */
19392e68c22fSElen Song 		} else {
19404e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
19414e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
194234df42f5SElen Song 
19434e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19444e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
19452e68c22fSElen Song 		}
1946ab4382d2SGreg Kroah-Hartman 	} else {
1947ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
19484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1949ab4382d2SGreg Kroah-Hartman 	}
1950ab4382d2SGreg Kroah-Hartman 
1951ab4382d2SGreg Kroah-Hartman 	return 0;
1952ab5e4e41SRichard Genoud 
1953ab5e4e41SRichard Genoud free_irq:
1954ab5e4e41SRichard Genoud 	free_irq(port->irq, port);
1955ab5e4e41SRichard Genoud 
1956ab5e4e41SRichard Genoud 	return retval;
1957ab4382d2SGreg Kroah-Hartman }
1958ab4382d2SGreg Kroah-Hartman 
1959ab4382d2SGreg Kroah-Hartman /*
1960479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1961479e9b94SPeter Hurley  * buffer is reset.
1962479e9b94SPeter Hurley  */
1963479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1964479e9b94SPeter Hurley {
1965479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1966479e9b94SPeter Hurley 
1967479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
19684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1969479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1970479e9b94SPeter Hurley 	}
1971479e9b94SPeter Hurley }
1972479e9b94SPeter Hurley 
1973479e9b94SPeter Hurley /*
1974ab4382d2SGreg Kroah-Hartman  * Disable the port
1975ab4382d2SGreg Kroah-Hartman  */
1976ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1977ab4382d2SGreg Kroah-Hartman {
1978ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19790cc7c6c7SMarek Roszko 
1980ab4382d2SGreg Kroah-Hartman 	/*
19818bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19828bc661bfSMarek Roszko 	 * cleanup
19838bc661bfSMarek Roszko 	 */
19848bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19858bc661bfSMarek Roszko 
19868bc661bfSMarek Roszko 	/*
19870cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19880cc7c6c7SMarek Roszko 	 * we destroy the buffers
19890cc7c6c7SMarek Roszko 	 */
19901e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
19910cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
19920cc7c6c7SMarek Roszko 
19930cc7c6c7SMarek Roszko 	/*
19940cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
19950cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1996ab4382d2SGreg Kroah-Hartman 	 */
1997ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1998ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1999ab4382d2SGreg Kroah-Hartman 
20004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
20014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
20020cc7c6c7SMarek Roszko 
20030cc7c6c7SMarek Roszko 
2004ab4382d2SGreg Kroah-Hartman 	/*
2005ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
2006ab4382d2SGreg Kroah-Hartman 	 */
2007a930e528SElen Song 	if (atmel_port->release_rx)
2008a930e528SElen Song 		atmel_port->release_rx(port);
2009a930e528SElen Song 	if (atmel_port->release_tx)
2010a930e528SElen Song 		atmel_port->release_tx(port);
2011ab4382d2SGreg Kroah-Hartman 
2012ab4382d2SGreg Kroah-Hartman 	/*
2013bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
2014bb7e73c5SMark Deneen 	 */
2015bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
2016bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
2017bb7e73c5SMark Deneen 
2018bb7e73c5SMark Deneen 	/*
2019ab5e4e41SRichard Genoud 	 * Free the interrupts
2020ab4382d2SGreg Kroah-Hartman 	 */
2021ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
2022ab5e4e41SRichard Genoud 	atmel_free_gpio_irq(port);
2023ab5e4e41SRichard Genoud 
2024ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
2025ab4382d2SGreg Kroah-Hartman 
2026479e9b94SPeter Hurley 	atmel_flush_buffer(port);
2027ab4382d2SGreg Kroah-Hartman }
2028ab4382d2SGreg Kroah-Hartman 
2029ab4382d2SGreg Kroah-Hartman /*
2030ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
2031ab4382d2SGreg Kroah-Hartman  */
2032ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2033ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
2034ab4382d2SGreg Kroah-Hartman {
2035ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2036ab4382d2SGreg Kroah-Hartman 
2037ab4382d2SGreg Kroah-Hartman 	switch (state) {
2038ab4382d2SGreg Kroah-Hartman 	case 0:
2039ab4382d2SGreg Kroah-Hartman 		/*
2040ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
2041ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
2042ab4382d2SGreg Kroah-Hartman 		 */
204391f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
2044ab4382d2SGreg Kroah-Hartman 
2045ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
20464e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
2047ab4382d2SGreg Kroah-Hartman 		break;
2048ab4382d2SGreg Kroah-Hartman 	case 3:
2049ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
20504e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
20514e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
2052ab4382d2SGreg Kroah-Hartman 
2053ab4382d2SGreg Kroah-Hartman 		/*
2054ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
2055ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
2056ab4382d2SGreg Kroah-Hartman 		 */
205791f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2058ab4382d2SGreg Kroah-Hartman 		break;
2059ab4382d2SGreg Kroah-Hartman 	default:
2060ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2061ab4382d2SGreg Kroah-Hartman 	}
2062ab4382d2SGreg Kroah-Hartman }
2063ab4382d2SGreg Kroah-Hartman 
2064ab4382d2SGreg Kroah-Hartman /*
2065ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2066ab4382d2SGreg Kroah-Hartman  */
2067ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2068ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2069ab4382d2SGreg Kroah-Hartman {
2070ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
20711cf6e8fcSCyrille Pitchen 	unsigned int old_mode, mode, imr, quot, baud;
2072ab4382d2SGreg Kroah-Hartman 
20731cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20744e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20751cf6e8fcSCyrille Pitchen 
20761cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20771cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20781cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2079ab4382d2SGreg Kroah-Hartman 
2080ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2081ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
2082ab4382d2SGreg Kroah-Hartman 
2083ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
2084ab4382d2SGreg Kroah-Hartman 		quot /= 8;
2085ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
2086ab4382d2SGreg Kroah-Hartman 	}
2087ab4382d2SGreg Kroah-Hartman 
2088ab4382d2SGreg Kroah-Hartman 	/* byte size */
2089ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2090ab4382d2SGreg Kroah-Hartman 	case CS5:
2091ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2092ab4382d2SGreg Kroah-Hartman 		break;
2093ab4382d2SGreg Kroah-Hartman 	case CS6:
2094ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2095ab4382d2SGreg Kroah-Hartman 		break;
2096ab4382d2SGreg Kroah-Hartman 	case CS7:
2097ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2098ab4382d2SGreg Kroah-Hartman 		break;
2099ab4382d2SGreg Kroah-Hartman 	default:
2100ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2101ab4382d2SGreg Kroah-Hartman 		break;
2102ab4382d2SGreg Kroah-Hartman 	}
2103ab4382d2SGreg Kroah-Hartman 
2104ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2105ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2106ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2107ab4382d2SGreg Kroah-Hartman 
2108ab4382d2SGreg Kroah-Hartman 	/* parity */
2109ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2110ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2111ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2112ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2113ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2114ab4382d2SGreg Kroah-Hartman 			else
2115ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2116ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2117ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2118ab4382d2SGreg Kroah-Hartman 		else
2119ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2120ab4382d2SGreg Kroah-Hartman 	} else
2121ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2122ab4382d2SGreg Kroah-Hartman 
2123ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2124ab4382d2SGreg Kroah-Hartman 
2125ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2126ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2127ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2128ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2129ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2130ab4382d2SGreg Kroah-Hartman 
213164e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2132ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
21334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2134ab4382d2SGreg Kroah-Hartman 
2135ab4382d2SGreg Kroah-Hartman 	/*
2136ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2137ab4382d2SGreg Kroah-Hartman 	 */
2138ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2139ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2140ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2141ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2142ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2143ab4382d2SGreg Kroah-Hartman 		/*
2144ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2145ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2146ab4382d2SGreg Kroah-Hartman 		 */
2147ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2148ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2149ab4382d2SGreg Kroah-Hartman 	}
2150ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2151ab4382d2SGreg Kroah-Hartman 
2152ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2153ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2154ab4382d2SGreg Kroah-Hartman 
2155ab4382d2SGreg Kroah-Hartman 	/*
2156ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2157ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2158ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2159ab4382d2SGreg Kroah-Hartman 	 */
21604e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
21614e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2162ab4382d2SGreg Kroah-Hartman 
2163ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
21644e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2165ab4382d2SGreg Kroah-Hartman 
21661cf6e8fcSCyrille Pitchen 	/* mode */
216713bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
21684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
21694e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2170ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
21711cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
21721cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
21731cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_HWHS;
21741cf6e8fcSCyrille Pitchen 	} else {
21751cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21761cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2177ab4382d2SGreg Kroah-Hartman 	}
2178ab4382d2SGreg Kroah-Hartman 
21791cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2181ab4382d2SGreg Kroah-Hartman 
21821cf6e8fcSCyrille Pitchen 	/*
21831cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21841cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21851cf6e8fcSCyrille Pitchen 	 */
21861cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21871cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21881cf6e8fcSCyrille Pitchen 
21891cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21901cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21911cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21921cf6e8fcSCyrille Pitchen 		} else {
21931cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21941cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21951cf6e8fcSCyrille Pitchen 		}
21961cf6e8fcSCyrille Pitchen 
21974e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21981cf6e8fcSCyrille Pitchen 	}
21991cf6e8fcSCyrille Pitchen 
2200ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
22014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
22024e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
22034e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2204ab4382d2SGreg Kroah-Hartman 
2205ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
22064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2207ab4382d2SGreg Kroah-Hartman 
2208ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2209ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
221035b675b9SRichard Genoud 		atmel_enable_ms(port);
221135b675b9SRichard Genoud 	else
221235b675b9SRichard Genoud 		atmel_disable_ms(port);
2213ab4382d2SGreg Kroah-Hartman 
2214ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2215ab4382d2SGreg Kroah-Hartman }
2216ab4382d2SGreg Kroah-Hartman 
2217732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
221842bd7a4fSViktar Palstsiuk {
2219732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
222042bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2221d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
222242bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2223d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
222442bd7a4fSViktar Palstsiuk 	} else {
222542bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2226cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2227cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2228cab68f89SPeter Hurley 			atmel_disable_ms(port);
2229cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2230cab68f89SPeter Hurley 		}
223142bd7a4fSViktar Palstsiuk 	}
223242bd7a4fSViktar Palstsiuk }
223342bd7a4fSViktar Palstsiuk 
2234ab4382d2SGreg Kroah-Hartman /*
2235ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2236ab4382d2SGreg Kroah-Hartman  */
2237ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2238ab4382d2SGreg Kroah-Hartman {
2239ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2240ab4382d2SGreg Kroah-Hartman }
2241ab4382d2SGreg Kroah-Hartman 
2242ab4382d2SGreg Kroah-Hartman /*
2243ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2244ab4382d2SGreg Kroah-Hartman  */
2245ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2246ab4382d2SGreg Kroah-Hartman {
2247ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2248ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2249ab4382d2SGreg Kroah-Hartman 
2250ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2251ab4382d2SGreg Kroah-Hartman 
2252ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2253ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2254ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2255ab4382d2SGreg Kroah-Hartman 	}
2256ab4382d2SGreg Kroah-Hartman }
2257ab4382d2SGreg Kroah-Hartman 
2258ab4382d2SGreg Kroah-Hartman /*
2259ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2260ab4382d2SGreg Kroah-Hartman  */
2261ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2262ab4382d2SGreg Kroah-Hartman {
2263ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2264ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2265ab4382d2SGreg Kroah-Hartman 
2266ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2267ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2268ab4382d2SGreg Kroah-Hartman 
2269ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2270ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2271ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2272ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2273ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2274ab4382d2SGreg Kroah-Hartman 		}
2275ab4382d2SGreg Kroah-Hartman 	}
2276ab4382d2SGreg Kroah-Hartman 
2277ab4382d2SGreg Kroah-Hartman 	return 0;
2278ab4382d2SGreg Kroah-Hartman }
2279ab4382d2SGreg Kroah-Hartman 
2280ab4382d2SGreg Kroah-Hartman /*
2281ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2282ab4382d2SGreg Kroah-Hartman  */
2283ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2284ab4382d2SGreg Kroah-Hartman {
2285ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2286ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2287ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2288ab4382d2SGreg Kroah-Hartman 	}
2289ab4382d2SGreg Kroah-Hartman }
2290ab4382d2SGreg Kroah-Hartman 
2291ab4382d2SGreg Kroah-Hartman /*
2292ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2293ab4382d2SGreg Kroah-Hartman  */
2294ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2295ab4382d2SGreg Kroah-Hartman {
2296ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2297ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2298ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2299ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2300ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2301ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2302ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2303ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2304ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2305*270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2306ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2307ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2308ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2309ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2310ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2311ab4382d2SGreg Kroah-Hartman 	return ret;
2312ab4382d2SGreg Kroah-Hartman }
2313ab4382d2SGreg Kroah-Hartman 
2314ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2315ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2316ab4382d2SGreg Kroah-Hartman {
23174e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2318ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2319ab4382d2SGreg Kroah-Hartman 
2320a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2321ab4382d2SGreg Kroah-Hartman }
2322ab4382d2SGreg Kroah-Hartman 
2323ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2324ab4382d2SGreg Kroah-Hartman {
23254e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2326ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2327ab4382d2SGreg Kroah-Hartman 
2328a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2329ab4382d2SGreg Kroah-Hartman }
2330ab4382d2SGreg Kroah-Hartman #endif
2331ab4382d2SGreg Kroah-Hartman 
2332ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2333ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2334ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2335ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2336ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2337ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2338ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2339ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2340ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2341ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2342ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2343ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2344ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
234542bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2346ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2347ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2348ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2349ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2350ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2351ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2352ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2353ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2354ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2355ab4382d2SGreg Kroah-Hartman #endif
2356ab4382d2SGreg Kroah-Hartman };
2357ab4382d2SGreg Kroah-Hartman 
2358ab4382d2SGreg Kroah-Hartman /*
2359ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2360ab4382d2SGreg Kroah-Hartman  */
236191f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2362ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2363ab4382d2SGreg Kroah-Hartman {
236491f8c2d8SBoris BREZILLON 	int ret;
2365ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2366574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2367ab4382d2SGreg Kroah-Hartman 
23684a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2369a930e528SElen Song 	atmel_set_ops(port);
2370a930e528SElen Song 
237113bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
237233d64c4fSElen Song 
2373ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2374ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2375ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2376ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2377ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2378ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2379ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
238013bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2381ab4382d2SGreg Kroah-Hartman 
2382ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2383ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
23841e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
2385ab4382d2SGreg Kroah-Hartman 
2386ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2387ab4382d2SGreg Kroah-Hartman 
23885fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2389ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
23901acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2391588edbf3SNicolas Ferre 	} else {
2392ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2393ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2394ab4382d2SGreg Kroah-Hartman 	}
2395ab4382d2SGreg Kroah-Hartman 
2396ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2397ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2398ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
239991f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
240091f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
240191f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
240291f8c2d8SBoris BREZILLON 			return ret;
240391f8c2d8SBoris BREZILLON 		}
240491f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
240591f8c2d8SBoris BREZILLON 		if (ret) {
240691f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
240791f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
240891f8c2d8SBoris BREZILLON 			return ret;
240991f8c2d8SBoris BREZILLON 		}
2410ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
241191f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2412ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2413ab4382d2SGreg Kroah-Hartman 	}
2414ab4382d2SGreg Kroah-Hartman 
2415ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
241613bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2417ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
241864e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2419ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2420ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2421ab4382d2SGreg Kroah-Hartman 	} else {
2422ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2423ab4382d2SGreg Kroah-Hartman 	}
242491f8c2d8SBoris BREZILLON 
242591f8c2d8SBoris BREZILLON 	return 0;
2426ab4382d2SGreg Kroah-Hartman }
2427ab4382d2SGreg Kroah-Hartman 
242869f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
242969f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2430ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2431ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2432ab4382d2SGreg Kroah-Hartman {
24334e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2434ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2435a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2436ab4382d2SGreg Kroah-Hartman }
2437ab4382d2SGreg Kroah-Hartman 
2438ab4382d2SGreg Kroah-Hartman /*
2439ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2440ab4382d2SGreg Kroah-Hartman  */
2441ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2442ab4382d2SGreg Kroah-Hartman {
2443ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2444ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2445ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2446ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2447ab4382d2SGreg Kroah-Hartman 
2448ab4382d2SGreg Kroah-Hartman 	/*
2449ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2450ab4382d2SGreg Kroah-Hartman 	 */
24514e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
24524e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
24534e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2454ab4382d2SGreg Kroah-Hartman 
2455ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
24564e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
24574e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2458ab4382d2SGreg Kroah-Hartman 
2459ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2460ab4382d2SGreg Kroah-Hartman 
2461ab4382d2SGreg Kroah-Hartman 	/*
2462ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2463ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2464ab4382d2SGreg Kroah-Hartman 	 */
2465ab4382d2SGreg Kroah-Hartman 	do {
24664e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2467ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2468ab4382d2SGreg Kroah-Hartman 
2469ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2470ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24714e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2472ab4382d2SGreg Kroah-Hartman 
2473ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2475ab4382d2SGreg Kroah-Hartman }
2476ab4382d2SGreg Kroah-Hartman 
2477ab4382d2SGreg Kroah-Hartman /*
2478ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2479ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2480ab4382d2SGreg Kroah-Hartman  */
2481ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2482ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2483ab4382d2SGreg Kroah-Hartman {
2484ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2485ab4382d2SGreg Kroah-Hartman 
2486ab4382d2SGreg Kroah-Hartman 	/*
2487ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2488ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2489ab4382d2SGreg Kroah-Hartman 	 */
24904e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2491ab4382d2SGreg Kroah-Hartman 	if (!quot)
2492ab4382d2SGreg Kroah-Hartman 		return;
2493ab4382d2SGreg Kroah-Hartman 
24944e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2495ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2496ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2497ab4382d2SGreg Kroah-Hartman 	else
2498ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2499ab4382d2SGreg Kroah-Hartman 
25004e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2501ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2502ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2503ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2504ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2505ab4382d2SGreg Kroah-Hartman 
2506ab4382d2SGreg Kroah-Hartman 	/*
2507ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2508ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2509ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2510ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2511ab4382d2SGreg Kroah-Hartman 	 */
2512ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2513ab4382d2SGreg Kroah-Hartman }
2514ab4382d2SGreg Kroah-Hartman 
2515ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2516ab4382d2SGreg Kroah-Hartman {
251791f8c2d8SBoris BREZILLON 	int ret;
2518ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2519ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2520ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2521ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2522ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2523ab4382d2SGreg Kroah-Hartman 
2524ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2525ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2526ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2527ab4382d2SGreg Kroah-Hartman 	}
2528ab4382d2SGreg Kroah-Hartman 
252991f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
253091f8c2d8SBoris BREZILLON 	if (ret)
253191f8c2d8SBoris BREZILLON 		return ret;
2532ab4382d2SGreg Kroah-Hartman 
25334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
25344e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
25354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2536ab4382d2SGreg Kroah-Hartman 
2537ab4382d2SGreg Kroah-Hartman 	if (options)
2538ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2539ab4382d2SGreg Kroah-Hartman 	else
2540ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2541ab4382d2SGreg Kroah-Hartman 
2542ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2543ab4382d2SGreg Kroah-Hartman }
2544ab4382d2SGreg Kroah-Hartman 
2545ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2546ab4382d2SGreg Kroah-Hartman 
2547ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2548ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2549ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2550ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2551ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2552ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2553ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2554ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2555ab4382d2SGreg Kroah-Hartman };
2556ab4382d2SGreg Kroah-Hartman 
2557ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2558ab4382d2SGreg Kroah-Hartman 
2559ab4382d2SGreg Kroah-Hartman /*
2560ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2561ab4382d2SGreg Kroah-Hartman  */
2562ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2563ab4382d2SGreg Kroah-Hartman {
256491f8c2d8SBoris BREZILLON 	int ret;
2565ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
25660d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2567574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2568efb8d21bSLinus Torvalds 		int id = pdata->num;
25694cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
25700d0a3cc1SVoss, Nikolaus 
25714cbf9f48SNicolas Ferre 		port->backup_imr = 0;
25724cbf9f48SNicolas Ferre 		port->uart.line = id;
25734cbf9f48SNicolas Ferre 
25744cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
257591f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
257691f8c2d8SBoris BREZILLON 		if (ret)
257791f8c2d8SBoris BREZILLON 			return ret;
2578ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2579ab4382d2SGreg Kroah-Hartman 	}
2580ab4382d2SGreg Kroah-Hartman 
2581ab4382d2SGreg Kroah-Hartman 	return 0;
2582ab4382d2SGreg Kroah-Hartman }
2583ab4382d2SGreg Kroah-Hartman 
2584ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2585ab4382d2SGreg Kroah-Hartman 
2586ab4382d2SGreg Kroah-Hartman /*
2587ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2588ab4382d2SGreg Kroah-Hartman  */
2589ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2590ab4382d2SGreg Kroah-Hartman {
2591ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2592ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2593ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2594ab4382d2SGreg Kroah-Hartman 
2595ab4382d2SGreg Kroah-Hartman 	return 0;
2596ab4382d2SGreg Kroah-Hartman }
2597ab4382d2SGreg Kroah-Hartman 
2598ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2599ab4382d2SGreg Kroah-Hartman 
2600ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2601ab4382d2SGreg Kroah-Hartman {
2602ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2603ab4382d2SGreg Kroah-Hartman }
2604ab4382d2SGreg Kroah-Hartman 
2605ab4382d2SGreg Kroah-Hartman #else
2606ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2607ab4382d2SGreg Kroah-Hartman 
2608ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2609ab4382d2SGreg Kroah-Hartman {
2610ab4382d2SGreg Kroah-Hartman 	return false;
2611ab4382d2SGreg Kroah-Hartman }
2612ab4382d2SGreg Kroah-Hartman #endif
2613ab4382d2SGreg Kroah-Hartman 
2614ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2615ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2616ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2617ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2618ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2619ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2620ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2621ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2622ab4382d2SGreg Kroah-Hartman };
2623ab4382d2SGreg Kroah-Hartman 
2624ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2625ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2626ab4382d2SGreg Kroah-Hartman {
2627ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2628ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2629ab4382d2SGreg Kroah-Hartman #else
2630ab4382d2SGreg Kroah-Hartman 	return false;
2631ab4382d2SGreg Kroah-Hartman #endif
2632ab4382d2SGreg Kroah-Hartman }
2633ab4382d2SGreg Kroah-Hartman 
2634ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2635ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2636ab4382d2SGreg Kroah-Hartman {
2637ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2638ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2639ab4382d2SGreg Kroah-Hartman 
2640ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2641ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
26424e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
26434e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2644ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2645ab4382d2SGreg Kroah-Hartman 	}
2646ab4382d2SGreg Kroah-Hartman 
2647ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2648ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
26492c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
26502c7af5baSBoris BREZILLON 		unsigned long flags;
26512c7af5baSBoris BREZILLON 
26522c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26532c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
26542c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2655ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
26562c7af5baSBoris BREZILLON 	}
2657ab4382d2SGreg Kroah-Hartman 
2658ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2659ab4382d2SGreg Kroah-Hartman 
2660ab4382d2SGreg Kroah-Hartman 	return 0;
2661ab4382d2SGreg Kroah-Hartman }
2662ab4382d2SGreg Kroah-Hartman 
2663ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2664ab4382d2SGreg Kroah-Hartman {
2665ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2666ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26672c7af5baSBoris BREZILLON 	unsigned long flags;
26682c7af5baSBoris BREZILLON 
26692c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26702c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26712c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26722c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26732c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26742c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26752c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26762c7af5baSBoris BREZILLON 	}
26772c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26782c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2679ab4382d2SGreg Kroah-Hartman 
2680ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2681ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2682ab4382d2SGreg Kroah-Hartman 
2683ab4382d2SGreg Kroah-Hartman 	return 0;
2684ab4382d2SGreg Kroah-Hartman }
2685ab4382d2SGreg Kroah-Hartman #else
2686ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2687ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2688ab4382d2SGreg Kroah-Hartman #endif
2689ab4382d2SGreg Kroah-Hartman 
2690e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2691e0b0baadSRichard Genoud {
2692ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
2693ab5e4e41SRichard Genoud 	struct gpio_desc *gpiod;
2694ab5e4e41SRichard Genoud 
26957d8c70d8SUwe Kleine-König 	p->gpios = mctrl_gpio_init_noauto(dev, 0);
2696722ccf41SUwe Kleine-König 	if (IS_ERR(p->gpios))
2697722ccf41SUwe Kleine-König 		return PTR_ERR(p->gpios);
2698e0b0baadSRichard Genoud 
2699ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++) {
2700ab5e4e41SRichard Genoud 		gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2701ab5e4e41SRichard Genoud 		if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2702ab5e4e41SRichard Genoud 			p->gpio_irq[i] = gpiod_to_irq(gpiod);
2703ab5e4e41SRichard Genoud 		else
2704ab5e4e41SRichard Genoud 			p->gpio_irq[i] = -EINVAL;
2705ab5e4e41SRichard Genoud 	}
2706ab5e4e41SRichard Genoud 
2707e0b0baadSRichard Genoud 	return 0;
2708e0b0baadSRichard Genoud }
2709e0b0baadSRichard Genoud 
2710b5199d46SCyrille Pitchen static void atmel_serial_probe_fifos(struct atmel_uart_port *port,
2711b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2712b5199d46SCyrille Pitchen {
2713b5199d46SCyrille Pitchen 	port->fifo_size = 0;
2714b5199d46SCyrille Pitchen 	port->rts_low = 0;
2715b5199d46SCyrille Pitchen 	port->rts_high = 0;
2716b5199d46SCyrille Pitchen 
2717b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2718b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2719b5199d46SCyrille Pitchen 				 &port->fifo_size))
2720b5199d46SCyrille Pitchen 		return;
2721b5199d46SCyrille Pitchen 
2722b5199d46SCyrille Pitchen 	if (!port->fifo_size)
2723b5199d46SCyrille Pitchen 		return;
2724b5199d46SCyrille Pitchen 
2725b5199d46SCyrille Pitchen 	if (port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2726b5199d46SCyrille Pitchen 		port->fifo_size = 0;
2727b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2728b5199d46SCyrille Pitchen 		return;
2729b5199d46SCyrille Pitchen 	}
2730b5199d46SCyrille Pitchen 
2731b5199d46SCyrille Pitchen 	/*
2732b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2733b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2734b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2735b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2736b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2737b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2738b5199d46SCyrille Pitchen 	 */
2739b5199d46SCyrille Pitchen 	port->rts_high = max_t(int, port->fifo_size >> 1,
2740b5199d46SCyrille Pitchen 			       port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2741b5199d46SCyrille Pitchen 	port->rts_low  = max_t(int, port->fifo_size >> 2,
2742b5199d46SCyrille Pitchen 			       port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2743b5199d46SCyrille Pitchen 
2744b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2745b5199d46SCyrille Pitchen 		 port->fifo_size);
2746b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2747b5199d46SCyrille Pitchen 		port->rts_high);
2748b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2749b5199d46SCyrille Pitchen 		port->rts_low);
2750b5199d46SCyrille Pitchen }
2751b5199d46SCyrille Pitchen 
27529671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2753ab4382d2SGreg Kroah-Hartman {
2754ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
27555fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2756574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2757ab4382d2SGreg Kroah-Hartman 	void *data;
27584cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2759bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2760ab4382d2SGreg Kroah-Hartman 
2761ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2762ab4382d2SGreg Kroah-Hartman 
27635fbe46b6SNicolas Ferre 	if (np)
27645fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
27655fbe46b6SNicolas Ferre 	else
27664cbf9f48SNicolas Ferre 		if (pdata)
27674cbf9f48SNicolas Ferre 			ret = pdata->num;
27684cbf9f48SNicolas Ferre 
27694cbf9f48SNicolas Ferre 	if (ret < 0)
27705fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
27714cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2772503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
27734cbf9f48SNicolas Ferre 
2774503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
27754cbf9f48SNicolas Ferre 		ret = -ENODEV;
27764cbf9f48SNicolas Ferre 		goto err;
27774cbf9f48SNicolas Ferre 	}
27784cbf9f48SNicolas Ferre 
2779503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
27804cbf9f48SNicolas Ferre 		/* port already in use */
27814cbf9f48SNicolas Ferre 		ret = -EBUSY;
27824cbf9f48SNicolas Ferre 		goto err;
27834cbf9f48SNicolas Ferre 	}
27844cbf9f48SNicolas Ferre 
27854cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2786ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
27874cbf9f48SNicolas Ferre 	port->uart.line = ret;
2788b5199d46SCyrille Pitchen 	atmel_serial_probe_fifos(port, pdev);
2789354e57f3SLinus Walleij 
27902c7af5baSBoris BREZILLON 	spin_lock_init(&port->lock_suspended);
27912c7af5baSBoris BREZILLON 
2792e0b0baadSRichard Genoud 	ret = atmel_init_gpios(port, &pdev->dev);
2793722ccf41SUwe Kleine-König 	if (ret < 0) {
2794722ccf41SUwe Kleine-König 		dev_err(&pdev->dev, "Failed to initialize GPIOs.");
27958f1bd8f2SUwe Kleine-König 		goto err_clear_bit;
2796722ccf41SUwe Kleine-König 	}
2797ab4382d2SGreg Kroah-Hartman 
279891f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
279991f8c2d8SBoris BREZILLON 	if (ret)
28006fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2801ab4382d2SGreg Kroah-Hartman 
280264e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2803ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2804ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2805ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2806ab4382d2SGreg Kroah-Hartman 		if (!data)
2807ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2808ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2809ab4382d2SGreg Kroah-Hartman 	}
2810ab4382d2SGreg Kroah-Hartman 
2811bd737f87SRicardo Ribalda Delgado 	rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2812bd737f87SRicardo Ribalda Delgado 
2813ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2814ab4382d2SGreg Kroah-Hartman 	if (ret)
2815ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2816ab4382d2SGreg Kroah-Hartman 
2817ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2818ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2819ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2820ab4382d2SGreg Kroah-Hartman 		/*
2821ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
282291f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2823ab4382d2SGreg Kroah-Hartman 		 */
282491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2825ab4382d2SGreg Kroah-Hartman 	}
2826ab4382d2SGreg Kroah-Hartman #endif
2827ab4382d2SGreg Kroah-Hartman 
2828ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2829ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2830ab4382d2SGreg Kroah-Hartman 
2831d4f64187SCyrille Pitchen 	/*
2832d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2833d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2834d4f64187SCyrille Pitchen 	 */
2835d4f64187SCyrille Pitchen 	clk_prepare_enable(port->clk);
2836d4f64187SCyrille Pitchen 
2837bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
28384e7decdaSCyrille Pitchen 		atmel_uart_writel(&port->uart, ATMEL_US_MR,
28394e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
28404e7decdaSCyrille Pitchen 		atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
2841fc887b15SLinus Torvalds 	}
2842fc887b15SLinus Torvalds 
2843055560b0SElen Song 	/*
2844055560b0SElen Song 	 * Get port name of usart or uart
2845055560b0SElen Song 	 */
2846892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2847055560b0SElen Song 
2848d4f64187SCyrille Pitchen 	/*
2849d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2850d4f64187SCyrille Pitchen 	 * is used
2851d4f64187SCyrille Pitchen 	 */
2852d4f64187SCyrille Pitchen 	clk_disable_unprepare(port->clk);
2853d4f64187SCyrille Pitchen 
2854ab4382d2SGreg Kroah-Hartman 	return 0;
2855ab4382d2SGreg Kroah-Hartman 
2856ab4382d2SGreg Kroah-Hartman err_add_port:
2857ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2858ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2859ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2860ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2861ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2862ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2863ab4382d2SGreg Kroah-Hartman 	}
28646fbb9bdfSCyrille Pitchen err_clear_bit:
28656fbb9bdfSCyrille Pitchen 	clear_bit(port->uart.line, atmel_ports_in_use);
28664cbf9f48SNicolas Ferre err:
2867ab4382d2SGreg Kroah-Hartman 	return ret;
2868ab4382d2SGreg Kroah-Hartman }
2869ab4382d2SGreg Kroah-Hartman 
2870ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2871ab4382d2SGreg Kroah-Hartman {
2872ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2873ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2874ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2875ab4382d2SGreg Kroah-Hartman 
2876f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2877f50c995fSMarek Roszko 
2878ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2879ab4382d2SGreg Kroah-Hartman 
2880ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2881ab4382d2SGreg Kroah-Hartman 
2882ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2883ab4382d2SGreg Kroah-Hartman 
2884ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2885ab4382d2SGreg Kroah-Hartman 
2886503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
28874cbf9f48SNicolas Ferre 
2888ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2889ab4382d2SGreg Kroah-Hartman 
2890ab4382d2SGreg Kroah-Hartman 	return ret;
2891ab4382d2SGreg Kroah-Hartman }
2892ab4382d2SGreg Kroah-Hartman 
2893ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2894ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
28952d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2896ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2897ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2898ab4382d2SGreg Kroah-Hartman 	.driver		= {
2899ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
29005fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2901ab4382d2SGreg Kroah-Hartman 	},
2902ab4382d2SGreg Kroah-Hartman };
2903ab4382d2SGreg Kroah-Hartman 
2904ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2905ab4382d2SGreg Kroah-Hartman {
2906ab4382d2SGreg Kroah-Hartman 	int ret;
2907ab4382d2SGreg Kroah-Hartman 
2908ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2909ab4382d2SGreg Kroah-Hartman 	if (ret)
2910ab4382d2SGreg Kroah-Hartman 		return ret;
2911ab4382d2SGreg Kroah-Hartman 
2912ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2913ab4382d2SGreg Kroah-Hartman 	if (ret)
2914ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2915ab4382d2SGreg Kroah-Hartman 
2916ab4382d2SGreg Kroah-Hartman 	return ret;
2917ab4382d2SGreg Kroah-Hartman }
2918ab4382d2SGreg Kroah-Hartman 
2919ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2920ab4382d2SGreg Kroah-Hartman {
2921ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2922ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2923ab4382d2SGreg Kroah-Hartman }
2924ab4382d2SGreg Kroah-Hartman 
2925ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2926ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2927ab4382d2SGreg Kroah-Hartman 
2928ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2929ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2930ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2931ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2932