xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision b5199d46817766c95ef759684658cd8e359c6d27)
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 
59*b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
60*b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
61*b5199d46SCyrille Pitchen /*
62*b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
63*b5199d46SCyrille Pitchen  * high and low thresholds
64*b5199d46SCyrille Pitchen  */
65*b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
66*b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
67*b5199d46SCyrille 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 /*
115ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
116ab4382d2SGreg Kroah-Hartman  */
117ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
118ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
119ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
120ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
121ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
122ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
123ab4382d2SGreg Kroah-Hartman 
12434df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
12564e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
126ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
127ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
128ab4382d2SGreg Kroah-Hartman 
12908f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
13064e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
131ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
132ab4382d2SGreg Kroah-Hartman 
13308f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
13434df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
13508f738beSElen Song 	struct dma_chan			*chan_tx;
13634df42f5SElen Song 	struct dma_chan			*chan_rx;
13708f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
13834df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
13908f738beSElen Song 	dma_cookie_t			cookie_tx;
14034df42f5SElen Song 	dma_cookie_t			cookie_rx;
14108f738beSElen Song 	struct scatterlist		sg_tx;
14234df42f5SElen Song 	struct scatterlist		sg_rx;
143ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
144ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
145ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
146d033e82dSLeilei Zhao 	unsigned int		status_change;
147ab4382d2SGreg Kroah-Hartman 
148ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
149ab4382d2SGreg Kroah-Hartman 
150e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
151ab5e4e41SRichard Genoud 	int			gpio_irq[UART_GPIO_MAX];
152ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
153*b5199d46SCyrille Pitchen 	u32			fifo_size;
154*b5199d46SCyrille Pitchen 	u32			rts_high;
155*b5199d46SCyrille Pitchen 	u32			rts_low;
156ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
157055560b0SElen Song 	bool			is_usart;	/* usart or uart */
1582e68c22fSElen Song 	struct timer_list	uart_timer;	/* uart timer */
1592c7af5baSBoris BREZILLON 
1602c7af5baSBoris BREZILLON 	bool			suspended;
1612c7af5baSBoris BREZILLON 	unsigned int		pending;
1622c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1632c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1642c7af5baSBoris BREZILLON 
165a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
166a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
167a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
168a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
169a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
170a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
171ab4382d2SGreg Kroah-Hartman };
172ab4382d2SGreg Kroah-Hartman 
173ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
174503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
175ab4382d2SGreg Kroah-Hartman 
176ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
177ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
178ab4382d2SGreg Kroah-Hartman #endif
179ab4382d2SGreg Kroah-Hartman 
1805fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1815fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1825fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1835fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1845fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1855fbe46b6SNicolas Ferre };
1865fbe46b6SNicolas Ferre 
1875fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
1885fbe46b6SNicolas Ferre #endif
1895fbe46b6SNicolas Ferre 
190ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
191ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
192ab4382d2SGreg Kroah-Hartman {
193ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
194ab4382d2SGreg Kroah-Hartman }
195ab4382d2SGreg Kroah-Hartman 
1964e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
1974e7decdaSCyrille Pitchen {
1984e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
1994e7decdaSCyrille Pitchen }
2004e7decdaSCyrille Pitchen 
2014e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2024e7decdaSCyrille Pitchen {
2034e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2044e7decdaSCyrille Pitchen }
2054e7decdaSCyrille Pitchen 
206*b5199d46SCyrille Pitchen static inline u8 atmel_uart_readb(struct uart_port *port, u32 reg)
207*b5199d46SCyrille Pitchen {
208*b5199d46SCyrille Pitchen 	return __raw_readb(port->membase + reg);
209*b5199d46SCyrille Pitchen }
210*b5199d46SCyrille Pitchen 
211*b5199d46SCyrille Pitchen static inline void atmel_uart_writeb(struct uart_port *port, u32 reg, u8 value)
212*b5199d46SCyrille Pitchen {
213*b5199d46SCyrille Pitchen 	__raw_writeb(value, port->membase + reg);
214*b5199d46SCyrille Pitchen }
215*b5199d46SCyrille Pitchen 
216ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
21764e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
218ab4382d2SGreg Kroah-Hartman {
219ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
220ab4382d2SGreg Kroah-Hartman 
22164e22ebeSElen Song 	return atmel_port->use_pdc_rx;
222ab4382d2SGreg Kroah-Hartman }
223ab4382d2SGreg Kroah-Hartman 
22464e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
225ab4382d2SGreg Kroah-Hartman {
226ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
227ab4382d2SGreg Kroah-Hartman 
22864e22ebeSElen Song 	return atmel_port->use_pdc_tx;
229ab4382d2SGreg Kroah-Hartman }
230ab4382d2SGreg Kroah-Hartman #else
23164e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
232ab4382d2SGreg Kroah-Hartman {
233ab4382d2SGreg Kroah-Hartman 	return false;
234ab4382d2SGreg Kroah-Hartman }
235ab4382d2SGreg Kroah-Hartman 
23664e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
237ab4382d2SGreg Kroah-Hartman {
238ab4382d2SGreg Kroah-Hartman 	return false;
239ab4382d2SGreg Kroah-Hartman }
240ab4382d2SGreg Kroah-Hartman #endif
241ab4382d2SGreg Kroah-Hartman 
24208f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
24308f738beSElen Song {
24408f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
24508f738beSElen Song 
24608f738beSElen Song 	return atmel_port->use_dma_tx;
24708f738beSElen Song }
24808f738beSElen Song 
24934df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
25034df42f5SElen Song {
25134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
25234df42f5SElen Song 
25334df42f5SElen Song 	return atmel_port->use_dma_rx;
25434df42f5SElen Song }
25534df42f5SElen Song 
256e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
257e0b0baadSRichard Genoud {
258e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
259e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
260e0b0baadSRichard Genoud 
2614e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
262e0b0baadSRichard Genoud 
263e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
264e0b0baadSRichard Genoud 
265e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
266e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
267e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
268e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
269e0b0baadSRichard Genoud 		else
270e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
271e0b0baadSRichard Genoud 	}
272e0b0baadSRichard Genoud 
273e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
274e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
275e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
276e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
277e0b0baadSRichard Genoud 		else
278e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
279e0b0baadSRichard Genoud 	}
280e0b0baadSRichard Genoud 
281e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
282e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
283e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
284e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
285e0b0baadSRichard Genoud 		else
286e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
287e0b0baadSRichard Genoud 	}
288e0b0baadSRichard Genoud 
289e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
290e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
291e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
292e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
293e0b0baadSRichard Genoud 		else
294e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
295e0b0baadSRichard Genoud 	}
296e0b0baadSRichard Genoud 
297e0b0baadSRichard Genoud 	return status;
298e0b0baadSRichard Genoud }
299e0b0baadSRichard Genoud 
300ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
30113bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
30213bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
303ab4382d2SGreg Kroah-Hartman {
304ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
305ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
306ab4382d2SGreg Kroah-Hartman 
307ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3084e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
309ab4382d2SGreg Kroah-Hartman 
3104e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
311ab4382d2SGreg Kroah-Hartman 
312ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
313ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
314ab4382d2SGreg Kroah-Hartman 
31513bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
316ab4382d2SGreg Kroah-Hartman 
317ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
318ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
319ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3204e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3214e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
322ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
323ab4382d2SGreg Kroah-Hartman 	} else {
324ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
32564e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
326ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
327ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
328ab4382d2SGreg Kroah-Hartman 		else
329ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
330ab4382d2SGreg Kroah-Hartman 	}
3314e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
332ab4382d2SGreg Kroah-Hartman 
333ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3344e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
335ab4382d2SGreg Kroah-Hartman 
33613bd3e6fSRicardo Ribalda Delgado 	return 0;
337ab4382d2SGreg Kroah-Hartman }
338ab4382d2SGreg Kroah-Hartman 
339ab4382d2SGreg Kroah-Hartman /*
340ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
341ab4382d2SGreg Kroah-Hartman  */
342ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
343ab4382d2SGreg Kroah-Hartman {
3444e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3454e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3464e7decdaSCyrille Pitchen 		0;
347ab4382d2SGreg Kroah-Hartman }
348ab4382d2SGreg Kroah-Hartman 
349ab4382d2SGreg Kroah-Hartman /*
350ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
351ab4382d2SGreg Kroah-Hartman  */
352ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
353ab4382d2SGreg Kroah-Hartman {
354ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3554e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3561cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
357ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
358ab4382d2SGreg Kroah-Hartman 
3591cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3601cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
3614e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3624e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
3631cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3641cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3651cf6e8fcSCyrille Pitchen 	}
3661cf6e8fcSCyrille Pitchen 
3671cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
3681cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
3691cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3701cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
3711cf6e8fcSCyrille Pitchen 
3721cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
3731cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
3741cf6e8fcSCyrille Pitchen 	} else {
3751cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3761cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
3771cf6e8fcSCyrille Pitchen 
3781cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
3791cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
3801cf6e8fcSCyrille Pitchen 	}
3811cf6e8fcSCyrille Pitchen 
382ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
3831cf6e8fcSCyrille Pitchen 		control |= rts_ready;
384ab4382d2SGreg Kroah-Hartman 	else
3851cf6e8fcSCyrille Pitchen 		control |= rts_paused;
386ab4382d2SGreg Kroah-Hartman 
387ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
388ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
389ab4382d2SGreg Kroah-Hartman 	else
390ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
391ab4382d2SGreg Kroah-Hartman 
3924e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
393ab4382d2SGreg Kroah-Hartman 
394e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
395e0b0baadSRichard Genoud 
396ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
3971cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
398ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
399ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
400ab4382d2SGreg Kroah-Hartman 	else
401ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
402ab4382d2SGreg Kroah-Hartman 
4034e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
404ab4382d2SGreg Kroah-Hartman }
405ab4382d2SGreg Kroah-Hartman 
406ab4382d2SGreg Kroah-Hartman /*
407ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
408ab4382d2SGreg Kroah-Hartman  */
409ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
410ab4382d2SGreg Kroah-Hartman {
411e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
412e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
413ab4382d2SGreg Kroah-Hartman 
4144e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
415ab4382d2SGreg Kroah-Hartman 
416ab4382d2SGreg Kroah-Hartman 	/*
417ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
418ab4382d2SGreg Kroah-Hartman 	 */
419ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
420ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
421ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
422ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
423ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
424ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
425ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
426ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
427ab4382d2SGreg Kroah-Hartman 
428e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
429ab4382d2SGreg Kroah-Hartman }
430ab4382d2SGreg Kroah-Hartman 
431ab4382d2SGreg Kroah-Hartman /*
432ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
433ab4382d2SGreg Kroah-Hartman  */
434ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
435ab4382d2SGreg Kroah-Hartman {
436ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
437ab4382d2SGreg Kroah-Hartman 
43864e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
439ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4404e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
441ab4382d2SGreg Kroah-Hartman 	}
442ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4434e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
444ab4382d2SGreg Kroah-Hartman 
44513bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
44613bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
447ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
448ab4382d2SGreg Kroah-Hartman }
449ab4382d2SGreg Kroah-Hartman 
450ab4382d2SGreg Kroah-Hartman /*
451ab4382d2SGreg Kroah-Hartman  * Start transmitting.
452ab4382d2SGreg Kroah-Hartman  */
453ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
454ab4382d2SGreg Kroah-Hartman {
455ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
456ab4382d2SGreg Kroah-Hartman 
45764e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
4584e7decdaSCyrille Pitchen 		if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
459ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
460ab4382d2SGreg Kroah-Hartman 			   really need this.*/
461ab4382d2SGreg Kroah-Hartman 			return;
462ab4382d2SGreg Kroah-Hartman 
46313bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
46413bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
465ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
466ab4382d2SGreg Kroah-Hartman 
467ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
4684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
469ab4382d2SGreg Kroah-Hartman 	}
470ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
4714e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
472ab4382d2SGreg Kroah-Hartman }
473ab4382d2SGreg Kroah-Hartman 
474ab4382d2SGreg Kroah-Hartman /*
475ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
476ab4382d2SGreg Kroah-Hartman  */
477ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
478ab4382d2SGreg Kroah-Hartman {
4794e7decdaSCyrille Pitchen 	/* reset status and receiver */
4804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
481ab4382d2SGreg Kroah-Hartman 
4824e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
48357c36868SSiftar, Gabe 
48464e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
485ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
4864e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
4874e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
488ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
4894e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
490ab4382d2SGreg Kroah-Hartman 	} else {
4914e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
492ab4382d2SGreg Kroah-Hartman 	}
493ab4382d2SGreg Kroah-Hartman }
494ab4382d2SGreg Kroah-Hartman 
495ab4382d2SGreg Kroah-Hartman /*
496ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
497ab4382d2SGreg Kroah-Hartman  */
498ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
499ab4382d2SGreg Kroah-Hartman {
5004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
50157c36868SSiftar, Gabe 
50264e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
503ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5044e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5064e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
507ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
508ab4382d2SGreg Kroah-Hartman 	} else {
5094e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
510ab4382d2SGreg Kroah-Hartman 	}
511ab4382d2SGreg Kroah-Hartman }
512ab4382d2SGreg Kroah-Hartman 
513ab4382d2SGreg Kroah-Hartman /*
514ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
515ab4382d2SGreg Kroah-Hartman  */
516ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
517ab4382d2SGreg Kroah-Hartman {
518ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
519ab5e4e41SRichard Genoud 	uint32_t ier = 0;
520ab5e4e41SRichard Genoud 
521ab5e4e41SRichard Genoud 	/*
522ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
523ab5e4e41SRichard Genoud 	 */
524ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
525ab5e4e41SRichard Genoud 		return;
526ab5e4e41SRichard Genoud 
527ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
528ab5e4e41SRichard Genoud 
529ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
530ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
531ab5e4e41SRichard Genoud 	else
532ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
533ab5e4e41SRichard Genoud 
534ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
535ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
536ab5e4e41SRichard Genoud 	else
537ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
538ab5e4e41SRichard Genoud 
539ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
540ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
541ab5e4e41SRichard Genoud 	else
542ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
543ab5e4e41SRichard Genoud 
544ab5e4e41SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
545ab5e4e41SRichard Genoud 		enable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
546ab5e4e41SRichard Genoud 	else
547ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
548ab5e4e41SRichard Genoud 
5494e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
550ab4382d2SGreg Kroah-Hartman }
551ab4382d2SGreg Kroah-Hartman 
552ab4382d2SGreg Kroah-Hartman /*
55335b675b9SRichard Genoud  * Disable modem status interrupts
55435b675b9SRichard Genoud  */
55535b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
55635b675b9SRichard Genoud {
55735b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
55835b675b9SRichard Genoud 	uint32_t idr = 0;
55935b675b9SRichard Genoud 
56035b675b9SRichard Genoud 	/*
56135b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
56235b675b9SRichard Genoud 	 */
56335b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
56435b675b9SRichard Genoud 		return;
56535b675b9SRichard Genoud 
56635b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
56735b675b9SRichard Genoud 
56835b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_CTS] >= 0)
56935b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_CTS]);
57035b675b9SRichard Genoud 	else
57135b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
57235b675b9SRichard Genoud 
57335b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DSR] >= 0)
57435b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DSR]);
57535b675b9SRichard Genoud 	else
57635b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
57735b675b9SRichard Genoud 
57835b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_RI] >= 0)
57935b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_RI]);
58035b675b9SRichard Genoud 	else
58135b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
58235b675b9SRichard Genoud 
58335b675b9SRichard Genoud 	if (atmel_port->gpio_irq[UART_GPIO_DCD] >= 0)
58435b675b9SRichard Genoud 		disable_irq(atmel_port->gpio_irq[UART_GPIO_DCD]);
58535b675b9SRichard Genoud 	else
58635b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
58735b675b9SRichard Genoud 
5884e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
58935b675b9SRichard Genoud }
59035b675b9SRichard Genoud 
59135b675b9SRichard Genoud /*
592ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
593ab4382d2SGreg Kroah-Hartman  */
594ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
595ab4382d2SGreg Kroah-Hartman {
596ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
5974e7decdaSCyrille Pitchen 		/* start break */
5984e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
599ab4382d2SGreg Kroah-Hartman 	else
6004e7decdaSCyrille Pitchen 		/* stop break */
6014e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
602ab4382d2SGreg Kroah-Hartman }
603ab4382d2SGreg Kroah-Hartman 
604ab4382d2SGreg Kroah-Hartman /*
605ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
606ab4382d2SGreg Kroah-Hartman  */
607ab4382d2SGreg Kroah-Hartman static void
608ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
609ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
610ab4382d2SGreg Kroah-Hartman {
611ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
612ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
613ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
614ab4382d2SGreg Kroah-Hartman 
615ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
616ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
617ab4382d2SGreg Kroah-Hartman 		return;
618ab4382d2SGreg Kroah-Hartman 
619ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
620ab4382d2SGreg Kroah-Hartman 	c->status	= status;
621ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
622ab4382d2SGreg Kroah-Hartman 
623ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
624ab4382d2SGreg Kroah-Hartman 	smp_wmb();
625ab4382d2SGreg Kroah-Hartman 
626ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
627ab4382d2SGreg Kroah-Hartman }
628ab4382d2SGreg Kroah-Hartman 
629ab4382d2SGreg Kroah-Hartman /*
630ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
631ab4382d2SGreg Kroah-Hartman  */
632ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
633ab4382d2SGreg Kroah-Hartman {
634ab4382d2SGreg Kroah-Hartman 	/* clear error */
6354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
636ab4382d2SGreg Kroah-Hartman 
637ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
638ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
639ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
640ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
641ab4382d2SGreg Kroah-Hartman 	}
642ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
643ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
644ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
645ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
646ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
647ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
648ab4382d2SGreg Kroah-Hartman }
649ab4382d2SGreg Kroah-Hartman 
650ab4382d2SGreg Kroah-Hartman /*
651ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
652ab4382d2SGreg Kroah-Hartman  */
653ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
654ab4382d2SGreg Kroah-Hartman {
655ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
656ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
657ab4382d2SGreg Kroah-Hartman 
6584e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
659ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
660*b5199d46SCyrille Pitchen 		ch = atmel_uart_readb(port, ATMEL_US_RHR);
661ab4382d2SGreg Kroah-Hartman 
662ab4382d2SGreg Kroah-Hartman 		/*
663ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
664ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
665ab4382d2SGreg Kroah-Hartman 		 */
666ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
667ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
668ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
669ab4382d2SGreg Kroah-Hartman 
670ab4382d2SGreg Kroah-Hartman 			/* clear error */
6714e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
672ab4382d2SGreg Kroah-Hartman 
673ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
674ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
675ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
6764e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
6774e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
678ab4382d2SGreg Kroah-Hartman 			} else {
679ab4382d2SGreg Kroah-Hartman 				/*
680ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
681ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
682ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
683ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
684ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
685ab4382d2SGreg Kroah-Hartman 				 */
6864e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
6874e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
688ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
689ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
690ab4382d2SGreg Kroah-Hartman 			}
691ab4382d2SGreg Kroah-Hartman 		}
692ab4382d2SGreg Kroah-Hartman 
693ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
6944e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
695ab4382d2SGreg Kroah-Hartman 	}
696ab4382d2SGreg Kroah-Hartman 
697ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
698ab4382d2SGreg Kroah-Hartman }
699ab4382d2SGreg Kroah-Hartman 
700ab4382d2SGreg Kroah-Hartman /*
701ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
702ab4382d2SGreg Kroah-Hartman  * disabled)
703ab4382d2SGreg Kroah-Hartman  */
704ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
705ab4382d2SGreg Kroah-Hartman {
706ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
707ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
708ab4382d2SGreg Kroah-Hartman 
7094e7decdaSCyrille Pitchen 	if (port->x_char &&
7104e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
711*b5199d46SCyrille Pitchen 		atmel_uart_writeb(port, ATMEL_US_THR, port->x_char);
712ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
713ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
714ab4382d2SGreg Kroah-Hartman 	}
715ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
716ab4382d2SGreg Kroah-Hartman 		return;
717ab4382d2SGreg Kroah-Hartman 
7184e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7194e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
720*b5199d46SCyrille Pitchen 		atmel_uart_writeb(port, ATMEL_US_THR, xmit->buf[xmit->tail]);
721ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
722ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
723ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
724ab4382d2SGreg Kroah-Hartman 			break;
725ab4382d2SGreg Kroah-Hartman 	}
726ab4382d2SGreg Kroah-Hartman 
727ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
728ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
729ab4382d2SGreg Kroah-Hartman 
730ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
731ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7324e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7334e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
734ab4382d2SGreg Kroah-Hartman }
735ab4382d2SGreg Kroah-Hartman 
73608f738beSElen Song static void atmel_complete_tx_dma(void *arg)
73708f738beSElen Song {
73808f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
73908f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
74008f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
74108f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
74208f738beSElen Song 	unsigned long flags;
74308f738beSElen Song 
74408f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
74508f738beSElen Song 
74608f738beSElen Song 	if (chan)
74708f738beSElen Song 		dmaengine_terminate_all(chan);
74808f738beSElen Song 	xmit->tail += sg_dma_len(&atmel_port->sg_tx);
74908f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
75008f738beSElen Song 
75108f738beSElen Song 	port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
75208f738beSElen Song 
75308f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
75408f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
75508f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
75608f738beSElen Song 	atmel_port->desc_tx = NULL;
75708f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
75808f738beSElen Song 
75908f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
76008f738beSElen Song 		uart_write_wakeup(port);
76108f738beSElen Song 
7621842dc2eSCyrille Pitchen 	/*
7631842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7641842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7651842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7661842dc2eSCyrille Pitchen 	 */
76708f738beSElen Song 	if (!uart_circ_empty(xmit))
76808f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
76908f738beSElen Song 
77008f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
77108f738beSElen Song }
77208f738beSElen Song 
77308f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
77408f738beSElen Song {
77508f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
77608f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
77708f738beSElen Song 
77808f738beSElen Song 	if (chan) {
77908f738beSElen Song 		dmaengine_terminate_all(chan);
78008f738beSElen Song 		dma_release_channel(chan);
78108f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
78248479148SWolfram Sang 				DMA_TO_DEVICE);
78308f738beSElen Song 	}
78408f738beSElen Song 
78508f738beSElen Song 	atmel_port->desc_tx = NULL;
78608f738beSElen Song 	atmel_port->chan_tx = NULL;
78708f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
78808f738beSElen Song }
78908f738beSElen Song 
79008f738beSElen Song /*
79108f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
79208f738beSElen Song  */
79308f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
79408f738beSElen Song {
79508f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
79608f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
79708f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
79808f738beSElen Song 	struct dma_async_tx_descriptor *desc;
79908f738beSElen Song 	struct scatterlist *sg = &atmel_port->sg_tx;
80008f738beSElen Song 
80108f738beSElen Song 	/* Make sure we have an idle channel */
80208f738beSElen Song 	if (atmel_port->desc_tx != NULL)
80308f738beSElen Song 		return;
80408f738beSElen Song 
80508f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
80608f738beSElen Song 		/*
80708f738beSElen Song 		 * DMA is idle now.
80808f738beSElen Song 		 * Port xmit buffer is already mapped,
80908f738beSElen Song 		 * and it is one page... Just adjust
81008f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
81108f738beSElen Song 		 * we have to transmit till the end, and then the rest.
81208f738beSElen Song 		 * Take the port lock to get a
81308f738beSElen Song 		 * consistent xmit buffer state.
81408f738beSElen Song 		 */
81508f738beSElen Song 		sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
81608f738beSElen Song 		sg_dma_address(sg) = (sg_dma_address(sg) &
81708f738beSElen Song 					~(UART_XMIT_SIZE - 1))
81808f738beSElen Song 					+ sg->offset;
81908f738beSElen Song 		sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
82008f738beSElen Song 						xmit->tail,
82108f738beSElen Song 						UART_XMIT_SIZE);
82208f738beSElen Song 		BUG_ON(!sg_dma_len(sg));
82308f738beSElen Song 
82408f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
82508f738beSElen Song 					       sg,
82608f738beSElen Song 					       1,
82708f738beSElen Song 					       DMA_MEM_TO_DEV,
82808f738beSElen Song 					       DMA_PREP_INTERRUPT |
82908f738beSElen Song 					       DMA_CTRL_ACK);
83008f738beSElen Song 		if (!desc) {
83108f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
83208f738beSElen Song 			return;
83308f738beSElen Song 		}
83408f738beSElen Song 
835485819b5SCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg, 1, DMA_TO_DEVICE);
83608f738beSElen Song 
83708f738beSElen Song 		atmel_port->desc_tx = desc;
83808f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
83908f738beSElen Song 		desc->callback_param = atmel_port;
84008f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
84108f738beSElen Song 
84208f738beSElen Song 	} else {
84313bd3e6fSRicardo Ribalda Delgado 		if (port->rs485.flags & SER_RS485_ENABLED) {
84408f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
84508f738beSElen Song 			atmel_start_rx(port);
84608f738beSElen Song 		}
84708f738beSElen Song 	}
84808f738beSElen Song 
84908f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
85008f738beSElen Song 		uart_write_wakeup(port);
85108f738beSElen Song }
85208f738beSElen Song 
85308f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
85408f738beSElen Song {
85508f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
85608f738beSElen Song 	dma_cap_mask_t		mask;
85708f738beSElen Song 	struct dma_slave_config config;
85808f738beSElen Song 	int ret, nent;
85908f738beSElen Song 
86008f738beSElen Song 	dma_cap_zero(mask);
86108f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
86208f738beSElen Song 
86308f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
86408f738beSElen Song 	if (atmel_port->chan_tx == NULL)
86508f738beSElen Song 		goto chan_err;
86608f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
86708f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
86808f738beSElen Song 
86908f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
87008f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
87108f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
8722c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
87308f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
87408f738beSElen Song 			virt_to_page(port->state->xmit.buf),
87508f738beSElen Song 			UART_XMIT_SIZE,
87608f738beSElen Song 			(int)port->state->xmit.buf & ~PAGE_MASK);
87708f738beSElen Song 	nent = dma_map_sg(port->dev,
87808f738beSElen Song 				&atmel_port->sg_tx,
87908f738beSElen Song 				1,
88048479148SWolfram Sang 				DMA_TO_DEVICE);
88108f738beSElen Song 
88208f738beSElen Song 	if (!nent) {
88308f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
88408f738beSElen Song 		goto chan_err;
88508f738beSElen Song 	} else {
88608f738beSElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
88708f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
88808f738beSElen Song 			port->state->xmit.buf,
88908f738beSElen Song 			sg_dma_address(&atmel_port->sg_tx));
89008f738beSElen Song 	}
89108f738beSElen Song 
89208f738beSElen Song 	/* Configure the slave DMA */
89308f738beSElen Song 	memset(&config, 0, sizeof(config));
89408f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
89508f738beSElen Song 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
89608f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
897a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
89808f738beSElen Song 
8995483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9005483c10eSMaxime Ripard 				     &config);
90108f738beSElen Song 	if (ret) {
90208f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
90308f738beSElen Song 		goto chan_err;
90408f738beSElen Song 	}
90508f738beSElen Song 
90608f738beSElen Song 	return 0;
90708f738beSElen Song 
90808f738beSElen Song chan_err:
90908f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
91008f738beSElen Song 	atmel_port->use_dma_tx = 0;
91108f738beSElen Song 	if (atmel_port->chan_tx)
91208f738beSElen Song 		atmel_release_tx_dma(port);
91308f738beSElen Song 	return -EINVAL;
91408f738beSElen Song }
91508f738beSElen Song 
91634df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
91734df42f5SElen Song {
91834df42f5SElen Song 	struct uart_port *port = arg;
91934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
92034df42f5SElen Song 
92134df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
92234df42f5SElen Song }
92334df42f5SElen Song 
92434df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
92534df42f5SElen Song {
92634df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
92734df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
92834df42f5SElen Song 
92934df42f5SElen Song 	if (chan) {
93034df42f5SElen Song 		dmaengine_terminate_all(chan);
93134df42f5SElen Song 		dma_release_channel(chan);
93234df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
93348479148SWolfram Sang 				DMA_FROM_DEVICE);
93434df42f5SElen Song 	}
93534df42f5SElen Song 
93634df42f5SElen Song 	atmel_port->desc_rx = NULL;
93734df42f5SElen Song 	atmel_port->chan_rx = NULL;
93834df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
93934df42f5SElen Song }
94034df42f5SElen Song 
94134df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
94234df42f5SElen Song {
94334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
94466f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
94534df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
94634df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
94734df42f5SElen Song 	struct dma_tx_state state;
94834df42f5SElen Song 	enum dma_status dmastat;
94966f37aafSCyrille Pitchen 	size_t count;
95034df42f5SElen Song 
95134df42f5SElen Song 
95234df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
9534e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
95434df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
95534df42f5SElen Song 				atmel_port->cookie_rx,
95634df42f5SElen Song 				&state);
95734df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
95834df42f5SElen Song 	if (dmastat == DMA_ERROR) {
95934df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
9604e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
96134df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
96234df42f5SElen Song 		return;
96334df42f5SElen Song 	}
96466f37aafSCyrille Pitchen 
96566f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
96666f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
96766f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
96866f37aafSCyrille Pitchen 			    1,
969485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
97034df42f5SElen Song 
97134df42f5SElen Song 	/*
97266f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
97366f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
97466f37aafSCyrille Pitchen 	 * framework.
97566f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
97666f37aafSCyrille Pitchen 	 * length.
97734df42f5SElen Song 	 */
97866f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
97966f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
98066f37aafSCyrille Pitchen 	/*
98166f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
98266f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
98366f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
98466f37aafSCyrille Pitchen 	 *
98566f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
98666f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
98766f37aafSCyrille Pitchen 	 *
98866f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
98966f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
99066f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
99166f37aafSCyrille Pitchen 	 */
99266f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
99366f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
99434df42f5SElen Song 
99566f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
99666f37aafSCyrille Pitchen 		ring->tail = 0;
99734df42f5SElen Song 		port->icount.rx += count;
99834df42f5SElen Song 	}
99934df42f5SElen Song 
100066f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
100166f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
100266f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
100366f37aafSCyrille Pitchen 
100466f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
100566f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
100666f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
100766f37aafSCyrille Pitchen 			ring->head = 0;
100866f37aafSCyrille Pitchen 		ring->tail = ring->head;
100966f37aafSCyrille Pitchen 		port->icount.rx += count;
101066f37aafSCyrille Pitchen 	}
101166f37aafSCyrille Pitchen 
101266f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
101366f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
101466f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
101566f37aafSCyrille Pitchen 			       1,
1016485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
101766f37aafSCyrille Pitchen 
101866f37aafSCyrille Pitchen 	/*
101966f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
102066f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
102166f37aafSCyrille Pitchen 	 */
102266f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
102366f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
102466f37aafSCyrille Pitchen 	spin_lock(&port->lock);
102566f37aafSCyrille Pitchen 
10264e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
102734df42f5SElen Song }
102834df42f5SElen Song 
102934df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
103034df42f5SElen Song {
103134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
103234df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
103334df42f5SElen Song 	dma_cap_mask_t		mask;
103434df42f5SElen Song 	struct dma_slave_config config;
103534df42f5SElen Song 	struct circ_buf		*ring;
103634df42f5SElen Song 	int ret, nent;
103734df42f5SElen Song 
103834df42f5SElen Song 	ring = &atmel_port->rx_ring;
103934df42f5SElen Song 
104034df42f5SElen Song 	dma_cap_zero(mask);
104134df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
104234df42f5SElen Song 
104334df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
104434df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
104534df42f5SElen Song 		goto chan_err;
104634df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
104734df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
104834df42f5SElen Song 
104934df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
105034df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
105134df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
10522c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
105334df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
105434df42f5SElen Song 		    virt_to_page(ring->buf),
1055a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
105634df42f5SElen Song 		    (int)ring->buf & ~PAGE_MASK);
105734df42f5SElen Song 	nent = dma_map_sg(port->dev,
105834df42f5SElen Song 			  &atmel_port->sg_rx,
105934df42f5SElen Song 			  1,
106048479148SWolfram Sang 			  DMA_FROM_DEVICE);
106134df42f5SElen Song 
106234df42f5SElen Song 	if (!nent) {
106334df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
106434df42f5SElen Song 		goto chan_err;
106534df42f5SElen Song 	} else {
106634df42f5SElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
106734df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
106834df42f5SElen Song 			ring->buf,
106934df42f5SElen Song 			sg_dma_address(&atmel_port->sg_rx));
107034df42f5SElen Song 	}
107134df42f5SElen Song 
107234df42f5SElen Song 	/* Configure the slave DMA */
107334df42f5SElen Song 	memset(&config, 0, sizeof(config));
107434df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
107534df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
107634df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1077a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
107834df42f5SElen Song 
10795483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
10805483c10eSMaxime Ripard 				     &config);
108134df42f5SElen Song 	if (ret) {
108234df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
108334df42f5SElen Song 		goto chan_err;
108434df42f5SElen Song 	}
108534df42f5SElen Song 	/*
108634df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
108734df42f5SElen Song 	 * each one is half ring buffer size
108834df42f5SElen Song 	 */
108934df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
109034df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
109134df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
109234df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
109334df42f5SElen Song 					 DMA_DEV_TO_MEM,
109434df42f5SElen Song 					 DMA_PREP_INTERRUPT);
109534df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
109634df42f5SElen Song 	desc->callback_param = port;
109734df42f5SElen Song 	atmel_port->desc_rx = desc;
109834df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
109934df42f5SElen Song 
110034df42f5SElen Song 	return 0;
110134df42f5SElen Song 
110234df42f5SElen Song chan_err:
110334df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
110434df42f5SElen Song 	atmel_port->use_dma_rx = 0;
110534df42f5SElen Song 	if (atmel_port->chan_rx)
110634df42f5SElen Song 		atmel_release_rx_dma(port);
110734df42f5SElen Song 	return -EINVAL;
110834df42f5SElen Song }
110934df42f5SElen Song 
11102e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
11112e68c22fSElen Song {
11122e68c22fSElen Song 	struct uart_port *port = (void *)data;
11132e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11142e68c22fSElen Song 
11152e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
11162e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
11172e68c22fSElen Song }
11182e68c22fSElen Song 
1119ab4382d2SGreg Kroah-Hartman /*
1120ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1121ab4382d2SGreg Kroah-Hartman  */
1122ab4382d2SGreg Kroah-Hartman static void
1123ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1124ab4382d2SGreg Kroah-Hartman {
1125ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1126ab4382d2SGreg Kroah-Hartman 
112764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1128ab4382d2SGreg Kroah-Hartman 		/*
1129ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1130ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1131ab4382d2SGreg Kroah-Hartman 		 *
1132ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1133ab4382d2SGreg Kroah-Hartman 		 * the moment.
1134ab4382d2SGreg Kroah-Hartman 		 */
1135ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
11364e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
11374e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
1138ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
1139ab4382d2SGreg Kroah-Hartman 		}
1140ab4382d2SGreg Kroah-Hartman 
1141ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1142ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1143ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1144ab4382d2SGreg Kroah-Hartman 	}
1145ab4382d2SGreg Kroah-Hartman 
114634df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
114734df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
11484e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
11494e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
115034df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
115134df42f5SElen Song 		}
115234df42f5SElen Song 	}
115334df42f5SElen Song 
1154ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1155ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1156ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1157ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1158ab4382d2SGreg Kroah-Hartman 		/*
1159ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1160ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1161ab4382d2SGreg Kroah-Hartman 		 */
11624e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
11634e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1164ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1165ab4382d2SGreg Kroah-Hartman 	}
1166ab4382d2SGreg Kroah-Hartman }
1167ab4382d2SGreg Kroah-Hartman 
1168ab4382d2SGreg Kroah-Hartman /*
1169ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1170ab4382d2SGreg Kroah-Hartman  */
1171ab4382d2SGreg Kroah-Hartman static void
1172ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1173ab4382d2SGreg Kroah-Hartman {
1174ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1175ab4382d2SGreg Kroah-Hartman 
1176ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1177ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
11784e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
11794e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1180ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1181ab4382d2SGreg Kroah-Hartman 	}
1182ab4382d2SGreg Kroah-Hartman }
1183ab4382d2SGreg Kroah-Hartman 
1184ab4382d2SGreg Kroah-Hartman /*
1185ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1186ab4382d2SGreg Kroah-Hartman  */
1187ab4382d2SGreg Kroah-Hartman static void
1188ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1189ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1190ab4382d2SGreg Kroah-Hartman {
1191ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1192ab4382d2SGreg Kroah-Hartman 
1193ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1194ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1195ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1196d033e82dSLeilei Zhao 		atmel_port->status_change = atmel_port->irq_status ^
1197d033e82dSLeilei Zhao 					    atmel_port->irq_status_prev;
1198d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
1199ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1200ab4382d2SGreg Kroah-Hartman 	}
1201ab4382d2SGreg Kroah-Hartman }
1202ab4382d2SGreg Kroah-Hartman 
1203ab4382d2SGreg Kroah-Hartman /*
1204ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1205ab4382d2SGreg Kroah-Hartman  */
1206ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1207ab4382d2SGreg Kroah-Hartman {
1208ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1209ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12102c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1211ab5e4e41SRichard Genoud 	bool gpio_handled = false;
1212ab4382d2SGreg Kroah-Hartman 
12132c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
12142c7af5baSBoris BREZILLON 
1215ab4382d2SGreg Kroah-Hartman 	do {
1216e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
12174e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
12182c7af5baSBoris BREZILLON 		pending = status & mask;
1219ab5e4e41SRichard Genoud 		if (!gpio_handled) {
1220ab5e4e41SRichard Genoud 			/*
1221ab5e4e41SRichard Genoud 			 * Dealing with GPIO interrupt
1222ab5e4e41SRichard Genoud 			 */
1223ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_CTS])
1224ab5e4e41SRichard Genoud 				pending |= ATMEL_US_CTSIC;
1225ab5e4e41SRichard Genoud 
1226ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DSR])
1227ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DSRIC;
1228ab5e4e41SRichard Genoud 
1229ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_RI])
1230ab5e4e41SRichard Genoud 				pending |= ATMEL_US_RIIC;
1231ab5e4e41SRichard Genoud 
1232ab5e4e41SRichard Genoud 			if (irq == atmel_port->gpio_irq[UART_GPIO_DCD])
1233ab5e4e41SRichard Genoud 				pending |= ATMEL_US_DCDIC;
1234ab5e4e41SRichard Genoud 
1235ab5e4e41SRichard Genoud 			gpio_handled = true;
1236ab5e4e41SRichard Genoud 		}
1237ab4382d2SGreg Kroah-Hartman 		if (!pending)
1238ab4382d2SGreg Kroah-Hartman 			break;
1239ab4382d2SGreg Kroah-Hartman 
12402c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
12412c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
12422c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
12434e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
12442c7af5baSBoris BREZILLON 			pm_system_wakeup();
12452c7af5baSBoris BREZILLON 			break;
12462c7af5baSBoris BREZILLON 		}
12472c7af5baSBoris BREZILLON 
1248ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1249ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1250ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1251ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1252ab4382d2SGreg Kroah-Hartman 
12532c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
12542c7af5baSBoris BREZILLON 
1255ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1256ab4382d2SGreg Kroah-Hartman }
1257ab4382d2SGreg Kroah-Hartman 
1258a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1259a930e528SElen Song {
1260a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1261a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1262a930e528SElen Song 
1263a930e528SElen Song 	dma_unmap_single(port->dev,
1264a930e528SElen Song 			 pdc->dma_addr,
1265a930e528SElen Song 			 pdc->dma_size,
1266a930e528SElen Song 			 DMA_TO_DEVICE);
1267a930e528SElen Song }
1268a930e528SElen Song 
1269ab4382d2SGreg Kroah-Hartman /*
1270ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1271ab4382d2SGreg Kroah-Hartman  */
127264e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1273ab4382d2SGreg Kroah-Hartman {
1274ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1275ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1276ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1277ab4382d2SGreg Kroah-Hartman 	int count;
1278ab4382d2SGreg Kroah-Hartman 
1279ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
12804e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1281ab4382d2SGreg Kroah-Hartman 		return;
1282ab4382d2SGreg Kroah-Hartman 
1283ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1284ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1285ab4382d2SGreg Kroah-Hartman 
1286ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1287ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1288ab4382d2SGreg Kroah-Hartman 
1289ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1290ab4382d2SGreg Kroah-Hartman 
1291ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
12924e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1293ab4382d2SGreg Kroah-Hartman 
1294ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1295ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1296ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1297ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1298ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1299ab4382d2SGreg Kroah-Hartman 
1300ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1301ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1302ab4382d2SGreg Kroah-Hartman 
13034e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13044e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1306ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13074e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1308ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13094e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13104e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1311ab4382d2SGreg Kroah-Hartman 	} else {
131213bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
131313bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1314ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1315ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1316ab4382d2SGreg Kroah-Hartman 		}
1317ab4382d2SGreg Kroah-Hartman 	}
1318ab4382d2SGreg Kroah-Hartman 
1319ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1320ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1321ab4382d2SGreg Kroah-Hartman }
1322ab4382d2SGreg Kroah-Hartman 
1323a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1324a930e528SElen Song {
1325a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1326a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1327a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1328a930e528SElen Song 
1329a930e528SElen Song 	pdc->buf = xmit->buf;
1330a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1331a930e528SElen Song 					pdc->buf,
1332a930e528SElen Song 					UART_XMIT_SIZE,
1333a930e528SElen Song 					DMA_TO_DEVICE);
1334a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1335a930e528SElen Song 	pdc->ofs = 0;
1336a930e528SElen Song 
1337a930e528SElen Song 	return 0;
1338a930e528SElen Song }
1339a930e528SElen Song 
1340ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1341ab4382d2SGreg Kroah-Hartman {
1342ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1343ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1344ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1345ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1346ab4382d2SGreg Kroah-Hartman 
1347ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1348ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1349ab4382d2SGreg Kroah-Hartman 
1350ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1351ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1352ab4382d2SGreg Kroah-Hartman 
1353ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1354ab4382d2SGreg Kroah-Hartman 
1355ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1356ab4382d2SGreg Kroah-Hartman 
1357ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1358ab4382d2SGreg Kroah-Hartman 		status = c.status;
1359ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1360ab4382d2SGreg Kroah-Hartman 
1361ab4382d2SGreg Kroah-Hartman 		/*
1362ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1363ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1364ab4382d2SGreg Kroah-Hartman 		 */
1365ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1366ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1367ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1368ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1369ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1370ab4382d2SGreg Kroah-Hartman 
1371ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1372ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1373ab4382d2SGreg Kroah-Hartman 					continue;
1374ab4382d2SGreg Kroah-Hartman 			}
1375ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1376ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1377ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1378ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1379ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1380ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1381ab4382d2SGreg Kroah-Hartman 
1382ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1383ab4382d2SGreg Kroah-Hartman 
1384ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1385ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1386ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1387ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1388ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1389ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1390ab4382d2SGreg Kroah-Hartman 		}
1391ab4382d2SGreg Kroah-Hartman 
1392ab4382d2SGreg Kroah-Hartman 
1393ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1394ab4382d2SGreg Kroah-Hartman 			continue;
1395ab4382d2SGreg Kroah-Hartman 
1396ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1397ab4382d2SGreg Kroah-Hartman 	}
1398ab4382d2SGreg Kroah-Hartman 
1399ab4382d2SGreg Kroah-Hartman 	/*
1400ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1401ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1402ab4382d2SGreg Kroah-Hartman 	 */
1403ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14042e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1405ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1406ab4382d2SGreg Kroah-Hartman }
1407ab4382d2SGreg Kroah-Hartman 
1408a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1409a930e528SElen Song {
1410a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1411a930e528SElen Song 	int i;
1412a930e528SElen Song 
1413a930e528SElen Song 	for (i = 0; i < 2; i++) {
1414a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1415a930e528SElen Song 
1416a930e528SElen Song 		dma_unmap_single(port->dev,
1417a930e528SElen Song 				 pdc->dma_addr,
1418a930e528SElen Song 				 pdc->dma_size,
1419a930e528SElen Song 				 DMA_FROM_DEVICE);
1420a930e528SElen Song 		kfree(pdc->buf);
1421a930e528SElen Song 	}
1422a930e528SElen Song }
1423a930e528SElen Song 
142464e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1425ab4382d2SGreg Kroah-Hartman {
1426ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
142705c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1428ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1429ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1430ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1431ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1432ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1433ab4382d2SGreg Kroah-Hartman 
1434ab4382d2SGreg Kroah-Hartman 	do {
1435ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
14364e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1437ab4382d2SGreg Kroah-Hartman 
1438ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
14394e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1440ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1441ab4382d2SGreg Kroah-Hartman 
1442ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1443ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1444ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1445ab4382d2SGreg Kroah-Hartman 		 * find out.
1446ab4382d2SGreg Kroah-Hartman 		 *
1447ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1448ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1449ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1450ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1451ab4382d2SGreg Kroah-Hartman 		 */
1452ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1453ab4382d2SGreg Kroah-Hartman 
1454ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1455ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1456ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1457ab4382d2SGreg Kroah-Hartman 
1458ab4382d2SGreg Kroah-Hartman 			/*
1459ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1460ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1461ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1462ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1463ab4382d2SGreg Kroah-Hartman 			 */
1464ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1465ab4382d2SGreg Kroah-Hartman 
146605c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
146705c7cd39SJiri Slaby 						count);
1468ab4382d2SGreg Kroah-Hartman 
1469ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1470ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1471ab4382d2SGreg Kroah-Hartman 
1472ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1473ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1474ab4382d2SGreg Kroah-Hartman 		}
1475ab4382d2SGreg Kroah-Hartman 
1476ab4382d2SGreg Kroah-Hartman 		/*
1477ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1478ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1479ab4382d2SGreg Kroah-Hartman 		 */
1480ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1481ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
14824e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
14834e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1484ab4382d2SGreg Kroah-Hartman 
1485ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1486ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1487ab4382d2SGreg Kroah-Hartman 		}
1488ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1489ab4382d2SGreg Kroah-Hartman 
1490ab4382d2SGreg Kroah-Hartman 	/*
1491ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1492ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1493ab4382d2SGreg Kroah-Hartman 	 */
1494ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14952e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1496ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1497ab4382d2SGreg Kroah-Hartman 
14984e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
14994e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1500ab4382d2SGreg Kroah-Hartman }
1501ab4382d2SGreg Kroah-Hartman 
1502a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1503a930e528SElen Song {
1504a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1505a930e528SElen Song 	int i;
1506a930e528SElen Song 
1507a930e528SElen Song 	for (i = 0; i < 2; i++) {
1508a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1509a930e528SElen Song 
1510a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1511a930e528SElen Song 		if (pdc->buf == NULL) {
1512a930e528SElen Song 			if (i != 0) {
1513a930e528SElen Song 				dma_unmap_single(port->dev,
1514a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1515a930e528SElen Song 					PDC_BUFFER_SIZE,
1516a930e528SElen Song 					DMA_FROM_DEVICE);
1517a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1518a930e528SElen Song 			}
1519a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1520a930e528SElen Song 			return -ENOMEM;
1521a930e528SElen Song 		}
1522a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1523a930e528SElen Song 						pdc->buf,
1524a930e528SElen Song 						PDC_BUFFER_SIZE,
1525a930e528SElen Song 						DMA_FROM_DEVICE);
1526a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1527a930e528SElen Song 		pdc->ofs = 0;
1528a930e528SElen Song 	}
1529a930e528SElen Song 
1530a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1531a930e528SElen Song 
15324e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
15334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1534a930e528SElen Song 
15354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
15364e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
15374e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1538a930e528SElen Song 
1539a930e528SElen Song 	return 0;
1540a930e528SElen Song }
1541a930e528SElen Song 
1542ab4382d2SGreg Kroah-Hartman /*
1543ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1544ab4382d2SGreg Kroah-Hartman  */
1545ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1546ab4382d2SGreg Kroah-Hartman {
1547ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1548ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1549d033e82dSLeilei Zhao 	unsigned int status = atmel_port->irq_status;
1550d033e82dSLeilei Zhao 	unsigned int status_change = atmel_port->status_change;
1551ab4382d2SGreg Kroah-Hartman 
1552ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1553ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1554ab4382d2SGreg Kroah-Hartman 
1555a930e528SElen Song 	atmel_port->schedule_tx(port);
1556ab4382d2SGreg Kroah-Hartman 
1557ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1558ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1559ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1560ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1561ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1562ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1563ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1564ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1565ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1566ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1567ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1568ab4382d2SGreg Kroah-Hartman 
1569ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1570ab4382d2SGreg Kroah-Hartman 
1571d033e82dSLeilei Zhao 		atmel_port->status_change = 0;
1572ab4382d2SGreg Kroah-Hartman 	}
1573ab4382d2SGreg Kroah-Hartman 
1574a930e528SElen Song 	atmel_port->schedule_rx(port);
1575ab4382d2SGreg Kroah-Hartman 
1576ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1577ab4382d2SGreg Kroah-Hartman }
1578ab4382d2SGreg Kroah-Hartman 
15794a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
158033d64c4fSElen Song 				struct platform_device *pdev)
158133d64c4fSElen Song {
158233d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1583574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
158433d64c4fSElen Song 
158533d64c4fSElen Song 	if (np) {
158633d64c4fSElen Song 		/* DMA/PDC usage specification */
158733d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
158833d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
158933d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
159033d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
159133d64c4fSElen Song 			} else {
159233d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
159333d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
159433d64c4fSElen Song 			}
159533d64c4fSElen Song 		} else {
159633d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
159733d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
159833d64c4fSElen Song 		}
159933d64c4fSElen Song 
160033d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
160133d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
160233d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
160333d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
160433d64c4fSElen Song 			} else {
160533d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
160633d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
160733d64c4fSElen Song 			}
160833d64c4fSElen Song 		} else {
160933d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
161033d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
161133d64c4fSElen Song 		}
161233d64c4fSElen Song 
161333d64c4fSElen Song 	} else {
161433d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
161533d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
161633d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
161733d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
161833d64c4fSElen Song 	}
161933d64c4fSElen Song 
162033d64c4fSElen Song }
162133d64c4fSElen Song 
162213bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
162333d64c4fSElen Song 				struct platform_device *pdev)
162433d64c4fSElen Song {
162533d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1626574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
162733d64c4fSElen Song 
162833d64c4fSElen Song 	if (np) {
162933d64c4fSElen Song 		u32 rs485_delay[2];
163033d64c4fSElen Song 		/* rs485 properties */
163133d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
163233d64c4fSElen Song 					rs485_delay, 2) == 0) {
163313bd3e6fSRicardo Ribalda Delgado 			struct serial_rs485 *rs485conf = &port->rs485;
163433d64c4fSElen Song 
163533d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
163633d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
163733d64c4fSElen Song 			rs485conf->flags = 0;
163833d64c4fSElen Song 
163933d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
164033d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
164133d64c4fSElen Song 
164233d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
164333d64c4fSElen Song 								NULL))
164433d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
164533d64c4fSElen Song 		}
164633d64c4fSElen Song 	} else {
164713bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
164833d64c4fSElen Song 	}
164933d64c4fSElen Song 
165033d64c4fSElen Song }
165133d64c4fSElen Song 
1652a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1653a930e528SElen Song {
1654a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1655a930e528SElen Song 
165634df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
165734df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
165834df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
165934df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
166034df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1661a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1662a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1663a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1664a930e528SElen Song 	} else {
1665a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1666a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1667a930e528SElen Song 		atmel_port->release_rx = NULL;
1668a930e528SElen Song 	}
1669a930e528SElen Song 
167008f738beSElen Song 	if (atmel_use_dma_tx(port)) {
167108f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
167208f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
167308f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
167408f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1675a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1676a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1677a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1678a930e528SElen Song 	} else {
1679a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1680a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1681a930e528SElen Song 		atmel_port->release_tx = NULL;
1682a930e528SElen Song 	}
1683a930e528SElen Song }
1684a930e528SElen Song 
1685ab4382d2SGreg Kroah-Hartman /*
1686055560b0SElen Song  * Get ip name usart or uart
1687055560b0SElen Song  */
1688892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1689055560b0SElen Song {
1690055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
16914e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1692731d9caeSNicolas Ferre 	u32 version;
1693055560b0SElen Song 	int usart, uart;
1694055560b0SElen Song 	/* usart and uart ascii */
1695055560b0SElen Song 	usart = 0x55534152;
1696055560b0SElen Song 	uart = 0x44424755;
1697055560b0SElen Song 
1698055560b0SElen Song 	atmel_port->is_usart = false;
1699055560b0SElen Song 
1700055560b0SElen Song 	if (name == usart) {
1701055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1702055560b0SElen Song 		atmel_port->is_usart = true;
1703055560b0SElen Song 	} else if (name == uart) {
1704055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1705055560b0SElen Song 		atmel_port->is_usart = false;
1706055560b0SElen Song 	} else {
1707731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17084e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1709731d9caeSNicolas Ferre 		switch (version) {
1710731d9caeSNicolas Ferre 		case 0x302:
1711731d9caeSNicolas Ferre 		case 0x10213:
1712731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1713731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1714731d9caeSNicolas Ferre 			break;
1715731d9caeSNicolas Ferre 		case 0x203:
1716731d9caeSNicolas Ferre 		case 0x10202:
1717731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1718731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1719731d9caeSNicolas Ferre 			break;
1720731d9caeSNicolas Ferre 		default:
1721731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1722731d9caeSNicolas Ferre 		}
1723055560b0SElen Song 	}
1724055560b0SElen Song }
1725055560b0SElen Song 
1726ab5e4e41SRichard Genoud static void atmel_free_gpio_irq(struct uart_port *port)
1727ab5e4e41SRichard Genoud {
1728ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1729ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1730ab5e4e41SRichard Genoud 
1731ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++)
1732ab5e4e41SRichard Genoud 		if (atmel_port->gpio_irq[i] >= 0)
1733ab5e4e41SRichard Genoud 			free_irq(atmel_port->gpio_irq[i], port);
1734ab5e4e41SRichard Genoud }
1735ab5e4e41SRichard Genoud 
1736ab5e4e41SRichard Genoud static int atmel_request_gpio_irq(struct uart_port *port)
1737ab5e4e41SRichard Genoud {
1738ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1739ab5e4e41SRichard Genoud 	int *irq = atmel_port->gpio_irq;
1740ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
1741ab5e4e41SRichard Genoud 	int err = 0;
1742ab5e4e41SRichard Genoud 
1743ab5e4e41SRichard Genoud 	for (i = 0; (i < UART_GPIO_MAX) && !err; i++) {
1744ab5e4e41SRichard Genoud 		if (irq[i] < 0)
1745ab5e4e41SRichard Genoud 			continue;
1746ab5e4e41SRichard Genoud 
1747ab5e4e41SRichard Genoud 		irq_set_status_flags(irq[i], IRQ_NOAUTOEN);
1748ab5e4e41SRichard Genoud 		err = request_irq(irq[i], atmel_interrupt, IRQ_TYPE_EDGE_BOTH,
1749ab5e4e41SRichard Genoud 				  "atmel_serial", port);
1750ab5e4e41SRichard Genoud 		if (err)
1751ab5e4e41SRichard Genoud 			dev_err(port->dev, "atmel_startup - Can't get %d irq\n",
1752ab5e4e41SRichard Genoud 				irq[i]);
1753ab5e4e41SRichard Genoud 	}
1754ab5e4e41SRichard Genoud 
1755ab5e4e41SRichard Genoud 	/*
1756ab5e4e41SRichard Genoud 	 * If something went wrong, rollback.
1757ab5e4e41SRichard Genoud 	 */
1758ab5e4e41SRichard Genoud 	while (err && (--i >= 0))
1759ab5e4e41SRichard Genoud 		if (irq[i] >= 0)
1760ab5e4e41SRichard Genoud 			free_irq(irq[i], port);
1761ab5e4e41SRichard Genoud 
1762ab5e4e41SRichard Genoud 	return err;
1763ab5e4e41SRichard Genoud }
1764ab5e4e41SRichard Genoud 
1765055560b0SElen Song /*
1766ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1767ab4382d2SGreg Kroah-Hartman  */
1768ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1769ab4382d2SGreg Kroah-Hartman {
177033d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1771ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1772ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1773ab4382d2SGreg Kroah-Hartman 	int retval;
1774ab4382d2SGreg Kroah-Hartman 
1775ab4382d2SGreg Kroah-Hartman 	/*
1776ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1777ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1778ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1779ab4382d2SGreg Kroah-Hartman 	 */
17804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1781ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1782ab4382d2SGreg Kroah-Hartman 
1783ab4382d2SGreg Kroah-Hartman 	/*
1784ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1785ab4382d2SGreg Kroah-Hartman 	 */
17862c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
17872c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1788ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1789ab4382d2SGreg Kroah-Hartman 	if (retval) {
1790ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1791ab4382d2SGreg Kroah-Hartman 		return retval;
1792ab4382d2SGreg Kroah-Hartman 	}
1793ab4382d2SGreg Kroah-Hartman 
1794ab4382d2SGreg Kroah-Hartman 	/*
1795ab5e4e41SRichard Genoud 	 * Get the GPIO lines IRQ
1796ab5e4e41SRichard Genoud 	 */
1797ab5e4e41SRichard Genoud 	retval = atmel_request_gpio_irq(port);
1798ab5e4e41SRichard Genoud 	if (retval)
1799ab5e4e41SRichard Genoud 		goto free_irq;
1800ab5e4e41SRichard Genoud 
18011e125786SLeilei Zhao 	tasklet_enable(&atmel_port->tasklet);
18021e125786SLeilei Zhao 
1803ab5e4e41SRichard Genoud 	/*
1804ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1805ab4382d2SGreg Kroah-Hartman 	 */
180633d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
18074d9628a1SLeilei Zhao 	atmel_set_ops(port);
180833d64c4fSElen Song 
1809a930e528SElen Song 	if (atmel_port->prepare_rx) {
1810a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1811a930e528SElen Song 		if (retval < 0)
1812a930e528SElen Song 			atmel_set_ops(port);
1813ab4382d2SGreg Kroah-Hartman 	}
1814ab4382d2SGreg Kroah-Hartman 
1815a930e528SElen Song 	if (atmel_port->prepare_tx) {
1816a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1817a930e528SElen Song 		if (retval < 0)
1818a930e528SElen Song 			atmel_set_ops(port);
1819ab4382d2SGreg Kroah-Hartman 	}
1820ab4382d2SGreg Kroah-Hartman 
1821*b5199d46SCyrille Pitchen 	/*
1822*b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1823*b5199d46SCyrille Pitchen 	 */
1824*b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1825*b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1826*b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1827*b5199d46SCyrille Pitchen 		unsigned int fmr;
1828*b5199d46SCyrille Pitchen 
1829*b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1830*b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1831*b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1832*b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1833*b5199d46SCyrille Pitchen 
1834*b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1835*b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1836*b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1837*b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1838*b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1839*b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1840*b5199d46SCyrille Pitchen 
1841*b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1842*b5199d46SCyrille Pitchen 	}
1843*b5199d46SCyrille Pitchen 
1844ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1845e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1846ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1847ab4382d2SGreg Kroah-Hartman 
1848ab4382d2SGreg Kroah-Hartman 	/*
1849ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1850ab4382d2SGreg Kroah-Hartman 	 */
18514e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1852ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
18534e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1854ab4382d2SGreg Kroah-Hartman 
18552e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
18562e68c22fSElen Song 			atmel_uart_timer_callback,
18572e68c22fSElen Song 			(unsigned long)port);
18588bc661bfSMarek Roszko 
18598bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18608bc661bfSMarek Roszko 		/* set UART timeout */
18618bc661bfSMarek Roszko 		if (!atmel_port->is_usart) {
18622e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18632e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18642e68c22fSElen Song 		/* set USART timeout */
18652e68c22fSElen Song 		} else {
18664e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
18674e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1868ab4382d2SGreg Kroah-Hartman 
18694e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18704e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18712e68c22fSElen Song 		}
1872ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
18734e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
187434df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18752e68c22fSElen Song 		/* set UART timeout */
18762e68c22fSElen Song 		if (!atmel_port->is_usart) {
18772e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18782e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18792e68c22fSElen Song 		/* set USART timeout */
18802e68c22fSElen Song 		} else {
18814e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_RTOR, PDC_RX_TIMEOUT);
18824e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
188334df42f5SElen Song 
18844e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18854e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
18862e68c22fSElen Song 		}
1887ab4382d2SGreg Kroah-Hartman 	} else {
1888ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
18894e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1890ab4382d2SGreg Kroah-Hartman 	}
1891ab4382d2SGreg Kroah-Hartman 
1892ab4382d2SGreg Kroah-Hartman 	return 0;
1893ab5e4e41SRichard Genoud 
1894ab5e4e41SRichard Genoud free_irq:
1895ab5e4e41SRichard Genoud 	free_irq(port->irq, port);
1896ab5e4e41SRichard Genoud 
1897ab5e4e41SRichard Genoud 	return retval;
1898ab4382d2SGreg Kroah-Hartman }
1899ab4382d2SGreg Kroah-Hartman 
1900ab4382d2SGreg Kroah-Hartman /*
1901479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1902479e9b94SPeter Hurley  * buffer is reset.
1903479e9b94SPeter Hurley  */
1904479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1905479e9b94SPeter Hurley {
1906479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1907479e9b94SPeter Hurley 
1908479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
19094e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1910479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1911479e9b94SPeter Hurley 	}
1912479e9b94SPeter Hurley }
1913479e9b94SPeter Hurley 
1914479e9b94SPeter Hurley /*
1915ab4382d2SGreg Kroah-Hartman  * Disable the port
1916ab4382d2SGreg Kroah-Hartman  */
1917ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1918ab4382d2SGreg Kroah-Hartman {
1919ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19200cc7c6c7SMarek Roszko 
1921ab4382d2SGreg Kroah-Hartman 	/*
19228bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19238bc661bfSMarek Roszko 	 * cleanup
19248bc661bfSMarek Roszko 	 */
19258bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19268bc661bfSMarek Roszko 
19278bc661bfSMarek Roszko 	/*
19280cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19290cc7c6c7SMarek Roszko 	 * we destroy the buffers
19300cc7c6c7SMarek Roszko 	 */
19311e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
19320cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
19330cc7c6c7SMarek Roszko 
19340cc7c6c7SMarek Roszko 	/*
19350cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
19360cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1937ab4382d2SGreg Kroah-Hartman 	 */
1938ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1939ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1940ab4382d2SGreg Kroah-Hartman 
19414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
19424e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
19430cc7c6c7SMarek Roszko 
19440cc7c6c7SMarek Roszko 
1945ab4382d2SGreg Kroah-Hartman 	/*
1946ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1947ab4382d2SGreg Kroah-Hartman 	 */
1948a930e528SElen Song 	if (atmel_port->release_rx)
1949a930e528SElen Song 		atmel_port->release_rx(port);
1950a930e528SElen Song 	if (atmel_port->release_tx)
1951a930e528SElen Song 		atmel_port->release_tx(port);
1952ab4382d2SGreg Kroah-Hartman 
1953ab4382d2SGreg Kroah-Hartman 	/*
1954bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1955bb7e73c5SMark Deneen 	 */
1956bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1957bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1958bb7e73c5SMark Deneen 
1959bb7e73c5SMark Deneen 	/*
1960ab5e4e41SRichard Genoud 	 * Free the interrupts
1961ab4382d2SGreg Kroah-Hartman 	 */
1962ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1963ab5e4e41SRichard Genoud 	atmel_free_gpio_irq(port);
1964ab5e4e41SRichard Genoud 
1965ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1966ab4382d2SGreg Kroah-Hartman 
1967479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1968ab4382d2SGreg Kroah-Hartman }
1969ab4382d2SGreg Kroah-Hartman 
1970ab4382d2SGreg Kroah-Hartman /*
1971ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1972ab4382d2SGreg Kroah-Hartman  */
1973ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1974ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1975ab4382d2SGreg Kroah-Hartman {
1976ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1977ab4382d2SGreg Kroah-Hartman 
1978ab4382d2SGreg Kroah-Hartman 	switch (state) {
1979ab4382d2SGreg Kroah-Hartman 	case 0:
1980ab4382d2SGreg Kroah-Hartman 		/*
1981ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1982ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1983ab4382d2SGreg Kroah-Hartman 		 */
198491f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1985ab4382d2SGreg Kroah-Hartman 
1986ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
19874e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
1988ab4382d2SGreg Kroah-Hartman 		break;
1989ab4382d2SGreg Kroah-Hartman 	case 3:
1990ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
19914e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
19924e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
1993ab4382d2SGreg Kroah-Hartman 
1994ab4382d2SGreg Kroah-Hartman 		/*
1995ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1996ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1997ab4382d2SGreg Kroah-Hartman 		 */
199891f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1999ab4382d2SGreg Kroah-Hartman 		break;
2000ab4382d2SGreg Kroah-Hartman 	default:
2001ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2002ab4382d2SGreg Kroah-Hartman 	}
2003ab4382d2SGreg Kroah-Hartman }
2004ab4382d2SGreg Kroah-Hartman 
2005ab4382d2SGreg Kroah-Hartman /*
2006ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2007ab4382d2SGreg Kroah-Hartman  */
2008ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2009ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2010ab4382d2SGreg Kroah-Hartman {
2011ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
20121cf6e8fcSCyrille Pitchen 	unsigned int old_mode, mode, imr, quot, baud;
2013ab4382d2SGreg Kroah-Hartman 
20141cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20154e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20161cf6e8fcSCyrille Pitchen 
20171cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20181cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20191cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2020ab4382d2SGreg Kroah-Hartman 
2021ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2022ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
2023ab4382d2SGreg Kroah-Hartman 
2024ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
2025ab4382d2SGreg Kroah-Hartman 		quot /= 8;
2026ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
2027ab4382d2SGreg Kroah-Hartman 	}
2028ab4382d2SGreg Kroah-Hartman 
2029ab4382d2SGreg Kroah-Hartman 	/* byte size */
2030ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2031ab4382d2SGreg Kroah-Hartman 	case CS5:
2032ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2033ab4382d2SGreg Kroah-Hartman 		break;
2034ab4382d2SGreg Kroah-Hartman 	case CS6:
2035ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2036ab4382d2SGreg Kroah-Hartman 		break;
2037ab4382d2SGreg Kroah-Hartman 	case CS7:
2038ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2039ab4382d2SGreg Kroah-Hartman 		break;
2040ab4382d2SGreg Kroah-Hartman 	default:
2041ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2042ab4382d2SGreg Kroah-Hartman 		break;
2043ab4382d2SGreg Kroah-Hartman 	}
2044ab4382d2SGreg Kroah-Hartman 
2045ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2046ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2047ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2048ab4382d2SGreg Kroah-Hartman 
2049ab4382d2SGreg Kroah-Hartman 	/* parity */
2050ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2051ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2052ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2053ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2054ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2055ab4382d2SGreg Kroah-Hartman 			else
2056ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2057ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2058ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2059ab4382d2SGreg Kroah-Hartman 		else
2060ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2061ab4382d2SGreg Kroah-Hartman 	} else
2062ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2063ab4382d2SGreg Kroah-Hartman 
2064ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2065ab4382d2SGreg Kroah-Hartman 
2066ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2067ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2068ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2069ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2070ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2071ab4382d2SGreg Kroah-Hartman 
207264e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2073ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
20744e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2075ab4382d2SGreg Kroah-Hartman 
2076ab4382d2SGreg Kroah-Hartman 	/*
2077ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2078ab4382d2SGreg Kroah-Hartman 	 */
2079ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2080ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2081ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2082ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2083ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2084ab4382d2SGreg Kroah-Hartman 		/*
2085ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2086ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2087ab4382d2SGreg Kroah-Hartman 		 */
2088ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2089ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2090ab4382d2SGreg Kroah-Hartman 	}
2091ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2092ab4382d2SGreg Kroah-Hartman 
2093ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2094ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2095ab4382d2SGreg Kroah-Hartman 
2096ab4382d2SGreg Kroah-Hartman 	/*
2097ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2098ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2099ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2100ab4382d2SGreg Kroah-Hartman 	 */
21014e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
21024e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2103ab4382d2SGreg Kroah-Hartman 
2104ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
21054e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2106ab4382d2SGreg Kroah-Hartman 
21071cf6e8fcSCyrille Pitchen 	/* mode */
210813bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
21094e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
21104e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2111ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
21121cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
21131cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
21141cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_HWHS;
21151cf6e8fcSCyrille Pitchen 	} else {
21161cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21171cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2118ab4382d2SGreg Kroah-Hartman 	}
2119ab4382d2SGreg Kroah-Hartman 
21201cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21214e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2122ab4382d2SGreg Kroah-Hartman 
21231cf6e8fcSCyrille Pitchen 	/*
21241cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21251cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21261cf6e8fcSCyrille Pitchen 	 */
21271cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21281cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21291cf6e8fcSCyrille Pitchen 
21301cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21311cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21321cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21331cf6e8fcSCyrille Pitchen 		} else {
21341cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21351cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21361cf6e8fcSCyrille Pitchen 		}
21371cf6e8fcSCyrille Pitchen 
21384e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21391cf6e8fcSCyrille Pitchen 	}
21401cf6e8fcSCyrille Pitchen 
2141ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
21424e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
21434e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
21444e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2145ab4382d2SGreg Kroah-Hartman 
2146ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
21474e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2148ab4382d2SGreg Kroah-Hartman 
2149ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2150ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
215135b675b9SRichard Genoud 		atmel_enable_ms(port);
215235b675b9SRichard Genoud 	else
215335b675b9SRichard Genoud 		atmel_disable_ms(port);
2154ab4382d2SGreg Kroah-Hartman 
2155ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2156ab4382d2SGreg Kroah-Hartman }
2157ab4382d2SGreg Kroah-Hartman 
2158732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
215942bd7a4fSViktar Palstsiuk {
2160732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
216142bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2162d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
216342bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2164d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
216542bd7a4fSViktar Palstsiuk 	} else {
216642bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2167cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2168cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2169cab68f89SPeter Hurley 			atmel_disable_ms(port);
2170cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2171cab68f89SPeter Hurley 		}
217242bd7a4fSViktar Palstsiuk 	}
217342bd7a4fSViktar Palstsiuk }
217442bd7a4fSViktar Palstsiuk 
2175ab4382d2SGreg Kroah-Hartman /*
2176ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2177ab4382d2SGreg Kroah-Hartman  */
2178ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2179ab4382d2SGreg Kroah-Hartman {
2180ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2181ab4382d2SGreg Kroah-Hartman }
2182ab4382d2SGreg Kroah-Hartman 
2183ab4382d2SGreg Kroah-Hartman /*
2184ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2185ab4382d2SGreg Kroah-Hartman  */
2186ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2187ab4382d2SGreg Kroah-Hartman {
2188ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2189ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2190ab4382d2SGreg Kroah-Hartman 
2191ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2192ab4382d2SGreg Kroah-Hartman 
2193ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2194ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2195ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2196ab4382d2SGreg Kroah-Hartman 	}
2197ab4382d2SGreg Kroah-Hartman }
2198ab4382d2SGreg Kroah-Hartman 
2199ab4382d2SGreg Kroah-Hartman /*
2200ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2201ab4382d2SGreg Kroah-Hartman  */
2202ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2203ab4382d2SGreg Kroah-Hartman {
2204ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2205ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2206ab4382d2SGreg Kroah-Hartman 
2207ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2208ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2209ab4382d2SGreg Kroah-Hartman 
2210ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2211ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2212ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2213ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2214ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2215ab4382d2SGreg Kroah-Hartman 		}
2216ab4382d2SGreg Kroah-Hartman 	}
2217ab4382d2SGreg Kroah-Hartman 
2218ab4382d2SGreg Kroah-Hartman 	return 0;
2219ab4382d2SGreg Kroah-Hartman }
2220ab4382d2SGreg Kroah-Hartman 
2221ab4382d2SGreg Kroah-Hartman /*
2222ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2223ab4382d2SGreg Kroah-Hartman  */
2224ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2225ab4382d2SGreg Kroah-Hartman {
2226ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2227ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2228ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2229ab4382d2SGreg Kroah-Hartman 	}
2230ab4382d2SGreg Kroah-Hartman }
2231ab4382d2SGreg Kroah-Hartman 
2232ab4382d2SGreg Kroah-Hartman /*
2233ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2234ab4382d2SGreg Kroah-Hartman  */
2235ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2236ab4382d2SGreg Kroah-Hartman {
2237ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2238ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2239ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2240ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2241ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2242ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2243ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2244ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2245ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2246ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
2247ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2248ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2249ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2250ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2251ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2252ab4382d2SGreg Kroah-Hartman 	return ret;
2253ab4382d2SGreg Kroah-Hartman }
2254ab4382d2SGreg Kroah-Hartman 
2255ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2256ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2257ab4382d2SGreg Kroah-Hartman {
22584e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2259ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2260ab4382d2SGreg Kroah-Hartman 
2261*b5199d46SCyrille Pitchen 	return atmel_uart_readb(port, ATMEL_US_RHR);
2262ab4382d2SGreg Kroah-Hartman }
2263ab4382d2SGreg Kroah-Hartman 
2264ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2265ab4382d2SGreg Kroah-Hartman {
22664e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2267ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2268ab4382d2SGreg Kroah-Hartman 
2269*b5199d46SCyrille Pitchen 	atmel_uart_writeb(port, ATMEL_US_THR, ch);
2270ab4382d2SGreg Kroah-Hartman }
2271ab4382d2SGreg Kroah-Hartman #endif
2272ab4382d2SGreg Kroah-Hartman 
2273ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2274ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2275ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2276ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2277ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2278ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2279ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2280ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2281ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2282ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2283ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2284ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2285ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
228642bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2287ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2288ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2289ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2290ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2291ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2292ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2293ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2294ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2295ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2296ab4382d2SGreg Kroah-Hartman #endif
2297ab4382d2SGreg Kroah-Hartman };
2298ab4382d2SGreg Kroah-Hartman 
2299ab4382d2SGreg Kroah-Hartman /*
2300ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2301ab4382d2SGreg Kroah-Hartman  */
230291f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2303ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2304ab4382d2SGreg Kroah-Hartman {
230591f8c2d8SBoris BREZILLON 	int ret;
2306ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2307574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2308ab4382d2SGreg Kroah-Hartman 
23094a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2310a930e528SElen Song 	atmel_set_ops(port);
2311a930e528SElen Song 
231213bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
231333d64c4fSElen Song 
2314ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2315ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2316ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2317ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2318ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2319ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2320ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
232113bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2322ab4382d2SGreg Kroah-Hartman 
2323ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2324ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
23251e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
2326ab4382d2SGreg Kroah-Hartman 
2327ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2328ab4382d2SGreg Kroah-Hartman 
23295fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2330ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
23311acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2332588edbf3SNicolas Ferre 	} else {
2333ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2334ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2335ab4382d2SGreg Kroah-Hartman 	}
2336ab4382d2SGreg Kroah-Hartman 
2337ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2338ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2339ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
234091f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
234191f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
234291f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
234391f8c2d8SBoris BREZILLON 			return ret;
234491f8c2d8SBoris BREZILLON 		}
234591f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
234691f8c2d8SBoris BREZILLON 		if (ret) {
234791f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
234891f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
234991f8c2d8SBoris BREZILLON 			return ret;
235091f8c2d8SBoris BREZILLON 		}
2351ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
235291f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2353ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2354ab4382d2SGreg Kroah-Hartman 	}
2355ab4382d2SGreg Kroah-Hartman 
2356ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
235713bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2358ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
235964e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2360ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2361ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2362ab4382d2SGreg Kroah-Hartman 	} else {
2363ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2364ab4382d2SGreg Kroah-Hartman 	}
236591f8c2d8SBoris BREZILLON 
236691f8c2d8SBoris BREZILLON 	return 0;
2367ab4382d2SGreg Kroah-Hartman }
2368ab4382d2SGreg Kroah-Hartman 
236969f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
237069f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2371ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2372ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2373ab4382d2SGreg Kroah-Hartman {
23744e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2375ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2376*b5199d46SCyrille Pitchen 	atmel_uart_writeb(port, ATMEL_US_THR, ch);
2377ab4382d2SGreg Kroah-Hartman }
2378ab4382d2SGreg Kroah-Hartman 
2379ab4382d2SGreg Kroah-Hartman /*
2380ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2381ab4382d2SGreg Kroah-Hartman  */
2382ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2383ab4382d2SGreg Kroah-Hartman {
2384ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2385ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2386ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2387ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2388ab4382d2SGreg Kroah-Hartman 
2389ab4382d2SGreg Kroah-Hartman 	/*
2390ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2391ab4382d2SGreg Kroah-Hartman 	 */
23924e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
23934e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
23944e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2395ab4382d2SGreg Kroah-Hartman 
2396ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
23974e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
23984e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2399ab4382d2SGreg Kroah-Hartman 
2400ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2401ab4382d2SGreg Kroah-Hartman 
2402ab4382d2SGreg Kroah-Hartman 	/*
2403ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2404ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2405ab4382d2SGreg Kroah-Hartman 	 */
2406ab4382d2SGreg Kroah-Hartman 	do {
24074e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2408ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2409ab4382d2SGreg Kroah-Hartman 
2410ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2411ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24124e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2413ab4382d2SGreg Kroah-Hartman 
2414ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24154e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2416ab4382d2SGreg Kroah-Hartman }
2417ab4382d2SGreg Kroah-Hartman 
2418ab4382d2SGreg Kroah-Hartman /*
2419ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2420ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2421ab4382d2SGreg Kroah-Hartman  */
2422ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2423ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2424ab4382d2SGreg Kroah-Hartman {
2425ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2426ab4382d2SGreg Kroah-Hartman 
2427ab4382d2SGreg Kroah-Hartman 	/*
2428ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2429ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2430ab4382d2SGreg Kroah-Hartman 	 */
24314e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2432ab4382d2SGreg Kroah-Hartman 	if (!quot)
2433ab4382d2SGreg Kroah-Hartman 		return;
2434ab4382d2SGreg Kroah-Hartman 
24354e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2436ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2437ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2438ab4382d2SGreg Kroah-Hartman 	else
2439ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2440ab4382d2SGreg Kroah-Hartman 
24414e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2442ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2443ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2444ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2445ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2446ab4382d2SGreg Kroah-Hartman 
2447ab4382d2SGreg Kroah-Hartman 	/*
2448ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2449ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2450ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2451ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2452ab4382d2SGreg Kroah-Hartman 	 */
2453ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2454ab4382d2SGreg Kroah-Hartman }
2455ab4382d2SGreg Kroah-Hartman 
2456ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2457ab4382d2SGreg Kroah-Hartman {
245891f8c2d8SBoris BREZILLON 	int ret;
2459ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2460ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2461ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2462ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2463ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2464ab4382d2SGreg Kroah-Hartman 
2465ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2466ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2467ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2468ab4382d2SGreg Kroah-Hartman 	}
2469ab4382d2SGreg Kroah-Hartman 
247091f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
247191f8c2d8SBoris BREZILLON 	if (ret)
247291f8c2d8SBoris BREZILLON 		return ret;
2473ab4382d2SGreg Kroah-Hartman 
24744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
24754e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
24764e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2477ab4382d2SGreg Kroah-Hartman 
2478ab4382d2SGreg Kroah-Hartman 	if (options)
2479ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2480ab4382d2SGreg Kroah-Hartman 	else
2481ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2482ab4382d2SGreg Kroah-Hartman 
2483ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2484ab4382d2SGreg Kroah-Hartman }
2485ab4382d2SGreg Kroah-Hartman 
2486ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2487ab4382d2SGreg Kroah-Hartman 
2488ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2489ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2490ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2491ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2492ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2493ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2494ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2495ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2496ab4382d2SGreg Kroah-Hartman };
2497ab4382d2SGreg Kroah-Hartman 
2498ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2499ab4382d2SGreg Kroah-Hartman 
2500ab4382d2SGreg Kroah-Hartman /*
2501ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2502ab4382d2SGreg Kroah-Hartman  */
2503ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2504ab4382d2SGreg Kroah-Hartman {
250591f8c2d8SBoris BREZILLON 	int ret;
2506ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
25070d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2508574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2509efb8d21bSLinus Torvalds 		int id = pdata->num;
25104cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
25110d0a3cc1SVoss, Nikolaus 
25124cbf9f48SNicolas Ferre 		port->backup_imr = 0;
25134cbf9f48SNicolas Ferre 		port->uart.line = id;
25144cbf9f48SNicolas Ferre 
25154cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
251691f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
251791f8c2d8SBoris BREZILLON 		if (ret)
251891f8c2d8SBoris BREZILLON 			return ret;
2519ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2520ab4382d2SGreg Kroah-Hartman 	}
2521ab4382d2SGreg Kroah-Hartman 
2522ab4382d2SGreg Kroah-Hartman 	return 0;
2523ab4382d2SGreg Kroah-Hartman }
2524ab4382d2SGreg Kroah-Hartman 
2525ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2526ab4382d2SGreg Kroah-Hartman 
2527ab4382d2SGreg Kroah-Hartman /*
2528ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2529ab4382d2SGreg Kroah-Hartman  */
2530ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2531ab4382d2SGreg Kroah-Hartman {
2532ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2533ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2534ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2535ab4382d2SGreg Kroah-Hartman 
2536ab4382d2SGreg Kroah-Hartman 	return 0;
2537ab4382d2SGreg Kroah-Hartman }
2538ab4382d2SGreg Kroah-Hartman 
2539ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2540ab4382d2SGreg Kroah-Hartman 
2541ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2542ab4382d2SGreg Kroah-Hartman {
2543ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2544ab4382d2SGreg Kroah-Hartman }
2545ab4382d2SGreg Kroah-Hartman 
2546ab4382d2SGreg Kroah-Hartman #else
2547ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2548ab4382d2SGreg Kroah-Hartman 
2549ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2550ab4382d2SGreg Kroah-Hartman {
2551ab4382d2SGreg Kroah-Hartman 	return false;
2552ab4382d2SGreg Kroah-Hartman }
2553ab4382d2SGreg Kroah-Hartman #endif
2554ab4382d2SGreg Kroah-Hartman 
2555ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2556ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2557ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2558ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2559ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2560ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2561ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2562ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2563ab4382d2SGreg Kroah-Hartman };
2564ab4382d2SGreg Kroah-Hartman 
2565ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2566ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2567ab4382d2SGreg Kroah-Hartman {
2568ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2569ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2570ab4382d2SGreg Kroah-Hartman #else
2571ab4382d2SGreg Kroah-Hartman 	return false;
2572ab4382d2SGreg Kroah-Hartman #endif
2573ab4382d2SGreg Kroah-Hartman }
2574ab4382d2SGreg Kroah-Hartman 
2575ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2576ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2577ab4382d2SGreg Kroah-Hartman {
2578ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2579ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2580ab4382d2SGreg Kroah-Hartman 
2581ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2582ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
25834e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
25844e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2585ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2586ab4382d2SGreg Kroah-Hartman 	}
2587ab4382d2SGreg Kroah-Hartman 
2588ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2589ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
25902c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
25912c7af5baSBoris BREZILLON 		unsigned long flags;
25922c7af5baSBoris BREZILLON 
25932c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25942c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
25952c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2596ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
25972c7af5baSBoris BREZILLON 	}
2598ab4382d2SGreg Kroah-Hartman 
2599ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2600ab4382d2SGreg Kroah-Hartman 
2601ab4382d2SGreg Kroah-Hartman 	return 0;
2602ab4382d2SGreg Kroah-Hartman }
2603ab4382d2SGreg Kroah-Hartman 
2604ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2605ab4382d2SGreg Kroah-Hartman {
2606ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2607ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26082c7af5baSBoris BREZILLON 	unsigned long flags;
26092c7af5baSBoris BREZILLON 
26102c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26112c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26122c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26132c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26142c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26152c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26162c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26172c7af5baSBoris BREZILLON 	}
26182c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26192c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2620ab4382d2SGreg Kroah-Hartman 
2621ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2622ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2623ab4382d2SGreg Kroah-Hartman 
2624ab4382d2SGreg Kroah-Hartman 	return 0;
2625ab4382d2SGreg Kroah-Hartman }
2626ab4382d2SGreg Kroah-Hartman #else
2627ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2628ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2629ab4382d2SGreg Kroah-Hartman #endif
2630ab4382d2SGreg Kroah-Hartman 
2631e0b0baadSRichard Genoud static int atmel_init_gpios(struct atmel_uart_port *p, struct device *dev)
2632e0b0baadSRichard Genoud {
2633ab5e4e41SRichard Genoud 	enum mctrl_gpio_idx i;
2634ab5e4e41SRichard Genoud 	struct gpio_desc *gpiod;
2635ab5e4e41SRichard Genoud 
2636e0b0baadSRichard Genoud 	p->gpios = mctrl_gpio_init(dev, 0);
2637722ccf41SUwe Kleine-König 	if (IS_ERR(p->gpios))
2638722ccf41SUwe Kleine-König 		return PTR_ERR(p->gpios);
2639e0b0baadSRichard Genoud 
2640ab5e4e41SRichard Genoud 	for (i = 0; i < UART_GPIO_MAX; i++) {
2641ab5e4e41SRichard Genoud 		gpiod = mctrl_gpio_to_gpiod(p->gpios, i);
2642ab5e4e41SRichard Genoud 		if (gpiod && (gpiod_get_direction(gpiod) == GPIOF_DIR_IN))
2643ab5e4e41SRichard Genoud 			p->gpio_irq[i] = gpiod_to_irq(gpiod);
2644ab5e4e41SRichard Genoud 		else
2645ab5e4e41SRichard Genoud 			p->gpio_irq[i] = -EINVAL;
2646ab5e4e41SRichard Genoud 	}
2647ab5e4e41SRichard Genoud 
2648e0b0baadSRichard Genoud 	return 0;
2649e0b0baadSRichard Genoud }
2650e0b0baadSRichard Genoud 
2651*b5199d46SCyrille Pitchen static void atmel_serial_probe_fifos(struct atmel_uart_port *port,
2652*b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2653*b5199d46SCyrille Pitchen {
2654*b5199d46SCyrille Pitchen 	port->fifo_size = 0;
2655*b5199d46SCyrille Pitchen 	port->rts_low = 0;
2656*b5199d46SCyrille Pitchen 	port->rts_high = 0;
2657*b5199d46SCyrille Pitchen 
2658*b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2659*b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2660*b5199d46SCyrille Pitchen 				 &port->fifo_size))
2661*b5199d46SCyrille Pitchen 		return;
2662*b5199d46SCyrille Pitchen 
2663*b5199d46SCyrille Pitchen 	if (!port->fifo_size)
2664*b5199d46SCyrille Pitchen 		return;
2665*b5199d46SCyrille Pitchen 
2666*b5199d46SCyrille Pitchen 	if (port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2667*b5199d46SCyrille Pitchen 		port->fifo_size = 0;
2668*b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2669*b5199d46SCyrille Pitchen 		return;
2670*b5199d46SCyrille Pitchen 	}
2671*b5199d46SCyrille Pitchen 
2672*b5199d46SCyrille Pitchen 	/*
2673*b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2674*b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2675*b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2676*b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2677*b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2678*b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2679*b5199d46SCyrille Pitchen 	 */
2680*b5199d46SCyrille Pitchen 	port->rts_high = max_t(int, port->fifo_size >> 1,
2681*b5199d46SCyrille Pitchen 			       port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2682*b5199d46SCyrille Pitchen 	port->rts_low  = max_t(int, port->fifo_size >> 2,
2683*b5199d46SCyrille Pitchen 			       port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2684*b5199d46SCyrille Pitchen 
2685*b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2686*b5199d46SCyrille Pitchen 		 port->fifo_size);
2687*b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2688*b5199d46SCyrille Pitchen 		port->rts_high);
2689*b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2690*b5199d46SCyrille Pitchen 		port->rts_low);
2691*b5199d46SCyrille Pitchen }
2692*b5199d46SCyrille Pitchen 
26939671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2694ab4382d2SGreg Kroah-Hartman {
2695ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
26965fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2697574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2698ab4382d2SGreg Kroah-Hartman 	void *data;
26994cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2700bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2701ab4382d2SGreg Kroah-Hartman 
2702ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2703ab4382d2SGreg Kroah-Hartman 
27045fbe46b6SNicolas Ferre 	if (np)
27055fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
27065fbe46b6SNicolas Ferre 	else
27074cbf9f48SNicolas Ferre 		if (pdata)
27084cbf9f48SNicolas Ferre 			ret = pdata->num;
27094cbf9f48SNicolas Ferre 
27104cbf9f48SNicolas Ferre 	if (ret < 0)
27115fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
27124cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2713503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
27144cbf9f48SNicolas Ferre 
2715503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
27164cbf9f48SNicolas Ferre 		ret = -ENODEV;
27174cbf9f48SNicolas Ferre 		goto err;
27184cbf9f48SNicolas Ferre 	}
27194cbf9f48SNicolas Ferre 
2720503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
27214cbf9f48SNicolas Ferre 		/* port already in use */
27224cbf9f48SNicolas Ferre 		ret = -EBUSY;
27234cbf9f48SNicolas Ferre 		goto err;
27244cbf9f48SNicolas Ferre 	}
27254cbf9f48SNicolas Ferre 
27264cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2727ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
27284cbf9f48SNicolas Ferre 	port->uart.line = ret;
2729*b5199d46SCyrille Pitchen 	atmel_serial_probe_fifos(port, pdev);
2730354e57f3SLinus Walleij 
27312c7af5baSBoris BREZILLON 	spin_lock_init(&port->lock_suspended);
27322c7af5baSBoris BREZILLON 
2733e0b0baadSRichard Genoud 	ret = atmel_init_gpios(port, &pdev->dev);
2734722ccf41SUwe Kleine-König 	if (ret < 0) {
2735722ccf41SUwe Kleine-König 		dev_err(&pdev->dev, "Failed to initialize GPIOs.");
2736722ccf41SUwe Kleine-König 		goto err;
2737722ccf41SUwe Kleine-König 	}
2738ab4382d2SGreg Kroah-Hartman 
273991f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
274091f8c2d8SBoris BREZILLON 	if (ret)
27416fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2742ab4382d2SGreg Kroah-Hartman 
274364e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2744ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2745ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2746ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2747ab4382d2SGreg Kroah-Hartman 		if (!data)
2748ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2749ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2750ab4382d2SGreg Kroah-Hartman 	}
2751ab4382d2SGreg Kroah-Hartman 
2752bd737f87SRicardo Ribalda Delgado 	rs485_enabled = port->uart.rs485.flags & SER_RS485_ENABLED;
2753bd737f87SRicardo Ribalda Delgado 
2754ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2755ab4382d2SGreg Kroah-Hartman 	if (ret)
2756ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2757ab4382d2SGreg Kroah-Hartman 
2758ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2759ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2760ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2761ab4382d2SGreg Kroah-Hartman 		/*
2762ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
276391f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2764ab4382d2SGreg Kroah-Hartman 		 */
276591f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2766ab4382d2SGreg Kroah-Hartman 	}
2767ab4382d2SGreg Kroah-Hartman #endif
2768ab4382d2SGreg Kroah-Hartman 
2769ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2770ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2771ab4382d2SGreg Kroah-Hartman 
2772d4f64187SCyrille Pitchen 	/*
2773d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2774d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2775d4f64187SCyrille Pitchen 	 */
2776d4f64187SCyrille Pitchen 	clk_prepare_enable(port->clk);
2777d4f64187SCyrille Pitchen 
2778bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
27794e7decdaSCyrille Pitchen 		atmel_uart_writel(&port->uart, ATMEL_US_MR,
27804e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
27814e7decdaSCyrille Pitchen 		atmel_uart_writel(&port->uart, ATMEL_US_CR, ATMEL_US_RTSEN);
2782fc887b15SLinus Torvalds 	}
2783fc887b15SLinus Torvalds 
2784055560b0SElen Song 	/*
2785055560b0SElen Song 	 * Get port name of usart or uart
2786055560b0SElen Song 	 */
2787892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2788055560b0SElen Song 
2789d4f64187SCyrille Pitchen 	/*
2790d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2791d4f64187SCyrille Pitchen 	 * is used
2792d4f64187SCyrille Pitchen 	 */
2793d4f64187SCyrille Pitchen 	clk_disable_unprepare(port->clk);
2794d4f64187SCyrille Pitchen 
2795ab4382d2SGreg Kroah-Hartman 	return 0;
2796ab4382d2SGreg Kroah-Hartman 
2797ab4382d2SGreg Kroah-Hartman err_add_port:
2798ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2799ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2800ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2801ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2802ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2803ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2804ab4382d2SGreg Kroah-Hartman 	}
28056fbb9bdfSCyrille Pitchen err_clear_bit:
28066fbb9bdfSCyrille Pitchen 	clear_bit(port->uart.line, atmel_ports_in_use);
28074cbf9f48SNicolas Ferre err:
2808ab4382d2SGreg Kroah-Hartman 	return ret;
2809ab4382d2SGreg Kroah-Hartman }
2810ab4382d2SGreg Kroah-Hartman 
2811ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2812ab4382d2SGreg Kroah-Hartman {
2813ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2814ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2815ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2816ab4382d2SGreg Kroah-Hartman 
2817f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2818f50c995fSMarek Roszko 
2819ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2820ab4382d2SGreg Kroah-Hartman 
2821ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2822ab4382d2SGreg Kroah-Hartman 
2823ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2824ab4382d2SGreg Kroah-Hartman 
2825ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2826ab4382d2SGreg Kroah-Hartman 
2827503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
28284cbf9f48SNicolas Ferre 
2829ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2830ab4382d2SGreg Kroah-Hartman 
2831ab4382d2SGreg Kroah-Hartman 	return ret;
2832ab4382d2SGreg Kroah-Hartman }
2833ab4382d2SGreg Kroah-Hartman 
2834ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2835ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
28362d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2837ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2838ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2839ab4382d2SGreg Kroah-Hartman 	.driver		= {
2840ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
28415fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2842ab4382d2SGreg Kroah-Hartman 	},
2843ab4382d2SGreg Kroah-Hartman };
2844ab4382d2SGreg Kroah-Hartman 
2845ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2846ab4382d2SGreg Kroah-Hartman {
2847ab4382d2SGreg Kroah-Hartman 	int ret;
2848ab4382d2SGreg Kroah-Hartman 
2849ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2850ab4382d2SGreg Kroah-Hartman 	if (ret)
2851ab4382d2SGreg Kroah-Hartman 		return ret;
2852ab4382d2SGreg Kroah-Hartman 
2853ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2854ab4382d2SGreg Kroah-Hartman 	if (ret)
2855ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2856ab4382d2SGreg Kroah-Hartman 
2857ab4382d2SGreg Kroah-Hartman 	return ret;
2858ab4382d2SGreg Kroah-Hartman }
2859ab4382d2SGreg Kroah-Hartman 
2860ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2861ab4382d2SGreg Kroah-Hartman {
2862ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2863ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2864ab4382d2SGreg Kroah-Hartman }
2865ab4382d2SGreg Kroah-Hartman 
2866ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2867ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2868ab4382d2SGreg Kroah-Hartman 
2869ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2870ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2871ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2872ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2873