xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 91f8c2d805f5c5fd82bf8655065148b2e16967ff)
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>
42784557deSJean-Christophe PLAGNIOL-VILLARD #include <linux/pinctrl/consumer.h>
43bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.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)
101ab4382d2SGreg Kroah-Hartman 
102ab4382d2SGreg Kroah-Hartman  /* PDC registers */
103ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
104ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port)	__raw_readl((port)->membase + ATMEL_PDC_PTSR)
105ab4382d2SGreg Kroah-Hartman 
106ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
107ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port)	__raw_readl((port)->membase + ATMEL_PDC_RPR)
108ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
109ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
110ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
111ab4382d2SGreg Kroah-Hartman 
112ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
113ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
114ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port)	__raw_readl((port)->membase + ATMEL_PDC_TCR)
115ab4382d2SGreg Kroah-Hartman 
116ab4382d2SGreg Kroah-Hartman static int (*atmel_open_hook)(struct uart_port *);
117ab4382d2SGreg Kroah-Hartman static void (*atmel_close_hook)(struct uart_port *);
118ab4382d2SGreg Kroah-Hartman 
119ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
120ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
121ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
122ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
123ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
124ab4382d2SGreg Kroah-Hartman };
125ab4382d2SGreg Kroah-Hartman 
126ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
127ab4382d2SGreg Kroah-Hartman 	u16		status;
128ab4382d2SGreg Kroah-Hartman 	u16		ch;
129ab4382d2SGreg Kroah-Hartman };
130ab4382d2SGreg Kroah-Hartman 
131ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
132ab4382d2SGreg Kroah-Hartman 
133ab4382d2SGreg Kroah-Hartman /*
134ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
135ab4382d2SGreg Kroah-Hartman  */
136ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
137ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
138ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
139ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
140ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
141ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
142ab4382d2SGreg Kroah-Hartman 
143ab4382d2SGreg Kroah-Hartman 	short			use_dma_rx;	/* enable PDC receiver */
144ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
145ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
146ab4382d2SGreg Kroah-Hartman 
147ab4382d2SGreg Kroah-Hartman 	short			use_dma_tx;	/* enable PDC transmitter */
148ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
149ab4382d2SGreg Kroah-Hartman 
150ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
151ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
152ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
153ab4382d2SGreg Kroah-Hartman 
154ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
155ab4382d2SGreg Kroah-Hartman 
156ab4382d2SGreg Kroah-Hartman 	struct serial_rs485	rs485;		/* rs485 settings */
157ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
158ab4382d2SGreg Kroah-Hartman };
159ab4382d2SGreg Kroah-Hartman 
160ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
161503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
162ab4382d2SGreg Kroah-Hartman 
163ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
164ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
165ab4382d2SGreg Kroah-Hartman #endif
166ab4382d2SGreg Kroah-Hartman 
1675fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1685fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1695fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1705fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1715fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1725fbe46b6SNicolas Ferre };
1735fbe46b6SNicolas Ferre 
1745fbe46b6SNicolas Ferre MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
1755fbe46b6SNicolas Ferre #endif
1765fbe46b6SNicolas Ferre 
177ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
178ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
179ab4382d2SGreg Kroah-Hartman {
180ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
181ab4382d2SGreg Kroah-Hartman }
182ab4382d2SGreg Kroah-Hartman 
183ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
184ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_rx(struct uart_port *port)
185ab4382d2SGreg Kroah-Hartman {
186ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
187ab4382d2SGreg Kroah-Hartman 
188ab4382d2SGreg Kroah-Hartman 	return atmel_port->use_dma_rx;
189ab4382d2SGreg Kroah-Hartman }
190ab4382d2SGreg Kroah-Hartman 
191ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_tx(struct uart_port *port)
192ab4382d2SGreg Kroah-Hartman {
193ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
194ab4382d2SGreg Kroah-Hartman 
195ab4382d2SGreg Kroah-Hartman 	return atmel_port->use_dma_tx;
196ab4382d2SGreg Kroah-Hartman }
197ab4382d2SGreg Kroah-Hartman #else
198ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_rx(struct uart_port *port)
199ab4382d2SGreg Kroah-Hartman {
200ab4382d2SGreg Kroah-Hartman 	return false;
201ab4382d2SGreg Kroah-Hartman }
202ab4382d2SGreg Kroah-Hartman 
203ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_tx(struct uart_port *port)
204ab4382d2SGreg Kroah-Hartman {
205ab4382d2SGreg Kroah-Hartman 	return false;
206ab4382d2SGreg Kroah-Hartman }
207ab4382d2SGreg Kroah-Hartman #endif
208ab4382d2SGreg Kroah-Hartman 
209ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
210ab4382d2SGreg Kroah-Hartman void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
211ab4382d2SGreg Kroah-Hartman {
212ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
213ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
214dbf1115dSClaudio Scordino 	unsigned long flags;
215ab4382d2SGreg Kroah-Hartman 
216dbf1115dSClaudio Scordino 	spin_lock_irqsave(&port->lock, flags);
217ab4382d2SGreg Kroah-Hartman 
218ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
219ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
220ab4382d2SGreg Kroah-Hartman 
221ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port);
222ab4382d2SGreg Kroah-Hartman 
223ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
224ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
225ab4382d2SGreg Kroah-Hartman 
226ab4382d2SGreg Kroah-Hartman 	atmel_port->rs485 = *rs485conf;
227ab4382d2SGreg Kroah-Hartman 
228ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
229ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
230ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
23193f3350cSClaudio Scordino 		if ((rs485conf->delay_rts_after_send) > 0)
232ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
233ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
234ab4382d2SGreg Kroah-Hartman 	} else {
235ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
236ab4382d2SGreg Kroah-Hartman 		if (atmel_use_dma_tx(port))
237ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
238ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
239ab4382d2SGreg Kroah-Hartman 		else
240ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
241ab4382d2SGreg Kroah-Hartman 	}
242ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
243ab4382d2SGreg Kroah-Hartman 
244ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
245ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
246ab4382d2SGreg Kroah-Hartman 
247dbf1115dSClaudio Scordino 	spin_unlock_irqrestore(&port->lock, flags);
248ab4382d2SGreg Kroah-Hartman 
249ab4382d2SGreg Kroah-Hartman }
250ab4382d2SGreg Kroah-Hartman 
251ab4382d2SGreg Kroah-Hartman /*
252ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
253ab4382d2SGreg Kroah-Hartman  */
254ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
255ab4382d2SGreg Kroah-Hartman {
256ab4382d2SGreg Kroah-Hartman 	return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
257ab4382d2SGreg Kroah-Hartman }
258ab4382d2SGreg Kroah-Hartman 
259ab4382d2SGreg Kroah-Hartman /*
260ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
261ab4382d2SGreg Kroah-Hartman  */
262ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
263ab4382d2SGreg Kroah-Hartman {
264ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
265ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
266ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
267ab4382d2SGreg Kroah-Hartman 
268ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91RM9200
269ab4382d2SGreg Kroah-Hartman 	if (cpu_is_at91rm9200()) {
270ab4382d2SGreg Kroah-Hartman 		/*
271ab4382d2SGreg Kroah-Hartman 		 * AT91RM9200 Errata #39: RTS0 is not internally connected
272ab4382d2SGreg Kroah-Hartman 		 * to PA21. We need to drive the pin manually.
273ab4382d2SGreg Kroah-Hartman 		 */
274ab4382d2SGreg Kroah-Hartman 		if (port->mapbase == AT91RM9200_BASE_US0) {
275ab4382d2SGreg Kroah-Hartman 			if (mctrl & TIOCM_RTS)
276ab4382d2SGreg Kroah-Hartman 				at91_set_gpio_value(AT91_PIN_PA21, 0);
277ab4382d2SGreg Kroah-Hartman 			else
278ab4382d2SGreg Kroah-Hartman 				at91_set_gpio_value(AT91_PIN_PA21, 1);
279ab4382d2SGreg Kroah-Hartman 		}
280ab4382d2SGreg Kroah-Hartman 	}
281ab4382d2SGreg Kroah-Hartman #endif
282ab4382d2SGreg Kroah-Hartman 
283ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
284ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSEN;
285ab4382d2SGreg Kroah-Hartman 	else
286ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSDIS;
287ab4382d2SGreg Kroah-Hartman 
288ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
289ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
290ab4382d2SGreg Kroah-Hartman 	else
291ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
292ab4382d2SGreg Kroah-Hartman 
293ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, control);
294ab4382d2SGreg Kroah-Hartman 
295ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
296ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
297ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
298ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
299ab4382d2SGreg Kroah-Hartman 	else
300ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
301ab4382d2SGreg Kroah-Hartman 
302ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
303ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
304ab4382d2SGreg Kroah-Hartman 
305ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
306ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
30793f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
308ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
309ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
310ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
311ab4382d2SGreg Kroah-Hartman 	} else {
312ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
313ab4382d2SGreg Kroah-Hartman 	}
314ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
315ab4382d2SGreg Kroah-Hartman }
316ab4382d2SGreg Kroah-Hartman 
317ab4382d2SGreg Kroah-Hartman /*
318ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
319ab4382d2SGreg Kroah-Hartman  */
320ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
321ab4382d2SGreg Kroah-Hartman {
322ab4382d2SGreg Kroah-Hartman 	unsigned int status, ret = 0;
323ab4382d2SGreg Kroah-Hartman 
324ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
325ab4382d2SGreg Kroah-Hartman 
326ab4382d2SGreg Kroah-Hartman 	/*
327ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
328ab4382d2SGreg Kroah-Hartman 	 */
329ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
330ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
331ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
332ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
333ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
334ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
335ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
336ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
337ab4382d2SGreg Kroah-Hartman 
338ab4382d2SGreg Kroah-Hartman 	return ret;
339ab4382d2SGreg Kroah-Hartman }
340ab4382d2SGreg Kroah-Hartman 
341ab4382d2SGreg Kroah-Hartman /*
342ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
343ab4382d2SGreg Kroah-Hartman  */
344ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
345ab4382d2SGreg Kroah-Hartman {
346ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
347ab4382d2SGreg Kroah-Hartman 
348ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
349ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
350ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
351ab4382d2SGreg Kroah-Hartman 	}
352ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
353ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
354ab4382d2SGreg Kroah-Hartman 
35583cac9f3SBernhard Roth 	if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
35683cac9f3SBernhard Roth 	    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
357ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
358ab4382d2SGreg Kroah-Hartman }
359ab4382d2SGreg Kroah-Hartman 
360ab4382d2SGreg Kroah-Hartman /*
361ab4382d2SGreg Kroah-Hartman  * Start transmitting.
362ab4382d2SGreg Kroah-Hartman  */
363ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
364ab4382d2SGreg Kroah-Hartman {
365ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
366ab4382d2SGreg Kroah-Hartman 
367ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
368ab4382d2SGreg Kroah-Hartman 		if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
369ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
370ab4382d2SGreg Kroah-Hartman 			   really need this.*/
371ab4382d2SGreg Kroah-Hartman 			return;
372ab4382d2SGreg Kroah-Hartman 
37383cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
37483cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
375ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
376ab4382d2SGreg Kroah-Hartman 
377ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
378ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
379ab4382d2SGreg Kroah-Hartman 	}
380ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
381ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
382ab4382d2SGreg Kroah-Hartman }
383ab4382d2SGreg Kroah-Hartman 
384ab4382d2SGreg Kroah-Hartman /*
385ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
386ab4382d2SGreg Kroah-Hartman  */
387ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
388ab4382d2SGreg Kroah-Hartman {
389ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);  /* reset status and receiver */
390ab4382d2SGreg Kroah-Hartman 
39157c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXEN);
39257c36868SSiftar, Gabe 
393ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
394ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
395ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
396ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
397ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
398ab4382d2SGreg Kroah-Hartman 	} else {
399ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
400ab4382d2SGreg Kroah-Hartman 	}
401ab4382d2SGreg Kroah-Hartman }
402ab4382d2SGreg Kroah-Hartman 
403ab4382d2SGreg Kroah-Hartman /*
404ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
405ab4382d2SGreg Kroah-Hartman  */
406ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
407ab4382d2SGreg Kroah-Hartman {
40857c36868SSiftar, Gabe 	UART_PUT_CR(port, ATMEL_US_RXDIS);
40957c36868SSiftar, Gabe 
410ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
411ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
412ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
413ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
414ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
415ab4382d2SGreg Kroah-Hartman 	} else {
416ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXRDY);
417ab4382d2SGreg Kroah-Hartman 	}
418ab4382d2SGreg Kroah-Hartman }
419ab4382d2SGreg Kroah-Hartman 
420ab4382d2SGreg Kroah-Hartman /*
421ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
422ab4382d2SGreg Kroah-Hartman  */
423ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
424ab4382d2SGreg Kroah-Hartman {
425ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
426ab4382d2SGreg Kroah-Hartman 			| ATMEL_US_DCDIC | ATMEL_US_CTSIC);
427ab4382d2SGreg Kroah-Hartman }
428ab4382d2SGreg Kroah-Hartman 
429ab4382d2SGreg Kroah-Hartman /*
430ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
431ab4382d2SGreg Kroah-Hartman  */
432ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
433ab4382d2SGreg Kroah-Hartman {
434ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
435ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTBRK);	/* start break */
436ab4382d2SGreg Kroah-Hartman 	else
437ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STPBRK);	/* stop break */
438ab4382d2SGreg Kroah-Hartman }
439ab4382d2SGreg Kroah-Hartman 
440ab4382d2SGreg Kroah-Hartman /*
441ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
442ab4382d2SGreg Kroah-Hartman  */
443ab4382d2SGreg Kroah-Hartman static void
444ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
445ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
446ab4382d2SGreg Kroah-Hartman {
447ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
448ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
449ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
450ab4382d2SGreg Kroah-Hartman 
451ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
452ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
453ab4382d2SGreg Kroah-Hartman 		return;
454ab4382d2SGreg Kroah-Hartman 
455ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
456ab4382d2SGreg Kroah-Hartman 	c->status	= status;
457ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
458ab4382d2SGreg Kroah-Hartman 
459ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
460ab4382d2SGreg Kroah-Hartman 	smp_wmb();
461ab4382d2SGreg Kroah-Hartman 
462ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
463ab4382d2SGreg Kroah-Hartman }
464ab4382d2SGreg Kroah-Hartman 
465ab4382d2SGreg Kroah-Hartman /*
466ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
467ab4382d2SGreg Kroah-Hartman  */
468ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
469ab4382d2SGreg Kroah-Hartman {
470ab4382d2SGreg Kroah-Hartman 	/* clear error */
471ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
472ab4382d2SGreg Kroah-Hartman 
473ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
474ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
475ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
476ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
477ab4382d2SGreg Kroah-Hartman 	}
478ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
479ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
480ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
481ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
482ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
483ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
484ab4382d2SGreg Kroah-Hartman }
485ab4382d2SGreg Kroah-Hartman 
486ab4382d2SGreg Kroah-Hartman /*
487ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
488ab4382d2SGreg Kroah-Hartman  */
489ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
490ab4382d2SGreg Kroah-Hartman {
491ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
492ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
493ab4382d2SGreg Kroah-Hartman 
494ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
495ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
496ab4382d2SGreg Kroah-Hartman 		ch = UART_GET_CHAR(port);
497ab4382d2SGreg Kroah-Hartman 
498ab4382d2SGreg Kroah-Hartman 		/*
499ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
500ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
501ab4382d2SGreg Kroah-Hartman 		 */
502ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
503ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
504ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
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 			    && !atmel_port->break_active) {
511ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
512ab4382d2SGreg Kroah-Hartman 				UART_PUT_IER(port, ATMEL_US_RXBRK);
513ab4382d2SGreg Kroah-Hartman 			} else {
514ab4382d2SGreg Kroah-Hartman 				/*
515ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
516ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
517ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
518ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
519ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
520ab4382d2SGreg Kroah-Hartman 				 */
521ab4382d2SGreg Kroah-Hartman 				UART_PUT_IDR(port, ATMEL_US_RXBRK);
522ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
523ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
524ab4382d2SGreg Kroah-Hartman 			}
525ab4382d2SGreg Kroah-Hartman 		}
526ab4382d2SGreg Kroah-Hartman 
527ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
528ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
529ab4382d2SGreg Kroah-Hartman 	}
530ab4382d2SGreg Kroah-Hartman 
531ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
532ab4382d2SGreg Kroah-Hartman }
533ab4382d2SGreg Kroah-Hartman 
534ab4382d2SGreg Kroah-Hartman /*
535ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
536ab4382d2SGreg Kroah-Hartman  * disabled)
537ab4382d2SGreg Kroah-Hartman  */
538ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
539ab4382d2SGreg Kroah-Hartman {
540ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
541ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
542ab4382d2SGreg Kroah-Hartman 
543ab4382d2SGreg Kroah-Hartman 	if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
544ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, port->x_char);
545ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
546ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
547ab4382d2SGreg Kroah-Hartman 	}
548ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
549ab4382d2SGreg Kroah-Hartman 		return;
550ab4382d2SGreg Kroah-Hartman 
551ab4382d2SGreg Kroah-Hartman 	while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
552ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
553ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
554ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
555ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
556ab4382d2SGreg Kroah-Hartman 			break;
557ab4382d2SGreg Kroah-Hartman 	}
558ab4382d2SGreg Kroah-Hartman 
559ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
560ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
561ab4382d2SGreg Kroah-Hartman 
562ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
563ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
564ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
565ab4382d2SGreg Kroah-Hartman }
566ab4382d2SGreg Kroah-Hartman 
567ab4382d2SGreg Kroah-Hartman /*
568ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
569ab4382d2SGreg Kroah-Hartman  */
570ab4382d2SGreg Kroah-Hartman static void
571ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
572ab4382d2SGreg Kroah-Hartman {
573ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
574ab4382d2SGreg Kroah-Hartman 
575ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
576ab4382d2SGreg Kroah-Hartman 		/*
577ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
578ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
579ab4382d2SGreg Kroah-Hartman 		 *
580ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
581ab4382d2SGreg Kroah-Hartman 		 * the moment.
582ab4382d2SGreg Kroah-Hartman 		 */
583ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
584ab4382d2SGreg Kroah-Hartman 			UART_PUT_IDR(port, (ATMEL_US_ENDRX
585ab4382d2SGreg Kroah-Hartman 						| ATMEL_US_TIMEOUT));
586ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
587ab4382d2SGreg Kroah-Hartman 		}
588ab4382d2SGreg Kroah-Hartman 
589ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
590ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
591ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
592ab4382d2SGreg Kroah-Hartman 	}
593ab4382d2SGreg Kroah-Hartman 
594ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
595ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
596ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
597ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
598ab4382d2SGreg Kroah-Hartman 		/*
599ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
600ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
601ab4382d2SGreg Kroah-Hartman 		 */
602ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_RSTSTA);
603ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXBRK);
604ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
605ab4382d2SGreg Kroah-Hartman 	}
606ab4382d2SGreg Kroah-Hartman }
607ab4382d2SGreg Kroah-Hartman 
608ab4382d2SGreg Kroah-Hartman /*
609ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
610ab4382d2SGreg Kroah-Hartman  */
611ab4382d2SGreg Kroah-Hartman static void
612ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
613ab4382d2SGreg Kroah-Hartman {
614ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
615ab4382d2SGreg Kroah-Hartman 
616ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
617ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
618ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, atmel_port->tx_done_mask);
619ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
620ab4382d2SGreg Kroah-Hartman 	}
621ab4382d2SGreg Kroah-Hartman }
622ab4382d2SGreg Kroah-Hartman 
623ab4382d2SGreg Kroah-Hartman /*
624ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
625ab4382d2SGreg Kroah-Hartman  */
626ab4382d2SGreg Kroah-Hartman static void
627ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
628ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
629ab4382d2SGreg Kroah-Hartman {
630ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
631ab4382d2SGreg Kroah-Hartman 
632ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
633ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
634ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
635ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
636ab4382d2SGreg Kroah-Hartman 	}
637ab4382d2SGreg Kroah-Hartman }
638ab4382d2SGreg Kroah-Hartman 
639ab4382d2SGreg Kroah-Hartman /*
640ab4382d2SGreg Kroah-Hartman  * Interrupt handler
641ab4382d2SGreg Kroah-Hartman  */
642ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
643ab4382d2SGreg Kroah-Hartman {
644ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
645ab4382d2SGreg Kroah-Hartman 	unsigned int status, pending, pass_counter = 0;
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman 	do {
648ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
649ab4382d2SGreg Kroah-Hartman 		pending = status & UART_GET_IMR(port);
650ab4382d2SGreg Kroah-Hartman 		if (!pending)
651ab4382d2SGreg Kroah-Hartman 			break;
652ab4382d2SGreg Kroah-Hartman 
653ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
654ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
655ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
656ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
657ab4382d2SGreg Kroah-Hartman 
658ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
659ab4382d2SGreg Kroah-Hartman }
660ab4382d2SGreg Kroah-Hartman 
661ab4382d2SGreg Kroah-Hartman /*
662ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
663ab4382d2SGreg Kroah-Hartman  */
664ab4382d2SGreg Kroah-Hartman static void atmel_tx_dma(struct uart_port *port)
665ab4382d2SGreg Kroah-Hartman {
666ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
667ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
668ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
669ab4382d2SGreg Kroah-Hartman 	int count;
670ab4382d2SGreg Kroah-Hartman 
671ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
672ab4382d2SGreg Kroah-Hartman 	if (UART_GET_TCR(port))
673ab4382d2SGreg Kroah-Hartman 		return;
674ab4382d2SGreg Kroah-Hartman 
675ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
676ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
677ab4382d2SGreg Kroah-Hartman 
678ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
679ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
680ab4382d2SGreg Kroah-Hartman 
681ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
682ab4382d2SGreg Kroah-Hartman 
683ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
684ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
685ab4382d2SGreg Kroah-Hartman 
686ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
687ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
688ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
689ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
690ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
691ab4382d2SGreg Kroah-Hartman 
692ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
693ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
694ab4382d2SGreg Kroah-Hartman 
695ab4382d2SGreg Kroah-Hartman 		UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
696ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, count);
697ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
698ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
699ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
700ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
701ab4382d2SGreg Kroah-Hartman 	} else {
70283cac9f3SBernhard Roth 		if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
70383cac9f3SBernhard Roth 		    !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
704ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
705ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
706ab4382d2SGreg Kroah-Hartman 		}
707ab4382d2SGreg Kroah-Hartman 	}
708ab4382d2SGreg Kroah-Hartman 
709ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
710ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
711ab4382d2SGreg Kroah-Hartman }
712ab4382d2SGreg Kroah-Hartman 
713ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
714ab4382d2SGreg Kroah-Hartman {
715ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
716ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
717ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
718ab4382d2SGreg Kroah-Hartman 	unsigned int status;
719ab4382d2SGreg Kroah-Hartman 
720ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
721ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
722ab4382d2SGreg Kroah-Hartman 
723ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
724ab4382d2SGreg Kroah-Hartman 		smp_rmb();
725ab4382d2SGreg Kroah-Hartman 
726ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
727ab4382d2SGreg Kroah-Hartman 
728ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
729ab4382d2SGreg Kroah-Hartman 
730ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
731ab4382d2SGreg Kroah-Hartman 		status = c.status;
732ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
733ab4382d2SGreg Kroah-Hartman 
734ab4382d2SGreg Kroah-Hartman 		/*
735ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
736ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
737ab4382d2SGreg Kroah-Hartman 		 */
738ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
739ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
740ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
741ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
742ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
743ab4382d2SGreg Kroah-Hartman 
744ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
745ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
746ab4382d2SGreg Kroah-Hartman 					continue;
747ab4382d2SGreg Kroah-Hartman 			}
748ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
749ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
750ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
751ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
752ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
753ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
754ab4382d2SGreg Kroah-Hartman 
755ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
756ab4382d2SGreg Kroah-Hartman 
757ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
758ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
759ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
760ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
761ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
762ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
763ab4382d2SGreg Kroah-Hartman 		}
764ab4382d2SGreg Kroah-Hartman 
765ab4382d2SGreg Kroah-Hartman 
766ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
767ab4382d2SGreg Kroah-Hartman 			continue;
768ab4382d2SGreg Kroah-Hartman 
769ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
770ab4382d2SGreg Kroah-Hartman 	}
771ab4382d2SGreg Kroah-Hartman 
772ab4382d2SGreg Kroah-Hartman 	/*
773ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
774ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
775ab4382d2SGreg Kroah-Hartman 	 */
776ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
7772e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
778ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
779ab4382d2SGreg Kroah-Hartman }
780ab4382d2SGreg Kroah-Hartman 
781ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_dma(struct uart_port *port)
782ab4382d2SGreg Kroah-Hartman {
783ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
78405c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
785ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
786ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
787ab4382d2SGreg Kroah-Hartman 	unsigned int head;
788ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
789ab4382d2SGreg Kroah-Hartman 	unsigned int count;
790ab4382d2SGreg Kroah-Hartman 
791ab4382d2SGreg Kroah-Hartman 	do {
792ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
793ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
794ab4382d2SGreg Kroah-Hartman 
795ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
796ab4382d2SGreg Kroah-Hartman 		head = UART_GET_RPR(port) - pdc->dma_addr;
797ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
798ab4382d2SGreg Kroah-Hartman 
799ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
800ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
801ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
802ab4382d2SGreg Kroah-Hartman 		 * find out.
803ab4382d2SGreg Kroah-Hartman 		 *
804ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
805ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
806ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
807ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
808ab4382d2SGreg Kroah-Hartman 		 */
809ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
810ab4382d2SGreg Kroah-Hartman 
811ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
812ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
813ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
814ab4382d2SGreg Kroah-Hartman 
815ab4382d2SGreg Kroah-Hartman 			/*
816ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
817ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
818ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
819ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
820ab4382d2SGreg Kroah-Hartman 			 */
821ab4382d2SGreg Kroah-Hartman 			count = head - tail;
822ab4382d2SGreg Kroah-Hartman 
82305c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
82405c7cd39SJiri Slaby 						count);
825ab4382d2SGreg Kroah-Hartman 
826ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
827ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
828ab4382d2SGreg Kroah-Hartman 
829ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
830ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
831ab4382d2SGreg Kroah-Hartman 		}
832ab4382d2SGreg Kroah-Hartman 
833ab4382d2SGreg Kroah-Hartman 		/*
834ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
835ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
836ab4382d2SGreg Kroah-Hartman 		 */
837ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
838ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
839ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNPR(port, pdc->dma_addr);
840ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNCR(port, pdc->dma_size);
841ab4382d2SGreg Kroah-Hartman 
842ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
843ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
844ab4382d2SGreg Kroah-Hartman 		}
845ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
846ab4382d2SGreg Kroah-Hartman 
847ab4382d2SGreg Kroah-Hartman 	/*
848ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
849ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
850ab4382d2SGreg Kroah-Hartman 	 */
851ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
8522e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
853ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
854ab4382d2SGreg Kroah-Hartman 
855ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
856ab4382d2SGreg Kroah-Hartman }
857ab4382d2SGreg Kroah-Hartman 
858ab4382d2SGreg Kroah-Hartman /*
859ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
860ab4382d2SGreg Kroah-Hartman  */
861ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
862ab4382d2SGreg Kroah-Hartman {
863ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
864ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
865ab4382d2SGreg Kroah-Hartman 	unsigned int status;
866ab4382d2SGreg Kroah-Hartman 	unsigned int status_change;
867ab4382d2SGreg Kroah-Hartman 
868ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
869ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
870ab4382d2SGreg Kroah-Hartman 
871ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port))
872ab4382d2SGreg Kroah-Hartman 		atmel_tx_dma(port);
873ab4382d2SGreg Kroah-Hartman 	else
874ab4382d2SGreg Kroah-Hartman 		atmel_tx_chars(port);
875ab4382d2SGreg Kroah-Hartman 
876ab4382d2SGreg Kroah-Hartman 	status = atmel_port->irq_status;
877ab4382d2SGreg Kroah-Hartman 	status_change = status ^ atmel_port->irq_status_prev;
878ab4382d2SGreg Kroah-Hartman 
879ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
880ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
881ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
882ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
883ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
884ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
885ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
886ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
887ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
888ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
889ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
890ab4382d2SGreg Kroah-Hartman 
891ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
892ab4382d2SGreg Kroah-Hartman 
893ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status_prev = status;
894ab4382d2SGreg Kroah-Hartman 	}
895ab4382d2SGreg Kroah-Hartman 
896ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port))
897ab4382d2SGreg Kroah-Hartman 		atmel_rx_from_dma(port);
898ab4382d2SGreg Kroah-Hartman 	else
899ab4382d2SGreg Kroah-Hartman 		atmel_rx_from_ring(port);
900ab4382d2SGreg Kroah-Hartman 
901ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
902ab4382d2SGreg Kroah-Hartman }
903ab4382d2SGreg Kroah-Hartman 
904ab4382d2SGreg Kroah-Hartman /*
905ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
906ab4382d2SGreg Kroah-Hartman  */
907ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
908ab4382d2SGreg Kroah-Hartman {
909ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
910ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
911ab4382d2SGreg Kroah-Hartman 	int retval;
912ab4382d2SGreg Kroah-Hartman 
913ab4382d2SGreg Kroah-Hartman 	/*
914ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
915ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
916ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
917ab4382d2SGreg Kroah-Hartman 	 */
918ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
919ab4382d2SGreg Kroah-Hartman 
920ab4382d2SGreg Kroah-Hartman 	/*
921ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
922ab4382d2SGreg Kroah-Hartman 	 */
923ab4382d2SGreg Kroah-Hartman 	retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
924ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
925ab4382d2SGreg Kroah-Hartman 	if (retval) {
926ab4382d2SGreg Kroah-Hartman 		printk("atmel_serial: atmel_startup - Can't get irq\n");
927ab4382d2SGreg Kroah-Hartman 		return retval;
928ab4382d2SGreg Kroah-Hartman 	}
929ab4382d2SGreg Kroah-Hartman 
930ab4382d2SGreg Kroah-Hartman 	/*
931ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
932ab4382d2SGreg Kroah-Hartman 	 */
933ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
934ab4382d2SGreg Kroah-Hartman 		int i;
935ab4382d2SGreg Kroah-Hartman 
936ab4382d2SGreg Kroah-Hartman 		for (i = 0; i < 2; i++) {
937ab4382d2SGreg Kroah-Hartman 			struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
938ab4382d2SGreg Kroah-Hartman 
939ab4382d2SGreg Kroah-Hartman 			pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
940ab4382d2SGreg Kroah-Hartman 			if (pdc->buf == NULL) {
941ab4382d2SGreg Kroah-Hartman 				if (i != 0) {
942ab4382d2SGreg Kroah-Hartman 					dma_unmap_single(port->dev,
943ab4382d2SGreg Kroah-Hartman 						atmel_port->pdc_rx[0].dma_addr,
944ab4382d2SGreg Kroah-Hartman 						PDC_BUFFER_SIZE,
945ab4382d2SGreg Kroah-Hartman 						DMA_FROM_DEVICE);
946ab4382d2SGreg Kroah-Hartman 					kfree(atmel_port->pdc_rx[0].buf);
947ab4382d2SGreg Kroah-Hartman 				}
948ab4382d2SGreg Kroah-Hartman 				free_irq(port->irq, port);
949ab4382d2SGreg Kroah-Hartman 				return -ENOMEM;
950ab4382d2SGreg Kroah-Hartman 			}
951ab4382d2SGreg Kroah-Hartman 			pdc->dma_addr = dma_map_single(port->dev,
952ab4382d2SGreg Kroah-Hartman 						       pdc->buf,
953ab4382d2SGreg Kroah-Hartman 						       PDC_BUFFER_SIZE,
954ab4382d2SGreg Kroah-Hartman 						       DMA_FROM_DEVICE);
955ab4382d2SGreg Kroah-Hartman 			pdc->dma_size = PDC_BUFFER_SIZE;
956ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
957ab4382d2SGreg Kroah-Hartman 		}
958ab4382d2SGreg Kroah-Hartman 
959ab4382d2SGreg Kroah-Hartman 		atmel_port->pdc_rx_idx = 0;
960ab4382d2SGreg Kroah-Hartman 
961ab4382d2SGreg Kroah-Hartman 		UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
962ab4382d2SGreg Kroah-Hartman 		UART_PUT_RCR(port, PDC_BUFFER_SIZE);
963ab4382d2SGreg Kroah-Hartman 
964ab4382d2SGreg Kroah-Hartman 		UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
965ab4382d2SGreg Kroah-Hartman 		UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
966ab4382d2SGreg Kroah-Hartman 	}
967ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
968ab4382d2SGreg Kroah-Hartman 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
969ab4382d2SGreg Kroah-Hartman 		struct circ_buf *xmit = &port->state->xmit;
970ab4382d2SGreg Kroah-Hartman 
971ab4382d2SGreg Kroah-Hartman 		pdc->buf = xmit->buf;
972ab4382d2SGreg Kroah-Hartman 		pdc->dma_addr = dma_map_single(port->dev,
973ab4382d2SGreg Kroah-Hartman 					       pdc->buf,
974ab4382d2SGreg Kroah-Hartman 					       UART_XMIT_SIZE,
975ab4382d2SGreg Kroah-Hartman 					       DMA_TO_DEVICE);
976ab4382d2SGreg Kroah-Hartman 		pdc->dma_size = UART_XMIT_SIZE;
977ab4382d2SGreg Kroah-Hartman 		pdc->ofs = 0;
978ab4382d2SGreg Kroah-Hartman 	}
979ab4382d2SGreg Kroah-Hartman 
980ab4382d2SGreg Kroah-Hartman 	/*
981ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "open" function (to register
982ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
983ab4382d2SGreg Kroah-Hartman 	 */
984ab4382d2SGreg Kroah-Hartman 	if (atmel_open_hook) {
985ab4382d2SGreg Kroah-Hartman 		retval = atmel_open_hook(port);
986ab4382d2SGreg Kroah-Hartman 		if (retval) {
987ab4382d2SGreg Kroah-Hartman 			free_irq(port->irq, port);
988ab4382d2SGreg Kroah-Hartman 			return retval;
989ab4382d2SGreg Kroah-Hartman 		}
990ab4382d2SGreg Kroah-Hartman 	}
991ab4382d2SGreg Kroah-Hartman 
992ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
993ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status_prev = UART_GET_CSR(port);
994ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
995ab4382d2SGreg Kroah-Hartman 
996ab4382d2SGreg Kroah-Hartman 	/*
997ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
998ab4382d2SGreg Kroah-Hartman 	 */
999ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1000ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
1001ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1002ab4382d2SGreg Kroah-Hartman 
1003ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
1004ab4382d2SGreg Kroah-Hartman 		/* set UART timeout */
1005ab4382d2SGreg Kroah-Hartman 		UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1006ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
1007ab4382d2SGreg Kroah-Hartman 
1008ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1009ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
1010ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
1011ab4382d2SGreg Kroah-Hartman 	} else {
1012ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
1013ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
1014ab4382d2SGreg Kroah-Hartman 	}
1015ab4382d2SGreg Kroah-Hartman 
1016ab4382d2SGreg Kroah-Hartman 	return 0;
1017ab4382d2SGreg Kroah-Hartman }
1018ab4382d2SGreg Kroah-Hartman 
1019ab4382d2SGreg Kroah-Hartman /*
1020ab4382d2SGreg Kroah-Hartman  * Disable the port
1021ab4382d2SGreg Kroah-Hartman  */
1022ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1023ab4382d2SGreg Kroah-Hartman {
1024ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1025ab4382d2SGreg Kroah-Hartman 	/*
1026ab4382d2SGreg Kroah-Hartman 	 * Ensure everything is stopped.
1027ab4382d2SGreg Kroah-Hartman 	 */
1028ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1029ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1030ab4382d2SGreg Kroah-Hartman 
1031ab4382d2SGreg Kroah-Hartman 	/*
1032ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1033ab4382d2SGreg Kroah-Hartman 	 */
1034ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
1035ab4382d2SGreg Kroah-Hartman 		int i;
1036ab4382d2SGreg Kroah-Hartman 
1037ab4382d2SGreg Kroah-Hartman 		for (i = 0; i < 2; i++) {
1038ab4382d2SGreg Kroah-Hartman 			struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1039ab4382d2SGreg Kroah-Hartman 
1040ab4382d2SGreg Kroah-Hartman 			dma_unmap_single(port->dev,
1041ab4382d2SGreg Kroah-Hartman 					 pdc->dma_addr,
1042ab4382d2SGreg Kroah-Hartman 					 pdc->dma_size,
1043ab4382d2SGreg Kroah-Hartman 					 DMA_FROM_DEVICE);
1044ab4382d2SGreg Kroah-Hartman 			kfree(pdc->buf);
1045ab4382d2SGreg Kroah-Hartman 		}
1046ab4382d2SGreg Kroah-Hartman 	}
1047ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
1048ab4382d2SGreg Kroah-Hartman 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1049ab4382d2SGreg Kroah-Hartman 
1050ab4382d2SGreg Kroah-Hartman 		dma_unmap_single(port->dev,
1051ab4382d2SGreg Kroah-Hartman 				 pdc->dma_addr,
1052ab4382d2SGreg Kroah-Hartman 				 pdc->dma_size,
1053ab4382d2SGreg Kroah-Hartman 				 DMA_TO_DEVICE);
1054ab4382d2SGreg Kroah-Hartman 	}
1055ab4382d2SGreg Kroah-Hartman 
1056ab4382d2SGreg Kroah-Hartman 	/*
1057ab4382d2SGreg Kroah-Hartman 	 * Disable all interrupts, port and break condition.
1058ab4382d2SGreg Kroah-Hartman 	 */
1059ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
1060ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1061ab4382d2SGreg Kroah-Hartman 
1062ab4382d2SGreg Kroah-Hartman 	/*
1063ab4382d2SGreg Kroah-Hartman 	 * Free the interrupt
1064ab4382d2SGreg Kroah-Hartman 	 */
1065ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1066ab4382d2SGreg Kroah-Hartman 
1067ab4382d2SGreg Kroah-Hartman 	/*
1068ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "close" function (to unregister
1069ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
1070ab4382d2SGreg Kroah-Hartman 	 */
1071ab4382d2SGreg Kroah-Hartman 	if (atmel_close_hook)
1072ab4382d2SGreg Kroah-Hartman 		atmel_close_hook(port);
1073ab4382d2SGreg Kroah-Hartman }
1074ab4382d2SGreg Kroah-Hartman 
1075ab4382d2SGreg Kroah-Hartman /*
1076ab4382d2SGreg Kroah-Hartman  * Flush any TX data submitted for DMA. Called when the TX circular
1077ab4382d2SGreg Kroah-Hartman  * buffer is reset.
1078ab4382d2SGreg Kroah-Hartman  */
1079ab4382d2SGreg Kroah-Hartman static void atmel_flush_buffer(struct uart_port *port)
1080ab4382d2SGreg Kroah-Hartman {
1081ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1082ab4382d2SGreg Kroah-Hartman 
1083ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
1084ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, 0);
1085ab4382d2SGreg Kroah-Hartman 		atmel_port->pdc_tx.ofs = 0;
1086ab4382d2SGreg Kroah-Hartman 	}
1087ab4382d2SGreg Kroah-Hartman }
1088ab4382d2SGreg Kroah-Hartman 
1089ab4382d2SGreg Kroah-Hartman /*
1090ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1091ab4382d2SGreg Kroah-Hartman  */
1092ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1093ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1094ab4382d2SGreg Kroah-Hartman {
1095ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1096ab4382d2SGreg Kroah-Hartman 
1097ab4382d2SGreg Kroah-Hartman 	switch (state) {
1098ab4382d2SGreg Kroah-Hartman 	case 0:
1099ab4382d2SGreg Kroah-Hartman 		/*
1100ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1101ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1102ab4382d2SGreg Kroah-Hartman 		 */
1103*91f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1104ab4382d2SGreg Kroah-Hartman 
1105ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1106ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->backup_imr);
1107ab4382d2SGreg Kroah-Hartman 		break;
1108ab4382d2SGreg Kroah-Hartman 	case 3:
1109ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1110ab4382d2SGreg Kroah-Hartman 		atmel_port->backup_imr = UART_GET_IMR(port);
1111ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, -1);
1112ab4382d2SGreg Kroah-Hartman 
1113ab4382d2SGreg Kroah-Hartman 		/*
1114ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1115ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1116ab4382d2SGreg Kroah-Hartman 		 */
1117*91f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1118ab4382d2SGreg Kroah-Hartman 		break;
1119ab4382d2SGreg Kroah-Hartman 	default:
1120ab4382d2SGreg Kroah-Hartman 		printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1121ab4382d2SGreg Kroah-Hartman 	}
1122ab4382d2SGreg Kroah-Hartman }
1123ab4382d2SGreg Kroah-Hartman 
1124ab4382d2SGreg Kroah-Hartman /*
1125ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1126ab4382d2SGreg Kroah-Hartman  */
1127ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1128ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1129ab4382d2SGreg Kroah-Hartman {
1130ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1131ab4382d2SGreg Kroah-Hartman 	unsigned int mode, imr, quot, baud;
1132ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1133ab4382d2SGreg Kroah-Hartman 
1134ab4382d2SGreg Kroah-Hartman 	/* Get current mode register */
1135ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1136ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_NBSTOP | ATMEL_US_PAR
1137ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_USMODE);
1138ab4382d2SGreg Kroah-Hartman 
1139ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1140ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1141ab4382d2SGreg Kroah-Hartman 
1142ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1143ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1144ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1145ab4382d2SGreg Kroah-Hartman 	}
1146ab4382d2SGreg Kroah-Hartman 
1147ab4382d2SGreg Kroah-Hartman 	/* byte size */
1148ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1149ab4382d2SGreg Kroah-Hartman 	case CS5:
1150ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1151ab4382d2SGreg Kroah-Hartman 		break;
1152ab4382d2SGreg Kroah-Hartman 	case CS6:
1153ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1154ab4382d2SGreg Kroah-Hartman 		break;
1155ab4382d2SGreg Kroah-Hartman 	case CS7:
1156ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1157ab4382d2SGreg Kroah-Hartman 		break;
1158ab4382d2SGreg Kroah-Hartman 	default:
1159ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1160ab4382d2SGreg Kroah-Hartman 		break;
1161ab4382d2SGreg Kroah-Hartman 	}
1162ab4382d2SGreg Kroah-Hartman 
1163ab4382d2SGreg Kroah-Hartman 	/* stop bits */
1164ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
1165ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
1166ab4382d2SGreg Kroah-Hartman 
1167ab4382d2SGreg Kroah-Hartman 	/* parity */
1168ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
1169ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
1170ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
1171ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
1172ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
1173ab4382d2SGreg Kroah-Hartman 			else
1174ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
1175ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
1176ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
1177ab4382d2SGreg Kroah-Hartman 		else
1178ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
1179ab4382d2SGreg Kroah-Hartman 	} else
1180ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
1181ab4382d2SGreg Kroah-Hartman 
1182ab4382d2SGreg Kroah-Hartman 	/* hardware handshake (RTS/CTS) */
1183ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
1184ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_HWHS;
1185ab4382d2SGreg Kroah-Hartman 	else
1186ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_NORMAL;
1187ab4382d2SGreg Kroah-Hartman 
1188ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
1189ab4382d2SGreg Kroah-Hartman 
1190ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
1191ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
1192ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1193ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & (BRKINT | PARMRK))
1194ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
1195ab4382d2SGreg Kroah-Hartman 
1196ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port))
1197ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
1198ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, port->read_status_mask);
1199ab4382d2SGreg Kroah-Hartman 
1200ab4382d2SGreg Kroah-Hartman 	/*
1201ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
1202ab4382d2SGreg Kroah-Hartman 	 */
1203ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
1204ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
1205ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1206ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
1207ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
1208ab4382d2SGreg Kroah-Hartman 		/*
1209ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
1210ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
1211ab4382d2SGreg Kroah-Hartman 		 */
1212ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
1213ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
1214ab4382d2SGreg Kroah-Hartman 	}
1215ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
1216ab4382d2SGreg Kroah-Hartman 
1217ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
1218ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
1219ab4382d2SGreg Kroah-Hartman 
1220ab4382d2SGreg Kroah-Hartman 	/*
1221ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
1222ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
1223ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
1224ab4382d2SGreg Kroah-Hartman 	 */
1225ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
1226ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1227ab4382d2SGreg Kroah-Hartman 
1228ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
1229ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1230ab4382d2SGreg Kroah-Hartman 
1231ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
1232ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
1233ab4382d2SGreg Kroah-Hartman 
1234ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1235ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
123693f3350cSClaudio Scordino 		if ((atmel_port->rs485.delay_rts_after_send) > 0)
1237ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
1238ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
1239ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
1240ab4382d2SGreg Kroah-Hartman 	} else {
1241ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
1242ab4382d2SGreg Kroah-Hartman 	}
1243ab4382d2SGreg Kroah-Hartman 
1244ab4382d2SGreg Kroah-Hartman 	/* set the parity, stop bits and data size */
1245ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
1246ab4382d2SGreg Kroah-Hartman 
1247ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
1248ab4382d2SGreg Kroah-Hartman 	UART_PUT_BRGR(port, quot);
1249ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1250ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1251ab4382d2SGreg Kroah-Hartman 
1252ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
1253ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
1254ab4382d2SGreg Kroah-Hartman 
1255ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
1256ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
1257ab4382d2SGreg Kroah-Hartman 		port->ops->enable_ms(port);
1258ab4382d2SGreg Kroah-Hartman 
1259ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
1260ab4382d2SGreg Kroah-Hartman }
1261ab4382d2SGreg Kroah-Hartman 
126242bd7a4fSViktar Palstsiuk static void atmel_set_ldisc(struct uart_port *port, int new)
126342bd7a4fSViktar Palstsiuk {
1264b54bf3b2SJiri Slaby 	if (new == N_PPS) {
126542bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
126642bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
126742bd7a4fSViktar Palstsiuk 	} else {
126842bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
126942bd7a4fSViktar Palstsiuk 	}
127042bd7a4fSViktar Palstsiuk }
127142bd7a4fSViktar Palstsiuk 
1272ab4382d2SGreg Kroah-Hartman /*
1273ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
1274ab4382d2SGreg Kroah-Hartman  */
1275ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
1276ab4382d2SGreg Kroah-Hartman {
1277ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1278ab4382d2SGreg Kroah-Hartman }
1279ab4382d2SGreg Kroah-Hartman 
1280ab4382d2SGreg Kroah-Hartman /*
1281ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
1282ab4382d2SGreg Kroah-Hartman  */
1283ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
1284ab4382d2SGreg Kroah-Hartman {
1285ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1286ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1287ab4382d2SGreg Kroah-Hartman 
1288ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
1289ab4382d2SGreg Kroah-Hartman 
1290ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1291ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
1292ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
1293ab4382d2SGreg Kroah-Hartman 	}
1294ab4382d2SGreg Kroah-Hartman }
1295ab4382d2SGreg Kroah-Hartman 
1296ab4382d2SGreg Kroah-Hartman /*
1297ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
1298ab4382d2SGreg Kroah-Hartman  */
1299ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
1300ab4382d2SGreg Kroah-Hartman {
1301ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1302ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1303ab4382d2SGreg Kroah-Hartman 
1304ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1305ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
1306ab4382d2SGreg Kroah-Hartman 
1307ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1308ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
1309ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
1310ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
1311ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
1312ab4382d2SGreg Kroah-Hartman 		}
1313ab4382d2SGreg Kroah-Hartman 	}
1314ab4382d2SGreg Kroah-Hartman 
1315ab4382d2SGreg Kroah-Hartman 	return 0;
1316ab4382d2SGreg Kroah-Hartman }
1317ab4382d2SGreg Kroah-Hartman 
1318ab4382d2SGreg Kroah-Hartman /*
1319ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
1320ab4382d2SGreg Kroah-Hartman  */
1321ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
1322ab4382d2SGreg Kroah-Hartman {
1323ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
1324ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
1325ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
1326ab4382d2SGreg Kroah-Hartman 	}
1327ab4382d2SGreg Kroah-Hartman }
1328ab4382d2SGreg Kroah-Hartman 
1329ab4382d2SGreg Kroah-Hartman /*
1330ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
1331ab4382d2SGreg Kroah-Hartman  */
1332ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1333ab4382d2SGreg Kroah-Hartman {
1334ab4382d2SGreg Kroah-Hartman 	int ret = 0;
1335ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1336ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1337ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
1338ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1339ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
1340ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1341ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
1342ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1343ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
1344ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1345ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
1346ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1347ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
1348ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1349ab4382d2SGreg Kroah-Hartman 	return ret;
1350ab4382d2SGreg Kroah-Hartman }
1351ab4382d2SGreg Kroah-Hartman 
1352ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
1353ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
1354ab4382d2SGreg Kroah-Hartman {
1355ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1356ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1357ab4382d2SGreg Kroah-Hartman 
1358ab4382d2SGreg Kroah-Hartman 	return UART_GET_CHAR(port);
1359ab4382d2SGreg Kroah-Hartman }
1360ab4382d2SGreg Kroah-Hartman 
1361ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1362ab4382d2SGreg Kroah-Hartman {
1363ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1364ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1365ab4382d2SGreg Kroah-Hartman 
1366ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
1367ab4382d2SGreg Kroah-Hartman }
1368ab4382d2SGreg Kroah-Hartman #endif
1369ab4382d2SGreg Kroah-Hartman 
1370ab4382d2SGreg Kroah-Hartman static int
1371ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1372ab4382d2SGreg Kroah-Hartman {
1373ab4382d2SGreg Kroah-Hartman 	struct serial_rs485 rs485conf;
1374ab4382d2SGreg Kroah-Hartman 
1375ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1376ab4382d2SGreg Kroah-Hartman 	case TIOCSRS485:
1377ab4382d2SGreg Kroah-Hartman 		if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1378ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
1379ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
1380ab4382d2SGreg Kroah-Hartman 
1381ab4382d2SGreg Kroah-Hartman 		atmel_config_rs485(port, &rs485conf);
1382ab4382d2SGreg Kroah-Hartman 		break;
1383ab4382d2SGreg Kroah-Hartman 
1384ab4382d2SGreg Kroah-Hartman 	case TIOCGRS485:
1385ab4382d2SGreg Kroah-Hartman 		if (copy_to_user((struct serial_rs485 *) arg,
1386ab4382d2SGreg Kroah-Hartman 					&(to_atmel_uart_port(port)->rs485),
1387ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
1388ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
1389ab4382d2SGreg Kroah-Hartman 		break;
1390ab4382d2SGreg Kroah-Hartman 
1391ab4382d2SGreg Kroah-Hartman 	default:
1392ab4382d2SGreg Kroah-Hartman 		return -ENOIOCTLCMD;
1393ab4382d2SGreg Kroah-Hartman 	}
1394ab4382d2SGreg Kroah-Hartman 	return 0;
1395ab4382d2SGreg Kroah-Hartman }
1396ab4382d2SGreg Kroah-Hartman 
1397ab4382d2SGreg Kroah-Hartman 
1398ab4382d2SGreg Kroah-Hartman 
1399ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
1400ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
1401ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
1402ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
1403ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
1404ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
1405ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
1406ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
1407ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
1408ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
1409ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
1410ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
1411ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
141242bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
1413ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
1414ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
1415ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
1416ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
1417ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
1418ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
1419ab4382d2SGreg Kroah-Hartman 	.ioctl		= atmel_ioctl,
1420ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
1421ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
1422ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
1423ab4382d2SGreg Kroah-Hartman #endif
1424ab4382d2SGreg Kroah-Hartman };
1425ab4382d2SGreg Kroah-Hartman 
14269671f099SBill Pemberton static void atmel_of_init_port(struct atmel_uart_port *atmel_port,
14275fbe46b6SNicolas Ferre 					 struct device_node *np)
14285fbe46b6SNicolas Ferre {
14295fbe46b6SNicolas Ferre 	u32 rs485_delay[2];
14305fbe46b6SNicolas Ferre 
14315fbe46b6SNicolas Ferre 	/* DMA/PDC usage specification */
14325fbe46b6SNicolas Ferre 	if (of_get_property(np, "atmel,use-dma-rx", NULL))
14335fbe46b6SNicolas Ferre 		atmel_port->use_dma_rx	= 1;
14345fbe46b6SNicolas Ferre 	else
14355fbe46b6SNicolas Ferre 		atmel_port->use_dma_rx	= 0;
14365fbe46b6SNicolas Ferre 	if (of_get_property(np, "atmel,use-dma-tx", NULL))
14375fbe46b6SNicolas Ferre 		atmel_port->use_dma_tx	= 1;
14385fbe46b6SNicolas Ferre 	else
14395fbe46b6SNicolas Ferre 		atmel_port->use_dma_tx	= 0;
14405fbe46b6SNicolas Ferre 
14415fbe46b6SNicolas Ferre 	/* rs485 properties */
14425fbe46b6SNicolas Ferre 	if (of_property_read_u32_array(np, "rs485-rts-delay",
14435fbe46b6SNicolas Ferre 					    rs485_delay, 2) == 0) {
14445fbe46b6SNicolas Ferre 		struct serial_rs485 *rs485conf = &atmel_port->rs485;
14455fbe46b6SNicolas Ferre 
14465fbe46b6SNicolas Ferre 		rs485conf->delay_rts_before_send = rs485_delay[0];
14475fbe46b6SNicolas Ferre 		rs485conf->delay_rts_after_send = rs485_delay[1];
14485fbe46b6SNicolas Ferre 		rs485conf->flags = 0;
14495fbe46b6SNicolas Ferre 
14505fbe46b6SNicolas Ferre 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
14515fbe46b6SNicolas Ferre 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
14525fbe46b6SNicolas Ferre 
14535fbe46b6SNicolas Ferre 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL))
14545fbe46b6SNicolas Ferre 			rs485conf->flags |= SER_RS485_ENABLED;
14555fbe46b6SNicolas Ferre 	}
14565fbe46b6SNicolas Ferre }
14575fbe46b6SNicolas Ferre 
1458ab4382d2SGreg Kroah-Hartman /*
1459ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
1460ab4382d2SGreg Kroah-Hartman  */
1461*91f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
1462ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
1463ab4382d2SGreg Kroah-Hartman {
1464*91f8c2d8SBoris BREZILLON 	int ret;
1465ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
14661acfc7ecSNicolas Ferre 	struct atmel_uart_data *pdata = pdev->dev.platform_data;
1467ab4382d2SGreg Kroah-Hartman 
14685fbe46b6SNicolas Ferre 	if (pdev->dev.of_node) {
14695fbe46b6SNicolas Ferre 		atmel_of_init_port(atmel_port, pdev->dev.of_node);
14705fbe46b6SNicolas Ferre 	} else {
14715fbe46b6SNicolas Ferre 		atmel_port->use_dma_rx	= pdata->use_dma_rx;
14725fbe46b6SNicolas Ferre 		atmel_port->use_dma_tx	= pdata->use_dma_tx;
14735fbe46b6SNicolas Ferre 		atmel_port->rs485	= pdata->rs485;
14745fbe46b6SNicolas Ferre 	}
1475ab4382d2SGreg Kroah-Hartman 
1476ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
1477ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
1478ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
1479ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
1480ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
1481ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
1482ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
1483ab4382d2SGreg Kroah-Hartman 
1484ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1485ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
1486ab4382d2SGreg Kroah-Hartman 
1487ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1488ab4382d2SGreg Kroah-Hartman 
14895fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
1490ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
14911acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
1492588edbf3SNicolas Ferre 	} else {
1493ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
1494ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
1495ab4382d2SGreg Kroah-Hartman 	}
1496ab4382d2SGreg Kroah-Hartman 
1497ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
1498ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
1499ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
1500*91f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
1501*91f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
1502*91f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
1503*91f8c2d8SBoris BREZILLON 			return ret;
1504*91f8c2d8SBoris BREZILLON 		}
1505*91f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
1506*91f8c2d8SBoris BREZILLON 		if (ret) {
1507*91f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
1508*91f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
1509*91f8c2d8SBoris BREZILLON 			return ret;
1510*91f8c2d8SBoris BREZILLON 		}
1511ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
1512*91f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1513ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
1514ab4382d2SGreg Kroah-Hartman 	}
1515ab4382d2SGreg Kroah-Hartman 
1516ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1517ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1518ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1519ab4382d2SGreg Kroah-Hartman 	else if (atmel_use_dma_tx(port)) {
1520ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
1521ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1522ab4382d2SGreg Kroah-Hartman 	} else {
1523ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
1524ab4382d2SGreg Kroah-Hartman 	}
1525*91f8c2d8SBoris BREZILLON 
1526*91f8c2d8SBoris BREZILLON 	return 0;
1527ab4382d2SGreg Kroah-Hartman }
1528ab4382d2SGreg Kroah-Hartman 
152969f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
153069f6a27bSJean-Christophe PLAGNIOL-VILLARD 
1531ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1532ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
1533ab4382d2SGreg Kroah-Hartman {
1534ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1535ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1536ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
1537ab4382d2SGreg Kroah-Hartman }
1538ab4382d2SGreg Kroah-Hartman 
1539ab4382d2SGreg Kroah-Hartman /*
1540ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
1541ab4382d2SGreg Kroah-Hartman  */
1542ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
1543ab4382d2SGreg Kroah-Hartman {
1544ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
1545ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1546ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
1547ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
1548ab4382d2SGreg Kroah-Hartman 
1549ab4382d2SGreg Kroah-Hartman 	/*
1550ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
1551ab4382d2SGreg Kroah-Hartman 	 */
1552ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
1553ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
1554ab4382d2SGreg Kroah-Hartman 
1555ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
1556ab4382d2SGreg Kroah-Hartman 	pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1557ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1558ab4382d2SGreg Kroah-Hartman 
1559ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
1560ab4382d2SGreg Kroah-Hartman 
1561ab4382d2SGreg Kroah-Hartman 	/*
1562ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
1563ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
1564ab4382d2SGreg Kroah-Hartman 	 */
1565ab4382d2SGreg Kroah-Hartman 	do {
1566ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
1567ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
1568ab4382d2SGreg Kroah-Hartman 
1569ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
1570ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
1571ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1572ab4382d2SGreg Kroah-Hartman 
1573ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
1574ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
1575ab4382d2SGreg Kroah-Hartman }
1576ab4382d2SGreg Kroah-Hartman 
1577ab4382d2SGreg Kroah-Hartman /*
1578ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
1579ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
1580ab4382d2SGreg Kroah-Hartman  */
1581ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1582ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
1583ab4382d2SGreg Kroah-Hartman {
1584ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
1585ab4382d2SGreg Kroah-Hartman 
1586ab4382d2SGreg Kroah-Hartman 	/*
1587ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
1588ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
1589ab4382d2SGreg Kroah-Hartman 	 */
1590ab4382d2SGreg Kroah-Hartman 	quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1591ab4382d2SGreg Kroah-Hartman 	if (!quot)
1592ab4382d2SGreg Kroah-Hartman 		return;
1593ab4382d2SGreg Kroah-Hartman 
1594ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1595ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
1596ab4382d2SGreg Kroah-Hartman 		*bits = 8;
1597ab4382d2SGreg Kroah-Hartman 	else
1598ab4382d2SGreg Kroah-Hartman 		*bits = 7;
1599ab4382d2SGreg Kroah-Hartman 
1600ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_PAR;
1601ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
1602ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
1603ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
1604ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
1605ab4382d2SGreg Kroah-Hartman 
1606ab4382d2SGreg Kroah-Hartman 	/*
1607ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
1608ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
1609ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
1610ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
1611ab4382d2SGreg Kroah-Hartman 	 */
1612ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
1613ab4382d2SGreg Kroah-Hartman }
1614ab4382d2SGreg Kroah-Hartman 
1615ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
1616ab4382d2SGreg Kroah-Hartman {
1617*91f8c2d8SBoris BREZILLON 	int ret;
1618ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
1619ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
1620ab4382d2SGreg Kroah-Hartman 	int bits = 8;
1621ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
1622ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
1623ab4382d2SGreg Kroah-Hartman 
1624ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
1625ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
1626ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
1627ab4382d2SGreg Kroah-Hartman 	}
1628ab4382d2SGreg Kroah-Hartman 
1629*91f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
1630*91f8c2d8SBoris BREZILLON 	if (ret)
1631*91f8c2d8SBoris BREZILLON 		return ret;
1632ab4382d2SGreg Kroah-Hartman 
1633ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1634ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1635ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1636ab4382d2SGreg Kroah-Hartman 
1637ab4382d2SGreg Kroah-Hartman 	if (options)
1638ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1639ab4382d2SGreg Kroah-Hartman 	else
1640ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
1641ab4382d2SGreg Kroah-Hartman 
1642ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
1643ab4382d2SGreg Kroah-Hartman }
1644ab4382d2SGreg Kroah-Hartman 
1645ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
1646ab4382d2SGreg Kroah-Hartman 
1647ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
1648ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
1649ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
1650ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
1651ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
1652ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
1653ab4382d2SGreg Kroah-Hartman 	.index		= -1,
1654ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
1655ab4382d2SGreg Kroah-Hartman };
1656ab4382d2SGreg Kroah-Hartman 
1657ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
1658ab4382d2SGreg Kroah-Hartman 
1659ab4382d2SGreg Kroah-Hartman /*
1660ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
1661ab4382d2SGreg Kroah-Hartman  */
1662ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
1663ab4382d2SGreg Kroah-Hartman {
1664*91f8c2d8SBoris BREZILLON 	int ret;
1665ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
16660d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
16670d0a3cc1SVoss, Nikolaus 			atmel_default_console_device->dev.platform_data;
1668efb8d21bSLinus Torvalds 		int id = pdata->num;
16694cbf9f48SNicolas Ferre 		struct atmel_uart_port *port = &atmel_ports[id];
16700d0a3cc1SVoss, Nikolaus 
16714cbf9f48SNicolas Ferre 		port->backup_imr = 0;
16724cbf9f48SNicolas Ferre 		port->uart.line = id;
16734cbf9f48SNicolas Ferre 
16744cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
1675*91f8c2d8SBoris BREZILLON 		ret = atmel_init_port(port, atmel_default_console_device);
1676*91f8c2d8SBoris BREZILLON 		if (ret)
1677*91f8c2d8SBoris BREZILLON 			return ret;
1678ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
1679ab4382d2SGreg Kroah-Hartman 	}
1680ab4382d2SGreg Kroah-Hartman 
1681ab4382d2SGreg Kroah-Hartman 	return 0;
1682ab4382d2SGreg Kroah-Hartman }
1683ab4382d2SGreg Kroah-Hartman 
1684ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
1685ab4382d2SGreg Kroah-Hartman 
1686ab4382d2SGreg Kroah-Hartman /*
1687ab4382d2SGreg Kroah-Hartman  * Late console initialization.
1688ab4382d2SGreg Kroah-Hartman  */
1689ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
1690ab4382d2SGreg Kroah-Hartman {
1691ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
1692ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
1693ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
1694ab4382d2SGreg Kroah-Hartman 
1695ab4382d2SGreg Kroah-Hartman 	return 0;
1696ab4382d2SGreg Kroah-Hartman }
1697ab4382d2SGreg Kroah-Hartman 
1698ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
1699ab4382d2SGreg Kroah-Hartman 
1700ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
1701ab4382d2SGreg Kroah-Hartman {
1702ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
1703ab4382d2SGreg Kroah-Hartman }
1704ab4382d2SGreg Kroah-Hartman 
1705ab4382d2SGreg Kroah-Hartman #else
1706ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
1707ab4382d2SGreg Kroah-Hartman 
1708ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
1709ab4382d2SGreg Kroah-Hartman {
1710ab4382d2SGreg Kroah-Hartman 	return false;
1711ab4382d2SGreg Kroah-Hartman }
1712ab4382d2SGreg Kroah-Hartman #endif
1713ab4382d2SGreg Kroah-Hartman 
1714ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
1715ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
1716ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
1717ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
1718ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
1719ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
1720ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
1721ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
1722ab4382d2SGreg Kroah-Hartman };
1723ab4382d2SGreg Kroah-Hartman 
1724ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
1725ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
1726ab4382d2SGreg Kroah-Hartman {
1727ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
1728ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
1729ab4382d2SGreg Kroah-Hartman #else
1730ab4382d2SGreg Kroah-Hartman 	return false;
1731ab4382d2SGreg Kroah-Hartman #endif
1732ab4382d2SGreg Kroah-Hartman }
1733ab4382d2SGreg Kroah-Hartman 
1734ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
1735ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
1736ab4382d2SGreg Kroah-Hartman {
1737ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
1738ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1739ab4382d2SGreg Kroah-Hartman 
1740ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
1741ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
1742ab4382d2SGreg Kroah-Hartman 		while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1743ab4382d2SGreg Kroah-Hartman 			cpu_relax();
1744ab4382d2SGreg Kroah-Hartman 	}
1745ab4382d2SGreg Kroah-Hartman 
1746ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
1747ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1748ab4382d2SGreg Kroah-Hartman 	if (atmel_serial_clk_will_stop())
1749ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
1750ab4382d2SGreg Kroah-Hartman 
1751ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
1752ab4382d2SGreg Kroah-Hartman 
1753ab4382d2SGreg Kroah-Hartman 	return 0;
1754ab4382d2SGreg Kroah-Hartman }
1755ab4382d2SGreg Kroah-Hartman 
1756ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
1757ab4382d2SGreg Kroah-Hartman {
1758ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
1759ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1760ab4382d2SGreg Kroah-Hartman 
1761ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
1762ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1763ab4382d2SGreg Kroah-Hartman 
1764ab4382d2SGreg Kroah-Hartman 	return 0;
1765ab4382d2SGreg Kroah-Hartman }
1766ab4382d2SGreg Kroah-Hartman #else
1767ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
1768ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
1769ab4382d2SGreg Kroah-Hartman #endif
1770ab4382d2SGreg Kroah-Hartman 
17719671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
1772ab4382d2SGreg Kroah-Hartman {
1773ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
17745fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
1775deba1a0dSJean-Christophe PLAGNIOL-VILLARD 	struct atmel_uart_data *pdata = pdev->dev.platform_data;
1776ab4382d2SGreg Kroah-Hartman 	void *data;
17774cbf9f48SNicolas Ferre 	int ret = -ENODEV;
1778784557deSJean-Christophe PLAGNIOL-VILLARD 	struct pinctrl *pinctrl;
1779ab4382d2SGreg Kroah-Hartman 
1780ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1781ab4382d2SGreg Kroah-Hartman 
17825fbe46b6SNicolas Ferre 	if (np)
17835fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
17845fbe46b6SNicolas Ferre 	else
17854cbf9f48SNicolas Ferre 		if (pdata)
17864cbf9f48SNicolas Ferre 			ret = pdata->num;
17874cbf9f48SNicolas Ferre 
17884cbf9f48SNicolas Ferre 	if (ret < 0)
17895fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
17904cbf9f48SNicolas Ferre 		 * auto-enumerate it */
1791503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
17924cbf9f48SNicolas Ferre 
1793503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
17944cbf9f48SNicolas Ferre 		ret = -ENODEV;
17954cbf9f48SNicolas Ferre 		goto err;
17964cbf9f48SNicolas Ferre 	}
17974cbf9f48SNicolas Ferre 
1798503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
17994cbf9f48SNicolas Ferre 		/* port already in use */
18004cbf9f48SNicolas Ferre 		ret = -EBUSY;
18014cbf9f48SNicolas Ferre 		goto err;
18024cbf9f48SNicolas Ferre 	}
18034cbf9f48SNicolas Ferre 
18044cbf9f48SNicolas Ferre 	port = &atmel_ports[ret];
1805ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
18064cbf9f48SNicolas Ferre 	port->uart.line = ret;
1807ab4382d2SGreg Kroah-Hartman 
1808*91f8c2d8SBoris BREZILLON 	ret = atmel_init_port(port, pdev);
1809*91f8c2d8SBoris BREZILLON 	if (ret)
1810*91f8c2d8SBoris BREZILLON 		goto err;
1811ab4382d2SGreg Kroah-Hartman 
1812784557deSJean-Christophe PLAGNIOL-VILLARD 	pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1813784557deSJean-Christophe PLAGNIOL-VILLARD 	if (IS_ERR(pinctrl)) {
1814784557deSJean-Christophe PLAGNIOL-VILLARD 		ret = PTR_ERR(pinctrl);
1815784557deSJean-Christophe PLAGNIOL-VILLARD 		goto err;
1816784557deSJean-Christophe PLAGNIOL-VILLARD 	}
1817784557deSJean-Christophe PLAGNIOL-VILLARD 
1818ab4382d2SGreg Kroah-Hartman 	if (!atmel_use_dma_rx(&port->uart)) {
1819ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
1820ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
1821ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1822ab4382d2SGreg Kroah-Hartman 		if (!data)
1823ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
1824ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
1825ab4382d2SGreg Kroah-Hartman 	}
1826ab4382d2SGreg Kroah-Hartman 
1827ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
1828ab4382d2SGreg Kroah-Hartman 	if (ret)
1829ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
1830ab4382d2SGreg Kroah-Hartman 
1831ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1832ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
1833ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1834ab4382d2SGreg Kroah-Hartman 		/*
1835ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
1836*91f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
1837ab4382d2SGreg Kroah-Hartman 		 */
1838*91f8c2d8SBoris BREZILLON 		clk_disable_unprepare(port->clk);
1839ab4382d2SGreg Kroah-Hartman 	}
1840ab4382d2SGreg Kroah-Hartman #endif
1841ab4382d2SGreg Kroah-Hartman 
1842ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
1843ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
1844ab4382d2SGreg Kroah-Hartman 
1845fc887b15SLinus Torvalds 	if (port->rs485.flags & SER_RS485_ENABLED) {
1846fc887b15SLinus Torvalds 		UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
1847fc887b15SLinus Torvalds 		UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1848fc887b15SLinus Torvalds 	}
1849fc887b15SLinus Torvalds 
1850ab4382d2SGreg Kroah-Hartman 	return 0;
1851ab4382d2SGreg Kroah-Hartman 
1852ab4382d2SGreg Kroah-Hartman err_add_port:
1853ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
1854ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
1855ab4382d2SGreg Kroah-Hartman err_alloc_ring:
1856ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
1857ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
1858ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
1859ab4382d2SGreg Kroah-Hartman 	}
18604cbf9f48SNicolas Ferre err:
1861ab4382d2SGreg Kroah-Hartman 	return ret;
1862ab4382d2SGreg Kroah-Hartman }
1863ab4382d2SGreg Kroah-Hartman 
1864ae8d8a14SBill Pemberton static int atmel_serial_remove(struct platform_device *pdev)
1865ab4382d2SGreg Kroah-Hartman {
1866ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
1867ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1868ab4382d2SGreg Kroah-Hartman 	int ret = 0;
1869ab4382d2SGreg Kroah-Hartman 
1870ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
1871ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, NULL);
1872ab4382d2SGreg Kroah-Hartman 
1873ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
1874ab4382d2SGreg Kroah-Hartman 
1875ab4382d2SGreg Kroah-Hartman 	tasklet_kill(&atmel_port->tasklet);
1876ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
1877ab4382d2SGreg Kroah-Hartman 
1878ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
1879ab4382d2SGreg Kroah-Hartman 
1880503bded9SPawel Wieczorkiewicz 	clear_bit(port->line, atmel_ports_in_use);
18814cbf9f48SNicolas Ferre 
1882ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
1883ab4382d2SGreg Kroah-Hartman 
1884ab4382d2SGreg Kroah-Hartman 	return ret;
1885ab4382d2SGreg Kroah-Hartman }
1886ab4382d2SGreg Kroah-Hartman 
1887ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
1888ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
18892d47b716SBill Pemberton 	.remove		= atmel_serial_remove,
1890ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
1891ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
1892ab4382d2SGreg Kroah-Hartman 	.driver		= {
1893ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
1894ab4382d2SGreg Kroah-Hartman 		.owner	= THIS_MODULE,
18955fbe46b6SNicolas Ferre 		.of_match_table	= of_match_ptr(atmel_serial_dt_ids),
1896ab4382d2SGreg Kroah-Hartman 	},
1897ab4382d2SGreg Kroah-Hartman };
1898ab4382d2SGreg Kroah-Hartman 
1899ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
1900ab4382d2SGreg Kroah-Hartman {
1901ab4382d2SGreg Kroah-Hartman 	int ret;
1902ab4382d2SGreg Kroah-Hartman 
1903ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
1904ab4382d2SGreg Kroah-Hartman 	if (ret)
1905ab4382d2SGreg Kroah-Hartman 		return ret;
1906ab4382d2SGreg Kroah-Hartman 
1907ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
1908ab4382d2SGreg Kroah-Hartman 	if (ret)
1909ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
1910ab4382d2SGreg Kroah-Hartman 
1911ab4382d2SGreg Kroah-Hartman 	return ret;
1912ab4382d2SGreg Kroah-Hartman }
1913ab4382d2SGreg Kroah-Hartman 
1914ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
1915ab4382d2SGreg Kroah-Hartman {
1916ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
1917ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
1918ab4382d2SGreg Kroah-Hartman }
1919ab4382d2SGreg Kroah-Hartman 
1920ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
1921ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
1922ab4382d2SGreg Kroah-Hartman 
1923ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
1924ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1925ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
1926ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
1927