xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 637ba54f6051903e1f0011b75ee36b2a29696c6d)
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/tty.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
355fbe46b6SNicolas Ferre #include <linux/of.h>
365fbe46b6SNicolas Ferre #include <linux/of_device.h>
37354e57f3SLinus Walleij #include <linux/of_gpio.h>
38ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
396b997babSVinod Koul #include <linux/dmaengine.h>
40ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
43bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
442e68c22fSElen Song #include <linux/timer.h>
45354e57f3SLinus Walleij #include <linux/gpio.h>
46e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
47e0b0baadSRichard Genoud #include <linux/err.h>
48ab5e4e41SRichard Genoud #include <linux/irq.h>
492c7af5baSBoris BREZILLON #include <linux/suspend.h>
50ab4382d2SGreg Kroah-Hartman 
51ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
52ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
53ab4382d2SGreg Kroah-Hartman 
54ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
55ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
56ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
57ab4382d2SGreg Kroah-Hartman 
58b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
59b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
60b5199d46SCyrille Pitchen /*
61b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
62b5199d46SCyrille Pitchen  * high and low thresholds
63b5199d46SCyrille Pitchen  */
64b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
65b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
66b5199d46SCyrille Pitchen 
67ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
68ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
69ab4382d2SGreg Kroah-Hartman #endif
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
72ab4382d2SGreg Kroah-Hartman 
73e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
74e0b0baadSRichard Genoud 
75ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
76ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
77ab4382d2SGreg Kroah-Hartman 
78ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
79ab4382d2SGreg Kroah-Hartman 
80ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
81ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
82ab4382d2SGreg Kroah-Hartman  * UART. */
83ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
84ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
85ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
86ab4382d2SGreg Kroah-Hartman 
87ab4382d2SGreg Kroah-Hartman #else
88ab4382d2SGreg Kroah-Hartman 
89ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
90ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
91ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
92ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
93ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
94ab4382d2SGreg Kroah-Hartman 
95ab4382d2SGreg Kroah-Hartman #endif
96ab4382d2SGreg Kroah-Hartman 
97ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
98ab4382d2SGreg Kroah-Hartman 
99ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
100ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
101ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
102ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
103ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
104ab4382d2SGreg Kroah-Hartman };
105ab4382d2SGreg Kroah-Hartman 
106ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
107ab4382d2SGreg Kroah-Hartman 	u16		status;
108ab4382d2SGreg Kroah-Hartman 	u16		ch;
109ab4382d2SGreg Kroah-Hartman };
110ab4382d2SGreg Kroah-Hartman 
111*637ba54fSLudovic Desroches /*
112*637ba54fSLudovic Desroches  * Be careful, the real size of the ring buffer is
113*637ba54fSLudovic Desroches  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
114*637ba54fSLudovic Desroches  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
115*637ba54fSLudovic Desroches  * DMA mode.
116*637ba54fSLudovic Desroches  */
117ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
118ab4382d2SGreg Kroah-Hartman 
119ab4382d2SGreg Kroah-Hartman /*
1209af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
1219af92fbfSAlexandre Belloni  * avr32: 4
1229af92fbfSAlexandre Belloni  */
1239af92fbfSAlexandre Belloni #define ATMEL_MAX_UART		7
1249af92fbfSAlexandre Belloni 
1259af92fbfSAlexandre Belloni /*
126ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
127ab4382d2SGreg Kroah-Hartman  */
128ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
129ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
130ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
131ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
132ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
133ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
134ab4382d2SGreg Kroah-Hartman 
13534df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
13664e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
137ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
138ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
139ab4382d2SGreg Kroah-Hartman 
14008f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
14164e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
142ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
143ab4382d2SGreg Kroah-Hartman 
14408f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
14534df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
14608f738beSElen Song 	struct dma_chan			*chan_tx;
14734df42f5SElen Song 	struct dma_chan			*chan_rx;
14808f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
14934df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
15008f738beSElen Song 	dma_cookie_t			cookie_tx;
15134df42f5SElen Song 	dma_cookie_t			cookie_rx;
15208f738beSElen Song 	struct scatterlist		sg_tx;
15334df42f5SElen Song 	struct scatterlist		sg_rx;
15400e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_rx;
15500e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_tx;
156ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
1575f258b3eSCyrille Pitchen 	unsigned int		tx_len;
158ab4382d2SGreg Kroah-Hartman 
159ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
160ab4382d2SGreg Kroah-Hartman 
161e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
162ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
163b5199d46SCyrille Pitchen 	u32			fifo_size;
164b5199d46SCyrille Pitchen 	u32			rts_high;
165b5199d46SCyrille Pitchen 	u32			rts_low;
166ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1672958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
1684b769371SNicolas Ferre 	bool			has_hw_timer;
1694b769371SNicolas Ferre 	struct timer_list	uart_timer;
1702c7af5baSBoris BREZILLON 
1712c7af5baSBoris BREZILLON 	bool			suspended;
1722c7af5baSBoris BREZILLON 	unsigned int		pending;
1732c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1742c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1752c7af5baSBoris BREZILLON 
176a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
177a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
178a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
179a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
180a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
181a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
182ab4382d2SGreg Kroah-Hartman };
183ab4382d2SGreg Kroah-Hartman 
184ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
185503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
186ab4382d2SGreg Kroah-Hartman 
187ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
188ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
189ab4382d2SGreg Kroah-Hartman #endif
190ab4382d2SGreg Kroah-Hartman 
1915fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1925fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1935fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1945fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1955fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1965fbe46b6SNicolas Ferre };
1975fbe46b6SNicolas Ferre #endif
1985fbe46b6SNicolas Ferre 
199ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
200ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
201ab4382d2SGreg Kroah-Hartman {
202ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
203ab4382d2SGreg Kroah-Hartman }
204ab4382d2SGreg Kroah-Hartman 
2054e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2064e7decdaSCyrille Pitchen {
2074e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2084e7decdaSCyrille Pitchen }
2094e7decdaSCyrille Pitchen 
2104e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2114e7decdaSCyrille Pitchen {
2124e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2134e7decdaSCyrille Pitchen }
2144e7decdaSCyrille Pitchen 
215a6499435SCyrille Pitchen #ifdef CONFIG_AVR32
216a6499435SCyrille Pitchen 
217a6499435SCyrille Pitchen /* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
218a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
219b5199d46SCyrille Pitchen {
220a6499435SCyrille Pitchen 	return __raw_readl(port->membase + ATMEL_US_RHR);
221b5199d46SCyrille Pitchen }
222b5199d46SCyrille Pitchen 
223a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
224b5199d46SCyrille Pitchen {
225a6499435SCyrille Pitchen 	__raw_writel(value, port->membase + ATMEL_US_THR);
226b5199d46SCyrille Pitchen }
227b5199d46SCyrille Pitchen 
228a6499435SCyrille Pitchen #else
229a6499435SCyrille Pitchen 
230a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
231a6499435SCyrille Pitchen {
232a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
233a6499435SCyrille Pitchen }
234a6499435SCyrille Pitchen 
235a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
236a6499435SCyrille Pitchen {
237a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
238a6499435SCyrille Pitchen }
239a6499435SCyrille Pitchen 
240a6499435SCyrille Pitchen #endif
241a6499435SCyrille Pitchen 
242ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
24364e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
244ab4382d2SGreg Kroah-Hartman {
245ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
246ab4382d2SGreg Kroah-Hartman 
24764e22ebeSElen Song 	return atmel_port->use_pdc_rx;
248ab4382d2SGreg Kroah-Hartman }
249ab4382d2SGreg Kroah-Hartman 
25064e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
251ab4382d2SGreg Kroah-Hartman {
252ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
253ab4382d2SGreg Kroah-Hartman 
25464e22ebeSElen Song 	return atmel_port->use_pdc_tx;
255ab4382d2SGreg Kroah-Hartman }
256ab4382d2SGreg Kroah-Hartman #else
25764e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
258ab4382d2SGreg Kroah-Hartman {
259ab4382d2SGreg Kroah-Hartman 	return false;
260ab4382d2SGreg Kroah-Hartman }
261ab4382d2SGreg Kroah-Hartman 
26264e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
263ab4382d2SGreg Kroah-Hartman {
264ab4382d2SGreg Kroah-Hartman 	return false;
265ab4382d2SGreg Kroah-Hartman }
266ab4382d2SGreg Kroah-Hartman #endif
267ab4382d2SGreg Kroah-Hartman 
26808f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26908f738beSElen Song {
27008f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27108f738beSElen Song 
27208f738beSElen Song 	return atmel_port->use_dma_tx;
27308f738beSElen Song }
27408f738beSElen Song 
27534df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27634df42f5SElen Song {
27734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27834df42f5SElen Song 
27934df42f5SElen Song 	return atmel_port->use_dma_rx;
28034df42f5SElen Song }
28134df42f5SElen Song 
2825be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2835be605acSAlexandre Belloni {
2845be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2855be605acSAlexandre Belloni 
2865be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2875be605acSAlexandre Belloni }
2885be605acSAlexandre Belloni 
289e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
290e0b0baadSRichard Genoud {
291e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
292e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
293e0b0baadSRichard Genoud 
2944e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
295e0b0baadSRichard Genoud 
296e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
297e0b0baadSRichard Genoud 
298e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
299e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
300e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
301e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
302e0b0baadSRichard Genoud 		else
303e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
304e0b0baadSRichard Genoud 	}
305e0b0baadSRichard Genoud 
306e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
307e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
308e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
309e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
310e0b0baadSRichard Genoud 		else
311e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
312e0b0baadSRichard Genoud 	}
313e0b0baadSRichard Genoud 
314e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
315e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
316e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
317e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
318e0b0baadSRichard Genoud 		else
319e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
320e0b0baadSRichard Genoud 	}
321e0b0baadSRichard Genoud 
322e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
323e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
324e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
325e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
326e0b0baadSRichard Genoud 		else
327e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
328e0b0baadSRichard Genoud 	}
329e0b0baadSRichard Genoud 
330e0b0baadSRichard Genoud 	return status;
331e0b0baadSRichard Genoud }
332e0b0baadSRichard Genoud 
333ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
33413bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
33513bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
336ab4382d2SGreg Kroah-Hartman {
337ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
338ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
339ab4382d2SGreg Kroah-Hartman 
340ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
342ab4382d2SGreg Kroah-Hartman 
3434e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
344ab4382d2SGreg Kroah-Hartman 
345ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
346ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
347ab4382d2SGreg Kroah-Hartman 
34813bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
349ab4382d2SGreg Kroah-Hartman 
350ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
351ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
352ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3534e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3544e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
355ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
356ab4382d2SGreg Kroah-Hartman 	} else {
357ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
35864e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
359ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
360ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
361ab4382d2SGreg Kroah-Hartman 		else
362ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
363ab4382d2SGreg Kroah-Hartman 	}
3644e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
365ab4382d2SGreg Kroah-Hartman 
366ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
368ab4382d2SGreg Kroah-Hartman 
36913bd3e6fSRicardo Ribalda Delgado 	return 0;
370ab4382d2SGreg Kroah-Hartman }
371ab4382d2SGreg Kroah-Hartman 
372ab4382d2SGreg Kroah-Hartman /*
373ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
374ab4382d2SGreg Kroah-Hartman  */
375ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
376ab4382d2SGreg Kroah-Hartman {
3774e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3784e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3794e7decdaSCyrille Pitchen 		0;
380ab4382d2SGreg Kroah-Hartman }
381ab4382d2SGreg Kroah-Hartman 
382ab4382d2SGreg Kroah-Hartman /*
383ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
384ab4382d2SGreg Kroah-Hartman  */
385ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
386ab4382d2SGreg Kroah-Hartman {
387ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3884e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3891cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
390ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
391ab4382d2SGreg Kroah-Hartman 
3921cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3931cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
3944e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3954e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
3961cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3971cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3981cf6e8fcSCyrille Pitchen 	}
3991cf6e8fcSCyrille Pitchen 
4001cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
4011cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
4021cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4031cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4041cf6e8fcSCyrille Pitchen 
4051cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4061cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4071cf6e8fcSCyrille Pitchen 	} else {
4081cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4091cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4101cf6e8fcSCyrille Pitchen 
4111cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4121cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4131cf6e8fcSCyrille Pitchen 	}
4141cf6e8fcSCyrille Pitchen 
415ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4161cf6e8fcSCyrille Pitchen 		control |= rts_ready;
417ab4382d2SGreg Kroah-Hartman 	else
4181cf6e8fcSCyrille Pitchen 		control |= rts_paused;
419ab4382d2SGreg Kroah-Hartman 
420ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
421ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
422ab4382d2SGreg Kroah-Hartman 	else
423ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
424ab4382d2SGreg Kroah-Hartman 
4254e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
426ab4382d2SGreg Kroah-Hartman 
427e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
428e0b0baadSRichard Genoud 
429ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
4301cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
431ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
432ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
433ab4382d2SGreg Kroah-Hartman 	else
434ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
435ab4382d2SGreg Kroah-Hartman 
4364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
437ab4382d2SGreg Kroah-Hartman }
438ab4382d2SGreg Kroah-Hartman 
439ab4382d2SGreg Kroah-Hartman /*
440ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
441ab4382d2SGreg Kroah-Hartman  */
442ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
443ab4382d2SGreg Kroah-Hartman {
444e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
445e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
446ab4382d2SGreg Kroah-Hartman 
4474e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
448ab4382d2SGreg Kroah-Hartman 
449ab4382d2SGreg Kroah-Hartman 	/*
450ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
451ab4382d2SGreg Kroah-Hartman 	 */
452ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
453ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
454ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
455ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
456ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
457ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
458ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
459ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
460ab4382d2SGreg Kroah-Hartman 
461e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
462ab4382d2SGreg Kroah-Hartman }
463ab4382d2SGreg Kroah-Hartman 
464ab4382d2SGreg Kroah-Hartman /*
465ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
466ab4382d2SGreg Kroah-Hartman  */
467ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
468ab4382d2SGreg Kroah-Hartman {
469ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
470ab4382d2SGreg Kroah-Hartman 
47164e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
472ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4734e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
474ab4382d2SGreg Kroah-Hartman 	}
475ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4764e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
477ab4382d2SGreg Kroah-Hartman 
47813bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
47913bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
480ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
481ab4382d2SGreg Kroah-Hartman }
482ab4382d2SGreg Kroah-Hartman 
483ab4382d2SGreg Kroah-Hartman /*
484ab4382d2SGreg Kroah-Hartman  * Start transmitting.
485ab4382d2SGreg Kroah-Hartman  */
486ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
487ab4382d2SGreg Kroah-Hartman {
488ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
489ab4382d2SGreg Kroah-Hartman 
49064e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
4914e7decdaSCyrille Pitchen 		if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
492ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
493ab4382d2SGreg Kroah-Hartman 			   really need this.*/
494ab4382d2SGreg Kroah-Hartman 			return;
495ab4382d2SGreg Kroah-Hartman 
49613bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
49713bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
498ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
499ab4382d2SGreg Kroah-Hartman 
500ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
5014e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
502ab4382d2SGreg Kroah-Hartman 	}
503ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
5044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
505ab4382d2SGreg Kroah-Hartman }
506ab4382d2SGreg Kroah-Hartman 
507ab4382d2SGreg Kroah-Hartman /*
508ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
509ab4382d2SGreg Kroah-Hartman  */
510ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
511ab4382d2SGreg Kroah-Hartman {
5124e7decdaSCyrille Pitchen 	/* reset status and receiver */
5134e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
514ab4382d2SGreg Kroah-Hartman 
5154e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
51657c36868SSiftar, Gabe 
51764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
518ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
5194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
5204e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
521ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
5224e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
523ab4382d2SGreg Kroah-Hartman 	} else {
5244e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
525ab4382d2SGreg Kroah-Hartman 	}
526ab4382d2SGreg Kroah-Hartman }
527ab4382d2SGreg Kroah-Hartman 
528ab4382d2SGreg Kroah-Hartman /*
529ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
530ab4382d2SGreg Kroah-Hartman  */
531ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
532ab4382d2SGreg Kroah-Hartman {
5334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
53457c36868SSiftar, Gabe 
53564e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
536ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5374e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5384e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5394e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
540ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
541ab4382d2SGreg Kroah-Hartman 	} else {
5424e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
543ab4382d2SGreg Kroah-Hartman 	}
544ab4382d2SGreg Kroah-Hartman }
545ab4382d2SGreg Kroah-Hartman 
546ab4382d2SGreg Kroah-Hartman /*
547ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
548ab4382d2SGreg Kroah-Hartman  */
549ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
550ab4382d2SGreg Kroah-Hartman {
551ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
552ab5e4e41SRichard Genoud 	uint32_t ier = 0;
553ab5e4e41SRichard Genoud 
554ab5e4e41SRichard Genoud 	/*
555ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
556ab5e4e41SRichard Genoud 	 */
557ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
558ab5e4e41SRichard Genoud 		return;
559ab5e4e41SRichard Genoud 
560ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
561ab5e4e41SRichard Genoud 
56218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
563ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
564ab5e4e41SRichard Genoud 
56518dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
566ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
567ab5e4e41SRichard Genoud 
56818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
569ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
570ab5e4e41SRichard Genoud 
57118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
572ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
573ab5e4e41SRichard Genoud 
5744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
57518dfef9cSUwe Kleine-König 
57618dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
577ab4382d2SGreg Kroah-Hartman }
578ab4382d2SGreg Kroah-Hartman 
579ab4382d2SGreg Kroah-Hartman /*
58035b675b9SRichard Genoud  * Disable modem status interrupts
58135b675b9SRichard Genoud  */
58235b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
58335b675b9SRichard Genoud {
58435b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
58535b675b9SRichard Genoud 	uint32_t idr = 0;
58635b675b9SRichard Genoud 
58735b675b9SRichard Genoud 	/*
58835b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
58935b675b9SRichard Genoud 	 */
59035b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
59135b675b9SRichard Genoud 		return;
59235b675b9SRichard Genoud 
59335b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
59435b675b9SRichard Genoud 
59518dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
59618dfef9cSUwe Kleine-König 
59718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
59835b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
59935b675b9SRichard Genoud 
60018dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
60135b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
60235b675b9SRichard Genoud 
60318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
60435b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
60535b675b9SRichard Genoud 
60618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
60735b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
60835b675b9SRichard Genoud 
6094e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
61035b675b9SRichard Genoud }
61135b675b9SRichard Genoud 
61235b675b9SRichard Genoud /*
613ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
614ab4382d2SGreg Kroah-Hartman  */
615ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
616ab4382d2SGreg Kroah-Hartman {
617ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
6184e7decdaSCyrille Pitchen 		/* start break */
6194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
620ab4382d2SGreg Kroah-Hartman 	else
6214e7decdaSCyrille Pitchen 		/* stop break */
6224e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
623ab4382d2SGreg Kroah-Hartman }
624ab4382d2SGreg Kroah-Hartman 
625ab4382d2SGreg Kroah-Hartman /*
626ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
627ab4382d2SGreg Kroah-Hartman  */
628ab4382d2SGreg Kroah-Hartman static void
629ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
630ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
631ab4382d2SGreg Kroah-Hartman {
632ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
633ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
634ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
635ab4382d2SGreg Kroah-Hartman 
636ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
637ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
638ab4382d2SGreg Kroah-Hartman 		return;
639ab4382d2SGreg Kroah-Hartman 
640ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
641ab4382d2SGreg Kroah-Hartman 	c->status	= status;
642ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
643ab4382d2SGreg Kroah-Hartman 
644ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
645ab4382d2SGreg Kroah-Hartman 	smp_wmb();
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
648ab4382d2SGreg Kroah-Hartman }
649ab4382d2SGreg Kroah-Hartman 
650ab4382d2SGreg Kroah-Hartman /*
651ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
652ab4382d2SGreg Kroah-Hartman  */
653ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
654ab4382d2SGreg Kroah-Hartman {
655ab4382d2SGreg Kroah-Hartman 	/* clear error */
6564e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
657ab4382d2SGreg Kroah-Hartman 
658ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
659ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
660ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
661ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
662ab4382d2SGreg Kroah-Hartman 	}
663ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
664ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
665ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
666ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
667ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
668ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
669ab4382d2SGreg Kroah-Hartman }
670ab4382d2SGreg Kroah-Hartman 
671ab4382d2SGreg Kroah-Hartman /*
672ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
673ab4382d2SGreg Kroah-Hartman  */
674ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
675ab4382d2SGreg Kroah-Hartman {
676ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
677ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
678ab4382d2SGreg Kroah-Hartman 
6794e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
680ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
681a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
682ab4382d2SGreg Kroah-Hartman 
683ab4382d2SGreg Kroah-Hartman 		/*
684ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
685ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
686ab4382d2SGreg Kroah-Hartman 		 */
687ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
688ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
689ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
690ab4382d2SGreg Kroah-Hartman 
691ab4382d2SGreg Kroah-Hartman 			/* clear error */
6924e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
693ab4382d2SGreg Kroah-Hartman 
694ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
695ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
696ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
6974e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
6984e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
699ab4382d2SGreg Kroah-Hartman 			} else {
700ab4382d2SGreg Kroah-Hartman 				/*
701ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
702ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
703ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
704ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
705ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
706ab4382d2SGreg Kroah-Hartman 				 */
7074e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
7084e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
709ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
710ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
711ab4382d2SGreg Kroah-Hartman 			}
712ab4382d2SGreg Kroah-Hartman 		}
713ab4382d2SGreg Kroah-Hartman 
714ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
7154e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
716ab4382d2SGreg Kroah-Hartman 	}
717ab4382d2SGreg Kroah-Hartman 
71800e8e658SNicolas Ferre 	tasklet_schedule(&atmel_port->tasklet_rx);
719ab4382d2SGreg Kroah-Hartman }
720ab4382d2SGreg Kroah-Hartman 
721ab4382d2SGreg Kroah-Hartman /*
722ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
723ab4382d2SGreg Kroah-Hartman  * disabled)
724ab4382d2SGreg Kroah-Hartman  */
725ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
726ab4382d2SGreg Kroah-Hartman {
727ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
728ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
729ab4382d2SGreg Kroah-Hartman 
7304e7decdaSCyrille Pitchen 	if (port->x_char &&
7314e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
732a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
733ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
734ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
735ab4382d2SGreg Kroah-Hartman 	}
736ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
737ab4382d2SGreg Kroah-Hartman 		return;
738ab4382d2SGreg Kroah-Hartman 
7394e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7404e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
741a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
742ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
743ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
744ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
745ab4382d2SGreg Kroah-Hartman 			break;
746ab4382d2SGreg Kroah-Hartman 	}
747ab4382d2SGreg Kroah-Hartman 
748ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
749ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
750ab4382d2SGreg Kroah-Hartman 
751ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
752ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7534e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7544e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
755ab4382d2SGreg Kroah-Hartman }
756ab4382d2SGreg Kroah-Hartman 
75708f738beSElen Song static void atmel_complete_tx_dma(void *arg)
75808f738beSElen Song {
75908f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
76008f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
76108f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
76208f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
76308f738beSElen Song 	unsigned long flags;
76408f738beSElen Song 
76508f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
76608f738beSElen Song 
76708f738beSElen Song 	if (chan)
76808f738beSElen Song 		dmaengine_terminate_all(chan);
7695f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
77008f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
77108f738beSElen Song 
7725f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
77308f738beSElen Song 
77408f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
77508f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
77608f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
77708f738beSElen Song 	atmel_port->desc_tx = NULL;
77808f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
77908f738beSElen Song 
78008f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
78108f738beSElen Song 		uart_write_wakeup(port);
78208f738beSElen Song 
7831842dc2eSCyrille Pitchen 	/*
7841842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7851842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7861842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7871842dc2eSCyrille Pitchen 	 */
78808f738beSElen Song 	if (!uart_circ_empty(xmit))
78900e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_tx);
79008f738beSElen Song 
79108f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
79208f738beSElen Song }
79308f738beSElen Song 
79408f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
79508f738beSElen Song {
79608f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
79708f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
79808f738beSElen Song 
79908f738beSElen Song 	if (chan) {
80008f738beSElen Song 		dmaengine_terminate_all(chan);
80108f738beSElen Song 		dma_release_channel(chan);
80208f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
80348479148SWolfram Sang 				DMA_TO_DEVICE);
80408f738beSElen Song 	}
80508f738beSElen Song 
80608f738beSElen Song 	atmel_port->desc_tx = NULL;
80708f738beSElen Song 	atmel_port->chan_tx = NULL;
80808f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
80908f738beSElen Song }
81008f738beSElen Song 
81108f738beSElen Song /*
81208f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
81308f738beSElen Song  */
81408f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
81508f738beSElen Song {
81608f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
81708f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
81808f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
81908f738beSElen Song 	struct dma_async_tx_descriptor *desc;
8205f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
8215f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
8225f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
82308f738beSElen Song 
82408f738beSElen Song 	/* Make sure we have an idle channel */
82508f738beSElen Song 	if (atmel_port->desc_tx != NULL)
82608f738beSElen Song 		return;
82708f738beSElen Song 
82808f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
82908f738beSElen Song 		/*
83008f738beSElen Song 		 * DMA is idle now.
83108f738beSElen Song 		 * Port xmit buffer is already mapped,
83208f738beSElen Song 		 * and it is one page... Just adjust
83308f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
83408f738beSElen Song 		 * we have to transmit till the end, and then the rest.
83508f738beSElen Song 		 * Take the port lock to get a
83608f738beSElen Song 		 * consistent xmit buffer state.
83708f738beSElen Song 		 */
8385f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
83908f738beSElen Song 					 xmit->tail,
84008f738beSElen Song 					 UART_XMIT_SIZE);
8415f258b3eSCyrille Pitchen 
8425f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
8435f258b3eSCyrille Pitchen 			/* multi data mode */
8445f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
8455f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
8465f258b3eSCyrille Pitchen 		} else {
8475f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
8485f258b3eSCyrille Pitchen 			part1_len = 0;
8495f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
8505f258b3eSCyrille Pitchen 		}
8515f258b3eSCyrille Pitchen 
8525f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
8535f258b3eSCyrille Pitchen 		sg_len = 0;
8545f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
8555f258b3eSCyrille Pitchen 		if (part1_len) {
8565f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8575f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8585f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
8595f258b3eSCyrille Pitchen 
8605f258b3eSCyrille Pitchen 			phys_addr += part1_len;
8615f258b3eSCyrille Pitchen 		}
8625f258b3eSCyrille Pitchen 
8635f258b3eSCyrille Pitchen 		if (part2_len) {
8645f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8655f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8665f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
8675f258b3eSCyrille Pitchen 		}
8685f258b3eSCyrille Pitchen 
8695f258b3eSCyrille Pitchen 		/*
8705f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
8715f258b3eSCyrille Pitchen 		 * xmit->tail correctly
8725f258b3eSCyrille Pitchen 		 */
8735f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
87408f738beSElen Song 
87508f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
8765f258b3eSCyrille Pitchen 					       sgl,
8775f258b3eSCyrille Pitchen 					       sg_len,
87808f738beSElen Song 					       DMA_MEM_TO_DEV,
87908f738beSElen Song 					       DMA_PREP_INTERRUPT |
88008f738beSElen Song 					       DMA_CTRL_ACK);
88108f738beSElen Song 		if (!desc) {
88208f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
88308f738beSElen Song 			return;
88408f738beSElen Song 		}
88508f738beSElen Song 
8865f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
88708f738beSElen Song 
88808f738beSElen Song 		atmel_port->desc_tx = desc;
88908f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
89008f738beSElen Song 		desc->callback_param = atmel_port;
89108f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
89208f738beSElen Song 
89308f738beSElen Song 	} else {
89413bd3e6fSRicardo Ribalda Delgado 		if (port->rs485.flags & SER_RS485_ENABLED) {
89508f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
89608f738beSElen Song 			atmel_start_rx(port);
89708f738beSElen Song 		}
89808f738beSElen Song 	}
89908f738beSElen Song 
90008f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
90108f738beSElen Song 		uart_write_wakeup(port);
90208f738beSElen Song }
90308f738beSElen Song 
90408f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
90508f738beSElen Song {
90608f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
90708f738beSElen Song 	dma_cap_mask_t		mask;
90808f738beSElen Song 	struct dma_slave_config config;
90908f738beSElen Song 	int ret, nent;
91008f738beSElen Song 
91108f738beSElen Song 	dma_cap_zero(mask);
91208f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
91308f738beSElen Song 
91408f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
91508f738beSElen Song 	if (atmel_port->chan_tx == NULL)
91608f738beSElen Song 		goto chan_err;
91708f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
91808f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
91908f738beSElen Song 
92008f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
92108f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
92208f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
9232c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
92408f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
92508f738beSElen Song 			virt_to_page(port->state->xmit.buf),
92608f738beSElen Song 			UART_XMIT_SIZE,
927c8d1f022SUwe Kleine-König 			(unsigned long)port->state->xmit.buf & ~PAGE_MASK);
92808f738beSElen Song 	nent = dma_map_sg(port->dev,
92908f738beSElen Song 				&atmel_port->sg_tx,
93008f738beSElen Song 				1,
93148479148SWolfram Sang 				DMA_TO_DEVICE);
93208f738beSElen Song 
93308f738beSElen Song 	if (!nent) {
93408f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
93508f738beSElen Song 		goto chan_err;
93608f738beSElen Song 	} else {
937c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
93808f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
93908f738beSElen Song 			port->state->xmit.buf,
940c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
94108f738beSElen Song 	}
94208f738beSElen Song 
94308f738beSElen Song 	/* Configure the slave DMA */
94408f738beSElen Song 	memset(&config, 0, sizeof(config));
94508f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
9465f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
9475f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
9485f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
94908f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
950a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
95108f738beSElen Song 
9525483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9535483c10eSMaxime Ripard 				     &config);
95408f738beSElen Song 	if (ret) {
95508f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
95608f738beSElen Song 		goto chan_err;
95708f738beSElen Song 	}
95808f738beSElen Song 
95908f738beSElen Song 	return 0;
96008f738beSElen Song 
96108f738beSElen Song chan_err:
96208f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
96308f738beSElen Song 	atmel_port->use_dma_tx = 0;
96408f738beSElen Song 	if (atmel_port->chan_tx)
96508f738beSElen Song 		atmel_release_tx_dma(port);
96608f738beSElen Song 	return -EINVAL;
96708f738beSElen Song }
96808f738beSElen Song 
96934df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
97034df42f5SElen Song {
97134df42f5SElen Song 	struct uart_port *port = arg;
97234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
97334df42f5SElen Song 
97400e8e658SNicolas Ferre 	tasklet_schedule(&atmel_port->tasklet_rx);
97534df42f5SElen Song }
97634df42f5SElen Song 
97734df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
97834df42f5SElen Song {
97934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
98034df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
98134df42f5SElen Song 
98234df42f5SElen Song 	if (chan) {
98334df42f5SElen Song 		dmaengine_terminate_all(chan);
98434df42f5SElen Song 		dma_release_channel(chan);
98534df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
98648479148SWolfram Sang 				DMA_FROM_DEVICE);
98734df42f5SElen Song 	}
98834df42f5SElen Song 
98934df42f5SElen Song 	atmel_port->desc_rx = NULL;
99034df42f5SElen Song 	atmel_port->chan_rx = NULL;
99134df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
99234df42f5SElen Song }
99334df42f5SElen Song 
99434df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
99534df42f5SElen Song {
99634df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
99766f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
99834df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
99934df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
100034df42f5SElen Song 	struct dma_tx_state state;
100134df42f5SElen Song 	enum dma_status dmastat;
100266f37aafSCyrille Pitchen 	size_t count;
100334df42f5SElen Song 
100434df42f5SElen Song 
100534df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
10064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
100734df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
100834df42f5SElen Song 				atmel_port->cookie_rx,
100934df42f5SElen Song 				&state);
101034df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
101134df42f5SElen Song 	if (dmastat == DMA_ERROR) {
101234df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
10134e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
101400e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
101534df42f5SElen Song 		return;
101634df42f5SElen Song 	}
101766f37aafSCyrille Pitchen 
101866f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
101966f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
102066f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
102166f37aafSCyrille Pitchen 			    1,
1022485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
102334df42f5SElen Song 
102434df42f5SElen Song 	/*
102566f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
102666f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
102766f37aafSCyrille Pitchen 	 * framework.
102866f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
102966f37aafSCyrille Pitchen 	 * length.
103034df42f5SElen Song 	 */
103166f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
103266f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
103366f37aafSCyrille Pitchen 	/*
103466f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
103566f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
103666f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
103766f37aafSCyrille Pitchen 	 *
103866f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
103966f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
104066f37aafSCyrille Pitchen 	 *
104166f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
104266f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
104366f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
104466f37aafSCyrille Pitchen 	 */
104566f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
104666f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
104734df42f5SElen Song 
104866f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
104966f37aafSCyrille Pitchen 		ring->tail = 0;
105034df42f5SElen Song 		port->icount.rx += count;
105134df42f5SElen Song 	}
105234df42f5SElen Song 
105366f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
105466f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
105566f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
105666f37aafSCyrille Pitchen 
105766f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
105866f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
105966f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
106066f37aafSCyrille Pitchen 			ring->head = 0;
106166f37aafSCyrille Pitchen 		ring->tail = ring->head;
106266f37aafSCyrille Pitchen 		port->icount.rx += count;
106366f37aafSCyrille Pitchen 	}
106466f37aafSCyrille Pitchen 
106566f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
106666f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
106766f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
106866f37aafSCyrille Pitchen 			       1,
1069485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
107066f37aafSCyrille Pitchen 
107166f37aafSCyrille Pitchen 	/*
107266f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
107366f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
107466f37aafSCyrille Pitchen 	 */
107566f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
107666f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
107766f37aafSCyrille Pitchen 	spin_lock(&port->lock);
107866f37aafSCyrille Pitchen 
10794e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
108034df42f5SElen Song }
108134df42f5SElen Song 
108234df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
108334df42f5SElen Song {
108434df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
108534df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
108634df42f5SElen Song 	dma_cap_mask_t		mask;
108734df42f5SElen Song 	struct dma_slave_config config;
108834df42f5SElen Song 	struct circ_buf		*ring;
108934df42f5SElen Song 	int ret, nent;
109034df42f5SElen Song 
109134df42f5SElen Song 	ring = &atmel_port->rx_ring;
109234df42f5SElen Song 
109334df42f5SElen Song 	dma_cap_zero(mask);
109434df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
109534df42f5SElen Song 
109634df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
109734df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
109834df42f5SElen Song 		goto chan_err;
109934df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
110034df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
110134df42f5SElen Song 
110234df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
110334df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
110434df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
11052c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
110634df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
110734df42f5SElen Song 		    virt_to_page(ring->buf),
1108a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
1109c8d1f022SUwe Kleine-König 		    (unsigned long)ring->buf & ~PAGE_MASK);
111034df42f5SElen Song 	nent = dma_map_sg(port->dev,
111134df42f5SElen Song 			  &atmel_port->sg_rx,
111234df42f5SElen Song 			  1,
111348479148SWolfram Sang 			  DMA_FROM_DEVICE);
111434df42f5SElen Song 
111534df42f5SElen Song 	if (!nent) {
111634df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
111734df42f5SElen Song 		goto chan_err;
111834df42f5SElen Song 	} else {
1119c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
112034df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
112134df42f5SElen Song 			ring->buf,
1122c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
112334df42f5SElen Song 	}
112434df42f5SElen Song 
112534df42f5SElen Song 	/* Configure the slave DMA */
112634df42f5SElen Song 	memset(&config, 0, sizeof(config));
112734df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
112834df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
112934df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1130a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
113134df42f5SElen Song 
11325483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
11335483c10eSMaxime Ripard 				     &config);
113434df42f5SElen Song 	if (ret) {
113534df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
113634df42f5SElen Song 		goto chan_err;
113734df42f5SElen Song 	}
113834df42f5SElen Song 	/*
113934df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
114034df42f5SElen Song 	 * each one is half ring buffer size
114134df42f5SElen Song 	 */
114234df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
114334df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
114434df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
114534df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
114634df42f5SElen Song 					 DMA_DEV_TO_MEM,
114734df42f5SElen Song 					 DMA_PREP_INTERRUPT);
114834df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
114934df42f5SElen Song 	desc->callback_param = port;
115034df42f5SElen Song 	atmel_port->desc_rx = desc;
115134df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
115234df42f5SElen Song 
115334df42f5SElen Song 	return 0;
115434df42f5SElen Song 
115534df42f5SElen Song chan_err:
115634df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
115734df42f5SElen Song 	atmel_port->use_dma_rx = 0;
115834df42f5SElen Song 	if (atmel_port->chan_rx)
115934df42f5SElen Song 		atmel_release_rx_dma(port);
116034df42f5SElen Song 	return -EINVAL;
116134df42f5SElen Song }
116234df42f5SElen Song 
11632e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
11642e68c22fSElen Song {
11652e68c22fSElen Song 	struct uart_port *port = (void *)data;
11662e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11672e68c22fSElen Song 
116800e8e658SNicolas Ferre 	tasklet_schedule(&atmel_port->tasklet_rx);
11692e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
11702e68c22fSElen Song }
11712e68c22fSElen Song 
1172ab4382d2SGreg Kroah-Hartman /*
1173ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1174ab4382d2SGreg Kroah-Hartman  */
1175ab4382d2SGreg Kroah-Hartman static void
1176ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1177ab4382d2SGreg Kroah-Hartman {
1178ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1179ab4382d2SGreg Kroah-Hartman 
118064e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1181ab4382d2SGreg Kroah-Hartman 		/*
1182ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1183ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1184ab4382d2SGreg Kroah-Hartman 		 *
1185ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1186ab4382d2SGreg Kroah-Hartman 		 * the moment.
1187ab4382d2SGreg Kroah-Hartman 		 */
1188ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
11894e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
11904e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
119100e8e658SNicolas Ferre 			tasklet_schedule(&atmel_port->tasklet_rx);
1192ab4382d2SGreg Kroah-Hartman 		}
1193ab4382d2SGreg Kroah-Hartman 
1194ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1195ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1196ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1197ab4382d2SGreg Kroah-Hartman 	}
1198ab4382d2SGreg Kroah-Hartman 
119934df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
120034df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
12014e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12024e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
120300e8e658SNicolas Ferre 			tasklet_schedule(&atmel_port->tasklet_rx);
120434df42f5SElen Song 		}
120534df42f5SElen Song 	}
120634df42f5SElen Song 
1207ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1208ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1209ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1210ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1211ab4382d2SGreg Kroah-Hartman 		/*
1212ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1213ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1214ab4382d2SGreg Kroah-Hartman 		 */
12154e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
12164e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1217ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1218ab4382d2SGreg Kroah-Hartman 	}
1219ab4382d2SGreg Kroah-Hartman }
1220ab4382d2SGreg Kroah-Hartman 
1221ab4382d2SGreg Kroah-Hartman /*
1222ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1223ab4382d2SGreg Kroah-Hartman  */
1224ab4382d2SGreg Kroah-Hartman static void
1225ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1226ab4382d2SGreg Kroah-Hartman {
1227ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1228ab4382d2SGreg Kroah-Hartman 
1229ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1230ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
12314e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
12324e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
123300e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_tx);
1234ab4382d2SGreg Kroah-Hartman 	}
1235ab4382d2SGreg Kroah-Hartman }
1236ab4382d2SGreg Kroah-Hartman 
1237ab4382d2SGreg Kroah-Hartman /*
1238ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1239ab4382d2SGreg Kroah-Hartman  */
1240ab4382d2SGreg Kroah-Hartman static void
1241ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1242ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1243ab4382d2SGreg Kroah-Hartman {
1244ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12459205218eSNicolas Ferre 	unsigned int status_change;
1246ab4382d2SGreg Kroah-Hartman 
1247ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1248ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
12499205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1250d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
12519205218eSNicolas Ferre 
12529205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
12539205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
12549205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
12559205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
12569205218eSNicolas Ferre 				port->icount.rng++;
12579205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
12589205218eSNicolas Ferre 				port->icount.dsr++;
12599205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
12609205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
12619205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
12629205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
12639205218eSNicolas Ferre 
12649205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
12659205218eSNicolas Ferre 		}
1266ab4382d2SGreg Kroah-Hartman 	}
1267ab4382d2SGreg Kroah-Hartman }
1268ab4382d2SGreg Kroah-Hartman 
1269ab4382d2SGreg Kroah-Hartman /*
1270ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1271ab4382d2SGreg Kroah-Hartman  */
1272ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1273ab4382d2SGreg Kroah-Hartman {
1274ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1275ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12762c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1277ab4382d2SGreg Kroah-Hartman 
12782c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
12792c7af5baSBoris BREZILLON 
1280ab4382d2SGreg Kroah-Hartman 	do {
1281e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
12824e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
12832c7af5baSBoris BREZILLON 		pending = status & mask;
1284ab4382d2SGreg Kroah-Hartman 		if (!pending)
1285ab4382d2SGreg Kroah-Hartman 			break;
1286ab4382d2SGreg Kroah-Hartman 
12872c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
12882c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
12892c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
12904e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
12912c7af5baSBoris BREZILLON 			pm_system_wakeup();
12922c7af5baSBoris BREZILLON 			break;
12932c7af5baSBoris BREZILLON 		}
12942c7af5baSBoris BREZILLON 
1295ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1296ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1297ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1298ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1299ab4382d2SGreg Kroah-Hartman 
13002c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
13012c7af5baSBoris BREZILLON 
1302ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1303ab4382d2SGreg Kroah-Hartman }
1304ab4382d2SGreg Kroah-Hartman 
1305a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1306a930e528SElen Song {
1307a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1308a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1309a930e528SElen Song 
1310a930e528SElen Song 	dma_unmap_single(port->dev,
1311a930e528SElen Song 			 pdc->dma_addr,
1312a930e528SElen Song 			 pdc->dma_size,
1313a930e528SElen Song 			 DMA_TO_DEVICE);
1314a930e528SElen Song }
1315a930e528SElen Song 
1316ab4382d2SGreg Kroah-Hartman /*
1317ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1318ab4382d2SGreg Kroah-Hartman  */
131964e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1320ab4382d2SGreg Kroah-Hartman {
1321ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1322ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1323ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1324ab4382d2SGreg Kroah-Hartman 	int count;
1325ab4382d2SGreg Kroah-Hartman 
1326ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
13274e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1328ab4382d2SGreg Kroah-Hartman 		return;
1329ab4382d2SGreg Kroah-Hartman 
1330ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1331ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1332ab4382d2SGreg Kroah-Hartman 
1333ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1334ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1335ab4382d2SGreg Kroah-Hartman 
1336ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1337ab4382d2SGreg Kroah-Hartman 
1338ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
13394e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1340ab4382d2SGreg Kroah-Hartman 
1341ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1342ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1343ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1344ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1345ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1346ab4382d2SGreg Kroah-Hartman 
1347ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1348ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1349ab4382d2SGreg Kroah-Hartman 
13504e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13514e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13524e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1353ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13544e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1355ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13564e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13574e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1358ab4382d2SGreg Kroah-Hartman 	} else {
135913bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
136013bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1361ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1362ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1363ab4382d2SGreg Kroah-Hartman 		}
1364ab4382d2SGreg Kroah-Hartman 	}
1365ab4382d2SGreg Kroah-Hartman 
1366ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1367ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1368ab4382d2SGreg Kroah-Hartman }
1369ab4382d2SGreg Kroah-Hartman 
1370a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1371a930e528SElen Song {
1372a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1373a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1374a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1375a930e528SElen Song 
1376a930e528SElen Song 	pdc->buf = xmit->buf;
1377a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1378a930e528SElen Song 					pdc->buf,
1379a930e528SElen Song 					UART_XMIT_SIZE,
1380a930e528SElen Song 					DMA_TO_DEVICE);
1381a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1382a930e528SElen Song 	pdc->ofs = 0;
1383a930e528SElen Song 
1384a930e528SElen Song 	return 0;
1385a930e528SElen Song }
1386a930e528SElen Song 
1387ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1388ab4382d2SGreg Kroah-Hartman {
1389ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1390ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1391ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1392ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1393ab4382d2SGreg Kroah-Hartman 
1394ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1395ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1396ab4382d2SGreg Kroah-Hartman 
1397ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1398ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1399ab4382d2SGreg Kroah-Hartman 
1400ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1401ab4382d2SGreg Kroah-Hartman 
1402ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1403ab4382d2SGreg Kroah-Hartman 
1404ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1405ab4382d2SGreg Kroah-Hartman 		status = c.status;
1406ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1407ab4382d2SGreg Kroah-Hartman 
1408ab4382d2SGreg Kroah-Hartman 		/*
1409ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1410ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1411ab4382d2SGreg Kroah-Hartman 		 */
1412ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1413ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1414ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1415ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1416ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1417ab4382d2SGreg Kroah-Hartman 
1418ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1419ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1420ab4382d2SGreg Kroah-Hartman 					continue;
1421ab4382d2SGreg Kroah-Hartman 			}
1422ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1423ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1424ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1425ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1426ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1427ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1428ab4382d2SGreg Kroah-Hartman 
1429ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1430ab4382d2SGreg Kroah-Hartman 
1431ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1432ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1433ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1434ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1435ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1436ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1437ab4382d2SGreg Kroah-Hartman 		}
1438ab4382d2SGreg Kroah-Hartman 
1439ab4382d2SGreg Kroah-Hartman 
1440ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1441ab4382d2SGreg Kroah-Hartman 			continue;
1442ab4382d2SGreg Kroah-Hartman 
1443ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1444ab4382d2SGreg Kroah-Hartman 	}
1445ab4382d2SGreg Kroah-Hartman 
1446ab4382d2SGreg Kroah-Hartman 	/*
1447ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1448ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1449ab4382d2SGreg Kroah-Hartman 	 */
1450ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14512e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1452ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1453ab4382d2SGreg Kroah-Hartman }
1454ab4382d2SGreg Kroah-Hartman 
1455a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1456a930e528SElen Song {
1457a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1458a930e528SElen Song 	int i;
1459a930e528SElen Song 
1460a930e528SElen Song 	for (i = 0; i < 2; i++) {
1461a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1462a930e528SElen Song 
1463a930e528SElen Song 		dma_unmap_single(port->dev,
1464a930e528SElen Song 				 pdc->dma_addr,
1465a930e528SElen Song 				 pdc->dma_size,
1466a930e528SElen Song 				 DMA_FROM_DEVICE);
1467a930e528SElen Song 		kfree(pdc->buf);
1468a930e528SElen Song 	}
1469a930e528SElen Song }
1470a930e528SElen Song 
147164e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1472ab4382d2SGreg Kroah-Hartman {
1473ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
147405c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1475ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1476ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1477ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1478ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1479ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1480ab4382d2SGreg Kroah-Hartman 
1481ab4382d2SGreg Kroah-Hartman 	do {
1482ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
14834e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1484ab4382d2SGreg Kroah-Hartman 
1485ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
14864e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1487ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1488ab4382d2SGreg Kroah-Hartman 
1489ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1490ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1491ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1492ab4382d2SGreg Kroah-Hartman 		 * find out.
1493ab4382d2SGreg Kroah-Hartman 		 *
1494ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1495ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1496ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1497ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1498ab4382d2SGreg Kroah-Hartman 		 */
1499ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1500ab4382d2SGreg Kroah-Hartman 
1501ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1502ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1503ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1504ab4382d2SGreg Kroah-Hartman 
1505ab4382d2SGreg Kroah-Hartman 			/*
1506ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1507ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1508ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1509ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1510ab4382d2SGreg Kroah-Hartman 			 */
1511ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1512ab4382d2SGreg Kroah-Hartman 
151305c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
151405c7cd39SJiri Slaby 						count);
1515ab4382d2SGreg Kroah-Hartman 
1516ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1517ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1518ab4382d2SGreg Kroah-Hartman 
1519ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1520ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1521ab4382d2SGreg Kroah-Hartman 		}
1522ab4382d2SGreg Kroah-Hartman 
1523ab4382d2SGreg Kroah-Hartman 		/*
1524ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1525ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1526ab4382d2SGreg Kroah-Hartman 		 */
1527ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1528ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
15294e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
15304e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1531ab4382d2SGreg Kroah-Hartman 
1532ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1533ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1534ab4382d2SGreg Kroah-Hartman 		}
1535ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1536ab4382d2SGreg Kroah-Hartman 
1537ab4382d2SGreg Kroah-Hartman 	/*
1538ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1539ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1540ab4382d2SGreg Kroah-Hartman 	 */
1541ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15422e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1543ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1544ab4382d2SGreg Kroah-Hartman 
15454e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
15464e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1547ab4382d2SGreg Kroah-Hartman }
1548ab4382d2SGreg Kroah-Hartman 
1549a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1550a930e528SElen Song {
1551a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1552a930e528SElen Song 	int i;
1553a930e528SElen Song 
1554a930e528SElen Song 	for (i = 0; i < 2; i++) {
1555a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1556a930e528SElen Song 
1557a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1558a930e528SElen Song 		if (pdc->buf == NULL) {
1559a930e528SElen Song 			if (i != 0) {
1560a930e528SElen Song 				dma_unmap_single(port->dev,
1561a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1562a930e528SElen Song 					PDC_BUFFER_SIZE,
1563a930e528SElen Song 					DMA_FROM_DEVICE);
1564a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1565a930e528SElen Song 			}
1566a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1567a930e528SElen Song 			return -ENOMEM;
1568a930e528SElen Song 		}
1569a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1570a930e528SElen Song 						pdc->buf,
1571a930e528SElen Song 						PDC_BUFFER_SIZE,
1572a930e528SElen Song 						DMA_FROM_DEVICE);
1573a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1574a930e528SElen Song 		pdc->ofs = 0;
1575a930e528SElen Song 	}
1576a930e528SElen Song 
1577a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1578a930e528SElen Song 
15794e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
15804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1581a930e528SElen Song 
15824e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
15834e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
15844e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1585a930e528SElen Song 
1586a930e528SElen Song 	return 0;
1587a930e528SElen Song }
1588a930e528SElen Song 
1589ab4382d2SGreg Kroah-Hartman /*
1590ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1591ab4382d2SGreg Kroah-Hartman  */
159200e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data)
1593ab4382d2SGreg Kroah-Hartman {
1594ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1595ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1596ab4382d2SGreg Kroah-Hartman 
1597ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1598ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1599a930e528SElen Song 	atmel_port->schedule_rx(port);
160000e8e658SNicolas Ferre 	spin_unlock(&port->lock);
160100e8e658SNicolas Ferre }
1602ab4382d2SGreg Kroah-Hartman 
160300e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data)
160400e8e658SNicolas Ferre {
160500e8e658SNicolas Ferre 	struct uart_port *port = (struct uart_port *)data;
160600e8e658SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
160700e8e658SNicolas Ferre 
160800e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
160900e8e658SNicolas Ferre 	spin_lock(&port->lock);
161000e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1611ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1612ab4382d2SGreg Kroah-Hartman }
1613ab4382d2SGreg Kroah-Hartman 
16144a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
161533d64c4fSElen Song 				struct platform_device *pdev)
161633d64c4fSElen Song {
161733d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1618574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
161933d64c4fSElen Song 
162033d64c4fSElen Song 	if (np) {
162133d64c4fSElen Song 		/* DMA/PDC usage specification */
162233d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
162333d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
162433d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
162533d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
162633d64c4fSElen Song 			} else {
162733d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
162833d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
162933d64c4fSElen Song 			}
163033d64c4fSElen Song 		} else {
163133d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
163233d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
163333d64c4fSElen Song 		}
163433d64c4fSElen Song 
163533d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
163633d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
163733d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
163833d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
163933d64c4fSElen Song 			} else {
164033d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
164133d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
164233d64c4fSElen Song 			}
164333d64c4fSElen Song 		} else {
164433d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
164533d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
164633d64c4fSElen Song 		}
164733d64c4fSElen Song 
164833d64c4fSElen Song 	} else {
164933d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
165033d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
165133d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
165233d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
165333d64c4fSElen Song 	}
165433d64c4fSElen Song 
165533d64c4fSElen Song }
165633d64c4fSElen Song 
165713bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
165833d64c4fSElen Song 				struct platform_device *pdev)
165933d64c4fSElen Song {
166033d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1661574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
166233d64c4fSElen Song 
166333d64c4fSElen Song 	if (np) {
166477bdec6fSJiri Slaby 		struct serial_rs485 *rs485conf = &port->rs485;
166533d64c4fSElen Song 		u32 rs485_delay[2];
166633d64c4fSElen Song 		/* rs485 properties */
166733d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
166833d64c4fSElen Song 					rs485_delay, 2) == 0) {
166933d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
167033d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
167133d64c4fSElen Song 			rs485conf->flags = 0;
167277bdec6fSJiri Slaby 		}
167333d64c4fSElen Song 
167433d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
167533d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
167633d64c4fSElen Song 
167733d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
167833d64c4fSElen Song 								NULL))
167933d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
168033d64c4fSElen Song 	} else {
168113bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
168233d64c4fSElen Song 	}
168333d64c4fSElen Song 
168433d64c4fSElen Song }
168533d64c4fSElen Song 
1686a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1687a930e528SElen Song {
1688a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1689a930e528SElen Song 
169034df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
169134df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
169234df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
169334df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
169434df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1695a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1696a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1697a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1698a930e528SElen Song 	} else {
1699a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1700a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1701a930e528SElen Song 		atmel_port->release_rx = NULL;
1702a930e528SElen Song 	}
1703a930e528SElen Song 
170408f738beSElen Song 	if (atmel_use_dma_tx(port)) {
170508f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
170608f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
170708f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
170808f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1709a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1710a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1711a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1712a930e528SElen Song 	} else {
1713a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1714a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1715a930e528SElen Song 		atmel_port->release_tx = NULL;
1716a930e528SElen Song 	}
1717a930e528SElen Song }
1718a930e528SElen Song 
1719ab4382d2SGreg Kroah-Hartman /*
1720055560b0SElen Song  * Get ip name usart or uart
1721055560b0SElen Song  */
1722892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1723055560b0SElen Song {
1724055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
17254e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1726731d9caeSNicolas Ferre 	u32 version;
17271d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
17284b769371SNicolas Ferre 	/* ASCII decoding for IP version */
17294b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
17304b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
17311d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1732055560b0SElen Song 
17334b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
1734055560b0SElen Song 
17352958cceeSLudovic Desroches 	if (name == new_uart) {
17362958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
17374b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
17382958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
17392958cceeSLudovic Desroches 	} else if (name == usart) {
17402958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
17412958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
17422958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
17434b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
17444b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1745055560b0SElen Song 	} else {
1746731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17474e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1748731d9caeSNicolas Ferre 		switch (version) {
1749731d9caeSNicolas Ferre 		case 0x302:
1750731d9caeSNicolas Ferre 		case 0x10213:
1751731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
17524b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
17532958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1754731d9caeSNicolas Ferre 			break;
1755731d9caeSNicolas Ferre 		case 0x203:
1756731d9caeSNicolas Ferre 		case 0x10202:
1757731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1758731d9caeSNicolas Ferre 			break;
1759731d9caeSNicolas Ferre 		default:
1760731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1761731d9caeSNicolas Ferre 		}
1762055560b0SElen Song 	}
1763055560b0SElen Song }
1764055560b0SElen Song 
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 
179400e8e658SNicolas Ferre 	tasklet_enable(&atmel_port->tasklet_rx);
179500e8e658SNicolas Ferre 	tasklet_enable(&atmel_port->tasklet_tx);
17961e125786SLeilei Zhao 
1797ab5e4e41SRichard Genoud 	/*
1798ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1799ab4382d2SGreg Kroah-Hartman 	 */
180033d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
18014d9628a1SLeilei Zhao 	atmel_set_ops(port);
180233d64c4fSElen Song 
1803a930e528SElen Song 	if (atmel_port->prepare_rx) {
1804a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1805a930e528SElen Song 		if (retval < 0)
1806a930e528SElen Song 			atmel_set_ops(port);
1807ab4382d2SGreg Kroah-Hartman 	}
1808ab4382d2SGreg Kroah-Hartman 
1809a930e528SElen Song 	if (atmel_port->prepare_tx) {
1810a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1811a930e528SElen Song 		if (retval < 0)
1812a930e528SElen Song 			atmel_set_ops(port);
1813ab4382d2SGreg Kroah-Hartman 	}
1814ab4382d2SGreg Kroah-Hartman 
1815b5199d46SCyrille Pitchen 	/*
1816b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1817b5199d46SCyrille Pitchen 	 */
1818b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1819b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1820b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1821b5199d46SCyrille Pitchen 		unsigned int fmr;
1822b5199d46SCyrille Pitchen 
1823b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1824b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1825b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1826b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1827b5199d46SCyrille Pitchen 
18285f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
18295f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
18305f258b3eSCyrille Pitchen 
1831b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1832b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1833b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1834b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1835b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1836b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1837b5199d46SCyrille Pitchen 
1838b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1839b5199d46SCyrille Pitchen 	}
1840b5199d46SCyrille Pitchen 
1841ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1842e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1843ab4382d2SGreg Kroah-Hartman 
1844ab4382d2SGreg Kroah-Hartman 	/*
1845ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1846ab4382d2SGreg Kroah-Hartman 	 */
18474e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1848ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
18494e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1850ab4382d2SGreg Kroah-Hartman 
18512e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
18522e68c22fSElen Song 			atmel_uart_timer_callback,
18532e68c22fSElen Song 			(unsigned long)port);
18548bc661bfSMarek Roszko 
18558bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18568bc661bfSMarek Roszko 		/* set UART timeout */
18574b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18582e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18592e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18602e68c22fSElen Song 		/* set USART timeout */
18612e68c22fSElen Song 		} else {
18622958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18632958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18644e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1865ab4382d2SGreg Kroah-Hartman 
18664e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18674e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18682e68c22fSElen Song 		}
1869ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
18704e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
187134df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18722e68c22fSElen Song 		/* set UART timeout */
18734b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18742e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18752e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18762e68c22fSElen Song 		/* set USART timeout */
18772e68c22fSElen Song 		} else {
18782958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18792958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18804e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
188134df42f5SElen Song 
18824e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18834e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
18842e68c22fSElen Song 		}
1885ab4382d2SGreg Kroah-Hartman 	} else {
1886ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
18874e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1888ab4382d2SGreg Kroah-Hartman 	}
1889ab4382d2SGreg Kroah-Hartman 
1890ab4382d2SGreg Kroah-Hartman 	return 0;
1891ab4382d2SGreg Kroah-Hartman }
1892ab4382d2SGreg Kroah-Hartman 
1893ab4382d2SGreg Kroah-Hartman /*
1894479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1895479e9b94SPeter Hurley  * buffer is reset.
1896479e9b94SPeter Hurley  */
1897479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1898479e9b94SPeter Hurley {
1899479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1900479e9b94SPeter Hurley 
1901479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
19024e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1903479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1904479e9b94SPeter Hurley 	}
1905479e9b94SPeter Hurley }
1906479e9b94SPeter Hurley 
1907479e9b94SPeter Hurley /*
1908ab4382d2SGreg Kroah-Hartman  * Disable the port
1909ab4382d2SGreg Kroah-Hartman  */
1910ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1911ab4382d2SGreg Kroah-Hartman {
1912ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19130cc7c6c7SMarek Roszko 
1914ab4382d2SGreg Kroah-Hartman 	/*
19158bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19168bc661bfSMarek Roszko 	 * cleanup
19178bc661bfSMarek Roszko 	 */
19188bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19198bc661bfSMarek Roszko 
19208bc661bfSMarek Roszko 	/*
19210cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19220cc7c6c7SMarek Roszko 	 * we destroy the buffers
19230cc7c6c7SMarek Roszko 	 */
192400e8e658SNicolas Ferre 	tasklet_disable(&atmel_port->tasklet_rx);
192500e8e658SNicolas Ferre 	tasklet_disable(&atmel_port->tasklet_tx);
192600e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
192700e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
19280cc7c6c7SMarek Roszko 
19290cc7c6c7SMarek Roszko 	/*
19300cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
19310cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1932ab4382d2SGreg Kroah-Hartman 	 */
1933ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1934ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1935ab4382d2SGreg Kroah-Hartman 
19364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
19374e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
19380cc7c6c7SMarek Roszko 
19390cc7c6c7SMarek Roszko 
1940ab4382d2SGreg Kroah-Hartman 	/*
1941ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1942ab4382d2SGreg Kroah-Hartman 	 */
1943a930e528SElen Song 	if (atmel_port->release_rx)
1944a930e528SElen Song 		atmel_port->release_rx(port);
1945a930e528SElen Song 	if (atmel_port->release_tx)
1946a930e528SElen Song 		atmel_port->release_tx(port);
1947ab4382d2SGreg Kroah-Hartman 
1948ab4382d2SGreg Kroah-Hartman 	/*
1949bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1950bb7e73c5SMark Deneen 	 */
1951bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1952bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1953bb7e73c5SMark Deneen 
1954bb7e73c5SMark Deneen 	/*
1955ab5e4e41SRichard Genoud 	 * Free the interrupts
1956ab4382d2SGreg Kroah-Hartman 	 */
1957ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1958ab5e4e41SRichard Genoud 
1959ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1960ab4382d2SGreg Kroah-Hartman 
1961479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1962ab4382d2SGreg Kroah-Hartman }
1963ab4382d2SGreg Kroah-Hartman 
1964ab4382d2SGreg Kroah-Hartman /*
1965ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1966ab4382d2SGreg Kroah-Hartman  */
1967ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1968ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1969ab4382d2SGreg Kroah-Hartman {
1970ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1971ab4382d2SGreg Kroah-Hartman 
1972ab4382d2SGreg Kroah-Hartman 	switch (state) {
1973ab4382d2SGreg Kroah-Hartman 	case 0:
1974ab4382d2SGreg Kroah-Hartman 		/*
1975ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1976ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1977ab4382d2SGreg Kroah-Hartman 		 */
197891f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1979ab4382d2SGreg Kroah-Hartman 
1980ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
19814e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
1982ab4382d2SGreg Kroah-Hartman 		break;
1983ab4382d2SGreg Kroah-Hartman 	case 3:
1984ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
19854e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
19864e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
1987ab4382d2SGreg Kroah-Hartman 
1988ab4382d2SGreg Kroah-Hartman 		/*
1989ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1990ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1991ab4382d2SGreg Kroah-Hartman 		 */
199291f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1993ab4382d2SGreg Kroah-Hartman 		break;
1994ab4382d2SGreg Kroah-Hartman 	default:
1995ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1996ab4382d2SGreg Kroah-Hartman 	}
1997ab4382d2SGreg Kroah-Hartman }
1998ab4382d2SGreg Kroah-Hartman 
1999ab4382d2SGreg Kroah-Hartman /*
2000ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2001ab4382d2SGreg Kroah-Hartman  */
2002ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2003ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2004ab4382d2SGreg Kroah-Hartman {
2005ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
20061cf6e8fcSCyrille Pitchen 	unsigned int old_mode, mode, imr, quot, baud;
2007ab4382d2SGreg Kroah-Hartman 
20081cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20094e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20101cf6e8fcSCyrille Pitchen 
20111cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20121cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20131cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2014ab4382d2SGreg Kroah-Hartman 
2015ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2016ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
2017ab4382d2SGreg Kroah-Hartman 
2018ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
2019ab4382d2SGreg Kroah-Hartman 		quot /= 8;
2020ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
2021ab4382d2SGreg Kroah-Hartman 	}
2022ab4382d2SGreg Kroah-Hartman 
2023ab4382d2SGreg Kroah-Hartman 	/* byte size */
2024ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2025ab4382d2SGreg Kroah-Hartman 	case CS5:
2026ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2027ab4382d2SGreg Kroah-Hartman 		break;
2028ab4382d2SGreg Kroah-Hartman 	case CS6:
2029ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2030ab4382d2SGreg Kroah-Hartman 		break;
2031ab4382d2SGreg Kroah-Hartman 	case CS7:
2032ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2033ab4382d2SGreg Kroah-Hartman 		break;
2034ab4382d2SGreg Kroah-Hartman 	default:
2035ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2036ab4382d2SGreg Kroah-Hartman 		break;
2037ab4382d2SGreg Kroah-Hartman 	}
2038ab4382d2SGreg Kroah-Hartman 
2039ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2040ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2041ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2042ab4382d2SGreg Kroah-Hartman 
2043ab4382d2SGreg Kroah-Hartman 	/* parity */
2044ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2045ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2046ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2047ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2048ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2049ab4382d2SGreg Kroah-Hartman 			else
2050ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2051ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2052ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2053ab4382d2SGreg Kroah-Hartman 		else
2054ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2055ab4382d2SGreg Kroah-Hartman 	} else
2056ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2057ab4382d2SGreg Kroah-Hartman 
2058ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2059ab4382d2SGreg Kroah-Hartman 
2060ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2061ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2062ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2063ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2064ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2065ab4382d2SGreg Kroah-Hartman 
206664e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2067ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
20684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2069ab4382d2SGreg Kroah-Hartman 
2070ab4382d2SGreg Kroah-Hartman 	/*
2071ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2072ab4382d2SGreg Kroah-Hartman 	 */
2073ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2074ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2075ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2076ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2077ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2078ab4382d2SGreg Kroah-Hartman 		/*
2079ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2080ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2081ab4382d2SGreg Kroah-Hartman 		 */
2082ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2083ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2084ab4382d2SGreg Kroah-Hartman 	}
2085ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2086ab4382d2SGreg Kroah-Hartman 
2087ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2088ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2089ab4382d2SGreg Kroah-Hartman 
2090ab4382d2SGreg Kroah-Hartman 	/*
2091ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2092ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2093ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2094ab4382d2SGreg Kroah-Hartman 	 */
20954e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
20964e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2097ab4382d2SGreg Kroah-Hartman 
2098ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
20994e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2100ab4382d2SGreg Kroah-Hartman 
21011cf6e8fcSCyrille Pitchen 	/* mode */
210213bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
21034e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
21044e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2105ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
21061cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
21071cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
21085be605acSAlexandre Belloni 		if (atmel_use_dma_rx(port) && !atmel_use_fifo(port)) {
21095be605acSAlexandre Belloni 			dev_info(port->dev, "not enabling hardware flow control because DMA is used");
21105be605acSAlexandre Belloni 			termios->c_cflag &= ~CRTSCTS;
21115be605acSAlexandre Belloni 		} else {
21121cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
21135be605acSAlexandre Belloni 		}
21141cf6e8fcSCyrille Pitchen 	} else {
21151cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21161cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2117ab4382d2SGreg Kroah-Hartman 	}
2118ab4382d2SGreg Kroah-Hartman 
21191cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21204e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2121ab4382d2SGreg Kroah-Hartman 
21221cf6e8fcSCyrille Pitchen 	/*
21231cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21241cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21251cf6e8fcSCyrille Pitchen 	 */
21261cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21271cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21281cf6e8fcSCyrille Pitchen 
21291cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21301cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21311cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21321cf6e8fcSCyrille Pitchen 		} else {
21331cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21341cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21351cf6e8fcSCyrille Pitchen 		}
21361cf6e8fcSCyrille Pitchen 
21374e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21381cf6e8fcSCyrille Pitchen 	}
21391cf6e8fcSCyrille Pitchen 
2140ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
21414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
21424e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
21434e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2144ab4382d2SGreg Kroah-Hartman 
2145ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
21464e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2147ab4382d2SGreg Kroah-Hartman 
2148ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2149ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
215035b675b9SRichard Genoud 		atmel_enable_ms(port);
215135b675b9SRichard Genoud 	else
215235b675b9SRichard Genoud 		atmel_disable_ms(port);
2153ab4382d2SGreg Kroah-Hartman 
2154ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2155ab4382d2SGreg Kroah-Hartman }
2156ab4382d2SGreg Kroah-Hartman 
2157732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
215842bd7a4fSViktar Palstsiuk {
2159732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
216042bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2161d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
216242bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2163d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
216442bd7a4fSViktar Palstsiuk 	} else {
216542bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2166cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2167cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2168cab68f89SPeter Hurley 			atmel_disable_ms(port);
2169cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2170cab68f89SPeter Hurley 		}
217142bd7a4fSViktar Palstsiuk 	}
217242bd7a4fSViktar Palstsiuk }
217342bd7a4fSViktar Palstsiuk 
2174ab4382d2SGreg Kroah-Hartman /*
2175ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2176ab4382d2SGreg Kroah-Hartman  */
2177ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2178ab4382d2SGreg Kroah-Hartman {
2179ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2180ab4382d2SGreg Kroah-Hartman }
2181ab4382d2SGreg Kroah-Hartman 
2182ab4382d2SGreg Kroah-Hartman /*
2183ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2184ab4382d2SGreg Kroah-Hartman  */
2185ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2186ab4382d2SGreg Kroah-Hartman {
2187ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2188ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2189ab4382d2SGreg Kroah-Hartman 
2190ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2191ab4382d2SGreg Kroah-Hartman 
2192ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2193ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2194ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2195ab4382d2SGreg Kroah-Hartman 	}
2196ab4382d2SGreg Kroah-Hartman }
2197ab4382d2SGreg Kroah-Hartman 
2198ab4382d2SGreg Kroah-Hartman /*
2199ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2200ab4382d2SGreg Kroah-Hartman  */
2201ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2202ab4382d2SGreg Kroah-Hartman {
2203ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2204ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2205ab4382d2SGreg Kroah-Hartman 
2206ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2207ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2208ab4382d2SGreg Kroah-Hartman 
2209ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2210ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2211ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2212ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2213ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2214ab4382d2SGreg Kroah-Hartman 		}
2215ab4382d2SGreg Kroah-Hartman 	}
2216ab4382d2SGreg Kroah-Hartman 
2217ab4382d2SGreg Kroah-Hartman 	return 0;
2218ab4382d2SGreg Kroah-Hartman }
2219ab4382d2SGreg Kroah-Hartman 
2220ab4382d2SGreg Kroah-Hartman /*
2221ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2222ab4382d2SGreg Kroah-Hartman  */
2223ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2224ab4382d2SGreg Kroah-Hartman {
2225ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2226ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2227ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2228ab4382d2SGreg Kroah-Hartman 	}
2229ab4382d2SGreg Kroah-Hartman }
2230ab4382d2SGreg Kroah-Hartman 
2231ab4382d2SGreg Kroah-Hartman /*
2232ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2233ab4382d2SGreg Kroah-Hartman  */
2234ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2235ab4382d2SGreg Kroah-Hartman {
2236ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2237ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2238ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2239ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2240ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2241ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2242ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2243ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2244ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2245270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2246ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2247ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2248ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2249ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2250ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2251ab4382d2SGreg Kroah-Hartman 	return ret;
2252ab4382d2SGreg Kroah-Hartman }
2253ab4382d2SGreg Kroah-Hartman 
2254ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2255ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2256ab4382d2SGreg Kroah-Hartman {
22574e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2258ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2259ab4382d2SGreg Kroah-Hartman 
2260a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2261ab4382d2SGreg Kroah-Hartman }
2262ab4382d2SGreg Kroah-Hartman 
2263ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2264ab4382d2SGreg Kroah-Hartman {
22654e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2266ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2267ab4382d2SGreg Kroah-Hartman 
2268a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2269ab4382d2SGreg Kroah-Hartman }
2270ab4382d2SGreg Kroah-Hartman #endif
2271ab4382d2SGreg Kroah-Hartman 
2272ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2273ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2274ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2275ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2276ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2277ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2278ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2279ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2280ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2281ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2282ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2283ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2284ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
228542bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2286ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2287ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2288ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2289ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2290ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2291ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2292ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2293ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2294ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2295ab4382d2SGreg Kroah-Hartman #endif
2296ab4382d2SGreg Kroah-Hartman };
2297ab4382d2SGreg Kroah-Hartman 
2298ab4382d2SGreg Kroah-Hartman /*
2299ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2300ab4382d2SGreg Kroah-Hartman  */
230191f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2302ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2303ab4382d2SGreg Kroah-Hartman {
230491f8c2d8SBoris BREZILLON 	int ret;
2305ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2306574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2307ab4382d2SGreg Kroah-Hartman 
23084a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2309a930e528SElen Song 	atmel_set_ops(port);
2310a930e528SElen Song 
231113bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
231233d64c4fSElen Song 
2313ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2314ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2315ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2316ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2317ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2318ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2319ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
232013bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2321ab4382d2SGreg Kroah-Hartman 
232200e8e658SNicolas Ferre 	tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
2323ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
232400e8e658SNicolas Ferre 	tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
232500e8e658SNicolas Ferre 			(unsigned long)port);
232600e8e658SNicolas Ferre 	tasklet_disable(&atmel_port->tasklet_rx);
232700e8e658SNicolas Ferre 	tasklet_disable(&atmel_port->tasklet_tx);
2328ab4382d2SGreg Kroah-Hartman 
2329ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2330ab4382d2SGreg Kroah-Hartman 
23315fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2332ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
23331acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2334588edbf3SNicolas Ferre 	} else {
2335ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2336ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2337ab4382d2SGreg Kroah-Hartman 	}
2338ab4382d2SGreg Kroah-Hartman 
2339ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2340ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2341ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
234291f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
234391f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
234491f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
234591f8c2d8SBoris BREZILLON 			return ret;
234691f8c2d8SBoris BREZILLON 		}
234791f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
234891f8c2d8SBoris BREZILLON 		if (ret) {
234991f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
235091f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
235191f8c2d8SBoris BREZILLON 			return ret;
235291f8c2d8SBoris BREZILLON 		}
2353ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
235491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2355ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2356ab4382d2SGreg Kroah-Hartman 	}
2357ab4382d2SGreg Kroah-Hartman 
2358ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
235913bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2360ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
236164e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2362ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2363ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2364ab4382d2SGreg Kroah-Hartman 	} else {
2365ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2366ab4382d2SGreg Kroah-Hartman 	}
236791f8c2d8SBoris BREZILLON 
236891f8c2d8SBoris BREZILLON 	return 0;
2369ab4382d2SGreg Kroah-Hartman }
2370ab4382d2SGreg Kroah-Hartman 
237169f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
237269f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2373ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2374ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2375ab4382d2SGreg Kroah-Hartman {
23764e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2377ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2378a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2379ab4382d2SGreg Kroah-Hartman }
2380ab4382d2SGreg Kroah-Hartman 
2381ab4382d2SGreg Kroah-Hartman /*
2382ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2383ab4382d2SGreg Kroah-Hartman  */
2384ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2385ab4382d2SGreg Kroah-Hartman {
2386ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2387ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2388ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2389ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2390ab4382d2SGreg Kroah-Hartman 
2391ab4382d2SGreg Kroah-Hartman 	/*
2392ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2393ab4382d2SGreg Kroah-Hartman 	 */
23944e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
23954e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
23964e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2397ab4382d2SGreg Kroah-Hartman 
2398ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
23994e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
24004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2401ab4382d2SGreg Kroah-Hartman 
2402ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2403ab4382d2SGreg Kroah-Hartman 
2404ab4382d2SGreg Kroah-Hartman 	/*
2405ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2406ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2407ab4382d2SGreg Kroah-Hartman 	 */
2408ab4382d2SGreg Kroah-Hartman 	do {
24094e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2410ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2411ab4382d2SGreg Kroah-Hartman 
2412ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2413ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24144e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2415ab4382d2SGreg Kroah-Hartman 
2416ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24174e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2418ab4382d2SGreg Kroah-Hartman }
2419ab4382d2SGreg Kroah-Hartman 
2420ab4382d2SGreg Kroah-Hartman /*
2421ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2422ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2423ab4382d2SGreg Kroah-Hartman  */
2424ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2425ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2426ab4382d2SGreg Kroah-Hartman {
2427ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2428ab4382d2SGreg Kroah-Hartman 
2429ab4382d2SGreg Kroah-Hartman 	/*
2430ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2431ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2432ab4382d2SGreg Kroah-Hartman 	 */
24334e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2434ab4382d2SGreg Kroah-Hartman 	if (!quot)
2435ab4382d2SGreg Kroah-Hartman 		return;
2436ab4382d2SGreg Kroah-Hartman 
24374e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2438ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2439ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2440ab4382d2SGreg Kroah-Hartman 	else
2441ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2442ab4382d2SGreg Kroah-Hartman 
24434e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2444ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2445ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2446ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2447ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2448ab4382d2SGreg Kroah-Hartman 
2449ab4382d2SGreg Kroah-Hartman 	/*
2450ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2451ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2452ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2453ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2454ab4382d2SGreg Kroah-Hartman 	 */
2455ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2456ab4382d2SGreg Kroah-Hartman }
2457ab4382d2SGreg Kroah-Hartman 
2458ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2459ab4382d2SGreg Kroah-Hartman {
246091f8c2d8SBoris BREZILLON 	int ret;
2461ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2462ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2463ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2464ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2465ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2466ab4382d2SGreg Kroah-Hartman 
2467ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2468ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2469ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2470ab4382d2SGreg Kroah-Hartman 	}
2471ab4382d2SGreg Kroah-Hartman 
247291f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
247391f8c2d8SBoris BREZILLON 	if (ret)
247491f8c2d8SBoris BREZILLON 		return ret;
2475ab4382d2SGreg Kroah-Hartman 
24764e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
24774e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
24784e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2479ab4382d2SGreg Kroah-Hartman 
2480ab4382d2SGreg Kroah-Hartman 	if (options)
2481ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2482ab4382d2SGreg Kroah-Hartman 	else
2483ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2484ab4382d2SGreg Kroah-Hartman 
2485ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2486ab4382d2SGreg Kroah-Hartman }
2487ab4382d2SGreg Kroah-Hartman 
2488ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2489ab4382d2SGreg Kroah-Hartman 
2490ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2491ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2492ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2493ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2494ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2495ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2496ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2497ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2498ab4382d2SGreg Kroah-Hartman };
2499ab4382d2SGreg Kroah-Hartman 
2500ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2501ab4382d2SGreg Kroah-Hartman 
2502ab4382d2SGreg Kroah-Hartman /*
2503ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2504ab4382d2SGreg Kroah-Hartman  */
2505ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2506ab4382d2SGreg Kroah-Hartman {
250791f8c2d8SBoris BREZILLON 	int ret;
2508ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
25090d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2510574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2511efb8d21bSLinus Torvalds 		int id = pdata->num;
2512b78cd169SJaeden Amero 		struct atmel_uart_port *atmel_port = &atmel_ports[id];
25130d0a3cc1SVoss, Nikolaus 
2514b78cd169SJaeden Amero 		atmel_port->backup_imr = 0;
2515b78cd169SJaeden Amero 		atmel_port->uart.line = id;
25164cbf9f48SNicolas Ferre 
25174cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
2518b78cd169SJaeden Amero 		ret = atmel_init_port(atmel_port, atmel_default_console_device);
251991f8c2d8SBoris BREZILLON 		if (ret)
252091f8c2d8SBoris BREZILLON 			return ret;
2521ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2522ab4382d2SGreg Kroah-Hartman 	}
2523ab4382d2SGreg Kroah-Hartman 
2524ab4382d2SGreg Kroah-Hartman 	return 0;
2525ab4382d2SGreg Kroah-Hartman }
2526ab4382d2SGreg Kroah-Hartman 
2527ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2528ab4382d2SGreg Kroah-Hartman 
2529ab4382d2SGreg Kroah-Hartman /*
2530ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2531ab4382d2SGreg Kroah-Hartman  */
2532ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2533ab4382d2SGreg Kroah-Hartman {
2534ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2535ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2536ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2537ab4382d2SGreg Kroah-Hartman 
2538ab4382d2SGreg Kroah-Hartman 	return 0;
2539ab4382d2SGreg Kroah-Hartman }
2540ab4382d2SGreg Kroah-Hartman 
2541ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2542ab4382d2SGreg Kroah-Hartman 
2543ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2544ab4382d2SGreg Kroah-Hartman {
2545ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2546ab4382d2SGreg Kroah-Hartman }
2547ab4382d2SGreg Kroah-Hartman 
2548ab4382d2SGreg Kroah-Hartman #else
2549ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2550ab4382d2SGreg Kroah-Hartman 
2551ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2552ab4382d2SGreg Kroah-Hartman {
2553ab4382d2SGreg Kroah-Hartman 	return false;
2554ab4382d2SGreg Kroah-Hartman }
2555ab4382d2SGreg Kroah-Hartman #endif
2556ab4382d2SGreg Kroah-Hartman 
2557ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2558ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2559ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2560ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2561ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2562ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2563ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2564ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2565ab4382d2SGreg Kroah-Hartman };
2566ab4382d2SGreg Kroah-Hartman 
2567ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2568ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2569ab4382d2SGreg Kroah-Hartman {
2570ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2571ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2572ab4382d2SGreg Kroah-Hartman #else
2573ab4382d2SGreg Kroah-Hartman 	return false;
2574ab4382d2SGreg Kroah-Hartman #endif
2575ab4382d2SGreg Kroah-Hartman }
2576ab4382d2SGreg Kroah-Hartman 
2577ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2578ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2579ab4382d2SGreg Kroah-Hartman {
2580ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2581ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2582ab4382d2SGreg Kroah-Hartman 
2583ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2584ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
25854e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
25864e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2587ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2588ab4382d2SGreg Kroah-Hartman 	}
2589ab4382d2SGreg Kroah-Hartman 
2590ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2591ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
25922c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
25932c7af5baSBoris BREZILLON 		unsigned long flags;
25942c7af5baSBoris BREZILLON 
25952c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25962c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
25972c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2598ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
25992c7af5baSBoris BREZILLON 	}
2600ab4382d2SGreg Kroah-Hartman 
2601ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2602ab4382d2SGreg Kroah-Hartman 
2603ab4382d2SGreg Kroah-Hartman 	return 0;
2604ab4382d2SGreg Kroah-Hartman }
2605ab4382d2SGreg Kroah-Hartman 
2606ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2607ab4382d2SGreg Kroah-Hartman {
2608ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2609ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26102c7af5baSBoris BREZILLON 	unsigned long flags;
26112c7af5baSBoris BREZILLON 
26122c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26132c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26142c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26152c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26162c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26172c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26182c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26192c7af5baSBoris BREZILLON 	}
26202c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26212c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2622ab4382d2SGreg Kroah-Hartman 
2623ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2624ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2625ab4382d2SGreg Kroah-Hartman 
2626ab4382d2SGreg Kroah-Hartman 	return 0;
2627ab4382d2SGreg Kroah-Hartman }
2628ab4382d2SGreg Kroah-Hartman #else
2629ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2630ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2631ab4382d2SGreg Kroah-Hartman #endif
2632ab4382d2SGreg Kroah-Hartman 
2633b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2634b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2635b5199d46SCyrille Pitchen {
2636b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2637b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2638b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2639b5199d46SCyrille Pitchen 
2640b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2641b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2642b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2643b5199d46SCyrille Pitchen 		return;
2644b5199d46SCyrille Pitchen 
2645b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2646b5199d46SCyrille Pitchen 		return;
2647b5199d46SCyrille Pitchen 
2648b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2649b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2650b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2651b5199d46SCyrille Pitchen 		return;
2652b5199d46SCyrille Pitchen 	}
2653b5199d46SCyrille Pitchen 
2654b5199d46SCyrille Pitchen 	/*
2655b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2656b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2657b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2658b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2659b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2660b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2661b5199d46SCyrille Pitchen 	 */
2662b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2663b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2664b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2665b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2666b5199d46SCyrille Pitchen 
2667b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2668b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2669b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2670b78cd169SJaeden Amero 		atmel_port->rts_high);
2671b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2672b78cd169SJaeden Amero 		atmel_port->rts_low);
2673b5199d46SCyrille Pitchen }
2674b5199d46SCyrille Pitchen 
26759671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2676ab4382d2SGreg Kroah-Hartman {
2677b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
26785fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2679574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2680ab4382d2SGreg Kroah-Hartman 	void *data;
26814cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2682bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2683ab4382d2SGreg Kroah-Hartman 
2684ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2685ab4382d2SGreg Kroah-Hartman 
26865fbe46b6SNicolas Ferre 	if (np)
26875fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
26885fbe46b6SNicolas Ferre 	else
26894cbf9f48SNicolas Ferre 		if (pdata)
26904cbf9f48SNicolas Ferre 			ret = pdata->num;
26914cbf9f48SNicolas Ferre 
26924cbf9f48SNicolas Ferre 	if (ret < 0)
26935fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
26944cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2695503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
26964cbf9f48SNicolas Ferre 
2697503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
26984cbf9f48SNicolas Ferre 		ret = -ENODEV;
26994cbf9f48SNicolas Ferre 		goto err;
27004cbf9f48SNicolas Ferre 	}
27014cbf9f48SNicolas Ferre 
2702503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
27034cbf9f48SNicolas Ferre 		/* port already in use */
27044cbf9f48SNicolas Ferre 		ret = -EBUSY;
27054cbf9f48SNicolas Ferre 		goto err;
27064cbf9f48SNicolas Ferre 	}
27074cbf9f48SNicolas Ferre 
2708b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2709b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2710b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2711b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2712354e57f3SLinus Walleij 
2713b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
27142c7af5baSBoris BREZILLON 
2715b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
271691f8c2d8SBoris BREZILLON 	if (ret)
27176fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2718ab4382d2SGreg Kroah-Hartman 
2719b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2720b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2721b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
272218dfef9cSUwe Kleine-König 		goto err_clear_bit;
272318dfef9cSUwe Kleine-König 	}
272418dfef9cSUwe Kleine-König 
2725b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2726ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2727ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2728ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2729ab4382d2SGreg Kroah-Hartman 		if (!data)
2730ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2731b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2732ab4382d2SGreg Kroah-Hartman 	}
2733ab4382d2SGreg Kroah-Hartman 
2734b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2735bd737f87SRicardo Ribalda Delgado 
2736b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2737ab4382d2SGreg Kroah-Hartman 	if (ret)
2738ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2739ab4382d2SGreg Kroah-Hartman 
2740ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2741b78cd169SJaeden Amero 	if (atmel_is_console_port(&atmel_port->uart)
2742ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2743ab4382d2SGreg Kroah-Hartman 		/*
2744ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
274591f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2746ab4382d2SGreg Kroah-Hartman 		 */
2747b78cd169SJaeden Amero 		clk_disable_unprepare(atmel_port->clk);
2748ab4382d2SGreg Kroah-Hartman 	}
2749ab4382d2SGreg Kroah-Hartman #endif
2750ab4382d2SGreg Kroah-Hartman 
2751ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2752b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2753ab4382d2SGreg Kroah-Hartman 
2754d4f64187SCyrille Pitchen 	/*
2755d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2756d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2757d4f64187SCyrille Pitchen 	 */
2758b78cd169SJaeden Amero 	clk_prepare_enable(atmel_port->clk);
2759d4f64187SCyrille Pitchen 
2760bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2761b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
27624e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2763b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2764b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2765fc887b15SLinus Torvalds 	}
2766fc887b15SLinus Torvalds 
2767055560b0SElen Song 	/*
2768055560b0SElen Song 	 * Get port name of usart or uart
2769055560b0SElen Song 	 */
2770b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2771055560b0SElen Song 
2772d4f64187SCyrille Pitchen 	/*
2773d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2774d4f64187SCyrille Pitchen 	 * is used
2775d4f64187SCyrille Pitchen 	 */
2776b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2777d4f64187SCyrille Pitchen 
2778ab4382d2SGreg Kroah-Hartman 	return 0;
2779ab4382d2SGreg Kroah-Hartman 
2780ab4382d2SGreg Kroah-Hartman err_add_port:
2781b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2782b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
2783ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2784b78cd169SJaeden Amero 	if (!atmel_is_console_port(&atmel_port->uart)) {
2785b78cd169SJaeden Amero 		clk_put(atmel_port->clk);
2786b78cd169SJaeden Amero 		atmel_port->clk = NULL;
2787ab4382d2SGreg Kroah-Hartman 	}
27886fbb9bdfSCyrille Pitchen err_clear_bit:
2789b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
27904cbf9f48SNicolas Ferre err:
2791ab4382d2SGreg Kroah-Hartman 	return ret;
2792ab4382d2SGreg Kroah-Hartman }
2793ab4382d2SGreg Kroah-Hartman 
2794f4a8ab04SRomain Izard /*
2795f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2796f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2797f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2798f4a8ab04SRomain Izard  *
2799f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
2800f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
2801f4a8ab04SRomain Izard  * port in the normal case.
2802f4a8ab04SRomain Izard  */
2803f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
2804f4a8ab04SRomain Izard {
2805f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
2806f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2807f4a8ab04SRomain Izard 	int ret = 0;
2808f4a8ab04SRomain Izard 
280900e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
281000e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
2811f4a8ab04SRomain Izard 
2812f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
2813f4a8ab04SRomain Izard 
2814f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
2815f4a8ab04SRomain Izard 
2816f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
2817f4a8ab04SRomain Izard 
2818f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
2819f4a8ab04SRomain Izard 
2820f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
2821f4a8ab04SRomain Izard 
2822f4a8ab04SRomain Izard 	clk_put(atmel_port->clk);
2823f4a8ab04SRomain Izard 	atmel_port->clk = NULL;
2824f4a8ab04SRomain Izard 
2825f4a8ab04SRomain Izard 	return ret;
2826f4a8ab04SRomain Izard }
2827f4a8ab04SRomain Izard 
2828ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2829ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
2830f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
2831ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2832ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2833ab4382d2SGreg Kroah-Hartman 	.driver		= {
2834ab4382d2SGreg Kroah-Hartman 		.name			= "atmel_usart",
28355fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
2836ab4382d2SGreg Kroah-Hartman 	},
2837ab4382d2SGreg Kroah-Hartman };
2838ab4382d2SGreg Kroah-Hartman 
2839ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2840ab4382d2SGreg Kroah-Hartman {
2841ab4382d2SGreg Kroah-Hartman 	int ret;
2842ab4382d2SGreg Kroah-Hartman 
2843ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2844ab4382d2SGreg Kroah-Hartman 	if (ret)
2845ab4382d2SGreg Kroah-Hartman 		return ret;
2846ab4382d2SGreg Kroah-Hartman 
2847ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2848ab4382d2SGreg Kroah-Hartman 	if (ret)
2849ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2850ab4382d2SGreg Kroah-Hartman 
2851ab4382d2SGreg Kroah-Hartman 	return ret;
2852ab4382d2SGreg Kroah-Hartman }
2853c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
2854