xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision bb7e73c598fb226c75f7625088a8f6a45a0fc892)
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;
8282e68c22fSElen Song 
8292e68c22fSElen Song 	if (!atmel_port->is_usart)
8302e68c22fSElen Song 		del_timer_sync(&atmel_port->uart_timer);
83134df42f5SElen Song }
83234df42f5SElen Song 
83334df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
83434df42f5SElen Song {
83534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
83634df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
83734df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
83834df42f5SElen Song 	struct dma_tx_state state;
83934df42f5SElen Song 	enum dma_status dmastat;
84034df42f5SElen Song 	size_t pending, count;
84134df42f5SElen Song 
84234df42f5SElen Song 
84334df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
84434df42f5SElen Song 	UART_PUT_CR(port, ATMEL_US_STTTO);
84534df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
84634df42f5SElen Song 				atmel_port->cookie_rx,
84734df42f5SElen Song 				&state);
84834df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
84934df42f5SElen Song 	if (dmastat == DMA_ERROR) {
85034df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
85134df42f5SElen Song 		UART_PUT_IER(port, ATMEL_US_TIMEOUT);
85234df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
85334df42f5SElen Song 		return;
85434df42f5SElen Song 	}
85534df42f5SElen Song 	/* current transfer size should no larger than dma buffer */
85634df42f5SElen Song 	pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
85734df42f5SElen Song 	BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
85834df42f5SElen Song 
85934df42f5SElen Song 	/*
86034df42f5SElen Song 	 * This will take the chars we have so far,
86134df42f5SElen Song 	 * ring->head will record the transfer size, only new bytes come
86234df42f5SElen Song 	 * will insert into the framework.
86334df42f5SElen Song 	 */
86434df42f5SElen Song 	if (pending > ring->head) {
86534df42f5SElen Song 		count = pending - ring->head;
86634df42f5SElen Song 
86734df42f5SElen Song 		atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
86834df42f5SElen Song 
86934df42f5SElen Song 		ring->head += count;
87034df42f5SElen Song 		if (ring->head == sg_dma_len(&atmel_port->sg_rx))
87134df42f5SElen Song 			ring->head = 0;
87234df42f5SElen Song 
87334df42f5SElen Song 		port->icount.rx += count;
87434df42f5SElen Song 	}
87534df42f5SElen Song 
87634df42f5SElen Song 	UART_PUT_IER(port, ATMEL_US_TIMEOUT);
87734df42f5SElen Song }
87834df42f5SElen Song 
87934df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
88034df42f5SElen Song {
88134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
88234df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
88334df42f5SElen Song 	dma_cap_mask_t		mask;
88434df42f5SElen Song 	struct dma_slave_config config;
88534df42f5SElen Song 	struct circ_buf		*ring;
88634df42f5SElen Song 	int ret, nent;
88734df42f5SElen Song 
88834df42f5SElen Song 	ring = &atmel_port->rx_ring;
88934df42f5SElen Song 
89034df42f5SElen Song 	dma_cap_zero(mask);
89134df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
89234df42f5SElen Song 
89334df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
89434df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
89534df42f5SElen Song 		goto chan_err;
89634df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
89734df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
89834df42f5SElen Song 
89934df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
90034df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
90134df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
90234df42f5SElen Song 	BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
90334df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
90434df42f5SElen Song 			virt_to_page(ring->buf),
90534df42f5SElen Song 			ATMEL_SERIAL_RINGSIZE,
90634df42f5SElen Song 			(int)ring->buf & ~PAGE_MASK);
90734df42f5SElen Song 			nent = dma_map_sg(port->dev,
90834df42f5SElen Song 					&atmel_port->sg_rx,
90934df42f5SElen Song 					1,
91034df42f5SElen Song 					DMA_DEV_TO_MEM);
91134df42f5SElen Song 
91234df42f5SElen Song 	if (!nent) {
91334df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
91434df42f5SElen Song 		goto chan_err;
91534df42f5SElen Song 	} else {
91634df42f5SElen Song 		dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
91734df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
91834df42f5SElen Song 			ring->buf,
91934df42f5SElen Song 			sg_dma_address(&atmel_port->sg_rx));
92034df42f5SElen Song 	}
92134df42f5SElen Song 
92234df42f5SElen Song 	/* Configure the slave DMA */
92334df42f5SElen Song 	memset(&config, 0, sizeof(config));
92434df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
92534df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
92634df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
92734df42f5SElen Song 
92834df42f5SElen Song 	ret = dmaengine_device_control(atmel_port->chan_rx,
92934df42f5SElen Song 					DMA_SLAVE_CONFIG,
93034df42f5SElen Song 					(unsigned long)&config);
93134df42f5SElen Song 	if (ret) {
93234df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
93334df42f5SElen Song 		goto chan_err;
93434df42f5SElen Song 	}
93534df42f5SElen Song 	/*
93634df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
93734df42f5SElen Song 	 * each one is half ring buffer size
93834df42f5SElen Song 	 */
93934df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
94034df42f5SElen Song 				sg_dma_address(&atmel_port->sg_rx),
94134df42f5SElen Song 				sg_dma_len(&atmel_port->sg_rx),
94234df42f5SElen Song 				sg_dma_len(&atmel_port->sg_rx)/2,
94334df42f5SElen Song 				DMA_DEV_TO_MEM,
94434df42f5SElen Song 				DMA_PREP_INTERRUPT);
94534df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
94634df42f5SElen Song 	desc->callback_param = port;
94734df42f5SElen Song 	atmel_port->desc_rx = desc;
94834df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
94934df42f5SElen Song 
95034df42f5SElen Song 	return 0;
95134df42f5SElen Song 
95234df42f5SElen Song chan_err:
95334df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
95434df42f5SElen Song 	atmel_port->use_dma_rx = 0;
95534df42f5SElen Song 	if (atmel_port->chan_rx)
95634df42f5SElen Song 		atmel_release_rx_dma(port);
95734df42f5SElen Song 	return -EINVAL;
95834df42f5SElen Song }
95934df42f5SElen Song 
9602e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
9612e68c22fSElen Song {
9622e68c22fSElen Song 	struct uart_port *port = (void *)data;
9632e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
9642e68c22fSElen Song 
9652e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
9662e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
9672e68c22fSElen Song }
9682e68c22fSElen Song 
969ab4382d2SGreg Kroah-Hartman /*
970ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
971ab4382d2SGreg Kroah-Hartman  */
972ab4382d2SGreg Kroah-Hartman static void
973ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
974ab4382d2SGreg Kroah-Hartman {
975ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
976ab4382d2SGreg Kroah-Hartman 
97764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
978ab4382d2SGreg Kroah-Hartman 		/*
979ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
980ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
981ab4382d2SGreg Kroah-Hartman 		 *
982ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
983ab4382d2SGreg Kroah-Hartman 		 * the moment.
984ab4382d2SGreg Kroah-Hartman 		 */
985ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
986ab4382d2SGreg Kroah-Hartman 			UART_PUT_IDR(port, (ATMEL_US_ENDRX
987ab4382d2SGreg Kroah-Hartman 						| ATMEL_US_TIMEOUT));
988ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
989ab4382d2SGreg Kroah-Hartman 		}
990ab4382d2SGreg Kroah-Hartman 
991ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
992ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
993ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
994ab4382d2SGreg Kroah-Hartman 	}
995ab4382d2SGreg Kroah-Hartman 
99634df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
99734df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
99834df42f5SElen Song 			UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
99934df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
100034df42f5SElen Song 		}
100134df42f5SElen Song 	}
100234df42f5SElen Song 
1003ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1004ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1005ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1006ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1007ab4382d2SGreg Kroah-Hartman 		/*
1008ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1009ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1010ab4382d2SGreg Kroah-Hartman 		 */
1011ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_RSTSTA);
1012ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXBRK);
1013ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1014ab4382d2SGreg Kroah-Hartman 	}
1015ab4382d2SGreg Kroah-Hartman }
1016ab4382d2SGreg Kroah-Hartman 
1017ab4382d2SGreg Kroah-Hartman /*
1018ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1019ab4382d2SGreg Kroah-Hartman  */
1020ab4382d2SGreg Kroah-Hartman static void
1021ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1022ab4382d2SGreg Kroah-Hartman {
1023ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1024ab4382d2SGreg Kroah-Hartman 
1025ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1026ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
1027ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, atmel_port->tx_done_mask);
1028ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1029ab4382d2SGreg Kroah-Hartman 	}
1030ab4382d2SGreg Kroah-Hartman }
1031ab4382d2SGreg Kroah-Hartman 
1032ab4382d2SGreg Kroah-Hartman /*
1033ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1034ab4382d2SGreg Kroah-Hartman  */
1035ab4382d2SGreg Kroah-Hartman static void
1036ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1037ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1038ab4382d2SGreg Kroah-Hartman {
1039ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1040ab4382d2SGreg Kroah-Hartman 
1041ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1042ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1043ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1044ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1045ab4382d2SGreg Kroah-Hartman 	}
1046ab4382d2SGreg Kroah-Hartman }
1047ab4382d2SGreg Kroah-Hartman 
1048ab4382d2SGreg Kroah-Hartman /*
1049ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1050ab4382d2SGreg Kroah-Hartman  */
1051ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1052ab4382d2SGreg Kroah-Hartman {
1053ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1054ab4382d2SGreg Kroah-Hartman 	unsigned int status, pending, pass_counter = 0;
1055ab4382d2SGreg Kroah-Hartman 
1056ab4382d2SGreg Kroah-Hartman 	do {
1057ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
1058ab4382d2SGreg Kroah-Hartman 		pending = status & UART_GET_IMR(port);
1059ab4382d2SGreg Kroah-Hartman 		if (!pending)
1060ab4382d2SGreg Kroah-Hartman 			break;
1061ab4382d2SGreg Kroah-Hartman 
1062ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1063ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1064ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1065ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1066ab4382d2SGreg Kroah-Hartman 
1067ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1068ab4382d2SGreg Kroah-Hartman }
1069ab4382d2SGreg Kroah-Hartman 
1070a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1071a930e528SElen Song {
1072a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1073a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1074a930e528SElen Song 
1075a930e528SElen Song 	dma_unmap_single(port->dev,
1076a930e528SElen Song 			 pdc->dma_addr,
1077a930e528SElen Song 			 pdc->dma_size,
1078a930e528SElen Song 			 DMA_TO_DEVICE);
1079a930e528SElen Song }
1080a930e528SElen Song 
1081ab4382d2SGreg Kroah-Hartman /*
1082ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1083ab4382d2SGreg Kroah-Hartman  */
108464e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1085ab4382d2SGreg Kroah-Hartman {
1086ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1087ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1088ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1089ab4382d2SGreg Kroah-Hartman 	int count;
1090ab4382d2SGreg Kroah-Hartman 
1091ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
1092ab4382d2SGreg Kroah-Hartman 	if (UART_GET_TCR(port))
1093ab4382d2SGreg Kroah-Hartman 		return;
1094ab4382d2SGreg Kroah-Hartman 
1095ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1096ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1097ab4382d2SGreg Kroah-Hartman 
1098ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1099ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1100ab4382d2SGreg Kroah-Hartman 
1101ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1102ab4382d2SGreg Kroah-Hartman 
1103ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
1104ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1105ab4382d2SGreg Kroah-Hartman 
1106ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1107ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1108ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1109ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1110ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1111ab4382d2SGreg Kroah-Hartman 
1112ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1113ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1114ab4382d2SGreg Kroah-Hartman 
1115ab4382d2SGreg Kroah-Hartman 		UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1116ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, count);
1117ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
1118ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1119ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
1120ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
1121ab4382d2SGreg Kroah-Hartman 	} else {
112283cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
112383cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1124ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1125ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1126ab4382d2SGreg Kroah-Hartman 		}
1127ab4382d2SGreg Kroah-Hartman 	}
1128ab4382d2SGreg Kroah-Hartman 
1129ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1130ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1131ab4382d2SGreg Kroah-Hartman }
1132ab4382d2SGreg Kroah-Hartman 
1133a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1134a930e528SElen Song {
1135a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1136a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1137a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1138a930e528SElen Song 
1139a930e528SElen Song 	pdc->buf = xmit->buf;
1140a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1141a930e528SElen Song 					pdc->buf,
1142a930e528SElen Song 					UART_XMIT_SIZE,
1143a930e528SElen Song 					DMA_TO_DEVICE);
1144a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1145a930e528SElen Song 	pdc->ofs = 0;
1146a930e528SElen Song 
1147a930e528SElen Song 	return 0;
1148a930e528SElen Song }
1149a930e528SElen Song 
1150ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1151ab4382d2SGreg Kroah-Hartman {
1152ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1153ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1154ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1155ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1156ab4382d2SGreg Kroah-Hartman 
1157ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1158ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1159ab4382d2SGreg Kroah-Hartman 
1160ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1161ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1162ab4382d2SGreg Kroah-Hartman 
1163ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1164ab4382d2SGreg Kroah-Hartman 
1165ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1166ab4382d2SGreg Kroah-Hartman 
1167ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1168ab4382d2SGreg Kroah-Hartman 		status = c.status;
1169ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1170ab4382d2SGreg Kroah-Hartman 
1171ab4382d2SGreg Kroah-Hartman 		/*
1172ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1173ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1174ab4382d2SGreg Kroah-Hartman 		 */
1175ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1176ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1177ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1178ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1179ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1180ab4382d2SGreg Kroah-Hartman 
1181ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1182ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1183ab4382d2SGreg Kroah-Hartman 					continue;
1184ab4382d2SGreg Kroah-Hartman 			}
1185ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1186ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1187ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1188ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1189ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1190ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1191ab4382d2SGreg Kroah-Hartman 
1192ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1193ab4382d2SGreg Kroah-Hartman 
1194ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1195ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1196ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1197ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1198ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1199ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1200ab4382d2SGreg Kroah-Hartman 		}
1201ab4382d2SGreg Kroah-Hartman 
1202ab4382d2SGreg Kroah-Hartman 
1203ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1204ab4382d2SGreg Kroah-Hartman 			continue;
1205ab4382d2SGreg Kroah-Hartman 
1206ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1207ab4382d2SGreg Kroah-Hartman 	}
1208ab4382d2SGreg Kroah-Hartman 
1209ab4382d2SGreg Kroah-Hartman 	/*
1210ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1211ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1212ab4382d2SGreg Kroah-Hartman 	 */
1213ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
12142e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1215ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1216ab4382d2SGreg Kroah-Hartman }
1217ab4382d2SGreg Kroah-Hartman 
1218a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1219a930e528SElen Song {
1220a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1221a930e528SElen Song 	int i;
1222a930e528SElen Song 
1223a930e528SElen Song 	for (i = 0; i < 2; i++) {
1224a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1225a930e528SElen Song 
1226a930e528SElen Song 		dma_unmap_single(port->dev,
1227a930e528SElen Song 				 pdc->dma_addr,
1228a930e528SElen Song 				 pdc->dma_size,
1229a930e528SElen Song 				 DMA_FROM_DEVICE);
1230a930e528SElen Song 		kfree(pdc->buf);
1231a930e528SElen Song 	}
12322e68c22fSElen Song 
12332e68c22fSElen Song 	if (!atmel_port->is_usart)
12342e68c22fSElen Song 		del_timer_sync(&atmel_port->uart_timer);
1235a930e528SElen Song }
1236a930e528SElen Song 
123764e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1238ab4382d2SGreg Kroah-Hartman {
1239ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
124005c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1241ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1242ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1243ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1244ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1245ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1246ab4382d2SGreg Kroah-Hartman 
1247ab4382d2SGreg Kroah-Hartman 	do {
1248ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
1249ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
1250ab4382d2SGreg Kroah-Hartman 
1251ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
1252ab4382d2SGreg Kroah-Hartman 		head = UART_GET_RPR(port) - pdc->dma_addr;
1253ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1254ab4382d2SGreg Kroah-Hartman 
1255ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1256ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1257ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1258ab4382d2SGreg Kroah-Hartman 		 * find out.
1259ab4382d2SGreg Kroah-Hartman 		 *
1260ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1261ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1262ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1263ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1264ab4382d2SGreg Kroah-Hartman 		 */
1265ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1266ab4382d2SGreg Kroah-Hartman 
1267ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1268ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1269ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1270ab4382d2SGreg Kroah-Hartman 
1271ab4382d2SGreg Kroah-Hartman 			/*
1272ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1273ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1274ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1275ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1276ab4382d2SGreg Kroah-Hartman 			 */
1277ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1278ab4382d2SGreg Kroah-Hartman 
127905c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
128005c7cd39SJiri Slaby 						count);
1281ab4382d2SGreg Kroah-Hartman 
1282ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1283ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1284ab4382d2SGreg Kroah-Hartman 
1285ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1286ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1287ab4382d2SGreg Kroah-Hartman 		}
1288ab4382d2SGreg Kroah-Hartman 
1289ab4382d2SGreg Kroah-Hartman 		/*
1290ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1291ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1292ab4382d2SGreg Kroah-Hartman 		 */
1293ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1294ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
1295ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNPR(port, pdc->dma_addr);
1296ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNCR(port, pdc->dma_size);
1297ab4382d2SGreg Kroah-Hartman 
1298ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1299ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1300ab4382d2SGreg Kroah-Hartman 		}
1301ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1302ab4382d2SGreg Kroah-Hartman 
1303ab4382d2SGreg Kroah-Hartman 	/*
1304ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1305ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1306ab4382d2SGreg Kroah-Hartman 	 */
1307ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
13082e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1309ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1310ab4382d2SGreg Kroah-Hartman 
1311ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1312ab4382d2SGreg Kroah-Hartman }
1313ab4382d2SGreg Kroah-Hartman 
1314a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1315a930e528SElen Song {
1316a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1317a930e528SElen Song 	int i;
1318a930e528SElen Song 
1319a930e528SElen Song 	for (i = 0; i < 2; i++) {
1320a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1321a930e528SElen Song 
1322a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1323a930e528SElen Song 		if (pdc->buf == NULL) {
1324a930e528SElen Song 			if (i != 0) {
1325a930e528SElen Song 				dma_unmap_single(port->dev,
1326a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1327a930e528SElen Song 					PDC_BUFFER_SIZE,
1328a930e528SElen Song 					DMA_FROM_DEVICE);
1329a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1330a930e528SElen Song 			}
1331a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1332a930e528SElen Song 			return -ENOMEM;
1333a930e528SElen Song 		}
1334a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1335a930e528SElen Song 						pdc->buf,
1336a930e528SElen Song 						PDC_BUFFER_SIZE,
1337a930e528SElen Song 						DMA_FROM_DEVICE);
1338a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1339a930e528SElen Song 		pdc->ofs = 0;
1340a930e528SElen Song 	}
1341a930e528SElen Song 
1342a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1343a930e528SElen Song 
1344a930e528SElen Song 	UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1345a930e528SElen Song 	UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1346a930e528SElen Song 
1347a930e528SElen Song 	UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1348a930e528SElen Song 	UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1349a930e528SElen Song 
1350a930e528SElen Song 	return 0;
1351a930e528SElen Song }
1352a930e528SElen Song 
1353ab4382d2SGreg Kroah-Hartman /*
1354ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1355ab4382d2SGreg Kroah-Hartman  */
1356ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1357ab4382d2SGreg Kroah-Hartman {
1358ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1359ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1360ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1361ab4382d2SGreg Kroah-Hartman 	unsigned int status_change;
1362ab4382d2SGreg Kroah-Hartman 
1363ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1364ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1365ab4382d2SGreg Kroah-Hartman 
1366a930e528SElen Song 	atmel_port->schedule_tx(port);
1367ab4382d2SGreg Kroah-Hartman 
1368ab4382d2SGreg Kroah-Hartman 	status = atmel_port->irq_status;
1369ab4382d2SGreg Kroah-Hartman 	status_change = status ^ atmel_port->irq_status_prev;
1370ab4382d2SGreg Kroah-Hartman 
1371ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1372ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1373ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1374ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1375ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1376ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1377ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1378ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1379ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1380ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1381ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1382ab4382d2SGreg Kroah-Hartman 
1383ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1384ab4382d2SGreg Kroah-Hartman 
1385ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status_prev = status;
1386ab4382d2SGreg Kroah-Hartman 	}
1387ab4382d2SGreg Kroah-Hartman 
1388a930e528SElen Song 	atmel_port->schedule_rx(port);
1389ab4382d2SGreg Kroah-Hartman 
1390ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1391ab4382d2SGreg Kroah-Hartman }
1392ab4382d2SGreg Kroah-Hartman 
139333d64c4fSElen Song static int atmel_init_property(struct atmel_uart_port *atmel_port,
139433d64c4fSElen Song 				struct platform_device *pdev)
139533d64c4fSElen Song {
139633d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1397574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
139833d64c4fSElen Song 
139933d64c4fSElen Song 	if (np) {
140033d64c4fSElen Song 		/* DMA/PDC usage specification */
140133d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
140233d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
140333d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
140433d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
140533d64c4fSElen Song 			} else {
140633d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
140733d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
140833d64c4fSElen Song 			}
140933d64c4fSElen Song 		} else {
141033d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
141133d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
141233d64c4fSElen Song 		}
141333d64c4fSElen Song 
141433d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
141533d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
141633d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
141733d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
141833d64c4fSElen Song 			} else {
141933d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
142033d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
142133d64c4fSElen Song 			}
142233d64c4fSElen Song 		} else {
142333d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
142433d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
142533d64c4fSElen Song 		}
142633d64c4fSElen Song 
142733d64c4fSElen Song 	} else {
142833d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
142933d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
143033d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
143133d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
143233d64c4fSElen Song 	}
143333d64c4fSElen Song 
143433d64c4fSElen Song 	return 0;
143533d64c4fSElen Song }
143633d64c4fSElen Song 
143733d64c4fSElen Song static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
143833d64c4fSElen Song 				struct platform_device *pdev)
143933d64c4fSElen Song {
144033d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1441574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
144233d64c4fSElen Song 
144333d64c4fSElen Song 	if (np) {
144433d64c4fSElen Song 		u32 rs485_delay[2];
144533d64c4fSElen Song 		/* rs485 properties */
144633d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
144733d64c4fSElen Song 					rs485_delay, 2) == 0) {
144833d64c4fSElen Song 			struct serial_rs485 *rs485conf = &atmel_port->rs485;
144933d64c4fSElen Song 
145033d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
145133d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
145233d64c4fSElen Song 			rs485conf->flags = 0;
145333d64c4fSElen Song 
145433d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
145533d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
145633d64c4fSElen Song 
145733d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
145833d64c4fSElen Song 								NULL))
145933d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
146033d64c4fSElen Song 		}
146133d64c4fSElen Song 	} else {
146233d64c4fSElen Song 		atmel_port->rs485       = pdata->rs485;
146333d64c4fSElen Song 	}
146433d64c4fSElen Song 
146533d64c4fSElen Song }
146633d64c4fSElen Song 
1467a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1468a930e528SElen Song {
1469a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1470a930e528SElen Song 
147134df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
147234df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
147334df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
147434df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
147534df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1476a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1477a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1478a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1479a930e528SElen Song 	} else {
1480a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1481a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1482a930e528SElen Song 		atmel_port->release_rx = NULL;
1483a930e528SElen Song 	}
1484a930e528SElen Song 
148508f738beSElen Song 	if (atmel_use_dma_tx(port)) {
148608f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
148708f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
148808f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
148908f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1490a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1491a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1492a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1493a930e528SElen Song 	} else {
1494a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1495a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1496a930e528SElen Song 		atmel_port->release_tx = NULL;
1497a930e528SElen Song 	}
1498a930e528SElen Song }
1499a930e528SElen Song 
1500ab4382d2SGreg Kroah-Hartman /*
1501055560b0SElen Song  * Get ip name usart or uart
1502055560b0SElen Song  */
1503892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1504055560b0SElen Song {
1505055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1506055560b0SElen Song 	int name = UART_GET_IP_NAME(port);
1507731d9caeSNicolas Ferre 	u32 version;
1508055560b0SElen Song 	int usart, uart;
1509055560b0SElen Song 	/* usart and uart ascii */
1510055560b0SElen Song 	usart = 0x55534152;
1511055560b0SElen Song 	uart = 0x44424755;
1512055560b0SElen Song 
1513055560b0SElen Song 	atmel_port->is_usart = false;
1514055560b0SElen Song 
1515055560b0SElen Song 	if (name == usart) {
1516055560b0SElen Song 		dev_dbg(port->dev, "This is usart\n");
1517055560b0SElen Song 		atmel_port->is_usart = true;
1518055560b0SElen Song 	} else if (name == uart) {
1519055560b0SElen Song 		dev_dbg(port->dev, "This is uart\n");
1520055560b0SElen Song 		atmel_port->is_usart = false;
1521055560b0SElen Song 	} else {
1522731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
1523731d9caeSNicolas Ferre 		version = UART_GET_IP_VERSION(port);
1524731d9caeSNicolas Ferre 		switch (version) {
1525731d9caeSNicolas Ferre 		case 0x302:
1526731d9caeSNicolas Ferre 		case 0x10213:
1527731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
1528731d9caeSNicolas Ferre 			atmel_port->is_usart = true;
1529731d9caeSNicolas Ferre 			break;
1530731d9caeSNicolas Ferre 		case 0x203:
1531731d9caeSNicolas Ferre 		case 0x10202:
1532731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1533731d9caeSNicolas Ferre 			atmel_port->is_usart = false;
1534731d9caeSNicolas Ferre 			break;
1535731d9caeSNicolas Ferre 		default:
1536731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1537731d9caeSNicolas Ferre 		}
1538055560b0SElen Song 	}
1539055560b0SElen Song }
1540055560b0SElen Song 
1541055560b0SElen Song /*
1542ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1543ab4382d2SGreg Kroah-Hartman  */
1544ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1545ab4382d2SGreg Kroah-Hartman {
154633d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1547ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1548ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1549ab4382d2SGreg Kroah-Hartman 	int retval;
1550ab4382d2SGreg Kroah-Hartman 
1551ab4382d2SGreg Kroah-Hartman 	/*
1552ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1553ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1554ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1555ab4382d2SGreg Kroah-Hartman 	 */
1556ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1557ab4382d2SGreg Kroah-Hartman 
1558ab4382d2SGreg Kroah-Hartman 	/*
1559ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1560ab4382d2SGreg Kroah-Hartman 	 */
1561ab4382d2SGreg Kroah-Hartman 	retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
1562ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1563ab4382d2SGreg Kroah-Hartman 	if (retval) {
1564ab4382d2SGreg Kroah-Hartman 		printk("atmel_serial: atmel_startup - Can't get irq\n");
1565ab4382d2SGreg Kroah-Hartman 		return retval;
1566ab4382d2SGreg Kroah-Hartman 	}
1567ab4382d2SGreg Kroah-Hartman 
1568ab4382d2SGreg Kroah-Hartman 	/*
1569ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1570ab4382d2SGreg Kroah-Hartman 	 */
157133d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
157233d64c4fSElen Song 
1573a930e528SElen Song 	if (atmel_port->prepare_rx) {
1574a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1575a930e528SElen Song 		if (retval < 0)
1576a930e528SElen Song 			atmel_set_ops(port);
1577ab4382d2SGreg Kroah-Hartman 	}
1578ab4382d2SGreg Kroah-Hartman 
1579a930e528SElen Song 	if (atmel_port->prepare_tx) {
1580a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1581a930e528SElen Song 		if (retval < 0)
1582a930e528SElen Song 			atmel_set_ops(port);
1583ab4382d2SGreg Kroah-Hartman 	}
1584ab4382d2SGreg Kroah-Hartman 	/*
1585ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "open" function (to register
1586ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
1587ab4382d2SGreg Kroah-Hartman 	 */
1588ab4382d2SGreg Kroah-Hartman 	if (atmel_open_hook) {
1589ab4382d2SGreg Kroah-Hartman 		retval = atmel_open_hook(port);
1590ab4382d2SGreg Kroah-Hartman 		if (retval) {
1591ab4382d2SGreg Kroah-Hartman 			free_irq(port->irq, port);
1592ab4382d2SGreg Kroah-Hartman 			return retval;
1593ab4382d2SGreg Kroah-Hartman 		}
1594ab4382d2SGreg Kroah-Hartman 	}
1595ab4382d2SGreg Kroah-Hartman 
1596ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1597ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status_prev = UART_GET_CSR(port);
1598ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1599ab4382d2SGreg Kroah-Hartman 
1600ab4382d2SGreg Kroah-Hartman 	/*
1601ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1602ab4382d2SGreg Kroah-Hartman 	 */
1603ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1604ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
1605ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1606ab4382d2SGreg Kroah-Hartman 
160764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1608ab4382d2SGreg Kroah-Hartman 		/* set UART timeout */
16092e68c22fSElen Song 		if (!atmel_port->is_usart) {
16102e68c22fSElen Song 			setup_timer(&atmel_port->uart_timer,
16112e68c22fSElen Song 					atmel_uart_timer_callback,
16122e68c22fSElen Song 					(unsigned long)port);
16132e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
16142e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
16152e68c22fSElen Song 		/* set USART timeout */
16162e68c22fSElen Song 		} else {
1617ab4382d2SGreg Kroah-Hartman 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1618ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_STTTO);
1619ab4382d2SGreg Kroah-Hartman 
1620ab4382d2SGreg Kroah-Hartman 			UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
16212e68c22fSElen Song 		}
1622ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
1623ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
162434df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
16252e68c22fSElen Song 		/* set UART timeout */
16262e68c22fSElen Song 		if (!atmel_port->is_usart) {
16272e68c22fSElen Song 			setup_timer(&atmel_port->uart_timer,
16282e68c22fSElen Song 					atmel_uart_timer_callback,
16292e68c22fSElen Song 					(unsigned long)port);
16302e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
16312e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
16322e68c22fSElen Song 		/* set USART timeout */
16332e68c22fSElen Song 		} else {
163434df42f5SElen Song 			UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
163534df42f5SElen Song 			UART_PUT_CR(port, ATMEL_US_STTTO);
163634df42f5SElen Song 
163734df42f5SElen Song 			UART_PUT_IER(port, ATMEL_US_TIMEOUT);
16382e68c22fSElen Song 		}
1639ab4382d2SGreg Kroah-Hartman 	} else {
1640ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
1641ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
1642ab4382d2SGreg Kroah-Hartman 	}
1643ab4382d2SGreg Kroah-Hartman 
1644ab4382d2SGreg Kroah-Hartman 	return 0;
1645ab4382d2SGreg Kroah-Hartman }
1646ab4382d2SGreg Kroah-Hartman 
1647ab4382d2SGreg Kroah-Hartman /*
1648ab4382d2SGreg Kroah-Hartman  * Disable the port
1649ab4382d2SGreg Kroah-Hartman  */
1650ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1651ab4382d2SGreg Kroah-Hartman {
1652ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
16530cc7c6c7SMarek Roszko 
1654ab4382d2SGreg Kroah-Hartman 	/*
16550cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
16560cc7c6c7SMarek Roszko 	 * we destroy the buffers
16570cc7c6c7SMarek Roszko 	 */
16580cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
16590cc7c6c7SMarek Roszko 
16600cc7c6c7SMarek Roszko 	/*
16610cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
16620cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1663ab4382d2SGreg Kroah-Hartman 	 */
1664ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1665ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1666ab4382d2SGreg Kroah-Hartman 
16670cc7c6c7SMarek Roszko 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
16680cc7c6c7SMarek Roszko 	UART_PUT_IDR(port, -1);
16690cc7c6c7SMarek Roszko 
16700cc7c6c7SMarek Roszko 
1671ab4382d2SGreg Kroah-Hartman 	/*
1672ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1673ab4382d2SGreg Kroah-Hartman 	 */
1674a930e528SElen Song 	if (atmel_port->release_rx)
1675a930e528SElen Song 		atmel_port->release_rx(port);
1676a930e528SElen Song 	if (atmel_port->release_tx)
1677a930e528SElen Song 		atmel_port->release_tx(port);
1678ab4382d2SGreg Kroah-Hartman 
1679ab4382d2SGreg Kroah-Hartman 	/*
1680*bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1681*bb7e73c5SMark Deneen 	 */
1682*bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1683*bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1684*bb7e73c5SMark Deneen 
1685*bb7e73c5SMark Deneen 	/*
1686ab4382d2SGreg Kroah-Hartman 	 * Free the interrupt
1687ab4382d2SGreg Kroah-Hartman 	 */
1688ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1689ab4382d2SGreg Kroah-Hartman 
1690ab4382d2SGreg Kroah-Hartman 	/*
1691ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "close" function (to unregister
1692ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
1693ab4382d2SGreg Kroah-Hartman 	 */
1694ab4382d2SGreg Kroah-Hartman 	if (atmel_close_hook)
1695ab4382d2SGreg Kroah-Hartman 		atmel_close_hook(port);
1696ab4382d2SGreg Kroah-Hartman }
1697ab4382d2SGreg Kroah-Hartman 
1698ab4382d2SGreg Kroah-Hartman /*
1699ab4382d2SGreg Kroah-Hartman  * Flush any TX data submitted for DMA. Called when the TX circular
1700ab4382d2SGreg Kroah-Hartman  * buffer is reset.
1701ab4382d2SGreg Kroah-Hartman  */
1702ab4382d2SGreg Kroah-Hartman static void atmel_flush_buffer(struct uart_port *port)
1703ab4382d2SGreg Kroah-Hartman {
1704ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1705ab4382d2SGreg Kroah-Hartman 
170664e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
1707ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, 0);
1708ab4382d2SGreg Kroah-Hartman 		atmel_port->pdc_tx.ofs = 0;
1709ab4382d2SGreg Kroah-Hartman 	}
1710ab4382d2SGreg Kroah-Hartman }
1711ab4382d2SGreg Kroah-Hartman 
1712ab4382d2SGreg Kroah-Hartman /*
1713ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1714ab4382d2SGreg Kroah-Hartman  */
1715ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1716ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1717ab4382d2SGreg Kroah-Hartman {
1718ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1719ab4382d2SGreg Kroah-Hartman 
1720ab4382d2SGreg Kroah-Hartman 	switch (state) {
1721ab4382d2SGreg Kroah-Hartman 	case 0:
1722ab4382d2SGreg Kroah-Hartman 		/*
1723ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1724ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1725ab4382d2SGreg Kroah-Hartman 		 */
172691f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1727ab4382d2SGreg Kroah-Hartman 
1728ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1729ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->backup_imr);
1730ab4382d2SGreg Kroah-Hartman 		break;
1731ab4382d2SGreg Kroah-Hartman 	case 3:
1732ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1733ab4382d2SGreg Kroah-Hartman 		atmel_port->backup_imr = UART_GET_IMR(port);
1734ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, -1);
1735ab4382d2SGreg Kroah-Hartman 
1736ab4382d2SGreg Kroah-Hartman 		/*
1737ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1738ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1739ab4382d2SGreg Kroah-Hartman 		 */
174091f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1741ab4382d2SGreg Kroah-Hartman 		break;
1742ab4382d2SGreg Kroah-Hartman 	default:
1743ab4382d2SGreg Kroah-Hartman 		printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1744ab4382d2SGreg Kroah-Hartman 	}
1745ab4382d2SGreg Kroah-Hartman }
1746ab4382d2SGreg Kroah-Hartman 
1747ab4382d2SGreg Kroah-Hartman /*
1748ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1749ab4382d2SGreg Kroah-Hartman  */
1750ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1751ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1752ab4382d2SGreg Kroah-Hartman {
1753ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1754ab4382d2SGreg Kroah-Hartman 	unsigned int mode, imr, quot, baud;
1755ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1756ab4382d2SGreg Kroah-Hartman 
1757ab4382d2SGreg Kroah-Hartman 	/* Get current mode register */
1758ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1759ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_NBSTOP | ATMEL_US_PAR
1760ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_USMODE);
1761ab4382d2SGreg Kroah-Hartman 
1762ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1763ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1764ab4382d2SGreg Kroah-Hartman 
1765ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1766ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1767ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1768ab4382d2SGreg Kroah-Hartman 	}
1769ab4382d2SGreg Kroah-Hartman 
1770ab4382d2SGreg Kroah-Hartman 	/* byte size */
1771ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1772ab4382d2SGreg Kroah-Hartman 	case CS5:
1773ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1774ab4382d2SGreg Kroah-Hartman 		break;
1775ab4382d2SGreg Kroah-Hartman 	case CS6:
1776ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1777ab4382d2SGreg Kroah-Hartman 		break;
1778ab4382d2SGreg Kroah-Hartman 	case CS7:
1779ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1780ab4382d2SGreg Kroah-Hartman 		break;
1781ab4382d2SGreg Kroah-Hartman 	default:
1782ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1783ab4382d2SGreg Kroah-Hartman 		break;
1784ab4382d2SGreg Kroah-Hartman 	}
1785ab4382d2SGreg Kroah-Hartman 
1786ab4382d2SGreg Kroah-Hartman 	/* stop bits */
1787ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
1788ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
1789ab4382d2SGreg Kroah-Hartman 
1790ab4382d2SGreg Kroah-Hartman 	/* parity */
1791ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
1792ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
1793ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
1794ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
1795ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
1796ab4382d2SGreg Kroah-Hartman 			else
1797ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
1798ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
1799ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
1800ab4382d2SGreg Kroah-Hartman 		else
1801ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
1802ab4382d2SGreg Kroah-Hartman 	} else
1803ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
1804ab4382d2SGreg Kroah-Hartman 
1805ab4382d2SGreg Kroah-Hartman 	/* hardware handshake (RTS/CTS) */
1806ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
1807ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_HWHS;
1808ab4382d2SGreg Kroah-Hartman 	else
1809ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_NORMAL;
1810ab4382d2SGreg Kroah-Hartman 
1811ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
1812ab4382d2SGreg Kroah-Hartman 
1813ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
1814ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
1815ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1816ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & (BRKINT | PARMRK))
1817ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
1818ab4382d2SGreg Kroah-Hartman 
181964e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
1820ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
1821ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, port->read_status_mask);
1822ab4382d2SGreg Kroah-Hartman 
1823ab4382d2SGreg Kroah-Hartman 	/*
1824ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
1825ab4382d2SGreg Kroah-Hartman 	 */
1826ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
1827ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
1828ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1829ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
1830ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
1831ab4382d2SGreg Kroah-Hartman 		/*
1832ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
1833ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
1834ab4382d2SGreg Kroah-Hartman 		 */
1835ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
1836ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
1837ab4382d2SGreg Kroah-Hartman 	}
1838ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
1839ab4382d2SGreg Kroah-Hartman 
1840ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
1841ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
1842ab4382d2SGreg Kroah-Hartman 
1843ab4382d2SGreg Kroah-Hartman 	/*
1844ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
1845ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
1846ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
1847ab4382d2SGreg Kroah-Hartman 	 */
1848ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
1849ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1850ab4382d2SGreg Kroah-Hartman 
1851ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
1852ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1853ab4382d2SGreg Kroah-Hartman 
1854ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
1855ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
1856ab4382d2SGreg Kroah-Hartman 
1857ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1858ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
185993f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
1860ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
1861ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
1862ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
1863ab4382d2SGreg Kroah-Hartman 	} else {
1864ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
1865ab4382d2SGreg Kroah-Hartman 	}
1866ab4382d2SGreg Kroah-Hartman 
1867ab4382d2SGreg Kroah-Hartman 	/* set the parity, stop bits and data size */
1868ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
1869ab4382d2SGreg Kroah-Hartman 
1870ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
1871ab4382d2SGreg Kroah-Hartman 	UART_PUT_BRGR(port, quot);
1872ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1873ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1874ab4382d2SGreg Kroah-Hartman 
1875ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
1876ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
1877ab4382d2SGreg Kroah-Hartman 
1878ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
1879ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
1880ab4382d2SGreg Kroah-Hartman 		port->ops->enable_ms(port);
1881ab4382d2SGreg Kroah-Hartman 
1882ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
1883ab4382d2SGreg Kroah-Hartman }
1884ab4382d2SGreg Kroah-Hartman 
188542bd7a4fSViktar Palstsiuk static void atmel_set_ldisc(struct uart_port *port, int new)
188642bd7a4fSViktar Palstsiuk {
1887b54bf3b2SJiri Slaby 	if (new == N_PPS) {
188842bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
188942bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
189042bd7a4fSViktar Palstsiuk 	} else {
189142bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
189242bd7a4fSViktar Palstsiuk 	}
189342bd7a4fSViktar Palstsiuk }
189442bd7a4fSViktar Palstsiuk 
1895ab4382d2SGreg Kroah-Hartman /*
1896ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
1897ab4382d2SGreg Kroah-Hartman  */
1898ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
1899ab4382d2SGreg Kroah-Hartman {
1900ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1901ab4382d2SGreg Kroah-Hartman }
1902ab4382d2SGreg Kroah-Hartman 
1903ab4382d2SGreg Kroah-Hartman /*
1904ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
1905ab4382d2SGreg Kroah-Hartman  */
1906ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
1907ab4382d2SGreg Kroah-Hartman {
1908ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1909ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1910ab4382d2SGreg Kroah-Hartman 
1911ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
1912ab4382d2SGreg Kroah-Hartman 
1913ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1914ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
1915ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
1916ab4382d2SGreg Kroah-Hartman 	}
1917ab4382d2SGreg Kroah-Hartman }
1918ab4382d2SGreg Kroah-Hartman 
1919ab4382d2SGreg Kroah-Hartman /*
1920ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
1921ab4382d2SGreg Kroah-Hartman  */
1922ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
1923ab4382d2SGreg Kroah-Hartman {
1924ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1925ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1926ab4382d2SGreg Kroah-Hartman 
1927ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1928ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
1929ab4382d2SGreg Kroah-Hartman 
1930ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1931ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
1932ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
1933ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
1934ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
1935ab4382d2SGreg Kroah-Hartman 		}
1936ab4382d2SGreg Kroah-Hartman 	}
1937ab4382d2SGreg Kroah-Hartman 
1938ab4382d2SGreg Kroah-Hartman 	return 0;
1939ab4382d2SGreg Kroah-Hartman }
1940ab4382d2SGreg Kroah-Hartman 
1941ab4382d2SGreg Kroah-Hartman /*
1942ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
1943ab4382d2SGreg Kroah-Hartman  */
1944ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
1945ab4382d2SGreg Kroah-Hartman {
1946ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
1947ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
1948ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
1949ab4382d2SGreg Kroah-Hartman 	}
1950ab4382d2SGreg Kroah-Hartman }
1951ab4382d2SGreg Kroah-Hartman 
1952ab4382d2SGreg Kroah-Hartman /*
1953ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
1954ab4382d2SGreg Kroah-Hartman  */
1955ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1956ab4382d2SGreg Kroah-Hartman {
1957ab4382d2SGreg Kroah-Hartman 	int ret = 0;
1958ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1959ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1960ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
1961ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1962ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
1963ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1964ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
1965ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1966ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
1967ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1968ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
1969ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1970ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
1971ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1972ab4382d2SGreg Kroah-Hartman 	return ret;
1973ab4382d2SGreg Kroah-Hartman }
1974ab4382d2SGreg Kroah-Hartman 
1975ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
1976ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
1977ab4382d2SGreg Kroah-Hartman {
1978ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1979ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1980ab4382d2SGreg Kroah-Hartman 
1981ab4382d2SGreg Kroah-Hartman 	return UART_GET_CHAR(port);
1982ab4382d2SGreg Kroah-Hartman }
1983ab4382d2SGreg Kroah-Hartman 
1984ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1985ab4382d2SGreg Kroah-Hartman {
1986ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1987ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1988ab4382d2SGreg Kroah-Hartman 
1989ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
1990ab4382d2SGreg Kroah-Hartman }
1991ab4382d2SGreg Kroah-Hartman #endif
1992ab4382d2SGreg Kroah-Hartman 
1993ab4382d2SGreg Kroah-Hartman static int
1994ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1995ab4382d2SGreg Kroah-Hartman {
1996ab4382d2SGreg Kroah-Hartman 	struct serial_rs485 rs485conf;
1997ab4382d2SGreg Kroah-Hartman 
1998ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1999ab4382d2SGreg Kroah-Hartman 	case TIOCSRS485:
2000ab4382d2SGreg Kroah-Hartman 		if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
2001ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
2002ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
2003ab4382d2SGreg Kroah-Hartman 
2004ab4382d2SGreg Kroah-Hartman 		atmel_config_rs485(port, &rs485conf);
2005ab4382d2SGreg Kroah-Hartman 		break;
2006ab4382d2SGreg Kroah-Hartman 
2007ab4382d2SGreg Kroah-Hartman 	case TIOCGRS485:
2008ab4382d2SGreg Kroah-Hartman 		if (copy_to_user((struct serial_rs485 *) arg,
2009ab4382d2SGreg Kroah-Hartman 					&(to_atmel_uart_port(port)->rs485),
2010ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
2011ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
2012ab4382d2SGreg Kroah-Hartman 		break;
2013ab4382d2SGreg Kroah-Hartman 
2014ab4382d2SGreg Kroah-Hartman 	default:
2015ab4382d2SGreg Kroah-Hartman 		return -ENOIOCTLCMD;
2016ab4382d2SGreg Kroah-Hartman 	}
2017ab4382d2SGreg Kroah-Hartman 	return 0;
2018ab4382d2SGreg Kroah-Hartman }
2019ab4382d2SGreg Kroah-Hartman 
2020ab4382d2SGreg Kroah-Hartman 
2021ab4382d2SGreg Kroah-Hartman 
2022ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2023ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2024ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2025ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2026ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2027ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2028ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2029ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2030ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2031ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2032ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2033ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2034ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
203542bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2036ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2037ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2038ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2039ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2040ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2041ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2042ab4382d2SGreg Kroah-Hartman 	.ioctl		= atmel_ioctl,
2043ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2044ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2045ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2046ab4382d2SGreg Kroah-Hartman #endif
2047ab4382d2SGreg Kroah-Hartman };
2048ab4382d2SGreg Kroah-Hartman 
2049ab4382d2SGreg Kroah-Hartman /*
2050ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2051ab4382d2SGreg Kroah-Hartman  */
205291f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2053ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2054ab4382d2SGreg Kroah-Hartman {
205591f8c2d8SBoris BREZILLON 	int ret;
2056ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2057574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2058ab4382d2SGreg Kroah-Hartman 
205933d64c4fSElen Song 	if (!atmel_init_property(atmel_port, pdev))
2060a930e528SElen Song 		atmel_set_ops(port);
2061a930e528SElen Song 
206233d64c4fSElen Song 	atmel_init_rs485(atmel_port, pdev);
206333d64c4fSElen Song 
2064ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2065ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2066ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2067ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2068ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2069ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2070ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
2071ab4382d2SGreg Kroah-Hartman 
2072ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2073ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
2074ab4382d2SGreg Kroah-Hartman 
2075ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2076ab4382d2SGreg Kroah-Hartman 
20775fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2078ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
20791acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2080588edbf3SNicolas Ferre 	} else {
2081ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2082ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2083ab4382d2SGreg Kroah-Hartman 	}
2084ab4382d2SGreg Kroah-Hartman 
2085ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2086ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2087ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
208891f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
208991f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
209091f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
209191f8c2d8SBoris BREZILLON 			return ret;
209291f8c2d8SBoris BREZILLON 		}
209391f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
209491f8c2d8SBoris BREZILLON 		if (ret) {
209591f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
209691f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
209791f8c2d8SBoris BREZILLON 			return ret;
209891f8c2d8SBoris BREZILLON 		}
2099ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
210091f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2101ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2102ab4382d2SGreg Kroah-Hartman 	}
2103ab4382d2SGreg Kroah-Hartman 
2104ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2105ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2106ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
210764e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2108ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2109ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2110ab4382d2SGreg Kroah-Hartman 	} else {
2111ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2112ab4382d2SGreg Kroah-Hartman 	}
211391f8c2d8SBoris BREZILLON 
211491f8c2d8SBoris BREZILLON 	return 0;
2115ab4382d2SGreg Kroah-Hartman }
2116ab4382d2SGreg Kroah-Hartman 
211769f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
211869f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2119ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2120ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2121ab4382d2SGreg Kroah-Hartman {
2122ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2123ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2124ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
2125ab4382d2SGreg Kroah-Hartman }
2126ab4382d2SGreg Kroah-Hartman 
2127ab4382d2SGreg Kroah-Hartman /*
2128ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2129ab4382d2SGreg Kroah-Hartman  */
2130ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2131ab4382d2SGreg Kroah-Hartman {
2132ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2133ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2134ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2135ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2136ab4382d2SGreg Kroah-Hartman 
2137ab4382d2SGreg Kroah-Hartman 	/*
2138ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2139ab4382d2SGreg Kroah-Hartman 	 */
2140ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
2141ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2142ab4382d2SGreg Kroah-Hartman 
2143ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
2144ab4382d2SGreg Kroah-Hartman 	pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2145ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2146ab4382d2SGreg Kroah-Hartman 
2147ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2148ab4382d2SGreg Kroah-Hartman 
2149ab4382d2SGreg Kroah-Hartman 	/*
2150ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2151ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2152ab4382d2SGreg Kroah-Hartman 	 */
2153ab4382d2SGreg Kroah-Hartman 	do {
2154ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
2155ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2156ab4382d2SGreg Kroah-Hartman 
2157ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2158ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
2159ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2160ab4382d2SGreg Kroah-Hartman 
2161ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
2162ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
2163ab4382d2SGreg Kroah-Hartman }
2164ab4382d2SGreg Kroah-Hartman 
2165ab4382d2SGreg Kroah-Hartman /*
2166ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2167ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2168ab4382d2SGreg Kroah-Hartman  */
2169ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2170ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2171ab4382d2SGreg Kroah-Hartman {
2172ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2173ab4382d2SGreg Kroah-Hartman 
2174ab4382d2SGreg Kroah-Hartman 	/*
2175ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2176ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2177ab4382d2SGreg Kroah-Hartman 	 */
2178ab4382d2SGreg Kroah-Hartman 	quot = UART_GET_BRGR(port) & ATMEL_US_CD;
2179ab4382d2SGreg Kroah-Hartman 	if (!quot)
2180ab4382d2SGreg Kroah-Hartman 		return;
2181ab4382d2SGreg Kroah-Hartman 
2182ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2183ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2184ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2185ab4382d2SGreg Kroah-Hartman 	else
2186ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2187ab4382d2SGreg Kroah-Hartman 
2188ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_PAR;
2189ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2190ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2191ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2192ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2193ab4382d2SGreg Kroah-Hartman 
2194ab4382d2SGreg Kroah-Hartman 	/*
2195ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2196ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2197ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2198ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2199ab4382d2SGreg Kroah-Hartman 	 */
2200ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2201ab4382d2SGreg Kroah-Hartman }
2202ab4382d2SGreg Kroah-Hartman 
2203ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2204ab4382d2SGreg Kroah-Hartman {
220591f8c2d8SBoris BREZILLON 	int ret;
2206ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2207ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2208ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2209ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2210ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2211ab4382d2SGreg Kroah-Hartman 
2212ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2213ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2214ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2215ab4382d2SGreg Kroah-Hartman 	}
2216ab4382d2SGreg Kroah-Hartman 
221791f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
221891f8c2d8SBoris BREZILLON 	if (ret)
221991f8c2d8SBoris BREZILLON 		return ret;
2220ab4382d2SGreg Kroah-Hartman 
2221ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
2222ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2223ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2224ab4382d2SGreg Kroah-Hartman 
2225ab4382d2SGreg Kroah-Hartman 	if (options)
2226ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2227ab4382d2SGreg Kroah-Hartman 	else
2228ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2229ab4382d2SGreg Kroah-Hartman 
2230ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2231ab4382d2SGreg Kroah-Hartman }
2232ab4382d2SGreg Kroah-Hartman 
2233ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2234ab4382d2SGreg Kroah-Hartman 
2235ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2236ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2237ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2238ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2239ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2240ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2241ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2242ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2243ab4382d2SGreg Kroah-Hartman };
2244ab4382d2SGreg Kroah-Hartman 
2245ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2246ab4382d2SGreg Kroah-Hartman 
2247ab4382d2SGreg Kroah-Hartman /*
2248ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2249ab4382d2SGreg Kroah-Hartman  */
2250ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2251ab4382d2SGreg Kroah-Hartman {
225291f8c2d8SBoris BREZILLON 	int ret;
2253ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
22540d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2255574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2256efb8d21bSLinus Torvalds 		int id = pdata->num;
22574cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
22580d0a3cc1SVoss, Nikolaus 
22594cbf9f48SNicolas Ferre 		port->backup_imr = 0;
22604cbf9f48SNicolas Ferre 		port->uart.line = id;
22614cbf9f48SNicolas Ferre 
22624cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
226391f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
226491f8c2d8SBoris BREZILLON 		if (ret)
226591f8c2d8SBoris BREZILLON 			return ret;
2266ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2267ab4382d2SGreg Kroah-Hartman 	}
2268ab4382d2SGreg Kroah-Hartman 
2269ab4382d2SGreg Kroah-Hartman 	return 0;
2270ab4382d2SGreg Kroah-Hartman }
2271ab4382d2SGreg Kroah-Hartman 
2272ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2273ab4382d2SGreg Kroah-Hartman 
2274ab4382d2SGreg Kroah-Hartman /*
2275ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2276ab4382d2SGreg Kroah-Hartman  */
2277ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2278ab4382d2SGreg Kroah-Hartman {
2279ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2280ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2281ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2282ab4382d2SGreg Kroah-Hartman 
2283ab4382d2SGreg Kroah-Hartman 	return 0;
2284ab4382d2SGreg Kroah-Hartman }
2285ab4382d2SGreg Kroah-Hartman 
2286ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2287ab4382d2SGreg Kroah-Hartman 
2288ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2289ab4382d2SGreg Kroah-Hartman {
2290ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2291ab4382d2SGreg Kroah-Hartman }
2292ab4382d2SGreg Kroah-Hartman 
2293ab4382d2SGreg Kroah-Hartman #else
2294ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2295ab4382d2SGreg Kroah-Hartman 
2296ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2297ab4382d2SGreg Kroah-Hartman {
2298ab4382d2SGreg Kroah-Hartman 	return false;
2299ab4382d2SGreg Kroah-Hartman }
2300ab4382d2SGreg Kroah-Hartman #endif
2301ab4382d2SGreg Kroah-Hartman 
2302ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2303ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2304ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2305ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2306ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2307ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2308ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2309ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2310ab4382d2SGreg Kroah-Hartman };
2311ab4382d2SGreg Kroah-Hartman 
2312ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2313ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2314ab4382d2SGreg Kroah-Hartman {
2315ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2316ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2317ab4382d2SGreg Kroah-Hartman #else
2318ab4382d2SGreg Kroah-Hartman 	return false;
2319ab4382d2SGreg Kroah-Hartman #endif
2320ab4382d2SGreg Kroah-Hartman }
2321ab4382d2SGreg Kroah-Hartman 
2322ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2323ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2324ab4382d2SGreg Kroah-Hartman {
2325ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2326ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2327ab4382d2SGreg Kroah-Hartman 
2328ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2329ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
2330ab4382d2SGreg Kroah-Hartman 		while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2331ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2332ab4382d2SGreg Kroah-Hartman 	}
2333ab4382d2SGreg Kroah-Hartman 
2334ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2335ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2336ab4382d2SGreg Kroah-Hartman 	if (atmel_serial_clk_will_stop())
2337ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
2338ab4382d2SGreg Kroah-Hartman 
2339ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2340ab4382d2SGreg Kroah-Hartman 
2341ab4382d2SGreg Kroah-Hartman 	return 0;
2342ab4382d2SGreg Kroah-Hartman }
2343ab4382d2SGreg Kroah-Hartman 
2344ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2345ab4382d2SGreg Kroah-Hartman {
2346ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2347ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2348ab4382d2SGreg Kroah-Hartman 
2349ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2350ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2351ab4382d2SGreg Kroah-Hartman 
2352ab4382d2SGreg Kroah-Hartman 	return 0;
2353ab4382d2SGreg Kroah-Hartman }
2354ab4382d2SGreg Kroah-Hartman #else
2355ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2356ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2357ab4382d2SGreg Kroah-Hartman #endif
2358ab4382d2SGreg Kroah-Hartman 
23599671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2360ab4382d2SGreg Kroah-Hartman {
2361ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
23625fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2363574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2364ab4382d2SGreg Kroah-Hartman 	void *data;
23654cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2366ab4382d2SGreg Kroah-Hartman 
2367ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2368ab4382d2SGreg Kroah-Hartman 
23695fbe46b6SNicolas Ferre 	if (np)
23705fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
23715fbe46b6SNicolas Ferre 	else
23724cbf9f48SNicolas Ferre 		if (pdata)
23734cbf9f48SNicolas Ferre 			ret = pdata->num;
23744cbf9f48SNicolas Ferre 
23754cbf9f48SNicolas Ferre 	if (ret < 0)
23765fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
23774cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2378503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
23794cbf9f48SNicolas Ferre 
2380503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
23814cbf9f48SNicolas Ferre 		ret = -ENODEV;
23824cbf9f48SNicolas Ferre 		goto err;
23834cbf9f48SNicolas Ferre 	}
23844cbf9f48SNicolas Ferre 
2385503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
23864cbf9f48SNicolas Ferre 		/* port already in use */
23874cbf9f48SNicolas Ferre 		ret = -EBUSY;
23884cbf9f48SNicolas Ferre 		goto err;
23894cbf9f48SNicolas Ferre 	}
23904cbf9f48SNicolas Ferre 
23914cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
2392ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
23934cbf9f48SNicolas Ferre 	port->uart.line = ret;
2394ab4382d2SGreg Kroah-Hartman 
239591f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
239691f8c2d8SBoris BREZILLON 	if (ret)
239791f8c2d8SBoris BREZILLON 		goto err;
2398ab4382d2SGreg Kroah-Hartman 
239964e22ebeSElen Song 	if (!atmel_use_pdc_rx(&port->uart)) {
2400ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2401ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2402ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2403ab4382d2SGreg Kroah-Hartman 		if (!data)
2404ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2405ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
2406ab4382d2SGreg Kroah-Hartman 	}
2407ab4382d2SGreg Kroah-Hartman 
2408ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
2409ab4382d2SGreg Kroah-Hartman 	if (ret)
2410ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2411ab4382d2SGreg Kroah-Hartman 
2412ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2413ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
2414ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2415ab4382d2SGreg Kroah-Hartman 		/*
2416ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
241791f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2418ab4382d2SGreg Kroah-Hartman 		 */
241991f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
2420ab4382d2SGreg Kroah-Hartman 	}
2421ab4382d2SGreg Kroah-Hartman #endif
2422ab4382d2SGreg Kroah-Hartman 
2423ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2424ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
2425ab4382d2SGreg Kroah-Hartman 
2426fc887b15SLinus Torvalds 	if (port->rs485.flags & SER_RS485_ENABLED) {
2427fc887b15SLinus Torvalds 		UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2428fc887b15SLinus Torvalds 		UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2429fc887b15SLinus Torvalds 	}
2430fc887b15SLinus Torvalds 
2431055560b0SElen Song 	/*
2432055560b0SElen Song 	 * Get port name of usart or uart
2433055560b0SElen Song 	 */
2434892db58bSNicolas Ferre 	atmel_get_ip_name(&port->uart);
2435055560b0SElen Song 
2436ab4382d2SGreg Kroah-Hartman 	return 0;
2437ab4382d2SGreg Kroah-Hartman 
2438ab4382d2SGreg Kroah-Hartman err_add_port:
2439ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
2440ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
2441ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2442ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
2443ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
2444ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
2445ab4382d2SGreg Kroah-Hartman 	}
24464cbf9f48SNicolas Ferre err:
2447ab4382d2SGreg Kroah-Hartman 	return ret;
2448ab4382d2SGreg Kroah-Hartman }
2449ab4382d2SGreg Kroah-Hartman 
2450ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
2451ab4382d2SGreg Kroah-Hartman {
2452ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2453ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2454ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2455ab4382d2SGreg Kroah-Hartman 
2456f50c995fSMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
2457f50c995fSMarek Roszko 
2458ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
2459ab4382d2SGreg Kroah-Hartman 
2460ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
2461ab4382d2SGreg Kroah-Hartman 
2462ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
2463ab4382d2SGreg Kroah-Hartman 
2464ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
2465ab4382d2SGreg Kroah-Hartman 
2466503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
24674cbf9f48SNicolas Ferre 
2468ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
2469ab4382d2SGreg Kroah-Hartman 
2470ab4382d2SGreg Kroah-Hartman 	return ret;
2471ab4382d2SGreg Kroah-Hartman }
2472ab4382d2SGreg Kroah-Hartman 
2473ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2474ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
24752d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
2476ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2477ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2478ab4382d2SGreg Kroah-Hartman 	.driver		= {
2479ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
2480ab4382d2SGreg Kroah-Hartman 		.owner	= THIS_MODULE,
24815fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
2482ab4382d2SGreg Kroah-Hartman 	},
2483ab4382d2SGreg Kroah-Hartman };
2484ab4382d2SGreg Kroah-Hartman 
2485ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2486ab4382d2SGreg Kroah-Hartman {
2487ab4382d2SGreg Kroah-Hartman 	int ret;
2488ab4382d2SGreg Kroah-Hartman 
2489ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2490ab4382d2SGreg Kroah-Hartman 	if (ret)
2491ab4382d2SGreg Kroah-Hartman 		return ret;
2492ab4382d2SGreg Kroah-Hartman 
2493ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2494ab4382d2SGreg Kroah-Hartman 	if (ret)
2495ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2496ab4382d2SGreg Kroah-Hartman 
2497ab4382d2SGreg Kroah-Hartman 	return ret;
2498ab4382d2SGreg Kroah-Hartman }
2499ab4382d2SGreg Kroah-Hartman 
2500ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
2501ab4382d2SGreg Kroah-Hartman {
2502ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
2503ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
2504ab4382d2SGreg Kroah-Hartman }
2505ab4382d2SGreg Kroah-Hartman 
2506ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
2507ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
2508ab4382d2SGreg Kroah-Hartman 
2509ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
2510ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2511ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
2512ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
2513