xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision ddaa603739fea273ca46d71bd1ff8fa06b8b30c6)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  Driver for Atmel AT91 / AT32 Serial ports
3ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7ab4382d2SGreg Kroah-Hartman  *
8ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
11ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
12ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
13ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
14ab4382d2SGreg Kroah-Hartman  *
15ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
16ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
19ab4382d2SGreg Kroah-Hartman  *
20ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
21ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
22ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23ab4382d2SGreg Kroah-Hartman  *
24ab4382d2SGreg Kroah-Hartman  */
25ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
35ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
365fbe46b6SNicolas Ferre #include <linux/of.h>
375fbe46b6SNicolas Ferre #include <linux/of_device.h>
38ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
39ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
40ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
42bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
432e68c22fSElen Song #include <linux/timer.h>
44ab4382d2SGreg Kroah-Hartman 
45ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
46ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
47ab4382d2SGreg Kroah-Hartman 
48ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARM
49ab4382d2SGreg Kroah-Hartman #include <mach/cpu.h>
5060e8972dSRussell King #include <asm/gpio.h>
51ab4382d2SGreg Kroah-Hartman #endif
52ab4382d2SGreg Kroah-Hartman 
53ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
54ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
55ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
56ab4382d2SGreg Kroah-Hartman 
57ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
58ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
59ab4382d2SGreg Kroah-Hartman #endif
60ab4382d2SGreg Kroah-Hartman 
61ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
62ab4382d2SGreg Kroah-Hartman 
63ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
64ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
65ab4382d2SGreg Kroah-Hartman 
66ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
67ab4382d2SGreg Kroah-Hartman 
68ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
69ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
70ab4382d2SGreg Kroah-Hartman  * UART. */
71ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
72ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
73ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
74ab4382d2SGreg Kroah-Hartman 
75ab4382d2SGreg Kroah-Hartman #else
76ab4382d2SGreg Kroah-Hartman 
77ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
78ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
79ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
80ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
81ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
82ab4382d2SGreg Kroah-Hartman 
83ab4382d2SGreg Kroah-Hartman #endif
84ab4382d2SGreg Kroah-Hartman 
85ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
86ab4382d2SGreg Kroah-Hartman 
87ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */
88ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_CR)
89ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port)	__raw_readl((port)->membase + ATMEL_US_MR)
90ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_MR)
91ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IER)
92ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IDR)
93ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port)	__raw_readl((port)->membase + ATMEL_US_IMR)
94ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port)	__raw_readl((port)->membase + ATMEL_US_CSR)
95ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port)	__raw_readl((port)->membase + ATMEL_US_RHR)
96ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_THR)
97ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port)	__raw_readl((port)->membase + ATMEL_US_BRGR)
98ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_BRGR)
99ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_RTOR)
100ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v)	__raw_writel(v, (port)->membase + ATMEL_US_TTGR)
101055560b0SElen Song #define UART_GET_IP_NAME(port)	__raw_readl((port)->membase + ATMEL_US_NAME)
102731d9caeSNicolas Ferre #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
103ab4382d2SGreg Kroah-Hartman 
104ab4382d2SGreg Kroah-Hartman  /* PDC registers */
105ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
106ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port)	__raw_readl((port)->membase + ATMEL_PDC_PTSR)
107ab4382d2SGreg Kroah-Hartman 
108ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
109ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port)	__raw_readl((port)->membase + ATMEL_PDC_RPR)
110ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
111ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
112ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
113ab4382d2SGreg Kroah-Hartman 
114ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
115ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
116ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port)	__raw_readl((port)->membase + ATMEL_PDC_TCR)
117ab4382d2SGreg Kroah-Hartman 
118ab4382d2SGreg Kroah-Hartman static int (*atmel_open_hook)(struct uart_port *);
119ab4382d2SGreg Kroah-Hartman static void (*atmel_close_hook)(struct uart_port *);
120ab4382d2SGreg Kroah-Hartman 
121ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
122ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
123ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
124ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
125ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
126ab4382d2SGreg Kroah-Hartman };
127ab4382d2SGreg Kroah-Hartman 
128ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
129ab4382d2SGreg Kroah-Hartman 	u16		status;
130ab4382d2SGreg Kroah-Hartman 	u16		ch;
131ab4382d2SGreg Kroah-Hartman };
132ab4382d2SGreg Kroah-Hartman 
133ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
134ab4382d2SGreg Kroah-Hartman 
135ab4382d2SGreg Kroah-Hartman /*
136ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
137ab4382d2SGreg Kroah-Hartman  */
138ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
139ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
140ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
141ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
142ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
143ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
144ab4382d2SGreg Kroah-Hartman 
14534df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
14664e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
147ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
148ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
149ab4382d2SGreg Kroah-Hartman 
15008f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
15164e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
152ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
153ab4382d2SGreg Kroah-Hartman 
15408f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
15534df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
15608f738beSElen Song 	struct dma_chan			*chan_tx;
15734df42f5SElen Song 	struct dma_chan			*chan_rx;
15808f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
15934df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
16008f738beSElen Song 	dma_cookie_t			cookie_tx;
16134df42f5SElen Song 	dma_cookie_t			cookie_rx;
16208f738beSElen Song 	struct scatterlist		sg_tx;
16334df42f5SElen Song 	struct scatterlist		sg_rx;
164ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
165ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
166ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
167ab4382d2SGreg Kroah-Hartman 
168ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
169ab4382d2SGreg Kroah-Hartman 
170ab4382d2SGreg Kroah-Hartman 	struct serial_rs485	rs485;		/* rs485 settings */
171ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
172055560b0SElen Song 	bool			is_usart;	/* usart or uart */
1732e68c22fSElen Song 	struct timer_list	uart_timer;	/* uart timer */
174a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
175a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
176a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
177a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
178a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
179a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
180ab4382d2SGreg Kroah-Hartman };
181ab4382d2SGreg Kroah-Hartman 
182ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
183503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
184ab4382d2SGreg Kroah-Hartman 
185ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
186ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
187ab4382d2SGreg Kroah-Hartman #endif
188ab4382d2SGreg Kroah-Hartman 
1895fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1905fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1915fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1925fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1935fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1945fbe46b6SNicolas Ferre };
1955fbe46b6SNicolas Ferre 
1965fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
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 
205ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
20664e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
207ab4382d2SGreg Kroah-Hartman {
208ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
209ab4382d2SGreg Kroah-Hartman 
21064e22ebeSElen Song 	return atmel_port->use_pdc_rx;
211ab4382d2SGreg Kroah-Hartman }
212ab4382d2SGreg Kroah-Hartman 
21364e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
214ab4382d2SGreg Kroah-Hartman {
215ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
216ab4382d2SGreg Kroah-Hartman 
21764e22ebeSElen Song 	return atmel_port->use_pdc_tx;
218ab4382d2SGreg Kroah-Hartman }
219ab4382d2SGreg Kroah-Hartman #else
22064e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
221ab4382d2SGreg Kroah-Hartman {
222ab4382d2SGreg Kroah-Hartman 	return false;
223ab4382d2SGreg Kroah-Hartman }
224ab4382d2SGreg Kroah-Hartman 
22564e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
226ab4382d2SGreg Kroah-Hartman {
227ab4382d2SGreg Kroah-Hartman 	return false;
228ab4382d2SGreg Kroah-Hartman }
229ab4382d2SGreg Kroah-Hartman #endif
230ab4382d2SGreg Kroah-Hartman 
23108f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
23208f738beSElen Song {
23308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
23408f738beSElen Song 
23508f738beSElen Song 	return atmel_port->use_dma_tx;
23608f738beSElen Song }
23708f738beSElen Song 
23834df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
23934df42f5SElen Song {
24034df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
24134df42f5SElen Song 
24234df42f5SElen Song 	return atmel_port->use_dma_rx;
24334df42f5SElen Song }
24434df42f5SElen Song 
245ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
246ab4382d2SGreg Kroah-Hartman void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
247ab4382d2SGreg Kroah-Hartman {
248ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
249ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
250dbf1115dSClaudio Scordino 	unsigned long flags;
251ab4382d2SGreg Kroah-Hartman 
252dbf1115dSClaudio Scordino 	spin_lock_irqsave(&port->lock, flags);
253ab4382d2SGreg Kroah-Hartman 
254ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
255ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
256ab4382d2SGreg Kroah-Hartman 
257ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port);
258ab4382d2SGreg Kroah-Hartman 
259ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
260ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
261ab4382d2SGreg Kroah-Hartman 
262ab4382d2SGreg Kroah-Hartman 	atmel_port->rs485 = *rs485conf;
263ab4382d2SGreg Kroah-Hartman 
264ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
265ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
266ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
26793f3350cSClaudio Scordino 		if ((rs485conf->delay_rts_after_send) > 0)
268ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
269ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
270ab4382d2SGreg Kroah-Hartman 	} else {
271ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
27264e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
273ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
274ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
275ab4382d2SGreg Kroah-Hartman 		else
276ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
277ab4382d2SGreg Kroah-Hartman 	}
278ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
279ab4382d2SGreg Kroah-Hartman 
280ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
281ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
282ab4382d2SGreg Kroah-Hartman 
283dbf1115dSClaudio Scordino 	spin_unlock_irqrestore(&port->lock, flags);
284ab4382d2SGreg Kroah-Hartman 
285ab4382d2SGreg Kroah-Hartman }
286ab4382d2SGreg Kroah-Hartman 
287ab4382d2SGreg Kroah-Hartman /*
288ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
289ab4382d2SGreg Kroah-Hartman  */
290ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
291ab4382d2SGreg Kroah-Hartman {
292ab4382d2SGreg Kroah-Hartman 	return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
293ab4382d2SGreg Kroah-Hartman }
294ab4382d2SGreg Kroah-Hartman 
295ab4382d2SGreg Kroah-Hartman /*
296ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
297ab4382d2SGreg Kroah-Hartman  */
298ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
299ab4382d2SGreg Kroah-Hartman {
300ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
301ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
302ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
303ab4382d2SGreg Kroah-Hartman 
304ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91RM9200
305ab4382d2SGreg Kroah-Hartman 	if (cpu_is_at91rm9200()) {
306ab4382d2SGreg Kroah-Hartman 		/*
307ab4382d2SGreg Kroah-Hartman 		 * AT91RM9200 Errata #39: RTS0 is not internally connected
308ab4382d2SGreg Kroah-Hartman 		 * to PA21. We need to drive the pin manually.
309ab4382d2SGreg Kroah-Hartman 		 */
310ab4382d2SGreg Kroah-Hartman 		if (port->mapbase == AT91RM9200_BASE_US0) {
311ab4382d2SGreg Kroah-Hartman 			if (mctrl & TIOCM_RTS)
312ab4382d2SGreg Kroah-Hartman 				at91_set_gpio_value(AT91_PIN_PA21, 0);
313ab4382d2SGreg Kroah-Hartman 			else
314ab4382d2SGreg Kroah-Hartman 				at91_set_gpio_value(AT91_PIN_PA21, 1);
315ab4382d2SGreg Kroah-Hartman 		}
316ab4382d2SGreg Kroah-Hartman 	}
317ab4382d2SGreg Kroah-Hartman #endif
318ab4382d2SGreg Kroah-Hartman 
319ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
320ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSEN;
321ab4382d2SGreg Kroah-Hartman 	else
322ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSDIS;
323ab4382d2SGreg Kroah-Hartman 
324ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
325ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
326ab4382d2SGreg Kroah-Hartman 	else
327ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
328ab4382d2SGreg Kroah-Hartman 
329ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, control);
330ab4382d2SGreg Kroah-Hartman 
331ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
332ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
333ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
334ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
335ab4382d2SGreg Kroah-Hartman 	else
336ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
337ab4382d2SGreg Kroah-Hartman 
338ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
339ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
340ab4382d2SGreg Kroah-Hartman 
341ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
342ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
34393f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
344ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
345ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
346ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
347ab4382d2SGreg Kroah-Hartman 	} else {
348ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
349ab4382d2SGreg Kroah-Hartman 	}
350ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
351ab4382d2SGreg Kroah-Hartman }
352ab4382d2SGreg Kroah-Hartman 
353ab4382d2SGreg Kroah-Hartman /*
354ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
355ab4382d2SGreg Kroah-Hartman  */
356ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
357ab4382d2SGreg Kroah-Hartman {
358ab4382d2SGreg Kroah-Hartman 	unsigned int status, ret = 0;
359ab4382d2SGreg Kroah-Hartman 
360ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
361ab4382d2SGreg Kroah-Hartman 
362ab4382d2SGreg Kroah-Hartman 	/*
363ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
364ab4382d2SGreg Kroah-Hartman 	 */
365ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
366ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
367ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
368ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
369ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
370ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
371ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
372ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
373ab4382d2SGreg Kroah-Hartman 
374ab4382d2SGreg Kroah-Hartman 	return ret;
375ab4382d2SGreg Kroah-Hartman }
376ab4382d2SGreg Kroah-Hartman 
377ab4382d2SGreg Kroah-Hartman /*
378ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
379ab4382d2SGreg Kroah-Hartman  */
380ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
381ab4382d2SGreg Kroah-Hartman {
382ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
383ab4382d2SGreg Kroah-Hartman 
38464e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
385ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
386ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
387ab4382d2SGreg Kroah-Hartman 	}
388ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
389ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
390ab4382d2SGreg Kroah-Hartman 
39183cac9f3SBernhard Roth 	if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
39283cac9f3SBernhard Roth 	    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
393ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
394ab4382d2SGreg Kroah-Hartman }
395ab4382d2SGreg Kroah-Hartman 
396ab4382d2SGreg Kroah-Hartman /*
397ab4382d2SGreg Kroah-Hartman  * Start transmitting.
398ab4382d2SGreg Kroah-Hartman  */
399ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
400ab4382d2SGreg Kroah-Hartman {
401ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
402ab4382d2SGreg Kroah-Hartman 
40364e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
404ab4382d2SGreg Kroah-Hartman 		if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
405ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
406ab4382d2SGreg Kroah-Hartman 			   really need this.*/
407ab4382d2SGreg Kroah-Hartman 			return;
408ab4382d2SGreg Kroah-Hartman 
40983cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
41083cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
411ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
412ab4382d2SGreg Kroah-Hartman 
413ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
414ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
415ab4382d2SGreg Kroah-Hartman 	}
416ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
417ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
418ab4382d2SGreg Kroah-Hartman }
419ab4382d2SGreg Kroah-Hartman 
420ab4382d2SGreg Kroah-Hartman /*
421ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
422ab4382d2SGreg Kroah-Hartman  */
423ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
424ab4382d2SGreg Kroah-Hartman {
425ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);  /* reset status and receiver */
426ab4382d2SGreg Kroah-Hartman 
42757c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXEN);
42857c36868SSiftar, Gabe 
42964e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
430ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
431ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
432ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
433ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
434ab4382d2SGreg Kroah-Hartman 	} else {
435ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
436ab4382d2SGreg Kroah-Hartman 	}
437ab4382d2SGreg Kroah-Hartman }
438ab4382d2SGreg Kroah-Hartman 
439ab4382d2SGreg Kroah-Hartman /*
440ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
441ab4382d2SGreg Kroah-Hartman  */
442ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
443ab4382d2SGreg Kroah-Hartman {
44457c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXDIS);
44557c36868SSiftar, Gabe 
44664e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
447ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
448ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
449ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
450ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
451ab4382d2SGreg Kroah-Hartman 	} else {
452ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXRDY);
453ab4382d2SGreg Kroah-Hartman 	}
454ab4382d2SGreg Kroah-Hartman }
455ab4382d2SGreg Kroah-Hartman 
456ab4382d2SGreg Kroah-Hartman /*
457ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
458ab4382d2SGreg Kroah-Hartman  */
459ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
460ab4382d2SGreg Kroah-Hartman {
461ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
462ab4382d2SGreg Kroah-Hartman 			| ATMEL_US_DCDIC | ATMEL_US_CTSIC);
463ab4382d2SGreg Kroah-Hartman }
464ab4382d2SGreg Kroah-Hartman 
465ab4382d2SGreg Kroah-Hartman /*
466ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
467ab4382d2SGreg Kroah-Hartman  */
468ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
469ab4382d2SGreg Kroah-Hartman {
470ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
471ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTBRK);	/* start break */
472ab4382d2SGreg Kroah-Hartman 	else
473ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STPBRK);	/* stop break */
474ab4382d2SGreg Kroah-Hartman }
475ab4382d2SGreg Kroah-Hartman 
476ab4382d2SGreg Kroah-Hartman /*
477ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
478ab4382d2SGreg Kroah-Hartman  */
479ab4382d2SGreg Kroah-Hartman static void
480ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
481ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
482ab4382d2SGreg Kroah-Hartman {
483ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
484ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
485ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
486ab4382d2SGreg Kroah-Hartman 
487ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
488ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
489ab4382d2SGreg Kroah-Hartman 		return;
490ab4382d2SGreg Kroah-Hartman 
491ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
492ab4382d2SGreg Kroah-Hartman 	c->status	= status;
493ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
494ab4382d2SGreg Kroah-Hartman 
495ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
496ab4382d2SGreg Kroah-Hartman 	smp_wmb();
497ab4382d2SGreg Kroah-Hartman 
498ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
499ab4382d2SGreg Kroah-Hartman }
500ab4382d2SGreg Kroah-Hartman 
501ab4382d2SGreg Kroah-Hartman /*
502ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
503ab4382d2SGreg Kroah-Hartman  */
504ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
505ab4382d2SGreg Kroah-Hartman {
506ab4382d2SGreg Kroah-Hartman 	/* clear error */
507ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
508ab4382d2SGreg Kroah-Hartman 
509ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
510ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
511ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
512ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
513ab4382d2SGreg Kroah-Hartman 	}
514ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
515ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
516ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
517ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
518ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
519ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
520ab4382d2SGreg Kroah-Hartman }
521ab4382d2SGreg Kroah-Hartman 
522ab4382d2SGreg Kroah-Hartman /*
523ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
524ab4382d2SGreg Kroah-Hartman  */
525ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
526ab4382d2SGreg Kroah-Hartman {
527ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
528ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
529ab4382d2SGreg Kroah-Hartman 
530ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
531ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
532ab4382d2SGreg Kroah-Hartman 		ch = UART_GET_CHAR(port);
533ab4382d2SGreg Kroah-Hartman 
534ab4382d2SGreg Kroah-Hartman 		/*
535ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
536ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
537ab4382d2SGreg Kroah-Hartman 		 */
538ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
539ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
540ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
541ab4382d2SGreg Kroah-Hartman 
542ab4382d2SGreg Kroah-Hartman 			/* clear error */
543ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_RSTSTA);
544ab4382d2SGreg Kroah-Hartman 
545ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
546ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
547ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
548ab4382d2SGreg Kroah-Hartman 				UART_PUT_IER(port, ATMEL_US_RXBRK);
549ab4382d2SGreg Kroah-Hartman 			} else {
550ab4382d2SGreg Kroah-Hartman 				/*
551ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
552ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
553ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
554ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
555ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
556ab4382d2SGreg Kroah-Hartman 				 */
557ab4382d2SGreg Kroah-Hartman 				UART_PUT_IDR(port, ATMEL_US_RXBRK);
558ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
559ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
560ab4382d2SGreg Kroah-Hartman 			}
561ab4382d2SGreg Kroah-Hartman 		}
562ab4382d2SGreg Kroah-Hartman 
563ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
564ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
565ab4382d2SGreg Kroah-Hartman 	}
566ab4382d2SGreg Kroah-Hartman 
567ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
568ab4382d2SGreg Kroah-Hartman }
569ab4382d2SGreg Kroah-Hartman 
570ab4382d2SGreg Kroah-Hartman /*
571ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
572ab4382d2SGreg Kroah-Hartman  * disabled)
573ab4382d2SGreg Kroah-Hartman  */
574ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
575ab4382d2SGreg Kroah-Hartman {
576ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
577ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
578ab4382d2SGreg Kroah-Hartman 
579ab4382d2SGreg Kroah-Hartman 	if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
580ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, port->x_char);
581ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
582ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
583ab4382d2SGreg Kroah-Hartman 	}
584ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
585ab4382d2SGreg Kroah-Hartman 		return;
586ab4382d2SGreg Kroah-Hartman 
587ab4382d2SGreg Kroah-Hartman 	while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
588ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
589ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
590ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
591ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
592ab4382d2SGreg Kroah-Hartman 			break;
593ab4382d2SGreg Kroah-Hartman 	}
594ab4382d2SGreg Kroah-Hartman 
595ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
596ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
597ab4382d2SGreg Kroah-Hartman 
598ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
599ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
600ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
601ab4382d2SGreg Kroah-Hartman }
602ab4382d2SGreg Kroah-Hartman 
60308f738beSElen Song static void atmel_complete_tx_dma(void *arg)
60408f738beSElen Song {
60508f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
60608f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
60708f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
60808f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
60908f738beSElen Song 	unsigned long flags;
61008f738beSElen Song 
61108f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
61208f738beSElen Song 
61308f738beSElen Song 	if (chan)
61408f738beSElen Song 		dmaengine_terminate_all(chan);
61508f738beSElen Song 	xmit->tail += sg_dma_len(&atmel_port->sg_tx);
61608f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
61708f738beSElen Song 
61808f738beSElen Song 	port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
61908f738beSElen Song 
62008f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
62108f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
62208f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
62308f738beSElen Song 	atmel_port->desc_tx = NULL;
62408f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
62508f738beSElen Song 
62608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
62708f738beSElen Song 		uart_write_wakeup(port);
62808f738beSElen Song 
62908f738beSElen Song 	/* Do we really need this? */
63008f738beSElen Song 	if (!uart_circ_empty(xmit))
63108f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
63208f738beSElen Song 
63308f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
63408f738beSElen Song }
63508f738beSElen Song 
63608f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
63708f738beSElen Song {
63808f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
63908f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
64008f738beSElen Song 
64108f738beSElen Song 	if (chan) {
64208f738beSElen Song 		dmaengine_terminate_all(chan);
64308f738beSElen Song 		dma_release_channel(chan);
64408f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
64508f738beSElen Song 				DMA_MEM_TO_DEV);
64608f738beSElen Song 	}
64708f738beSElen Song 
64808f738beSElen Song 	atmel_port->desc_tx = NULL;
64908f738beSElen Song 	atmel_port->chan_tx = NULL;
65008f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
65108f738beSElen Song }
65208f738beSElen Song 
65308f738beSElen Song /*
65408f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
65508f738beSElen Song  */
65608f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
65708f738beSElen Song {
65808f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
65908f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
66008f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
66108f738beSElen Song 	struct dma_async_tx_descriptor *desc;
66208f738beSElen Song 	struct scatterlist *sg = &atmel_port->sg_tx;
66308f738beSElen Song 
66408f738beSElen Song 	/* Make sure we have an idle channel */
66508f738beSElen Song 	if (atmel_port->desc_tx != NULL)
66608f738beSElen Song 		return;
66708f738beSElen Song 
66808f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
66908f738beSElen Song 		/*
67008f738beSElen Song 		 * DMA is idle now.
67108f738beSElen Song 		 * Port xmit buffer is already mapped,
67208f738beSElen Song 		 * and it is one page... Just adjust
67308f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
67408f738beSElen Song 		 * we have to transmit till the end, and then the rest.
67508f738beSElen Song 		 * Take the port lock to get a
67608f738beSElen Song 		 * consistent xmit buffer state.
67708f738beSElen Song 		 */
67808f738beSElen Song 		sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
67908f738beSElen Song 		sg_dma_address(sg) = (sg_dma_address(sg) &
68008f738beSElen Song 					~(UART_XMIT_SIZE - 1))
68108f738beSElen Song 					+ sg->offset;
68208f738beSElen Song 		sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
68308f738beSElen Song 						xmit->tail,
68408f738beSElen Song 						UART_XMIT_SIZE);
68508f738beSElen Song 		BUG_ON(!sg_dma_len(sg));
68608f738beSElen Song 
68708f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
68808f738beSElen Song 						sg,
68908f738beSElen Song 						1,
69008f738beSElen Song 						DMA_MEM_TO_DEV,
69108f738beSElen Song 						DMA_PREP_INTERRUPT |
69208f738beSElen Song 						DMA_CTRL_ACK);
69308f738beSElen Song 		if (!desc) {
69408f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
69508f738beSElen Song 			return;
69608f738beSElen Song 		}
69708f738beSElen Song 
69808f738beSElen Song 		dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
69908f738beSElen Song 
70008f738beSElen Song 		atmel_port->desc_tx = desc;
70108f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
70208f738beSElen Song 		desc->callback_param = atmel_port;
70308f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
70408f738beSElen Song 
70508f738beSElen Song 	} else {
70608f738beSElen Song 		if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
70708f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
70808f738beSElen Song 			atmel_start_rx(port);
70908f738beSElen Song 		}
71008f738beSElen Song 	}
71108f738beSElen Song 
71208f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
71308f738beSElen Song 		uart_write_wakeup(port);
71408f738beSElen Song }
71508f738beSElen Song 
71608f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
71708f738beSElen Song {
71808f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
71908f738beSElen Song 	dma_cap_mask_t		mask;
72008f738beSElen Song 	struct dma_slave_config config;
72108f738beSElen Song 	int ret, nent;
72208f738beSElen Song 
72308f738beSElen Song 	dma_cap_zero(mask);
72408f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
72508f738beSElen Song 
72608f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
72708f738beSElen Song 	if (atmel_port->chan_tx == NULL)
72808f738beSElen Song 		goto chan_err;
72908f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
73008f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
73108f738beSElen Song 
73208f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
73308f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
73408f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
73508f738beSElen Song 	BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
73608f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
73708f738beSElen Song 			virt_to_page(port->state->xmit.buf),
73808f738beSElen Song 			UART_XMIT_SIZE,
73908f738beSElen Song 			(int)port->state->xmit.buf & ~PAGE_MASK);
74008f738beSElen Song 	nent = dma_map_sg(port->dev,
74108f738beSElen Song 				&atmel_port->sg_tx,
74208f738beSElen Song 				1,
74308f738beSElen Song 				DMA_MEM_TO_DEV);
74408f738beSElen Song 
74508f738beSElen Song 	if (!nent) {
74608f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
74708f738beSElen Song 		goto chan_err;
74808f738beSElen Song 	} else {
74908f738beSElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
75008f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
75108f738beSElen Song 			port->state->xmit.buf,
75208f738beSElen Song 			sg_dma_address(&atmel_port->sg_tx));
75308f738beSElen Song 	}
75408f738beSElen Song 
75508f738beSElen Song 	/* Configure the slave DMA */
75608f738beSElen Song 	memset(&config, 0, sizeof(config));
75708f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
75808f738beSElen Song 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
75908f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
76008f738beSElen Song 
76108f738beSElen Song 	ret = dmaengine_device_control(atmel_port->chan_tx,
76208f738beSElen Song 					DMA_SLAVE_CONFIG,
76308f738beSElen Song 					(unsigned long)&config);
76408f738beSElen Song 	if (ret) {
76508f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
76608f738beSElen Song 		goto chan_err;
76708f738beSElen Song 	}
76808f738beSElen Song 
76908f738beSElen Song 	return 0;
77008f738beSElen Song 
77108f738beSElen Song chan_err:
77208f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
77308f738beSElen Song 	atmel_port->use_dma_tx = 0;
77408f738beSElen Song 	if (atmel_port->chan_tx)
77508f738beSElen Song 		atmel_release_tx_dma(port);
77608f738beSElen Song 	return -EINVAL;
77708f738beSElen Song }
77808f738beSElen Song 
77934df42f5SElen Song static void atmel_flip_buffer_rx_dma(struct uart_port *port,
78034df42f5SElen Song 					char *buf, size_t count)
78134df42f5SElen Song {
78234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
78334df42f5SElen Song 	struct tty_port *tport = &port->state->port;
78434df42f5SElen Song 
78534df42f5SElen Song 	dma_sync_sg_for_cpu(port->dev,
78634df42f5SElen Song 				&atmel_port->sg_rx,
78734df42f5SElen Song 				1,
78834df42f5SElen Song 				DMA_DEV_TO_MEM);
78934df42f5SElen Song 
79034df42f5SElen Song 	tty_insert_flip_string(tport, buf, count);
79134df42f5SElen Song 
79234df42f5SElen Song 	dma_sync_sg_for_device(port->dev,
79334df42f5SElen Song 				&atmel_port->sg_rx,
79434df42f5SElen Song 				1,
79534df42f5SElen Song 				DMA_DEV_TO_MEM);
79634df42f5SElen Song 	/*
79734df42f5SElen Song 	 * Drop the lock here since it might end up calling
79834df42f5SElen Song 	 * uart_start(), which takes the lock.
79934df42f5SElen Song 	 */
80034df42f5SElen Song 	spin_unlock(&port->lock);
80134df42f5SElen Song 	tty_flip_buffer_push(tport);
80234df42f5SElen Song 	spin_lock(&port->lock);
80334df42f5SElen Song }
80434df42f5SElen Song 
80534df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
80634df42f5SElen Song {
80734df42f5SElen Song 	struct uart_port *port = arg;
80834df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
80934df42f5SElen Song 
81034df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
81134df42f5SElen Song }
81234df42f5SElen Song 
81334df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
81434df42f5SElen Song {
81534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
81634df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
81734df42f5SElen Song 
81834df42f5SElen Song 	if (chan) {
81934df42f5SElen Song 		dmaengine_terminate_all(chan);
82034df42f5SElen Song 		dma_release_channel(chan);
82134df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
82234df42f5SElen Song 				DMA_DEV_TO_MEM);
82334df42f5SElen Song 	}
82434df42f5SElen Song 
82534df42f5SElen Song 	atmel_port->desc_rx = NULL;
82634df42f5SElen Song 	atmel_port->chan_rx = NULL;
82734df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
82834df42f5SElen Song }
82934df42f5SElen Song 
83034df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
83134df42f5SElen Song {
83234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
83334df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
83434df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
83534df42f5SElen Song 	struct dma_tx_state state;
83634df42f5SElen Song 	enum dma_status dmastat;
83734df42f5SElen Song 	size_t pending, count;
83834df42f5SElen Song 
83934df42f5SElen Song 
84034df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
84134df42f5SElen Song 	UART_PUT_CR(port, ATMEL_US_STTTO);
84234df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
84334df42f5SElen Song 				atmel_port->cookie_rx,
84434df42f5SElen Song 				&state);
84534df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
84634df42f5SElen Song 	if (dmastat == DMA_ERROR) {
84734df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
84834df42f5SElen Song 		UART_PUT_IER(port, ATMEL_US_TIMEOUT);
84934df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
85034df42f5SElen Song 		return;
85134df42f5SElen Song 	}
85234df42f5SElen Song 	/* current transfer size should no larger than dma buffer */
85334df42f5SElen Song 	pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
85434df42f5SElen Song 	BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
85534df42f5SElen Song 
85634df42f5SElen Song 	/*
85734df42f5SElen Song 	 * This will take the chars we have so far,
85834df42f5SElen Song 	 * ring->head will record the transfer size, only new bytes come
85934df42f5SElen Song 	 * will insert into the framework.
86034df42f5SElen Song 	 */
86134df42f5SElen Song 	if (pending > ring->head) {
86234df42f5SElen Song 		count = pending - ring->head;
86334df42f5SElen Song 
86434df42f5SElen Song 		atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
86534df42f5SElen Song 
86634df42f5SElen Song 		ring->head += count;
86734df42f5SElen Song 		if (ring->head == sg_dma_len(&atmel_port->sg_rx))
86834df42f5SElen Song 			ring->head = 0;
86934df42f5SElen Song 
87034df42f5SElen Song 		port->icount.rx += count;
87134df42f5SElen Song 	}
87234df42f5SElen Song 
87334df42f5SElen Song 	UART_PUT_IER(port, ATMEL_US_TIMEOUT);
87434df42f5SElen Song }
87534df42f5SElen Song 
87634df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
87734df42f5SElen Song {
87834df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
87934df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
88034df42f5SElen Song 	dma_cap_mask_t		mask;
88134df42f5SElen Song 	struct dma_slave_config config;
88234df42f5SElen Song 	struct circ_buf		*ring;
88334df42f5SElen Song 	int ret, nent;
88434df42f5SElen Song 
88534df42f5SElen Song 	ring = &atmel_port->rx_ring;
88634df42f5SElen Song 
88734df42f5SElen Song 	dma_cap_zero(mask);
88834df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
88934df42f5SElen Song 
89034df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
89134df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
89234df42f5SElen Song 		goto chan_err;
89334df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
89434df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
89534df42f5SElen Song 
89634df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
89734df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
89834df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
89934df42f5SElen Song 	BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
90034df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
90134df42f5SElen Song 			virt_to_page(ring->buf),
90234df42f5SElen Song 			ATMEL_SERIAL_RINGSIZE,
90334df42f5SElen Song 			(int)ring->buf & ~PAGE_MASK);
90434df42f5SElen Song 			nent = dma_map_sg(port->dev,
90534df42f5SElen Song 					&atmel_port->sg_rx,
90634df42f5SElen Song 					1,
90734df42f5SElen Song 					DMA_DEV_TO_MEM);
90834df42f5SElen Song 
90934df42f5SElen Song 	if (!nent) {
91034df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
91134df42f5SElen Song 		goto chan_err;
91234df42f5SElen Song 	} else {
91334df42f5SElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
91434df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
91534df42f5SElen Song 			ring->buf,
91634df42f5SElen Song 			sg_dma_address(&atmel_port->sg_rx));
91734df42f5SElen Song 	}
91834df42f5SElen Song 
91934df42f5SElen Song 	/* Configure the slave DMA */
92034df42f5SElen Song 	memset(&config, 0, sizeof(config));
92134df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
92234df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
92334df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
92434df42f5SElen Song 
92534df42f5SElen Song 	ret = dmaengine_device_control(atmel_port->chan_rx,
92634df42f5SElen Song 					DMA_SLAVE_CONFIG,
92734df42f5SElen Song 					(unsigned long)&config);
92834df42f5SElen Song 	if (ret) {
92934df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
93034df42f5SElen Song 		goto chan_err;
93134df42f5SElen Song 	}
93234df42f5SElen Song 	/*
93334df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
93434df42f5SElen Song 	 * each one is half ring buffer size
93534df42f5SElen Song 	 */
93634df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
93734df42f5SElen Song 				sg_dma_address(&atmel_port->sg_rx),
93834df42f5SElen Song 				sg_dma_len(&atmel_port->sg_rx),
93934df42f5SElen Song 				sg_dma_len(&atmel_port->sg_rx)/2,
94034df42f5SElen Song 				DMA_DEV_TO_MEM,
94134df42f5SElen Song 				DMA_PREP_INTERRUPT);
94234df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
94334df42f5SElen Song 	desc->callback_param = port;
94434df42f5SElen Song 	atmel_port->desc_rx = desc;
94534df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
94634df42f5SElen Song 
94734df42f5SElen Song 	return 0;
94834df42f5SElen Song 
94934df42f5SElen Song chan_err:
95034df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
95134df42f5SElen Song 	atmel_port->use_dma_rx = 0;
95234df42f5SElen Song 	if (atmel_port->chan_rx)
95334df42f5SElen Song 		atmel_release_rx_dma(port);
95434df42f5SElen Song 	return -EINVAL;
95534df42f5SElen Song }
95634df42f5SElen Song 
9572e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
9582e68c22fSElen Song {
9592e68c22fSElen Song 	struct uart_port *port = (void *)data;
9602e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
9612e68c22fSElen Song 
9622e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
9632e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
9642e68c22fSElen Song }
9652e68c22fSElen Song 
966ab4382d2SGreg Kroah-Hartman /*
967ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
968ab4382d2SGreg Kroah-Hartman  */
969ab4382d2SGreg Kroah-Hartman static void
970ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
971ab4382d2SGreg Kroah-Hartman {
972ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
973ab4382d2SGreg Kroah-Hartman 
97464e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
975ab4382d2SGreg Kroah-Hartman 		/*
976ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
977ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
978ab4382d2SGreg Kroah-Hartman 		 *
979ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
980ab4382d2SGreg Kroah-Hartman 		 * the moment.
981ab4382d2SGreg Kroah-Hartman 		 */
982ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
983ab4382d2SGreg Kroah-Hartman 			UART_PUT_IDR(port, (ATMEL_US_ENDRX
984ab4382d2SGreg Kroah-Hartman 						| ATMEL_US_TIMEOUT));
985ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
986ab4382d2SGreg Kroah-Hartman 		}
987ab4382d2SGreg Kroah-Hartman 
988ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
989ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
990ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
991ab4382d2SGreg Kroah-Hartman 	}
992ab4382d2SGreg Kroah-Hartman 
99334df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
99434df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
99534df42f5SElen Song 			UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
99634df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
99734df42f5SElen Song 		}
99834df42f5SElen Song 	}
99934df42f5SElen Song 
1000ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1001ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1002ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1003ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1004ab4382d2SGreg Kroah-Hartman 		/*
1005ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1006ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1007ab4382d2SGreg Kroah-Hartman 		 */
1008ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_RSTSTA);
1009ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXBRK);
1010ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1011ab4382d2SGreg Kroah-Hartman 	}
1012ab4382d2SGreg Kroah-Hartman }
1013ab4382d2SGreg Kroah-Hartman 
1014ab4382d2SGreg Kroah-Hartman /*
1015ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1016ab4382d2SGreg Kroah-Hartman  */
1017ab4382d2SGreg Kroah-Hartman static void
1018ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1019ab4382d2SGreg Kroah-Hartman {
1020ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1021ab4382d2SGreg Kroah-Hartman 
1022ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1023ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
1024ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, atmel_port->tx_done_mask);
1025ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1026ab4382d2SGreg Kroah-Hartman 	}
1027ab4382d2SGreg Kroah-Hartman }
1028ab4382d2SGreg Kroah-Hartman 
1029ab4382d2SGreg Kroah-Hartman /*
1030ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1031ab4382d2SGreg Kroah-Hartman  */
1032ab4382d2SGreg Kroah-Hartman static void
1033ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1034ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1035ab4382d2SGreg Kroah-Hartman {
1036ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1037ab4382d2SGreg Kroah-Hartman 
1038ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1039ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1040ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1041ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1042ab4382d2SGreg Kroah-Hartman 	}
1043ab4382d2SGreg Kroah-Hartman }
1044ab4382d2SGreg Kroah-Hartman 
1045ab4382d2SGreg Kroah-Hartman /*
1046ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1047ab4382d2SGreg Kroah-Hartman  */
1048ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1049ab4382d2SGreg Kroah-Hartman {
1050ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1051ab4382d2SGreg Kroah-Hartman 	unsigned int status, pending, pass_counter = 0;
1052ab4382d2SGreg Kroah-Hartman 
1053ab4382d2SGreg Kroah-Hartman 	do {
1054ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
1055ab4382d2SGreg Kroah-Hartman 		pending = status & UART_GET_IMR(port);
1056ab4382d2SGreg Kroah-Hartman 		if (!pending)
1057ab4382d2SGreg Kroah-Hartman 			break;
1058ab4382d2SGreg Kroah-Hartman 
1059ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1060ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1061ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1062ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1063ab4382d2SGreg Kroah-Hartman 
1064ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1065ab4382d2SGreg Kroah-Hartman }
1066ab4382d2SGreg Kroah-Hartman 
1067a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1068a930e528SElen Song {
1069a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1070a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1071a930e528SElen Song 
1072a930e528SElen Song 	dma_unmap_single(port->dev,
1073a930e528SElen Song 			 pdc->dma_addr,
1074a930e528SElen Song 			 pdc->dma_size,
1075a930e528SElen Song 			 DMA_TO_DEVICE);
1076a930e528SElen Song }
1077a930e528SElen Song 
1078ab4382d2SGreg Kroah-Hartman /*
1079ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1080ab4382d2SGreg Kroah-Hartman  */
108164e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1082ab4382d2SGreg Kroah-Hartman {
1083ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1084ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1085ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1086ab4382d2SGreg Kroah-Hartman 	int count;
1087ab4382d2SGreg Kroah-Hartman 
1088ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
1089ab4382d2SGreg Kroah-Hartman 	if (UART_GET_TCR(port))
1090ab4382d2SGreg Kroah-Hartman 		return;
1091ab4382d2SGreg Kroah-Hartman 
1092ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1093ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1094ab4382d2SGreg Kroah-Hartman 
1095ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1096ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1097ab4382d2SGreg Kroah-Hartman 
1098ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1099ab4382d2SGreg Kroah-Hartman 
1100ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
1101ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1102ab4382d2SGreg Kroah-Hartman 
1103ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1104ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1105ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1106ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1107ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1108ab4382d2SGreg Kroah-Hartman 
1109ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1110ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1111ab4382d2SGreg Kroah-Hartman 
1112ab4382d2SGreg Kroah-Hartman 		UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1113ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, count);
1114ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
1115ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1116ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
1117ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
1118ab4382d2SGreg Kroah-Hartman 	} else {
111983cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
112083cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1121ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1122ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1123ab4382d2SGreg Kroah-Hartman 		}
1124ab4382d2SGreg Kroah-Hartman 	}
1125ab4382d2SGreg Kroah-Hartman 
1126ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1127ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1128ab4382d2SGreg Kroah-Hartman }
1129ab4382d2SGreg Kroah-Hartman 
1130a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1131a930e528SElen Song {
1132a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1133a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1134a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1135a930e528SElen Song 
1136a930e528SElen Song 	pdc->buf = xmit->buf;
1137a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1138a930e528SElen Song 					pdc->buf,
1139a930e528SElen Song 					UART_XMIT_SIZE,
1140a930e528SElen Song 					DMA_TO_DEVICE);
1141a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1142a930e528SElen Song 	pdc->ofs = 0;
1143a930e528SElen Song 
1144a930e528SElen Song 	return 0;
1145a930e528SElen Song }
1146a930e528SElen Song 
1147ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1148ab4382d2SGreg Kroah-Hartman {
1149ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1150ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1151ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1152ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1153ab4382d2SGreg Kroah-Hartman 
1154ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1155ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1156ab4382d2SGreg Kroah-Hartman 
1157ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1158ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1159ab4382d2SGreg Kroah-Hartman 
1160ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1161ab4382d2SGreg Kroah-Hartman 
1162ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1163ab4382d2SGreg Kroah-Hartman 
1164ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1165ab4382d2SGreg Kroah-Hartman 		status = c.status;
1166ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1167ab4382d2SGreg Kroah-Hartman 
1168ab4382d2SGreg Kroah-Hartman 		/*
1169ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1170ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1171ab4382d2SGreg Kroah-Hartman 		 */
1172ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1173ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1174ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1175ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1176ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1177ab4382d2SGreg Kroah-Hartman 
1178ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1179ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1180ab4382d2SGreg Kroah-Hartman 					continue;
1181ab4382d2SGreg Kroah-Hartman 			}
1182ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1183ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1184ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1185ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1186ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1187ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1188ab4382d2SGreg Kroah-Hartman 
1189ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1190ab4382d2SGreg Kroah-Hartman 
1191ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1192ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1193ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1194ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1195ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1196ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1197ab4382d2SGreg Kroah-Hartman 		}
1198ab4382d2SGreg Kroah-Hartman 
1199ab4382d2SGreg Kroah-Hartman 
1200ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1201ab4382d2SGreg Kroah-Hartman 			continue;
1202ab4382d2SGreg Kroah-Hartman 
1203ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1204ab4382d2SGreg Kroah-Hartman 	}
1205ab4382d2SGreg Kroah-Hartman 
1206ab4382d2SGreg Kroah-Hartman 	/*
1207ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1208ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1209ab4382d2SGreg Kroah-Hartman 	 */
1210ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
12112e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1212ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1213ab4382d2SGreg Kroah-Hartman }
1214ab4382d2SGreg Kroah-Hartman 
1215a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1216a930e528SElen Song {
1217a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1218a930e528SElen Song 	int i;
1219a930e528SElen Song 
1220a930e528SElen Song 	for (i = 0; i < 2; i++) {
1221a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1222a930e528SElen Song 
1223a930e528SElen Song 		dma_unmap_single(port->dev,
1224a930e528SElen Song 				 pdc->dma_addr,
1225a930e528SElen Song 				 pdc->dma_size,
1226a930e528SElen Song 				 DMA_FROM_DEVICE);
1227a930e528SElen Song 		kfree(pdc->buf);
1228a930e528SElen Song 	}
1229a930e528SElen Song }
1230a930e528SElen Song 
123164e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1232ab4382d2SGreg Kroah-Hartman {
1233ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
123405c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1235ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1236ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1237ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1238ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1239ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1240ab4382d2SGreg Kroah-Hartman 
1241ab4382d2SGreg Kroah-Hartman 	do {
1242ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
1243ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
1244ab4382d2SGreg Kroah-Hartman 
1245ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
1246ab4382d2SGreg Kroah-Hartman 		head = UART_GET_RPR(port) - pdc->dma_addr;
1247ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1248ab4382d2SGreg Kroah-Hartman 
1249ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1250ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1251ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1252ab4382d2SGreg Kroah-Hartman 		 * find out.
1253ab4382d2SGreg Kroah-Hartman 		 *
1254ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1255ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1256ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1257ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1258ab4382d2SGreg Kroah-Hartman 		 */
1259ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1260ab4382d2SGreg Kroah-Hartman 
1261ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1262ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1263ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1264ab4382d2SGreg Kroah-Hartman 
1265ab4382d2SGreg Kroah-Hartman 			/*
1266ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1267ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1268ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1269ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1270ab4382d2SGreg Kroah-Hartman 			 */
1271ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1272ab4382d2SGreg Kroah-Hartman 
127305c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
127405c7cd39SJiri Slaby 						count);
1275ab4382d2SGreg Kroah-Hartman 
1276ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1277ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1278ab4382d2SGreg Kroah-Hartman 
1279ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1280ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1281ab4382d2SGreg Kroah-Hartman 		}
1282ab4382d2SGreg Kroah-Hartman 
1283ab4382d2SGreg Kroah-Hartman 		/*
1284ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1285ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1286ab4382d2SGreg Kroah-Hartman 		 */
1287ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1288ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
1289ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNPR(port, pdc->dma_addr);
1290ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNCR(port, pdc->dma_size);
1291ab4382d2SGreg Kroah-Hartman 
1292ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1293ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1294ab4382d2SGreg Kroah-Hartman 		}
1295ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1296ab4382d2SGreg Kroah-Hartman 
1297ab4382d2SGreg Kroah-Hartman 	/*
1298ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1299ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1300ab4382d2SGreg Kroah-Hartman 	 */
1301ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
13022e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1303ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1304ab4382d2SGreg Kroah-Hartman 
1305ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1306ab4382d2SGreg Kroah-Hartman }
1307ab4382d2SGreg Kroah-Hartman 
1308a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1309a930e528SElen Song {
1310a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1311a930e528SElen Song 	int i;
1312a930e528SElen Song 
1313a930e528SElen Song 	for (i = 0; i < 2; i++) {
1314a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1315a930e528SElen Song 
1316a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1317a930e528SElen Song 		if (pdc->buf == NULL) {
1318a930e528SElen Song 			if (i != 0) {
1319a930e528SElen Song 				dma_unmap_single(port->dev,
1320a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1321a930e528SElen Song 					PDC_BUFFER_SIZE,
1322a930e528SElen Song 					DMA_FROM_DEVICE);
1323a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1324a930e528SElen Song 			}
1325a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1326a930e528SElen Song 			return -ENOMEM;
1327a930e528SElen Song 		}
1328a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1329a930e528SElen Song 						pdc->buf,
1330a930e528SElen Song 						PDC_BUFFER_SIZE,
1331a930e528SElen Song 						DMA_FROM_DEVICE);
1332a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1333a930e528SElen Song 		pdc->ofs = 0;
1334a930e528SElen Song 	}
1335a930e528SElen Song 
1336a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1337a930e528SElen Song 
1338a930e528SElen Song 	UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1339a930e528SElen Song 	UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1340a930e528SElen Song 
1341a930e528SElen Song 	UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1342a930e528SElen Song 	UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1343a930e528SElen Song 
1344a930e528SElen Song 	return 0;
1345a930e528SElen Song }
1346a930e528SElen Song 
1347ab4382d2SGreg Kroah-Hartman /*
1348ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1349ab4382d2SGreg Kroah-Hartman  */
1350ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1351ab4382d2SGreg Kroah-Hartman {
1352ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1353ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1354ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1355ab4382d2SGreg Kroah-Hartman 	unsigned int status_change;
1356ab4382d2SGreg Kroah-Hartman 
1357ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1358ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1359ab4382d2SGreg Kroah-Hartman 
1360a930e528SElen Song 	atmel_port->schedule_tx(port);
1361ab4382d2SGreg Kroah-Hartman 
1362ab4382d2SGreg Kroah-Hartman 	status = atmel_port->irq_status;
1363ab4382d2SGreg Kroah-Hartman 	status_change = status ^ atmel_port->irq_status_prev;
1364ab4382d2SGreg Kroah-Hartman 
1365ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1366ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1367ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1368ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1369ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1370ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1371ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1372ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1373ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1374ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1375ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1376ab4382d2SGreg Kroah-Hartman 
1377ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1378ab4382d2SGreg Kroah-Hartman 
1379ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status_prev = status;
1380ab4382d2SGreg Kroah-Hartman 	}
1381ab4382d2SGreg Kroah-Hartman 
1382a930e528SElen Song 	atmel_port->schedule_rx(port);
1383ab4382d2SGreg Kroah-Hartman 
1384ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1385ab4382d2SGreg Kroah-Hartman }
1386ab4382d2SGreg Kroah-Hartman 
138733d64c4fSElen Song static int atmel_init_property(struct atmel_uart_port *atmel_port,
138833d64c4fSElen Song 				struct platform_device *pdev)
138933d64c4fSElen Song {
139033d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1391574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
139233d64c4fSElen Song 
139333d64c4fSElen Song 	if (np) {
139433d64c4fSElen Song 		/* DMA/PDC usage specification */
139533d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
139633d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
139733d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
139833d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
139933d64c4fSElen Song 			} else {
140033d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
140133d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
140233d64c4fSElen Song 			}
140333d64c4fSElen Song 		} else {
140433d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
140533d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
140633d64c4fSElen Song 		}
140733d64c4fSElen Song 
140833d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
140933d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
141033d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
141133d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
141233d64c4fSElen Song 			} else {
141333d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
141433d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
141533d64c4fSElen Song 			}
141633d64c4fSElen Song 		} else {
141733d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
141833d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
141933d64c4fSElen Song 		}
142033d64c4fSElen Song 
142133d64c4fSElen Song 	} else {
142233d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
142333d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
142433d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
142533d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
142633d64c4fSElen Song 	}
142733d64c4fSElen Song 
142833d64c4fSElen Song 	return 0;
142933d64c4fSElen Song }
143033d64c4fSElen Song 
143133d64c4fSElen Song static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
143233d64c4fSElen Song 				struct platform_device *pdev)
143333d64c4fSElen Song {
143433d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1435574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
143633d64c4fSElen Song 
143733d64c4fSElen Song 	if (np) {
143833d64c4fSElen Song 		u32 rs485_delay[2];
143933d64c4fSElen Song 		/* rs485 properties */
144033d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
144133d64c4fSElen Song 					rs485_delay, 2) == 0) {
144233d64c4fSElen Song 			struct serial_rs485 *rs485conf = &atmel_port->rs485;
144333d64c4fSElen Song 
144433d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
144533d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
144633d64c4fSElen Song 			rs485conf->flags = 0;
144733d64c4fSElen Song 
144833d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
144933d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
145033d64c4fSElen Song 
145133d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
145233d64c4fSElen Song 								NULL))
145333d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
145433d64c4fSElen Song 		}
145533d64c4fSElen Song 	} else {
145633d64c4fSElen Song 		atmel_port->rs485       = pdata->rs485;
145733d64c4fSElen Song 	}
145833d64c4fSElen Song 
145933d64c4fSElen Song }
146033d64c4fSElen Song 
1461a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1462a930e528SElen Song {
1463a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1464a930e528SElen Song 
146534df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
146634df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
146734df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
146834df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
146934df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1470a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1471a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1472a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1473a930e528SElen Song 	} else {
1474a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1475a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1476a930e528SElen Song 		atmel_port->release_rx = NULL;
1477a930e528SElen Song 	}
1478a930e528SElen Song 
147908f738beSElen Song 	if (atmel_use_dma_tx(port)) {
148008f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
148108f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
148208f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
148308f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1484a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1485a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1486a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1487a930e528SElen Song 	} else {
1488a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1489a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1490a930e528SElen Song 		atmel_port->release_tx = NULL;
1491a930e528SElen Song 	}
1492a930e528SElen Song }
1493a930e528SElen Song 
1494ab4382d2SGreg Kroah-Hartman /*
1495055560b0SElen Song  * Get ip name usart or uart
1496055560b0SElen Song  */
1497892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1498055560b0SElen Song {
1499055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1500055560b0SElen Song 	int name = UART_GET_IP_NAME(port);
1501731d9caeSNicolas Ferre 	u32 version;
1502055560b0SElen Song 	int usart, uart;
1503055560b0SElen Song 	/* usart and uart ascii */
1504055560b0SElen Song 	usart = 0x55534152;
1505055560b0SElen Song 	uart = 0x44424755;
1506055560b0SElen Song 
1507055560b0SElen Song 	atmel_port->is_usart = false;
1508055560b0SElen Song 
1509055560b0SElen Song 	if (name == usart) {
1510055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1511055560b0SElen Song 		atmel_port->is_usart = true;
1512055560b0SElen Song 	} else if (name == uart) {
1513055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1514055560b0SElen Song 		atmel_port->is_usart = false;
1515055560b0SElen Song 	} else {
1516731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
1517731d9caeSNicolas Ferre 		version = UART_GET_IP_VERSION(port);
1518731d9caeSNicolas Ferre 		switch (version) {
1519731d9caeSNicolas Ferre 		case 0x302:
1520731d9caeSNicolas Ferre 		case 0x10213:
1521731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1522731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1523731d9caeSNicolas Ferre 			break;
1524731d9caeSNicolas Ferre 		case 0x203:
1525731d9caeSNicolas Ferre 		case 0x10202:
1526731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1527731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1528731d9caeSNicolas Ferre 			break;
1529731d9caeSNicolas Ferre 		default:
1530731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1531731d9caeSNicolas Ferre 		}
1532055560b0SElen Song 	}
1533055560b0SElen Song }
1534055560b0SElen Song 
1535055560b0SElen Song /*
1536ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1537ab4382d2SGreg Kroah-Hartman  */
1538ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1539ab4382d2SGreg Kroah-Hartman {
154033d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1541ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1542ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1543ab4382d2SGreg Kroah-Hartman 	int retval;
1544ab4382d2SGreg Kroah-Hartman 
1545ab4382d2SGreg Kroah-Hartman 	/*
1546ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1547ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1548ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1549ab4382d2SGreg Kroah-Hartman 	 */
1550ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1551ab4382d2SGreg Kroah-Hartman 
1552ab4382d2SGreg Kroah-Hartman 	/*
1553ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1554ab4382d2SGreg Kroah-Hartman 	 */
1555ab4382d2SGreg Kroah-Hartman 	retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
1556ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1557ab4382d2SGreg Kroah-Hartman 	if (retval) {
1558*ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1559ab4382d2SGreg Kroah-Hartman 		return retval;
1560ab4382d2SGreg Kroah-Hartman 	}
1561ab4382d2SGreg Kroah-Hartman 
1562ab4382d2SGreg Kroah-Hartman 	/*
1563ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1564ab4382d2SGreg Kroah-Hartman 	 */
156533d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
156633d64c4fSElen Song 
1567a930e528SElen Song 	if (atmel_port->prepare_rx) {
1568a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1569a930e528SElen Song 		if (retval < 0)
1570a930e528SElen Song 			atmel_set_ops(port);
1571ab4382d2SGreg Kroah-Hartman 	}
1572ab4382d2SGreg Kroah-Hartman 
1573a930e528SElen Song 	if (atmel_port->prepare_tx) {
1574a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1575a930e528SElen Song 		if (retval < 0)
1576a930e528SElen Song 			atmel_set_ops(port);
1577ab4382d2SGreg Kroah-Hartman 	}
1578ab4382d2SGreg Kroah-Hartman 	/*
1579ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "open" function (to register
1580ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
1581ab4382d2SGreg Kroah-Hartman 	 */
1582ab4382d2SGreg Kroah-Hartman 	if (atmel_open_hook) {
1583ab4382d2SGreg Kroah-Hartman 		retval = atmel_open_hook(port);
1584ab4382d2SGreg Kroah-Hartman 		if (retval) {
1585ab4382d2SGreg Kroah-Hartman 			free_irq(port->irq, port);
1586ab4382d2SGreg Kroah-Hartman 			return retval;
1587ab4382d2SGreg Kroah-Hartman 		}
1588ab4382d2SGreg Kroah-Hartman 	}
1589ab4382d2SGreg Kroah-Hartman 
1590ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1591ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status_prev = UART_GET_CSR(port);
1592ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1593ab4382d2SGreg Kroah-Hartman 
1594ab4382d2SGreg Kroah-Hartman 	/*
1595ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1596ab4382d2SGreg Kroah-Hartman 	 */
1597ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1598ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
1599ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1600ab4382d2SGreg Kroah-Hartman 
16012e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
16022e68c22fSElen Song 			atmel_uart_timer_callback,
16032e68c22fSElen Song 			(unsigned long)port);
16048bc661bfSMarek Roszko 
16058bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
16068bc661bfSMarek Roszko 		/* set UART timeout */
16078bc661bfSMarek Roszko 		if (!atmel_port->is_usart) {
16082e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
16092e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
16102e68c22fSElen Song 		/* set USART timeout */
16112e68c22fSElen Song 		} else {
1612ab4382d2SGreg Kroah-Hartman 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1613ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_STTTO);
1614ab4382d2SGreg Kroah-Hartman 
1615ab4382d2SGreg Kroah-Hartman 			UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
16162e68c22fSElen Song 		}
1617ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
1618ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
161934df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
16202e68c22fSElen Song 		/* set UART timeout */
16212e68c22fSElen Song 		if (!atmel_port->is_usart) {
16222e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
16232e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
16242e68c22fSElen Song 		/* set USART timeout */
16252e68c22fSElen Song 		} else {
162634df42f5SElen Song 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
162734df42f5SElen Song 			UART_PUT_CR(port, ATMEL_US_STTTO);
162834df42f5SElen Song 
162934df42f5SElen Song 			UART_PUT_IER(port, ATMEL_US_TIMEOUT);
16302e68c22fSElen Song 		}
1631ab4382d2SGreg Kroah-Hartman 	} else {
1632ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
1633ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
1634ab4382d2SGreg Kroah-Hartman 	}
1635ab4382d2SGreg Kroah-Hartman 
1636ab4382d2SGreg Kroah-Hartman 	return 0;
1637ab4382d2SGreg Kroah-Hartman }
1638ab4382d2SGreg Kroah-Hartman 
1639ab4382d2SGreg Kroah-Hartman /*
1640ab4382d2SGreg Kroah-Hartman  * Disable the port
1641ab4382d2SGreg Kroah-Hartman  */
1642ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1643ab4382d2SGreg Kroah-Hartman {
1644ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
16450cc7c6c7SMarek Roszko 
1646ab4382d2SGreg Kroah-Hartman 	/*
16478bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
16488bc661bfSMarek Roszko 	 * cleanup
16498bc661bfSMarek Roszko 	 */
16508bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
16518bc661bfSMarek Roszko 
16528bc661bfSMarek Roszko 	/*
16530cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
16540cc7c6c7SMarek Roszko 	 * we destroy the buffers
16550cc7c6c7SMarek Roszko 	 */
16560cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
16570cc7c6c7SMarek Roszko 
16580cc7c6c7SMarek Roszko 	/*
16590cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
16600cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1661ab4382d2SGreg Kroah-Hartman 	 */
1662ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1663ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1664ab4382d2SGreg Kroah-Hartman 
16650cc7c6c7SMarek Roszko 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
16660cc7c6c7SMarek Roszko 	UART_PUT_IDR(port, -1);
16670cc7c6c7SMarek Roszko 
16680cc7c6c7SMarek Roszko 
1669ab4382d2SGreg Kroah-Hartman 	/*
1670ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1671ab4382d2SGreg Kroah-Hartman 	 */
1672a930e528SElen Song 	if (atmel_port->release_rx)
1673a930e528SElen Song 		atmel_port->release_rx(port);
1674a930e528SElen Song 	if (atmel_port->release_tx)
1675a930e528SElen Song 		atmel_port->release_tx(port);
1676ab4382d2SGreg Kroah-Hartman 
1677ab4382d2SGreg Kroah-Hartman 	/*
1678bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1679bb7e73c5SMark Deneen 	 */
1680bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1681bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1682bb7e73c5SMark Deneen 
1683bb7e73c5SMark Deneen 	/*
1684ab4382d2SGreg Kroah-Hartman 	 * Free the interrupt
1685ab4382d2SGreg Kroah-Hartman 	 */
1686ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1687ab4382d2SGreg Kroah-Hartman 
1688ab4382d2SGreg Kroah-Hartman 	/*
1689ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "close" function (to unregister
1690ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
1691ab4382d2SGreg Kroah-Hartman 	 */
1692ab4382d2SGreg Kroah-Hartman 	if (atmel_close_hook)
1693ab4382d2SGreg Kroah-Hartman 		atmel_close_hook(port);
1694ab4382d2SGreg Kroah-Hartman }
1695ab4382d2SGreg Kroah-Hartman 
1696ab4382d2SGreg Kroah-Hartman /*
1697ab4382d2SGreg Kroah-Hartman  * Flush any TX data submitted for DMA. Called when the TX circular
1698ab4382d2SGreg Kroah-Hartman  * buffer is reset.
1699ab4382d2SGreg Kroah-Hartman  */
1700ab4382d2SGreg Kroah-Hartman static void atmel_flush_buffer(struct uart_port *port)
1701ab4382d2SGreg Kroah-Hartman {
1702ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1703ab4382d2SGreg Kroah-Hartman 
170464e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
1705ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, 0);
1706ab4382d2SGreg Kroah-Hartman 		atmel_port->pdc_tx.ofs = 0;
1707ab4382d2SGreg Kroah-Hartman 	}
1708ab4382d2SGreg Kroah-Hartman }
1709ab4382d2SGreg Kroah-Hartman 
1710ab4382d2SGreg Kroah-Hartman /*
1711ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1712ab4382d2SGreg Kroah-Hartman  */
1713ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1714ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1715ab4382d2SGreg Kroah-Hartman {
1716ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1717ab4382d2SGreg Kroah-Hartman 
1718ab4382d2SGreg Kroah-Hartman 	switch (state) {
1719ab4382d2SGreg Kroah-Hartman 	case 0:
1720ab4382d2SGreg Kroah-Hartman 		/*
1721ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1722ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1723ab4382d2SGreg Kroah-Hartman 		 */
172491f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1725ab4382d2SGreg Kroah-Hartman 
1726ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1727ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->backup_imr);
1728ab4382d2SGreg Kroah-Hartman 		break;
1729ab4382d2SGreg Kroah-Hartman 	case 3:
1730ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1731ab4382d2SGreg Kroah-Hartman 		atmel_port->backup_imr = UART_GET_IMR(port);
1732ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, -1);
1733ab4382d2SGreg Kroah-Hartman 
1734ab4382d2SGreg Kroah-Hartman 		/*
1735ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1736ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1737ab4382d2SGreg Kroah-Hartman 		 */
173891f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1739ab4382d2SGreg Kroah-Hartman 		break;
1740ab4382d2SGreg Kroah-Hartman 	default:
1741*ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1742ab4382d2SGreg Kroah-Hartman 	}
1743ab4382d2SGreg Kroah-Hartman }
1744ab4382d2SGreg Kroah-Hartman 
1745ab4382d2SGreg Kroah-Hartman /*
1746ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1747ab4382d2SGreg Kroah-Hartman  */
1748ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1749ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1750ab4382d2SGreg Kroah-Hartman {
1751ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1752ab4382d2SGreg Kroah-Hartman 	unsigned int mode, imr, quot, baud;
1753ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1754ab4382d2SGreg Kroah-Hartman 
1755ab4382d2SGreg Kroah-Hartman 	/* Get current mode register */
1756ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1757ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_NBSTOP | ATMEL_US_PAR
1758ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_USMODE);
1759ab4382d2SGreg Kroah-Hartman 
1760ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1761ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1762ab4382d2SGreg Kroah-Hartman 
1763ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1764ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1765ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1766ab4382d2SGreg Kroah-Hartman 	}
1767ab4382d2SGreg Kroah-Hartman 
1768ab4382d2SGreg Kroah-Hartman 	/* byte size */
1769ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1770ab4382d2SGreg Kroah-Hartman 	case CS5:
1771ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1772ab4382d2SGreg Kroah-Hartman 		break;
1773ab4382d2SGreg Kroah-Hartman 	case CS6:
1774ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1775ab4382d2SGreg Kroah-Hartman 		break;
1776ab4382d2SGreg Kroah-Hartman 	case CS7:
1777ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1778ab4382d2SGreg Kroah-Hartman 		break;
1779ab4382d2SGreg Kroah-Hartman 	default:
1780ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1781ab4382d2SGreg Kroah-Hartman 		break;
1782ab4382d2SGreg Kroah-Hartman 	}
1783ab4382d2SGreg Kroah-Hartman 
1784ab4382d2SGreg Kroah-Hartman 	/* stop bits */
1785ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
1786ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
1787ab4382d2SGreg Kroah-Hartman 
1788ab4382d2SGreg Kroah-Hartman 	/* parity */
1789ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
1790ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
1791ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
1792ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
1793ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
1794ab4382d2SGreg Kroah-Hartman 			else
1795ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
1796ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
1797ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
1798ab4382d2SGreg Kroah-Hartman 		else
1799ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
1800ab4382d2SGreg Kroah-Hartman 	} else
1801ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
1802ab4382d2SGreg Kroah-Hartman 
1803ab4382d2SGreg Kroah-Hartman 	/* hardware handshake (RTS/CTS) */
1804ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
1805ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_HWHS;
1806ab4382d2SGreg Kroah-Hartman 	else
1807ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_NORMAL;
1808ab4382d2SGreg Kroah-Hartman 
1809ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
1810ab4382d2SGreg Kroah-Hartman 
1811ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
1812ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
1813ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1814ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & (BRKINT | PARMRK))
1815ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
1816ab4382d2SGreg Kroah-Hartman 
181764e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
1818ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
1819ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, port->read_status_mask);
1820ab4382d2SGreg Kroah-Hartman 
1821ab4382d2SGreg Kroah-Hartman 	/*
1822ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
1823ab4382d2SGreg Kroah-Hartman 	 */
1824ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
1825ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
1826ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1827ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
1828ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
1829ab4382d2SGreg Kroah-Hartman 		/*
1830ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
1831ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
1832ab4382d2SGreg Kroah-Hartman 		 */
1833ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
1834ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
1835ab4382d2SGreg Kroah-Hartman 	}
1836ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
1837ab4382d2SGreg Kroah-Hartman 
1838ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
1839ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
1840ab4382d2SGreg Kroah-Hartman 
1841ab4382d2SGreg Kroah-Hartman 	/*
1842ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
1843ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
1844ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
1845ab4382d2SGreg Kroah-Hartman 	 */
1846ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
1847ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1848ab4382d2SGreg Kroah-Hartman 
1849ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
1850ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1851ab4382d2SGreg Kroah-Hartman 
1852ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
1853ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
1854ab4382d2SGreg Kroah-Hartman 
1855ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
185693f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
1857ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
1858ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
1859ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
1860ab4382d2SGreg Kroah-Hartman 	}
1861ab4382d2SGreg Kroah-Hartman 
1862ab4382d2SGreg Kroah-Hartman 	/* set the parity, stop bits and data size */
1863ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
1864ab4382d2SGreg Kroah-Hartman 
1865ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
1866ab4382d2SGreg Kroah-Hartman 	UART_PUT_BRGR(port, quot);
1867ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1868ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1869ab4382d2SGreg Kroah-Hartman 
1870ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
1871ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
1872ab4382d2SGreg Kroah-Hartman 
1873ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
1874ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
1875ab4382d2SGreg Kroah-Hartman 		port->ops->enable_ms(port);
1876ab4382d2SGreg Kroah-Hartman 
1877ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
1878ab4382d2SGreg Kroah-Hartman }
1879ab4382d2SGreg Kroah-Hartman 
188042bd7a4fSViktar Palstsiuk static void atmel_set_ldisc(struct uart_port *port, int new)
188142bd7a4fSViktar Palstsiuk {
1882b54bf3b2SJiri Slaby 	if (new == N_PPS) {
188342bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
188442bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
188542bd7a4fSViktar Palstsiuk 	} else {
188642bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
188742bd7a4fSViktar Palstsiuk 	}
188842bd7a4fSViktar Palstsiuk }
188942bd7a4fSViktar Palstsiuk 
1890ab4382d2SGreg Kroah-Hartman /*
1891ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
1892ab4382d2SGreg Kroah-Hartman  */
1893ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
1894ab4382d2SGreg Kroah-Hartman {
1895ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1896ab4382d2SGreg Kroah-Hartman }
1897ab4382d2SGreg Kroah-Hartman 
1898ab4382d2SGreg Kroah-Hartman /*
1899ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
1900ab4382d2SGreg Kroah-Hartman  */
1901ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
1902ab4382d2SGreg Kroah-Hartman {
1903ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1904ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1905ab4382d2SGreg Kroah-Hartman 
1906ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
1907ab4382d2SGreg Kroah-Hartman 
1908ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1909ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
1910ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
1911ab4382d2SGreg Kroah-Hartman 	}
1912ab4382d2SGreg Kroah-Hartman }
1913ab4382d2SGreg Kroah-Hartman 
1914ab4382d2SGreg Kroah-Hartman /*
1915ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
1916ab4382d2SGreg Kroah-Hartman  */
1917ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
1918ab4382d2SGreg Kroah-Hartman {
1919ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1920ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1921ab4382d2SGreg Kroah-Hartman 
1922ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1923ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
1924ab4382d2SGreg Kroah-Hartman 
1925ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1926ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
1927ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
1928ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
1929ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
1930ab4382d2SGreg Kroah-Hartman 		}
1931ab4382d2SGreg Kroah-Hartman 	}
1932ab4382d2SGreg Kroah-Hartman 
1933ab4382d2SGreg Kroah-Hartman 	return 0;
1934ab4382d2SGreg Kroah-Hartman }
1935ab4382d2SGreg Kroah-Hartman 
1936ab4382d2SGreg Kroah-Hartman /*
1937ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
1938ab4382d2SGreg Kroah-Hartman  */
1939ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
1940ab4382d2SGreg Kroah-Hartman {
1941ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
1942ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
1943ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
1944ab4382d2SGreg Kroah-Hartman 	}
1945ab4382d2SGreg Kroah-Hartman }
1946ab4382d2SGreg Kroah-Hartman 
1947ab4382d2SGreg Kroah-Hartman /*
1948ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
1949ab4382d2SGreg Kroah-Hartman  */
1950ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1951ab4382d2SGreg Kroah-Hartman {
1952ab4382d2SGreg Kroah-Hartman 	int ret = 0;
1953ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1954ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1955ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
1956ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1957ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
1958ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1959ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
1960ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1961ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
1962ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1963ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
1964ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1965ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
1966ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1967ab4382d2SGreg Kroah-Hartman 	return ret;
1968ab4382d2SGreg Kroah-Hartman }
1969ab4382d2SGreg Kroah-Hartman 
1970ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
1971ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
1972ab4382d2SGreg Kroah-Hartman {
1973ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1974ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1975ab4382d2SGreg Kroah-Hartman 
1976ab4382d2SGreg Kroah-Hartman 	return UART_GET_CHAR(port);
1977ab4382d2SGreg Kroah-Hartman }
1978ab4382d2SGreg Kroah-Hartman 
1979ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1980ab4382d2SGreg Kroah-Hartman {
1981ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1982ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1983ab4382d2SGreg Kroah-Hartman 
1984ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
1985ab4382d2SGreg Kroah-Hartman }
1986ab4382d2SGreg Kroah-Hartman #endif
1987ab4382d2SGreg Kroah-Hartman 
1988ab4382d2SGreg Kroah-Hartman static int
1989ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1990ab4382d2SGreg Kroah-Hartman {
1991ab4382d2SGreg Kroah-Hartman 	struct serial_rs485 rs485conf;
1992ab4382d2SGreg Kroah-Hartman 
1993ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1994ab4382d2SGreg Kroah-Hartman 	case TIOCSRS485:
1995ab4382d2SGreg Kroah-Hartman 		if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1996ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
1997ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
1998ab4382d2SGreg Kroah-Hartman 
1999ab4382d2SGreg Kroah-Hartman 		atmel_config_rs485(port, &rs485conf);
2000ab4382d2SGreg Kroah-Hartman 		break;
2001ab4382d2SGreg Kroah-Hartman 
2002ab4382d2SGreg Kroah-Hartman 	case TIOCGRS485:
2003ab4382d2SGreg Kroah-Hartman 		if (copy_to_user((struct serial_rs485 *) arg,
2004ab4382d2SGreg Kroah-Hartman 					&(to_atmel_uart_port(port)->rs485),
2005ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
2006ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
2007ab4382d2SGreg Kroah-Hartman 		break;
2008ab4382d2SGreg Kroah-Hartman 
2009ab4382d2SGreg Kroah-Hartman 	default:
2010ab4382d2SGreg Kroah-Hartman 		return -ENOIOCTLCMD;
2011ab4382d2SGreg Kroah-Hartman 	}
2012ab4382d2SGreg Kroah-Hartman 	return 0;
2013ab4382d2SGreg Kroah-Hartman }
2014ab4382d2SGreg Kroah-Hartman 
2015ab4382d2SGreg Kroah-Hartman 
2016ab4382d2SGreg Kroah-Hartman 
2017ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2018ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2019ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2020ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2021ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2022ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2023ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2024ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2025ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2026ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2027ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2028ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2029ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
203042bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2031ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2032ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2033ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2034ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2035ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2036ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2037ab4382d2SGreg Kroah-Hartman 	.ioctl		= atmel_ioctl,
2038ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2039ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2040ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2041ab4382d2SGreg Kroah-Hartman #endif
2042ab4382d2SGreg Kroah-Hartman };
2043ab4382d2SGreg Kroah-Hartman 
2044ab4382d2SGreg Kroah-Hartman /*
2045ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2046ab4382d2SGreg Kroah-Hartman  */
204791f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2048ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2049ab4382d2SGreg Kroah-Hartman {
205091f8c2d8SBoris BREZILLON 	int ret;
2051ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2052574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2053ab4382d2SGreg Kroah-Hartman 
205433d64c4fSElen Song 	if (!atmel_init_property(atmel_port, pdev))
2055a930e528SElen Song 		atmel_set_ops(port);
2056a930e528SElen Song 
205733d64c4fSElen Song 	atmel_init_rs485(atmel_port, pdev);
205833d64c4fSElen Song 
2059ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2060ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2061ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2062ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2063ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2064ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2065ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
2066ab4382d2SGreg Kroah-Hartman 
2067ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2068ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
2069ab4382d2SGreg Kroah-Hartman 
2070ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2071ab4382d2SGreg Kroah-Hartman 
20725fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2073ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
20741acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2075588edbf3SNicolas Ferre 	} else {
2076ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2077ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2078ab4382d2SGreg Kroah-Hartman 	}
2079ab4382d2SGreg Kroah-Hartman 
2080ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2081ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2082ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
208391f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
208491f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
208591f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
208691f8c2d8SBoris BREZILLON 			return ret;
208791f8c2d8SBoris BREZILLON 		}
208891f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
208991f8c2d8SBoris BREZILLON 		if (ret) {
209091f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
209191f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
209291f8c2d8SBoris BREZILLON 			return ret;
209391f8c2d8SBoris BREZILLON 		}
2094ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
209591f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2096ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2097ab4382d2SGreg Kroah-Hartman 	}
2098ab4382d2SGreg Kroah-Hartman 
2099ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2100ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2101ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
210264e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2103ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2104ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2105ab4382d2SGreg Kroah-Hartman 	} else {
2106ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2107ab4382d2SGreg Kroah-Hartman 	}
210891f8c2d8SBoris BREZILLON 
210991f8c2d8SBoris BREZILLON 	return 0;
2110ab4382d2SGreg Kroah-Hartman }
2111ab4382d2SGreg Kroah-Hartman 
211269f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
211369f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2114ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2115ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2116ab4382d2SGreg Kroah-Hartman {
2117ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2118ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2119ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
2120ab4382d2SGreg Kroah-Hartman }
2121ab4382d2SGreg Kroah-Hartman 
2122ab4382d2SGreg Kroah-Hartman /*
2123ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2124ab4382d2SGreg Kroah-Hartman  */
2125ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2126ab4382d2SGreg Kroah-Hartman {
2127ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2128ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2129ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2130ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2131ab4382d2SGreg Kroah-Hartman 
2132ab4382d2SGreg Kroah-Hartman 	/*
2133ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2134ab4382d2SGreg Kroah-Hartman 	 */
2135ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
2136ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2137ab4382d2SGreg Kroah-Hartman 
2138ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
2139ab4382d2SGreg Kroah-Hartman 	pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2140ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2141ab4382d2SGreg Kroah-Hartman 
2142ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2143ab4382d2SGreg Kroah-Hartman 
2144ab4382d2SGreg Kroah-Hartman 	/*
2145ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2146ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2147ab4382d2SGreg Kroah-Hartman 	 */
2148ab4382d2SGreg Kroah-Hartman 	do {
2149ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
2150ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2151ab4382d2SGreg Kroah-Hartman 
2152ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2153ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
2154ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2155ab4382d2SGreg Kroah-Hartman 
2156ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
2157ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
2158ab4382d2SGreg Kroah-Hartman }
2159ab4382d2SGreg Kroah-Hartman 
2160ab4382d2SGreg Kroah-Hartman /*
2161ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2162ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2163ab4382d2SGreg Kroah-Hartman  */
2164ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2165ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2166ab4382d2SGreg Kroah-Hartman {
2167ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2168ab4382d2SGreg Kroah-Hartman 
2169ab4382d2SGreg Kroah-Hartman 	/*
2170ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2171ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2172ab4382d2SGreg Kroah-Hartman 	 */
2173ab4382d2SGreg Kroah-Hartman 	quot = UART_GET_BRGR(port) & ATMEL_US_CD;
2174ab4382d2SGreg Kroah-Hartman 	if (!quot)
2175ab4382d2SGreg Kroah-Hartman 		return;
2176ab4382d2SGreg Kroah-Hartman 
2177ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2178ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2179ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2180ab4382d2SGreg Kroah-Hartman 	else
2181ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2182ab4382d2SGreg Kroah-Hartman 
2183ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_PAR;
2184ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2185ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2186ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2187ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2188ab4382d2SGreg Kroah-Hartman 
2189ab4382d2SGreg Kroah-Hartman 	/*
2190ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2191ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2192ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2193ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2194ab4382d2SGreg Kroah-Hartman 	 */
2195ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2196ab4382d2SGreg Kroah-Hartman }
2197ab4382d2SGreg Kroah-Hartman 
2198ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2199ab4382d2SGreg Kroah-Hartman {
220091f8c2d8SBoris BREZILLON 	int ret;
2201ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2202ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2203ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2204ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2205ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2206ab4382d2SGreg Kroah-Hartman 
2207ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2208ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2209ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2210ab4382d2SGreg Kroah-Hartman 	}
2211ab4382d2SGreg Kroah-Hartman 
221291f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
221391f8c2d8SBoris BREZILLON 	if (ret)
221491f8c2d8SBoris BREZILLON 		return ret;
2215ab4382d2SGreg Kroah-Hartman 
2216ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
2217ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2218ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2219ab4382d2SGreg Kroah-Hartman 
2220ab4382d2SGreg Kroah-Hartman 	if (options)
2221ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2222ab4382d2SGreg Kroah-Hartman 	else
2223ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2224ab4382d2SGreg Kroah-Hartman 
2225ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2226ab4382d2SGreg Kroah-Hartman }
2227ab4382d2SGreg Kroah-Hartman 
2228ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2229ab4382d2SGreg Kroah-Hartman 
2230ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2231ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2232ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2233ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2234ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2235ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2236ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2237ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2238ab4382d2SGreg Kroah-Hartman };
2239ab4382d2SGreg Kroah-Hartman 
2240ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2241ab4382d2SGreg Kroah-Hartman 
2242ab4382d2SGreg Kroah-Hartman /*
2243ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2244ab4382d2SGreg Kroah-Hartman  */
2245ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2246ab4382d2SGreg Kroah-Hartman {
224791f8c2d8SBoris BREZILLON 	int ret;
2248ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
22490d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2250574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2251efb8d21bSLinus Torvalds 		int id = pdata->num;
22524cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
22530d0a3cc1SVoss, Nikolaus 
22544cbf9f48SNicolas Ferre 		port->backup_imr = 0;
22554cbf9f48SNicolas Ferre 		port->uart.line = id;
22564cbf9f48SNicolas Ferre 
22574cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
225891f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
225991f8c2d8SBoris BREZILLON 		if (ret)
226091f8c2d8SBoris BREZILLON 			return ret;
2261ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2262ab4382d2SGreg Kroah-Hartman 	}
2263ab4382d2SGreg Kroah-Hartman 
2264ab4382d2SGreg Kroah-Hartman 	return 0;
2265ab4382d2SGreg Kroah-Hartman }
2266ab4382d2SGreg Kroah-Hartman 
2267ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2268ab4382d2SGreg Kroah-Hartman 
2269ab4382d2SGreg Kroah-Hartman /*
2270ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2271ab4382d2SGreg Kroah-Hartman  */
2272ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2273ab4382d2SGreg Kroah-Hartman {
2274ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2275ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2276ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2277ab4382d2SGreg Kroah-Hartman 
2278ab4382d2SGreg Kroah-Hartman 	return 0;
2279ab4382d2SGreg Kroah-Hartman }
2280ab4382d2SGreg Kroah-Hartman 
2281ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2282ab4382d2SGreg Kroah-Hartman 
2283ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2284ab4382d2SGreg Kroah-Hartman {
2285ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2286ab4382d2SGreg Kroah-Hartman }
2287ab4382d2SGreg Kroah-Hartman 
2288ab4382d2SGreg Kroah-Hartman #else
2289ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2290ab4382d2SGreg Kroah-Hartman 
2291ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2292ab4382d2SGreg Kroah-Hartman {
2293ab4382d2SGreg Kroah-Hartman 	return false;
2294ab4382d2SGreg Kroah-Hartman }
2295ab4382d2SGreg Kroah-Hartman #endif
2296ab4382d2SGreg Kroah-Hartman 
2297ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2298ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2299ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2300ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2301ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2302ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2303ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2304ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2305ab4382d2SGreg Kroah-Hartman };
2306ab4382d2SGreg Kroah-Hartman 
2307ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2308ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2309ab4382d2SGreg Kroah-Hartman {
2310ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2311ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2312ab4382d2SGreg Kroah-Hartman #else
2313ab4382d2SGreg Kroah-Hartman 	return false;
2314ab4382d2SGreg Kroah-Hartman #endif
2315ab4382d2SGreg Kroah-Hartman }
2316ab4382d2SGreg Kroah-Hartman 
2317ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2318ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2319ab4382d2SGreg Kroah-Hartman {
2320ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2321ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2322ab4382d2SGreg Kroah-Hartman 
2323ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2324ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
2325ab4382d2SGreg Kroah-Hartman 		while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2326ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2327ab4382d2SGreg Kroah-Hartman 	}
2328ab4382d2SGreg Kroah-Hartman 
2329ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2330ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2331ab4382d2SGreg Kroah-Hartman 	if (atmel_serial_clk_will_stop())
2332ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
2333ab4382d2SGreg Kroah-Hartman 
2334ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2335ab4382d2SGreg Kroah-Hartman 
2336ab4382d2SGreg Kroah-Hartman 	return 0;
2337ab4382d2SGreg Kroah-Hartman }
2338ab4382d2SGreg Kroah-Hartman 
2339ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2340ab4382d2SGreg Kroah-Hartman {
2341ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2342ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2343ab4382d2SGreg Kroah-Hartman 
2344ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2345ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2346ab4382d2SGreg Kroah-Hartman 
2347ab4382d2SGreg Kroah-Hartman 	return 0;
2348ab4382d2SGreg Kroah-Hartman }
2349ab4382d2SGreg Kroah-Hartman #else
2350ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2351ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2352ab4382d2SGreg Kroah-Hartman #endif
2353ab4382d2SGreg Kroah-Hartman 
23549671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2355ab4382d2SGreg Kroah-Hartman {
2356ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
23575fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2358574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2359ab4382d2SGreg Kroah-Hartman 	void *data;
23604cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2361ab4382d2SGreg Kroah-Hartman 
2362ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2363ab4382d2SGreg Kroah-Hartman 
23645fbe46b6SNicolas Ferre 	if (np)
23655fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
23665fbe46b6SNicolas Ferre 	else
23674cbf9f48SNicolas Ferre 		if (pdata)
23684cbf9f48SNicolas Ferre 			ret = pdata->num;
23694cbf9f48SNicolas Ferre 
23704cbf9f48SNicolas Ferre 	if (ret < 0)
23715fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
23724cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2373503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
23744cbf9f48SNicolas Ferre 
2375503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
23764cbf9f48SNicolas Ferre 		ret = -ENODEV;
23774cbf9f48SNicolas Ferre 		goto err;
23784cbf9f48SNicolas Ferre 	}
23794cbf9f48SNicolas Ferre 
2380503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
23814cbf9f48SNicolas Ferre 		/* port already in use */
23824cbf9f48SNicolas Ferre 		ret = -EBUSY;
23834cbf9f48SNicolas Ferre 		goto err;
23844cbf9f48SNicolas Ferre 	}
23854cbf9f48SNicolas Ferre 
23864cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2387ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
23884cbf9f48SNicolas Ferre 	port->uart.line = ret;
2389ab4382d2SGreg Kroah-Hartman 
239091f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
239191f8c2d8SBoris BREZILLON 	if (ret)
239291f8c2d8SBoris BREZILLON 		goto err;
2393ab4382d2SGreg Kroah-Hartman 
239464e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2395ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2396ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2397ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2398ab4382d2SGreg Kroah-Hartman 		if (!data)
2399ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2400ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2401ab4382d2SGreg Kroah-Hartman 	}
2402ab4382d2SGreg Kroah-Hartman 
2403ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2404ab4382d2SGreg Kroah-Hartman 	if (ret)
2405ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2406ab4382d2SGreg Kroah-Hartman 
2407ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2408ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2409ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2410ab4382d2SGreg Kroah-Hartman 		/*
2411ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
241291f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2413ab4382d2SGreg Kroah-Hartman 		 */
241491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2415ab4382d2SGreg Kroah-Hartman 	}
2416ab4382d2SGreg Kroah-Hartman #endif
2417ab4382d2SGreg Kroah-Hartman 
2418ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2419ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2420ab4382d2SGreg Kroah-Hartman 
2421fc887b15SLinus Torvalds 	if (port->rs485.flags & SER_RS485_ENABLED) {
2422fc887b15SLinus Torvalds 		UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2423fc887b15SLinus Torvalds 		UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2424fc887b15SLinus Torvalds 	}
2425fc887b15SLinus Torvalds 
2426055560b0SElen Song 	/*
2427055560b0SElen Song 	 * Get port name of usart or uart
2428055560b0SElen Song 	 */
2429892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2430055560b0SElen Song 
2431ab4382d2SGreg Kroah-Hartman 	return 0;
2432ab4382d2SGreg Kroah-Hartman 
2433ab4382d2SGreg Kroah-Hartman err_add_port:
2434ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2435ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2436ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2437ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2438ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2439ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2440ab4382d2SGreg Kroah-Hartman 	}
24414cbf9f48SNicolas Ferre err:
2442ab4382d2SGreg Kroah-Hartman 	return ret;
2443ab4382d2SGreg Kroah-Hartman }
2444ab4382d2SGreg Kroah-Hartman 
2445ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2446ab4382d2SGreg Kroah-Hartman {
2447ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2448ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2449ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2450ab4382d2SGreg Kroah-Hartman 
2451f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2452f50c995fSMarek Roszko 
2453ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2454ab4382d2SGreg Kroah-Hartman 
2455ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2456ab4382d2SGreg Kroah-Hartman 
2457ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2458ab4382d2SGreg Kroah-Hartman 
2459ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2460ab4382d2SGreg Kroah-Hartman 
2461503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
24624cbf9f48SNicolas Ferre 
2463ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2464ab4382d2SGreg Kroah-Hartman 
2465ab4382d2SGreg Kroah-Hartman 	return ret;
2466ab4382d2SGreg Kroah-Hartman }
2467ab4382d2SGreg Kroah-Hartman 
2468ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2469ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
24702d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2471ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2472ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2473ab4382d2SGreg Kroah-Hartman 	.driver		= {
2474ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
2475ab4382d2SGreg Kroah-Hartman 		.owner	= THIS_MODULE,
24765fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2477ab4382d2SGreg Kroah-Hartman 	},
2478ab4382d2SGreg Kroah-Hartman };
2479ab4382d2SGreg Kroah-Hartman 
2480ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2481ab4382d2SGreg Kroah-Hartman {
2482ab4382d2SGreg Kroah-Hartman 	int ret;
2483ab4382d2SGreg Kroah-Hartman 
2484ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2485ab4382d2SGreg Kroah-Hartman 	if (ret)
2486ab4382d2SGreg Kroah-Hartman 		return ret;
2487ab4382d2SGreg Kroah-Hartman 
2488ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2489ab4382d2SGreg Kroah-Hartman 	if (ret)
2490ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2491ab4382d2SGreg Kroah-Hartman 
2492ab4382d2SGreg Kroah-Hartman 	return ret;
2493ab4382d2SGreg Kroah-Hartman }
2494ab4382d2SGreg Kroah-Hartman 
2495ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2496ab4382d2SGreg Kroah-Hartman {
2497ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2498ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2499ab4382d2SGreg Kroah-Hartman }
2500ab4382d2SGreg Kroah-Hartman 
2501ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2502ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2503ab4382d2SGreg Kroah-Hartman 
2504ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2505ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2506ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2507ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2508