xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 42bd7a4f68e7785dce656a379c3de0a74f5a4d84)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  linux/drivers/char/atmel_serial.c
3ab4382d2SGreg Kroah-Hartman  *
4ab4382d2SGreg Kroah-Hartman  *  Driver for Atmel AT91 / AT32 Serial ports
5ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
6ab4382d2SGreg Kroah-Hartman  *
7ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
8ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
11ab4382d2SGreg Kroah-Hartman  *
12ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
13ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
14ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
15ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
16ab4382d2SGreg Kroah-Hartman  *
17ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
18ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
21ab4382d2SGreg Kroah-Hartman  *
22ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
23ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
24ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25ab4382d2SGreg Kroah-Hartman  *
26ab4382d2SGreg Kroah-Hartman  */
27ab4382d2SGreg Kroah-Hartman #include <linux/module.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
35ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
36ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
37ab4382d2SGreg Kroah-Hartman #include <linux/platform_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>
42ab4382d2SGreg Kroah-Hartman 
43ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
44ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
45ab4382d2SGreg Kroah-Hartman 
46ab4382d2SGreg Kroah-Hartman #include <asm/mach/serial_at91.h>
47ab4382d2SGreg Kroah-Hartman #include <mach/board.h>
48ab4382d2SGreg Kroah-Hartman 
49ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARM
50ab4382d2SGreg Kroah-Hartman #include <mach/cpu.h>
51ab4382d2SGreg Kroah-Hartman #include <mach/gpio.h>
52ab4382d2SGreg Kroah-Hartman #endif
53ab4382d2SGreg Kroah-Hartman 
54ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
55ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
56ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
57ab4382d2SGreg Kroah-Hartman 
58ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
59ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
60ab4382d2SGreg Kroah-Hartman #endif
61ab4382d2SGreg Kroah-Hartman 
62ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
63ab4382d2SGreg Kroah-Hartman 
64ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
65ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
66ab4382d2SGreg Kroah-Hartman 
67ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
68ab4382d2SGreg Kroah-Hartman 
69ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
70ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
71ab4382d2SGreg Kroah-Hartman  * UART. */
72ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
73ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
74ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
75ab4382d2SGreg Kroah-Hartman 
76ab4382d2SGreg Kroah-Hartman #else
77ab4382d2SGreg Kroah-Hartman 
78ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
79ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
80ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
81ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
82ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
83ab4382d2SGreg Kroah-Hartman 
84ab4382d2SGreg Kroah-Hartman #endif
85ab4382d2SGreg Kroah-Hartman 
86ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
87ab4382d2SGreg Kroah-Hartman 
88ab4382d2SGreg Kroah-Hartman /* UART registers. CR is write-only, hence no GET macro */
89ab4382d2SGreg Kroah-Hartman #define UART_PUT_CR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_CR)
90ab4382d2SGreg Kroah-Hartman #define UART_GET_MR(port)	__raw_readl((port)->membase + ATMEL_US_MR)
91ab4382d2SGreg Kroah-Hartman #define UART_PUT_MR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_MR)
92ab4382d2SGreg Kroah-Hartman #define UART_PUT_IER(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IER)
93ab4382d2SGreg Kroah-Hartman #define UART_PUT_IDR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_IDR)
94ab4382d2SGreg Kroah-Hartman #define UART_GET_IMR(port)	__raw_readl((port)->membase + ATMEL_US_IMR)
95ab4382d2SGreg Kroah-Hartman #define UART_GET_CSR(port)	__raw_readl((port)->membase + ATMEL_US_CSR)
96ab4382d2SGreg Kroah-Hartman #define UART_GET_CHAR(port)	__raw_readl((port)->membase + ATMEL_US_RHR)
97ab4382d2SGreg Kroah-Hartman #define UART_PUT_CHAR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_THR)
98ab4382d2SGreg Kroah-Hartman #define UART_GET_BRGR(port)	__raw_readl((port)->membase + ATMEL_US_BRGR)
99ab4382d2SGreg Kroah-Hartman #define UART_PUT_BRGR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_BRGR)
100ab4382d2SGreg Kroah-Hartman #define UART_PUT_RTOR(port,v)	__raw_writel(v, (port)->membase + ATMEL_US_RTOR)
101ab4382d2SGreg Kroah-Hartman #define UART_PUT_TTGR(port, v)	__raw_writel(v, (port)->membase + ATMEL_US_TTGR)
102ab4382d2SGreg Kroah-Hartman 
103ab4382d2SGreg Kroah-Hartman  /* PDC registers */
104ab4382d2SGreg Kroah-Hartman #define UART_PUT_PTCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
105ab4382d2SGreg Kroah-Hartman #define UART_GET_PTSR(port)	__raw_readl((port)->membase + ATMEL_PDC_PTSR)
106ab4382d2SGreg Kroah-Hartman 
107ab4382d2SGreg Kroah-Hartman #define UART_PUT_RPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
108ab4382d2SGreg Kroah-Hartman #define UART_GET_RPR(port)	__raw_readl((port)->membase + ATMEL_PDC_RPR)
109ab4382d2SGreg Kroah-Hartman #define UART_PUT_RCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
110ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
111ab4382d2SGreg Kroah-Hartman #define UART_PUT_RNCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
112ab4382d2SGreg Kroah-Hartman 
113ab4382d2SGreg Kroah-Hartman #define UART_PUT_TPR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
114ab4382d2SGreg Kroah-Hartman #define UART_PUT_TCR(port,v)	__raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
115ab4382d2SGreg Kroah-Hartman #define UART_GET_TCR(port)	__raw_readl((port)->membase + ATMEL_PDC_TCR)
116ab4382d2SGreg Kroah-Hartman 
117ab4382d2SGreg Kroah-Hartman static int (*atmel_open_hook)(struct uart_port *);
118ab4382d2SGreg Kroah-Hartman static void (*atmel_close_hook)(struct uart_port *);
119ab4382d2SGreg Kroah-Hartman 
120ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
121ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
122ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
123ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
124ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
125ab4382d2SGreg Kroah-Hartman };
126ab4382d2SGreg Kroah-Hartman 
127ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
128ab4382d2SGreg Kroah-Hartman 	u16		status;
129ab4382d2SGreg Kroah-Hartman 	u16		ch;
130ab4382d2SGreg Kroah-Hartman };
131ab4382d2SGreg Kroah-Hartman 
132ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
133ab4382d2SGreg Kroah-Hartman 
134ab4382d2SGreg Kroah-Hartman /*
135ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
136ab4382d2SGreg Kroah-Hartman  */
137ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
138ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
139ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
140ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
141ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
142ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
143ab4382d2SGreg Kroah-Hartman 
144ab4382d2SGreg Kroah-Hartman 	short			use_dma_rx;	/* enable PDC receiver */
145ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
146ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
147ab4382d2SGreg Kroah-Hartman 
148ab4382d2SGreg Kroah-Hartman 	short			use_dma_tx;	/* enable PDC transmitter */
149ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
150ab4382d2SGreg Kroah-Hartman 
151ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
152ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
153ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
154ab4382d2SGreg Kroah-Hartman 
155ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
156ab4382d2SGreg Kroah-Hartman 
157ab4382d2SGreg Kroah-Hartman 	struct serial_rs485	rs485;		/* rs485 settings */
158ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
159ab4382d2SGreg Kroah-Hartman };
160ab4382d2SGreg Kroah-Hartman 
161ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[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 
167ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
168ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
169ab4382d2SGreg Kroah-Hartman {
170ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
171ab4382d2SGreg Kroah-Hartman }
172ab4382d2SGreg Kroah-Hartman 
173ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
174ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_rx(struct uart_port *port)
175ab4382d2SGreg Kroah-Hartman {
176ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
177ab4382d2SGreg Kroah-Hartman 
178ab4382d2SGreg Kroah-Hartman 	return atmel_port->use_dma_rx;
179ab4382d2SGreg Kroah-Hartman }
180ab4382d2SGreg Kroah-Hartman 
181ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_tx(struct uart_port *port)
182ab4382d2SGreg Kroah-Hartman {
183ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
184ab4382d2SGreg Kroah-Hartman 
185ab4382d2SGreg Kroah-Hartman 	return atmel_port->use_dma_tx;
186ab4382d2SGreg Kroah-Hartman }
187ab4382d2SGreg Kroah-Hartman #else
188ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_rx(struct uart_port *port)
189ab4382d2SGreg Kroah-Hartman {
190ab4382d2SGreg Kroah-Hartman 	return false;
191ab4382d2SGreg Kroah-Hartman }
192ab4382d2SGreg Kroah-Hartman 
193ab4382d2SGreg Kroah-Hartman static bool atmel_use_dma_tx(struct uart_port *port)
194ab4382d2SGreg Kroah-Hartman {
195ab4382d2SGreg Kroah-Hartman 	return false;
196ab4382d2SGreg Kroah-Hartman }
197ab4382d2SGreg Kroah-Hartman #endif
198ab4382d2SGreg Kroah-Hartman 
199ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
200ab4382d2SGreg Kroah-Hartman void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
201ab4382d2SGreg Kroah-Hartman {
202ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
203ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
204ab4382d2SGreg Kroah-Hartman 
205ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
206ab4382d2SGreg Kroah-Hartman 
207ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
208ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
209ab4382d2SGreg Kroah-Hartman 
210ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port);
211ab4382d2SGreg Kroah-Hartman 
212ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
213ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
214ab4382d2SGreg Kroah-Hartman 
215ab4382d2SGreg Kroah-Hartman 	atmel_port->rs485 = *rs485conf;
216ab4382d2SGreg Kroah-Hartman 
217ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
218ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
219ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
220ab4382d2SGreg Kroah-Hartman 		if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
221ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
222ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
223ab4382d2SGreg Kroah-Hartman 	} else {
224ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
225ab4382d2SGreg Kroah-Hartman 		if (atmel_use_dma_tx(port))
226ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
227ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
228ab4382d2SGreg Kroah-Hartman 		else
229ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
230ab4382d2SGreg Kroah-Hartman 	}
231ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
232ab4382d2SGreg Kroah-Hartman 
233ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
234ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
235ab4382d2SGreg Kroah-Hartman 
236ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
237ab4382d2SGreg Kroah-Hartman 
238ab4382d2SGreg Kroah-Hartman }
239ab4382d2SGreg Kroah-Hartman 
240ab4382d2SGreg Kroah-Hartman /*
241ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
242ab4382d2SGreg Kroah-Hartman  */
243ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
244ab4382d2SGreg Kroah-Hartman {
245ab4382d2SGreg Kroah-Hartman 	return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
246ab4382d2SGreg Kroah-Hartman }
247ab4382d2SGreg Kroah-Hartman 
248ab4382d2SGreg Kroah-Hartman /*
249ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
250ab4382d2SGreg Kroah-Hartman  */
251ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
252ab4382d2SGreg Kroah-Hartman {
253ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
254ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
255ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
256ab4382d2SGreg Kroah-Hartman 
257ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91RM9200
258ab4382d2SGreg Kroah-Hartman 	if (cpu_is_at91rm9200()) {
259ab4382d2SGreg Kroah-Hartman 		/*
260ab4382d2SGreg Kroah-Hartman 		 * AT91RM9200 Errata #39: RTS0 is not internally connected
261ab4382d2SGreg Kroah-Hartman 		 * to PA21. We need to drive the pin manually.
262ab4382d2SGreg Kroah-Hartman 		 */
263ab4382d2SGreg Kroah-Hartman 		if (port->mapbase == AT91RM9200_BASE_US0) {
264ab4382d2SGreg Kroah-Hartman 			if (mctrl & TIOCM_RTS)
265ab4382d2SGreg Kroah-Hartman 				at91_set_gpio_value(AT91_PIN_PA21, 0);
266ab4382d2SGreg Kroah-Hartman 			else
267ab4382d2SGreg Kroah-Hartman 				at91_set_gpio_value(AT91_PIN_PA21, 1);
268ab4382d2SGreg Kroah-Hartman 		}
269ab4382d2SGreg Kroah-Hartman 	}
270ab4382d2SGreg Kroah-Hartman #endif
271ab4382d2SGreg Kroah-Hartman 
272ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
273ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSEN;
274ab4382d2SGreg Kroah-Hartman 	else
275ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_RTSDIS;
276ab4382d2SGreg Kroah-Hartman 
277ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
278ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
279ab4382d2SGreg Kroah-Hartman 	else
280ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
281ab4382d2SGreg Kroah-Hartman 
282ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, control);
283ab4382d2SGreg Kroah-Hartman 
284ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
285ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
286ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
287ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
288ab4382d2SGreg Kroah-Hartman 	else
289ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
290ab4382d2SGreg Kroah-Hartman 
291ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
292ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
293ab4382d2SGreg Kroah-Hartman 
294ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
295ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
296ab4382d2SGreg Kroah-Hartman 		if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
297ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
298ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
299ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
300ab4382d2SGreg Kroah-Hartman 	} else {
301ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
302ab4382d2SGreg Kroah-Hartman 	}
303ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
304ab4382d2SGreg Kroah-Hartman }
305ab4382d2SGreg Kroah-Hartman 
306ab4382d2SGreg Kroah-Hartman /*
307ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
308ab4382d2SGreg Kroah-Hartman  */
309ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
310ab4382d2SGreg Kroah-Hartman {
311ab4382d2SGreg Kroah-Hartman 	unsigned int status, ret = 0;
312ab4382d2SGreg Kroah-Hartman 
313ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
314ab4382d2SGreg Kroah-Hartman 
315ab4382d2SGreg Kroah-Hartman 	/*
316ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
317ab4382d2SGreg Kroah-Hartman 	 */
318ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
319ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
320ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
321ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
322ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
323ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
324ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
325ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
326ab4382d2SGreg Kroah-Hartman 
327ab4382d2SGreg Kroah-Hartman 	return ret;
328ab4382d2SGreg Kroah-Hartman }
329ab4382d2SGreg Kroah-Hartman 
330ab4382d2SGreg Kroah-Hartman /*
331ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
332ab4382d2SGreg Kroah-Hartman  */
333ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
334ab4382d2SGreg Kroah-Hartman {
335ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
336ab4382d2SGreg Kroah-Hartman 
337ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
338ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
339ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
340ab4382d2SGreg Kroah-Hartman 	}
341ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
342ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, atmel_port->tx_done_mask);
343ab4382d2SGreg Kroah-Hartman 
344ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED)
345ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
346ab4382d2SGreg Kroah-Hartman }
347ab4382d2SGreg Kroah-Hartman 
348ab4382d2SGreg Kroah-Hartman /*
349ab4382d2SGreg Kroah-Hartman  * Start transmitting.
350ab4382d2SGreg Kroah-Hartman  */
351ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
352ab4382d2SGreg Kroah-Hartman {
353ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
354ab4382d2SGreg Kroah-Hartman 
355ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
356ab4382d2SGreg Kroah-Hartman 		if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
357ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
358ab4382d2SGreg Kroah-Hartman 			   really need this.*/
359ab4382d2SGreg Kroah-Hartman 			return;
360ab4382d2SGreg Kroah-Hartman 
361ab4382d2SGreg Kroah-Hartman 		if (atmel_port->rs485.flags & SER_RS485_ENABLED)
362ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
363ab4382d2SGreg Kroah-Hartman 
364ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
365ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
366ab4382d2SGreg Kroah-Hartman 	}
367ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
368ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, atmel_port->tx_done_mask);
369ab4382d2SGreg Kroah-Hartman }
370ab4382d2SGreg Kroah-Hartman 
371ab4382d2SGreg Kroah-Hartman /*
372ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
373ab4382d2SGreg Kroah-Hartman  */
374ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
375ab4382d2SGreg Kroah-Hartman {
376ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);  /* reset status and receiver */
377ab4382d2SGreg Kroah-Hartman 
378ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
379ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
380ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
381ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
382ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
383ab4382d2SGreg Kroah-Hartman 	} else {
384ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
385ab4382d2SGreg Kroah-Hartman 	}
386ab4382d2SGreg Kroah-Hartman }
387ab4382d2SGreg Kroah-Hartman 
388ab4382d2SGreg Kroah-Hartman /*
389ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
390ab4382d2SGreg Kroah-Hartman  */
391ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
392ab4382d2SGreg Kroah-Hartman {
393ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
394ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
395ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
396ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
397ab4382d2SGreg Kroah-Hartman 			port->read_status_mask);
398ab4382d2SGreg Kroah-Hartman 	} else {
399ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXRDY);
400ab4382d2SGreg Kroah-Hartman 	}
401ab4382d2SGreg Kroah-Hartman }
402ab4382d2SGreg Kroah-Hartman 
403ab4382d2SGreg Kroah-Hartman /*
404ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
405ab4382d2SGreg Kroah-Hartman  */
406ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
407ab4382d2SGreg Kroah-Hartman {
408ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
409ab4382d2SGreg Kroah-Hartman 			| ATMEL_US_DCDIC | ATMEL_US_CTSIC);
410ab4382d2SGreg Kroah-Hartman }
411ab4382d2SGreg Kroah-Hartman 
412ab4382d2SGreg Kroah-Hartman /*
413ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
414ab4382d2SGreg Kroah-Hartman  */
415ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
416ab4382d2SGreg Kroah-Hartman {
417ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
418ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTBRK);	/* start break */
419ab4382d2SGreg Kroah-Hartman 	else
420ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STPBRK);	/* stop break */
421ab4382d2SGreg Kroah-Hartman }
422ab4382d2SGreg Kroah-Hartman 
423ab4382d2SGreg Kroah-Hartman /*
424ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
425ab4382d2SGreg Kroah-Hartman  */
426ab4382d2SGreg Kroah-Hartman static void
427ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
428ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
429ab4382d2SGreg Kroah-Hartman {
430ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
431ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
432ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
433ab4382d2SGreg Kroah-Hartman 
434ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
435ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
436ab4382d2SGreg Kroah-Hartman 		return;
437ab4382d2SGreg Kroah-Hartman 
438ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
439ab4382d2SGreg Kroah-Hartman 	c->status	= status;
440ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
441ab4382d2SGreg Kroah-Hartman 
442ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
443ab4382d2SGreg Kroah-Hartman 	smp_wmb();
444ab4382d2SGreg Kroah-Hartman 
445ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
446ab4382d2SGreg Kroah-Hartman }
447ab4382d2SGreg Kroah-Hartman 
448ab4382d2SGreg Kroah-Hartman /*
449ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
450ab4382d2SGreg Kroah-Hartman  */
451ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
452ab4382d2SGreg Kroah-Hartman {
453ab4382d2SGreg Kroah-Hartman 	/* clear error */
454ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
455ab4382d2SGreg Kroah-Hartman 
456ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
457ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
458ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
459ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
460ab4382d2SGreg Kroah-Hartman 	}
461ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
462ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
463ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
464ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
465ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
466ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
467ab4382d2SGreg Kroah-Hartman }
468ab4382d2SGreg Kroah-Hartman 
469ab4382d2SGreg Kroah-Hartman /*
470ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
471ab4382d2SGreg Kroah-Hartman  */
472ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
473ab4382d2SGreg Kroah-Hartman {
474ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
475ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
476ab4382d2SGreg Kroah-Hartman 
477ab4382d2SGreg Kroah-Hartman 	status = UART_GET_CSR(port);
478ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
479ab4382d2SGreg Kroah-Hartman 		ch = UART_GET_CHAR(port);
480ab4382d2SGreg Kroah-Hartman 
481ab4382d2SGreg Kroah-Hartman 		/*
482ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
483ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
484ab4382d2SGreg Kroah-Hartman 		 */
485ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
486ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
487ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
488ab4382d2SGreg Kroah-Hartman 
489ab4382d2SGreg Kroah-Hartman 			/* clear error */
490ab4382d2SGreg Kroah-Hartman 			UART_PUT_CR(port, ATMEL_US_RSTSTA);
491ab4382d2SGreg Kroah-Hartman 
492ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
493ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
494ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
495ab4382d2SGreg Kroah-Hartman 				UART_PUT_IER(port, ATMEL_US_RXBRK);
496ab4382d2SGreg Kroah-Hartman 			} else {
497ab4382d2SGreg Kroah-Hartman 				/*
498ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
499ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
500ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
501ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
502ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
503ab4382d2SGreg Kroah-Hartman 				 */
504ab4382d2SGreg Kroah-Hartman 				UART_PUT_IDR(port, ATMEL_US_RXBRK);
505ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
506ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
507ab4382d2SGreg Kroah-Hartman 			}
508ab4382d2SGreg Kroah-Hartman 		}
509ab4382d2SGreg Kroah-Hartman 
510ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
511ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
512ab4382d2SGreg Kroah-Hartman 	}
513ab4382d2SGreg Kroah-Hartman 
514ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
515ab4382d2SGreg Kroah-Hartman }
516ab4382d2SGreg Kroah-Hartman 
517ab4382d2SGreg Kroah-Hartman /*
518ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
519ab4382d2SGreg Kroah-Hartman  * disabled)
520ab4382d2SGreg Kroah-Hartman  */
521ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
522ab4382d2SGreg Kroah-Hartman {
523ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
524ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
525ab4382d2SGreg Kroah-Hartman 
526ab4382d2SGreg Kroah-Hartman 	if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
527ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, port->x_char);
528ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
529ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
530ab4382d2SGreg Kroah-Hartman 	}
531ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
532ab4382d2SGreg Kroah-Hartman 		return;
533ab4382d2SGreg Kroah-Hartman 
534ab4382d2SGreg Kroah-Hartman 	while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
535ab4382d2SGreg Kroah-Hartman 		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
536ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
537ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
538ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
539ab4382d2SGreg Kroah-Hartman 			break;
540ab4382d2SGreg Kroah-Hartman 	}
541ab4382d2SGreg Kroah-Hartman 
542ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
543ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
544ab4382d2SGreg Kroah-Hartman 
545ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
546ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
547ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
548ab4382d2SGreg Kroah-Hartman }
549ab4382d2SGreg Kroah-Hartman 
550ab4382d2SGreg Kroah-Hartman /*
551ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
552ab4382d2SGreg Kroah-Hartman  */
553ab4382d2SGreg Kroah-Hartman static void
554ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
555ab4382d2SGreg Kroah-Hartman {
556ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
557ab4382d2SGreg Kroah-Hartman 
558ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
559ab4382d2SGreg Kroah-Hartman 		/*
560ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
561ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
562ab4382d2SGreg Kroah-Hartman 		 *
563ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
564ab4382d2SGreg Kroah-Hartman 		 * the moment.
565ab4382d2SGreg Kroah-Hartman 		 */
566ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
567ab4382d2SGreg Kroah-Hartman 			UART_PUT_IDR(port, (ATMEL_US_ENDRX
568ab4382d2SGreg Kroah-Hartman 						| ATMEL_US_TIMEOUT));
569ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
570ab4382d2SGreg Kroah-Hartman 		}
571ab4382d2SGreg Kroah-Hartman 
572ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
573ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
574ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
575ab4382d2SGreg Kroah-Hartman 	}
576ab4382d2SGreg Kroah-Hartman 
577ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
578ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
579ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
580ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
581ab4382d2SGreg Kroah-Hartman 		/*
582ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
583ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
584ab4382d2SGreg Kroah-Hartman 		 */
585ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_RSTSTA);
586ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, ATMEL_US_RXBRK);
587ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
588ab4382d2SGreg Kroah-Hartman 	}
589ab4382d2SGreg Kroah-Hartman }
590ab4382d2SGreg Kroah-Hartman 
591ab4382d2SGreg Kroah-Hartman /*
592ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
593ab4382d2SGreg Kroah-Hartman  */
594ab4382d2SGreg Kroah-Hartman static void
595ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
596ab4382d2SGreg Kroah-Hartman {
597ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
598ab4382d2SGreg Kroah-Hartman 
599ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
600ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
601ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, atmel_port->tx_done_mask);
602ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
603ab4382d2SGreg Kroah-Hartman 	}
604ab4382d2SGreg Kroah-Hartman }
605ab4382d2SGreg Kroah-Hartman 
606ab4382d2SGreg Kroah-Hartman /*
607ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
608ab4382d2SGreg Kroah-Hartman  */
609ab4382d2SGreg Kroah-Hartman static void
610ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
611ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
612ab4382d2SGreg Kroah-Hartman {
613ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
614ab4382d2SGreg Kroah-Hartman 
615ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
616ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
617ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
618ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
619ab4382d2SGreg Kroah-Hartman 	}
620ab4382d2SGreg Kroah-Hartman }
621ab4382d2SGreg Kroah-Hartman 
622ab4382d2SGreg Kroah-Hartman /*
623ab4382d2SGreg Kroah-Hartman  * Interrupt handler
624ab4382d2SGreg Kroah-Hartman  */
625ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
626ab4382d2SGreg Kroah-Hartman {
627ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
628ab4382d2SGreg Kroah-Hartman 	unsigned int status, pending, pass_counter = 0;
629ab4382d2SGreg Kroah-Hartman 
630ab4382d2SGreg Kroah-Hartman 	do {
631ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
632ab4382d2SGreg Kroah-Hartman 		pending = status & UART_GET_IMR(port);
633ab4382d2SGreg Kroah-Hartman 		if (!pending)
634ab4382d2SGreg Kroah-Hartman 			break;
635ab4382d2SGreg Kroah-Hartman 
636ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
637ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
638ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
639ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
640ab4382d2SGreg Kroah-Hartman 
641ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
642ab4382d2SGreg Kroah-Hartman }
643ab4382d2SGreg Kroah-Hartman 
644ab4382d2SGreg Kroah-Hartman /*
645ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
646ab4382d2SGreg Kroah-Hartman  */
647ab4382d2SGreg Kroah-Hartman static void atmel_tx_dma(struct uart_port *port)
648ab4382d2SGreg Kroah-Hartman {
649ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
650ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
651ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
652ab4382d2SGreg Kroah-Hartman 	int count;
653ab4382d2SGreg Kroah-Hartman 
654ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
655ab4382d2SGreg Kroah-Hartman 	if (UART_GET_TCR(port))
656ab4382d2SGreg Kroah-Hartman 		return;
657ab4382d2SGreg Kroah-Hartman 
658ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
659ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
660ab4382d2SGreg Kroah-Hartman 
661ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
662ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
663ab4382d2SGreg Kroah-Hartman 
664ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
665ab4382d2SGreg Kroah-Hartman 
666ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
667ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
668ab4382d2SGreg Kroah-Hartman 
669ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
670ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
671ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
672ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
673ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
674ab4382d2SGreg Kroah-Hartman 
675ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
676ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
677ab4382d2SGreg Kroah-Hartman 
678ab4382d2SGreg Kroah-Hartman 		UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
679ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, count);
680ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
681ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
682ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
683ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->tx_done_mask);
684ab4382d2SGreg Kroah-Hartman 	} else {
685ab4382d2SGreg Kroah-Hartman 		if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
686ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
687ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
688ab4382d2SGreg Kroah-Hartman 		}
689ab4382d2SGreg Kroah-Hartman 	}
690ab4382d2SGreg Kroah-Hartman 
691ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
692ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
693ab4382d2SGreg Kroah-Hartman }
694ab4382d2SGreg Kroah-Hartman 
695ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
696ab4382d2SGreg Kroah-Hartman {
697ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
698ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
699ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
700ab4382d2SGreg Kroah-Hartman 	unsigned int status;
701ab4382d2SGreg Kroah-Hartman 
702ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
703ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
704ab4382d2SGreg Kroah-Hartman 
705ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
706ab4382d2SGreg Kroah-Hartman 		smp_rmb();
707ab4382d2SGreg Kroah-Hartman 
708ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
709ab4382d2SGreg Kroah-Hartman 
710ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
711ab4382d2SGreg Kroah-Hartman 
712ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
713ab4382d2SGreg Kroah-Hartman 		status = c.status;
714ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
715ab4382d2SGreg Kroah-Hartman 
716ab4382d2SGreg Kroah-Hartman 		/*
717ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
718ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
719ab4382d2SGreg Kroah-Hartman 		 */
720ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
721ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
722ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
723ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
724ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
725ab4382d2SGreg Kroah-Hartman 
726ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
727ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
728ab4382d2SGreg Kroah-Hartman 					continue;
729ab4382d2SGreg Kroah-Hartman 			}
730ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
731ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
732ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
733ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
734ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
735ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
736ab4382d2SGreg Kroah-Hartman 
737ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
738ab4382d2SGreg Kroah-Hartman 
739ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
740ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
741ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
742ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
743ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
744ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
745ab4382d2SGreg Kroah-Hartman 		}
746ab4382d2SGreg Kroah-Hartman 
747ab4382d2SGreg Kroah-Hartman 
748ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
749ab4382d2SGreg Kroah-Hartman 			continue;
750ab4382d2SGreg Kroah-Hartman 
751ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
752ab4382d2SGreg Kroah-Hartman 	}
753ab4382d2SGreg Kroah-Hartman 
754ab4382d2SGreg Kroah-Hartman 	/*
755ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
756ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
757ab4382d2SGreg Kroah-Hartman 	 */
758ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
759ab4382d2SGreg Kroah-Hartman 	tty_flip_buffer_push(port->state->port.tty);
760ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
761ab4382d2SGreg Kroah-Hartman }
762ab4382d2SGreg Kroah-Hartman 
763ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_dma(struct uart_port *port)
764ab4382d2SGreg Kroah-Hartman {
765ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
766ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
767ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
768ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
769ab4382d2SGreg Kroah-Hartman 	unsigned int head;
770ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
771ab4382d2SGreg Kroah-Hartman 	unsigned int count;
772ab4382d2SGreg Kroah-Hartman 
773ab4382d2SGreg Kroah-Hartman 	do {
774ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
775ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
776ab4382d2SGreg Kroah-Hartman 
777ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
778ab4382d2SGreg Kroah-Hartman 		head = UART_GET_RPR(port) - pdc->dma_addr;
779ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
780ab4382d2SGreg Kroah-Hartman 
781ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
782ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
783ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
784ab4382d2SGreg Kroah-Hartman 		 * find out.
785ab4382d2SGreg Kroah-Hartman 		 *
786ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
787ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
788ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
789ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
790ab4382d2SGreg Kroah-Hartman 		 */
791ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
792ab4382d2SGreg Kroah-Hartman 
793ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
794ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
795ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
796ab4382d2SGreg Kroah-Hartman 
797ab4382d2SGreg Kroah-Hartman 			/*
798ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
799ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
800ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
801ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
802ab4382d2SGreg Kroah-Hartman 			 */
803ab4382d2SGreg Kroah-Hartman 			count = head - tail;
804ab4382d2SGreg Kroah-Hartman 
805ab4382d2SGreg Kroah-Hartman 			tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
806ab4382d2SGreg Kroah-Hartman 
807ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
808ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
809ab4382d2SGreg Kroah-Hartman 
810ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
811ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
812ab4382d2SGreg Kroah-Hartman 		}
813ab4382d2SGreg Kroah-Hartman 
814ab4382d2SGreg Kroah-Hartman 		/*
815ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
816ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
817ab4382d2SGreg Kroah-Hartman 		 */
818ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
819ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
820ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNPR(port, pdc->dma_addr);
821ab4382d2SGreg Kroah-Hartman 			UART_PUT_RNCR(port, pdc->dma_size);
822ab4382d2SGreg Kroah-Hartman 
823ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
824ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
825ab4382d2SGreg Kroah-Hartman 		}
826ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
827ab4382d2SGreg Kroah-Hartman 
828ab4382d2SGreg Kroah-Hartman 	/*
829ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
830ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
831ab4382d2SGreg Kroah-Hartman 	 */
832ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
833ab4382d2SGreg Kroah-Hartman 	tty_flip_buffer_push(tty);
834ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
835ab4382d2SGreg Kroah-Hartman 
836ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
837ab4382d2SGreg Kroah-Hartman }
838ab4382d2SGreg Kroah-Hartman 
839ab4382d2SGreg Kroah-Hartman /*
840ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
841ab4382d2SGreg Kroah-Hartman  */
842ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
843ab4382d2SGreg Kroah-Hartman {
844ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
845ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
846ab4382d2SGreg Kroah-Hartman 	unsigned int status;
847ab4382d2SGreg Kroah-Hartman 	unsigned int status_change;
848ab4382d2SGreg Kroah-Hartman 
849ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
850ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
851ab4382d2SGreg Kroah-Hartman 
852ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port))
853ab4382d2SGreg Kroah-Hartman 		atmel_tx_dma(port);
854ab4382d2SGreg Kroah-Hartman 	else
855ab4382d2SGreg Kroah-Hartman 		atmel_tx_chars(port);
856ab4382d2SGreg Kroah-Hartman 
857ab4382d2SGreg Kroah-Hartman 	status = atmel_port->irq_status;
858ab4382d2SGreg Kroah-Hartman 	status_change = status ^ atmel_port->irq_status_prev;
859ab4382d2SGreg Kroah-Hartman 
860ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
861ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
862ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
863ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
864ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
865ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
866ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
867ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
868ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
869ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
870ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
871ab4382d2SGreg Kroah-Hartman 
872ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
873ab4382d2SGreg Kroah-Hartman 
874ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status_prev = status;
875ab4382d2SGreg Kroah-Hartman 	}
876ab4382d2SGreg Kroah-Hartman 
877ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port))
878ab4382d2SGreg Kroah-Hartman 		atmel_rx_from_dma(port);
879ab4382d2SGreg Kroah-Hartman 	else
880ab4382d2SGreg Kroah-Hartman 		atmel_rx_from_ring(port);
881ab4382d2SGreg Kroah-Hartman 
882ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
883ab4382d2SGreg Kroah-Hartman }
884ab4382d2SGreg Kroah-Hartman 
885ab4382d2SGreg Kroah-Hartman /*
886ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
887ab4382d2SGreg Kroah-Hartman  */
888ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
889ab4382d2SGreg Kroah-Hartman {
890ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
891ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
892ab4382d2SGreg Kroah-Hartman 	int retval;
893ab4382d2SGreg Kroah-Hartman 
894ab4382d2SGreg Kroah-Hartman 	/*
895ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
896ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
897ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
898ab4382d2SGreg Kroah-Hartman 	 */
899ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
900ab4382d2SGreg Kroah-Hartman 
901ab4382d2SGreg Kroah-Hartman 	/*
902ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
903ab4382d2SGreg Kroah-Hartman 	 */
904ab4382d2SGreg Kroah-Hartman 	retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
905ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
906ab4382d2SGreg Kroah-Hartman 	if (retval) {
907ab4382d2SGreg Kroah-Hartman 		printk("atmel_serial: atmel_startup - Can't get irq\n");
908ab4382d2SGreg Kroah-Hartman 		return retval;
909ab4382d2SGreg Kroah-Hartman 	}
910ab4382d2SGreg Kroah-Hartman 
911ab4382d2SGreg Kroah-Hartman 	/*
912ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
913ab4382d2SGreg Kroah-Hartman 	 */
914ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
915ab4382d2SGreg Kroah-Hartman 		int i;
916ab4382d2SGreg Kroah-Hartman 
917ab4382d2SGreg Kroah-Hartman 		for (i = 0; i < 2; i++) {
918ab4382d2SGreg Kroah-Hartman 			struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
919ab4382d2SGreg Kroah-Hartman 
920ab4382d2SGreg Kroah-Hartman 			pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
921ab4382d2SGreg Kroah-Hartman 			if (pdc->buf == NULL) {
922ab4382d2SGreg Kroah-Hartman 				if (i != 0) {
923ab4382d2SGreg Kroah-Hartman 					dma_unmap_single(port->dev,
924ab4382d2SGreg Kroah-Hartman 						atmel_port->pdc_rx[0].dma_addr,
925ab4382d2SGreg Kroah-Hartman 						PDC_BUFFER_SIZE,
926ab4382d2SGreg Kroah-Hartman 						DMA_FROM_DEVICE);
927ab4382d2SGreg Kroah-Hartman 					kfree(atmel_port->pdc_rx[0].buf);
928ab4382d2SGreg Kroah-Hartman 				}
929ab4382d2SGreg Kroah-Hartman 				free_irq(port->irq, port);
930ab4382d2SGreg Kroah-Hartman 				return -ENOMEM;
931ab4382d2SGreg Kroah-Hartman 			}
932ab4382d2SGreg Kroah-Hartman 			pdc->dma_addr = dma_map_single(port->dev,
933ab4382d2SGreg Kroah-Hartman 						       pdc->buf,
934ab4382d2SGreg Kroah-Hartman 						       PDC_BUFFER_SIZE,
935ab4382d2SGreg Kroah-Hartman 						       DMA_FROM_DEVICE);
936ab4382d2SGreg Kroah-Hartman 			pdc->dma_size = PDC_BUFFER_SIZE;
937ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
938ab4382d2SGreg Kroah-Hartman 		}
939ab4382d2SGreg Kroah-Hartman 
940ab4382d2SGreg Kroah-Hartman 		atmel_port->pdc_rx_idx = 0;
941ab4382d2SGreg Kroah-Hartman 
942ab4382d2SGreg Kroah-Hartman 		UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
943ab4382d2SGreg Kroah-Hartman 		UART_PUT_RCR(port, PDC_BUFFER_SIZE);
944ab4382d2SGreg Kroah-Hartman 
945ab4382d2SGreg Kroah-Hartman 		UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
946ab4382d2SGreg Kroah-Hartman 		UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
947ab4382d2SGreg Kroah-Hartman 	}
948ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
949ab4382d2SGreg Kroah-Hartman 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
950ab4382d2SGreg Kroah-Hartman 		struct circ_buf *xmit = &port->state->xmit;
951ab4382d2SGreg Kroah-Hartman 
952ab4382d2SGreg Kroah-Hartman 		pdc->buf = xmit->buf;
953ab4382d2SGreg Kroah-Hartman 		pdc->dma_addr = dma_map_single(port->dev,
954ab4382d2SGreg Kroah-Hartman 					       pdc->buf,
955ab4382d2SGreg Kroah-Hartman 					       UART_XMIT_SIZE,
956ab4382d2SGreg Kroah-Hartman 					       DMA_TO_DEVICE);
957ab4382d2SGreg Kroah-Hartman 		pdc->dma_size = UART_XMIT_SIZE;
958ab4382d2SGreg Kroah-Hartman 		pdc->ofs = 0;
959ab4382d2SGreg Kroah-Hartman 	}
960ab4382d2SGreg Kroah-Hartman 
961ab4382d2SGreg Kroah-Hartman 	/*
962ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "open" function (to register
963ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
964ab4382d2SGreg Kroah-Hartman 	 */
965ab4382d2SGreg Kroah-Hartman 	if (atmel_open_hook) {
966ab4382d2SGreg Kroah-Hartman 		retval = atmel_open_hook(port);
967ab4382d2SGreg Kroah-Hartman 		if (retval) {
968ab4382d2SGreg Kroah-Hartman 			free_irq(port->irq, port);
969ab4382d2SGreg Kroah-Hartman 			return retval;
970ab4382d2SGreg Kroah-Hartman 		}
971ab4382d2SGreg Kroah-Hartman 	}
972ab4382d2SGreg Kroah-Hartman 
973ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
974ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status_prev = UART_GET_CSR(port);
975ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
976ab4382d2SGreg Kroah-Hartman 
977ab4382d2SGreg Kroah-Hartman 	/*
978ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
979ab4382d2SGreg Kroah-Hartman 	 */
980ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
981ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
982ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
983ab4382d2SGreg Kroah-Hartman 
984ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
985ab4382d2SGreg Kroah-Hartman 		/* set UART timeout */
986ab4382d2SGreg Kroah-Hartman 		UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
987ab4382d2SGreg Kroah-Hartman 		UART_PUT_CR(port, ATMEL_US_STTTO);
988ab4382d2SGreg Kroah-Hartman 
989ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
990ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
991ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
992ab4382d2SGreg Kroah-Hartman 	} else {
993ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
994ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, ATMEL_US_RXRDY);
995ab4382d2SGreg Kroah-Hartman 	}
996ab4382d2SGreg Kroah-Hartman 
997ab4382d2SGreg Kroah-Hartman 	return 0;
998ab4382d2SGreg Kroah-Hartman }
999ab4382d2SGreg Kroah-Hartman 
1000ab4382d2SGreg Kroah-Hartman /*
1001ab4382d2SGreg Kroah-Hartman  * Disable the port
1002ab4382d2SGreg Kroah-Hartman  */
1003ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1004ab4382d2SGreg Kroah-Hartman {
1005ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1006ab4382d2SGreg Kroah-Hartman 	/*
1007ab4382d2SGreg Kroah-Hartman 	 * Ensure everything is stopped.
1008ab4382d2SGreg Kroah-Hartman 	 */
1009ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1010ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1011ab4382d2SGreg Kroah-Hartman 
1012ab4382d2SGreg Kroah-Hartman 	/*
1013ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1014ab4382d2SGreg Kroah-Hartman 	 */
1015ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port)) {
1016ab4382d2SGreg Kroah-Hartman 		int i;
1017ab4382d2SGreg Kroah-Hartman 
1018ab4382d2SGreg Kroah-Hartman 		for (i = 0; i < 2; i++) {
1019ab4382d2SGreg Kroah-Hartman 			struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1020ab4382d2SGreg Kroah-Hartman 
1021ab4382d2SGreg Kroah-Hartman 			dma_unmap_single(port->dev,
1022ab4382d2SGreg Kroah-Hartman 					 pdc->dma_addr,
1023ab4382d2SGreg Kroah-Hartman 					 pdc->dma_size,
1024ab4382d2SGreg Kroah-Hartman 					 DMA_FROM_DEVICE);
1025ab4382d2SGreg Kroah-Hartman 			kfree(pdc->buf);
1026ab4382d2SGreg Kroah-Hartman 		}
1027ab4382d2SGreg Kroah-Hartman 	}
1028ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
1029ab4382d2SGreg Kroah-Hartman 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1030ab4382d2SGreg Kroah-Hartman 
1031ab4382d2SGreg Kroah-Hartman 		dma_unmap_single(port->dev,
1032ab4382d2SGreg Kroah-Hartman 				 pdc->dma_addr,
1033ab4382d2SGreg Kroah-Hartman 				 pdc->dma_size,
1034ab4382d2SGreg Kroah-Hartman 				 DMA_TO_DEVICE);
1035ab4382d2SGreg Kroah-Hartman 	}
1036ab4382d2SGreg Kroah-Hartman 
1037ab4382d2SGreg Kroah-Hartman 	/*
1038ab4382d2SGreg Kroah-Hartman 	 * Disable all interrupts, port and break condition.
1039ab4382d2SGreg Kroah-Hartman 	 */
1040ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA);
1041ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1042ab4382d2SGreg Kroah-Hartman 
1043ab4382d2SGreg Kroah-Hartman 	/*
1044ab4382d2SGreg Kroah-Hartman 	 * Free the interrupt
1045ab4382d2SGreg Kroah-Hartman 	 */
1046ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1047ab4382d2SGreg Kroah-Hartman 
1048ab4382d2SGreg Kroah-Hartman 	/*
1049ab4382d2SGreg Kroah-Hartman 	 * If there is a specific "close" function (to unregister
1050ab4382d2SGreg Kroah-Hartman 	 * control line interrupts)
1051ab4382d2SGreg Kroah-Hartman 	 */
1052ab4382d2SGreg Kroah-Hartman 	if (atmel_close_hook)
1053ab4382d2SGreg Kroah-Hartman 		atmel_close_hook(port);
1054ab4382d2SGreg Kroah-Hartman }
1055ab4382d2SGreg Kroah-Hartman 
1056ab4382d2SGreg Kroah-Hartman /*
1057ab4382d2SGreg Kroah-Hartman  * Flush any TX data submitted for DMA. Called when the TX circular
1058ab4382d2SGreg Kroah-Hartman  * buffer is reset.
1059ab4382d2SGreg Kroah-Hartman  */
1060ab4382d2SGreg Kroah-Hartman static void atmel_flush_buffer(struct uart_port *port)
1061ab4382d2SGreg Kroah-Hartman {
1062ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1063ab4382d2SGreg Kroah-Hartman 
1064ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_tx(port)) {
1065ab4382d2SGreg Kroah-Hartman 		UART_PUT_TCR(port, 0);
1066ab4382d2SGreg Kroah-Hartman 		atmel_port->pdc_tx.ofs = 0;
1067ab4382d2SGreg Kroah-Hartman 	}
1068ab4382d2SGreg Kroah-Hartman }
1069ab4382d2SGreg Kroah-Hartman 
1070ab4382d2SGreg Kroah-Hartman /*
1071ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1072ab4382d2SGreg Kroah-Hartman  */
1073ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1074ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1075ab4382d2SGreg Kroah-Hartman {
1076ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1077ab4382d2SGreg Kroah-Hartman 
1078ab4382d2SGreg Kroah-Hartman 	switch (state) {
1079ab4382d2SGreg Kroah-Hartman 	case 0:
1080ab4382d2SGreg Kroah-Hartman 		/*
1081ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1082ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1083ab4382d2SGreg Kroah-Hartman 		 */
1084ab4382d2SGreg Kroah-Hartman 		clk_enable(atmel_port->clk);
1085ab4382d2SGreg Kroah-Hartman 
1086ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
1087ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, atmel_port->backup_imr);
1088ab4382d2SGreg Kroah-Hartman 		break;
1089ab4382d2SGreg Kroah-Hartman 	case 3:
1090ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
1091ab4382d2SGreg Kroah-Hartman 		atmel_port->backup_imr = UART_GET_IMR(port);
1092ab4382d2SGreg Kroah-Hartman 		UART_PUT_IDR(port, -1);
1093ab4382d2SGreg Kroah-Hartman 
1094ab4382d2SGreg Kroah-Hartman 		/*
1095ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1096ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1097ab4382d2SGreg Kroah-Hartman 		 */
1098ab4382d2SGreg Kroah-Hartman 		clk_disable(atmel_port->clk);
1099ab4382d2SGreg Kroah-Hartman 		break;
1100ab4382d2SGreg Kroah-Hartman 	default:
1101ab4382d2SGreg Kroah-Hartman 		printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1102ab4382d2SGreg Kroah-Hartman 	}
1103ab4382d2SGreg Kroah-Hartman }
1104ab4382d2SGreg Kroah-Hartman 
1105ab4382d2SGreg Kroah-Hartman /*
1106ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1107ab4382d2SGreg Kroah-Hartman  */
1108ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1109ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1110ab4382d2SGreg Kroah-Hartman {
1111ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
1112ab4382d2SGreg Kroah-Hartman 	unsigned int mode, imr, quot, baud;
1113ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1114ab4382d2SGreg Kroah-Hartman 
1115ab4382d2SGreg Kroah-Hartman 	/* Get current mode register */
1116ab4382d2SGreg Kroah-Hartman 	mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1117ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_NBSTOP | ATMEL_US_PAR
1118ab4382d2SGreg Kroah-Hartman 					| ATMEL_US_USMODE);
1119ab4382d2SGreg Kroah-Hartman 
1120ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1121ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
1122ab4382d2SGreg Kroah-Hartman 
1123ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
1124ab4382d2SGreg Kroah-Hartman 		quot /= 8;
1125ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
1126ab4382d2SGreg Kroah-Hartman 	}
1127ab4382d2SGreg Kroah-Hartman 
1128ab4382d2SGreg Kroah-Hartman 	/* byte size */
1129ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
1130ab4382d2SGreg Kroah-Hartman 	case CS5:
1131ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
1132ab4382d2SGreg Kroah-Hartman 		break;
1133ab4382d2SGreg Kroah-Hartman 	case CS6:
1134ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
1135ab4382d2SGreg Kroah-Hartman 		break;
1136ab4382d2SGreg Kroah-Hartman 	case CS7:
1137ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
1138ab4382d2SGreg Kroah-Hartman 		break;
1139ab4382d2SGreg Kroah-Hartman 	default:
1140ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
1141ab4382d2SGreg Kroah-Hartman 		break;
1142ab4382d2SGreg Kroah-Hartman 	}
1143ab4382d2SGreg Kroah-Hartman 
1144ab4382d2SGreg Kroah-Hartman 	/* stop bits */
1145ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
1146ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
1147ab4382d2SGreg Kroah-Hartman 
1148ab4382d2SGreg Kroah-Hartman 	/* parity */
1149ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
1150ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
1151ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
1152ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
1153ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
1154ab4382d2SGreg Kroah-Hartman 			else
1155ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
1156ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
1157ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
1158ab4382d2SGreg Kroah-Hartman 		else
1159ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
1160ab4382d2SGreg Kroah-Hartman 	} else
1161ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
1162ab4382d2SGreg Kroah-Hartman 
1163ab4382d2SGreg Kroah-Hartman 	/* hardware handshake (RTS/CTS) */
1164ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CRTSCTS)
1165ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_HWHS;
1166ab4382d2SGreg Kroah-Hartman 	else
1167ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_NORMAL;
1168ab4382d2SGreg Kroah-Hartman 
1169ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
1170ab4382d2SGreg Kroah-Hartman 
1171ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
1172ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
1173ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1174ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & (BRKINT | PARMRK))
1175ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
1176ab4382d2SGreg Kroah-Hartman 
1177ab4382d2SGreg Kroah-Hartman 	if (atmel_use_dma_rx(port))
1178ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
1179ab4382d2SGreg Kroah-Hartman 		UART_PUT_IER(port, port->read_status_mask);
1180ab4382d2SGreg Kroah-Hartman 
1181ab4382d2SGreg Kroah-Hartman 	/*
1182ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
1183ab4382d2SGreg Kroah-Hartman 	 */
1184ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
1185ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
1186ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1187ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
1188ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
1189ab4382d2SGreg Kroah-Hartman 		/*
1190ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
1191ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
1192ab4382d2SGreg Kroah-Hartman 		 */
1193ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
1194ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
1195ab4382d2SGreg Kroah-Hartman 	}
1196ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
1197ab4382d2SGreg Kroah-Hartman 
1198ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
1199ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
1200ab4382d2SGreg Kroah-Hartman 
1201ab4382d2SGreg Kroah-Hartman 	/*
1202ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
1203ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
1204ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
1205ab4382d2SGreg Kroah-Hartman 	 */
1206ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
1207ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1208ab4382d2SGreg Kroah-Hartman 
1209ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
1210ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1211ab4382d2SGreg Kroah-Hartman 
1212ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
1213ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
1214ab4382d2SGreg Kroah-Hartman 
1215ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1216ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
1217ab4382d2SGreg Kroah-Hartman 		if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1218ab4382d2SGreg Kroah-Hartman 			UART_PUT_TTGR(port,
1219ab4382d2SGreg Kroah-Hartman 					atmel_port->rs485.delay_rts_after_send);
1220ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
1221ab4382d2SGreg Kroah-Hartman 	} else {
1222ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
1223ab4382d2SGreg Kroah-Hartman 	}
1224ab4382d2SGreg Kroah-Hartman 
1225ab4382d2SGreg Kroah-Hartman 	/* set the parity, stop bits and data size */
1226ab4382d2SGreg Kroah-Hartman 	UART_PUT_MR(port, mode);
1227ab4382d2SGreg Kroah-Hartman 
1228ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
1229ab4382d2SGreg Kroah-Hartman 	UART_PUT_BRGR(port, quot);
1230ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1231ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1232ab4382d2SGreg Kroah-Hartman 
1233ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
1234ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
1235ab4382d2SGreg Kroah-Hartman 
1236ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
1237ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
1238ab4382d2SGreg Kroah-Hartman 		port->ops->enable_ms(port);
1239ab4382d2SGreg Kroah-Hartman 
1240ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
1241ab4382d2SGreg Kroah-Hartman }
1242ab4382d2SGreg Kroah-Hartman 
1243*42bd7a4fSViktar Palstsiuk static void atmel_set_ldisc(struct uart_port *port, int new)
1244*42bd7a4fSViktar Palstsiuk {
1245*42bd7a4fSViktar Palstsiuk 	int line = port->line;
1246*42bd7a4fSViktar Palstsiuk 
1247*42bd7a4fSViktar Palstsiuk 	if (line >= port->state->port.tty->driver->num)
1248*42bd7a4fSViktar Palstsiuk 		return;
1249*42bd7a4fSViktar Palstsiuk 
1250*42bd7a4fSViktar Palstsiuk 	if (port->state->port.tty->ldisc->ops->num == N_PPS) {
1251*42bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
1252*42bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
1253*42bd7a4fSViktar Palstsiuk 	} else {
1254*42bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
1255*42bd7a4fSViktar Palstsiuk 	}
1256*42bd7a4fSViktar Palstsiuk }
1257*42bd7a4fSViktar Palstsiuk 
1258ab4382d2SGreg Kroah-Hartman /*
1259ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
1260ab4382d2SGreg Kroah-Hartman  */
1261ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
1262ab4382d2SGreg Kroah-Hartman {
1263ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1264ab4382d2SGreg Kroah-Hartman }
1265ab4382d2SGreg Kroah-Hartman 
1266ab4382d2SGreg Kroah-Hartman /*
1267ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
1268ab4382d2SGreg Kroah-Hartman  */
1269ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
1270ab4382d2SGreg Kroah-Hartman {
1271ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1272ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1273ab4382d2SGreg Kroah-Hartman 
1274ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
1275ab4382d2SGreg Kroah-Hartman 
1276ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1277ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
1278ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
1279ab4382d2SGreg Kroah-Hartman 	}
1280ab4382d2SGreg Kroah-Hartman }
1281ab4382d2SGreg Kroah-Hartman 
1282ab4382d2SGreg Kroah-Hartman /*
1283ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
1284ab4382d2SGreg Kroah-Hartman  */
1285ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
1286ab4382d2SGreg Kroah-Hartman {
1287ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
1288ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1289ab4382d2SGreg Kroah-Hartman 
1290ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1291ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
1292ab4382d2SGreg Kroah-Hartman 
1293ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
1294ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
1295ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
1296ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
1297ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
1298ab4382d2SGreg Kroah-Hartman 		}
1299ab4382d2SGreg Kroah-Hartman 	}
1300ab4382d2SGreg Kroah-Hartman 
1301ab4382d2SGreg Kroah-Hartman 	return 0;
1302ab4382d2SGreg Kroah-Hartman }
1303ab4382d2SGreg Kroah-Hartman 
1304ab4382d2SGreg Kroah-Hartman /*
1305ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
1306ab4382d2SGreg Kroah-Hartman  */
1307ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
1308ab4382d2SGreg Kroah-Hartman {
1309ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
1310ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
1311ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
1312ab4382d2SGreg Kroah-Hartman 	}
1313ab4382d2SGreg Kroah-Hartman }
1314ab4382d2SGreg Kroah-Hartman 
1315ab4382d2SGreg Kroah-Hartman /*
1316ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
1317ab4382d2SGreg Kroah-Hartman  */
1318ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1319ab4382d2SGreg Kroah-Hartman {
1320ab4382d2SGreg Kroah-Hartman 	int ret = 0;
1321ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1322ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1323ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
1324ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1325ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
1326ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1327ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
1328ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1329ab4382d2SGreg Kroah-Hartman 	if ((void *)port->mapbase != ser->iomem_base)
1330ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1331ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
1332ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1333ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
1334ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
1335ab4382d2SGreg Kroah-Hartman 	return ret;
1336ab4382d2SGreg Kroah-Hartman }
1337ab4382d2SGreg Kroah-Hartman 
1338ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
1339ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
1340ab4382d2SGreg Kroah-Hartman {
1341ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1342ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1343ab4382d2SGreg Kroah-Hartman 
1344ab4382d2SGreg Kroah-Hartman 	return UART_GET_CHAR(port);
1345ab4382d2SGreg Kroah-Hartman }
1346ab4382d2SGreg Kroah-Hartman 
1347ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1348ab4382d2SGreg Kroah-Hartman {
1349ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1350ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1351ab4382d2SGreg Kroah-Hartman 
1352ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
1353ab4382d2SGreg Kroah-Hartman }
1354ab4382d2SGreg Kroah-Hartman #endif
1355ab4382d2SGreg Kroah-Hartman 
1356ab4382d2SGreg Kroah-Hartman static int
1357ab4382d2SGreg Kroah-Hartman atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1358ab4382d2SGreg Kroah-Hartman {
1359ab4382d2SGreg Kroah-Hartman 	struct serial_rs485 rs485conf;
1360ab4382d2SGreg Kroah-Hartman 
1361ab4382d2SGreg Kroah-Hartman 	switch (cmd) {
1362ab4382d2SGreg Kroah-Hartman 	case TIOCSRS485:
1363ab4382d2SGreg Kroah-Hartman 		if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1364ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
1365ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
1366ab4382d2SGreg Kroah-Hartman 
1367ab4382d2SGreg Kroah-Hartman 		atmel_config_rs485(port, &rs485conf);
1368ab4382d2SGreg Kroah-Hartman 		break;
1369ab4382d2SGreg Kroah-Hartman 
1370ab4382d2SGreg Kroah-Hartman 	case TIOCGRS485:
1371ab4382d2SGreg Kroah-Hartman 		if (copy_to_user((struct serial_rs485 *) arg,
1372ab4382d2SGreg Kroah-Hartman 					&(to_atmel_uart_port(port)->rs485),
1373ab4382d2SGreg Kroah-Hartman 					sizeof(rs485conf)))
1374ab4382d2SGreg Kroah-Hartman 			return -EFAULT;
1375ab4382d2SGreg Kroah-Hartman 		break;
1376ab4382d2SGreg Kroah-Hartman 
1377ab4382d2SGreg Kroah-Hartman 	default:
1378ab4382d2SGreg Kroah-Hartman 		return -ENOIOCTLCMD;
1379ab4382d2SGreg Kroah-Hartman 	}
1380ab4382d2SGreg Kroah-Hartman 	return 0;
1381ab4382d2SGreg Kroah-Hartman }
1382ab4382d2SGreg Kroah-Hartman 
1383ab4382d2SGreg Kroah-Hartman 
1384ab4382d2SGreg Kroah-Hartman 
1385ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
1386ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
1387ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
1388ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
1389ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
1390ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
1391ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
1392ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
1393ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
1394ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
1395ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
1396ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
1397ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
1398*42bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
1399ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
1400ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
1401ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
1402ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
1403ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
1404ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
1405ab4382d2SGreg Kroah-Hartman 	.ioctl		= atmel_ioctl,
1406ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
1407ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
1408ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
1409ab4382d2SGreg Kroah-Hartman #endif
1410ab4382d2SGreg Kroah-Hartman };
1411ab4382d2SGreg Kroah-Hartman 
1412ab4382d2SGreg Kroah-Hartman /*
1413ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
1414ab4382d2SGreg Kroah-Hartman  */
1415ab4382d2SGreg Kroah-Hartman static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1416ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
1417ab4382d2SGreg Kroah-Hartman {
1418ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
1419ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_data *data = pdev->dev.platform_data;
1420ab4382d2SGreg Kroah-Hartman 
1421ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
1422ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
1423ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
1424ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
1425ab4382d2SGreg Kroah-Hartman 	port->line		= pdev->id;
1426ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
1427ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
1428ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
1429ab4382d2SGreg Kroah-Hartman 
1430ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1431ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
1432ab4382d2SGreg Kroah-Hartman 
1433ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1434ab4382d2SGreg Kroah-Hartman 
1435ab4382d2SGreg Kroah-Hartman 	if (data->regs)
1436ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
1437ab4382d2SGreg Kroah-Hartman 		port->membase = data->regs;
1438ab4382d2SGreg Kroah-Hartman 	else {
1439ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
1440ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
1441ab4382d2SGreg Kroah-Hartman 	}
1442ab4382d2SGreg Kroah-Hartman 
1443ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
1444ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
1445ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
1446ab4382d2SGreg Kroah-Hartman 		clk_enable(atmel_port->clk);
1447ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
1448ab4382d2SGreg Kroah-Hartman 		clk_disable(atmel_port->clk);
1449ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
1450ab4382d2SGreg Kroah-Hartman 	}
1451ab4382d2SGreg Kroah-Hartman 
1452ab4382d2SGreg Kroah-Hartman 	atmel_port->use_dma_rx = data->use_dma_rx;
1453ab4382d2SGreg Kroah-Hartman 	atmel_port->use_dma_tx = data->use_dma_tx;
1454ab4382d2SGreg Kroah-Hartman 	atmel_port->rs485	= data->rs485;
1455ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1456ab4382d2SGreg Kroah-Hartman 	if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1457ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1458ab4382d2SGreg Kroah-Hartman 	else if (atmel_use_dma_tx(port)) {
1459ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
1460ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1461ab4382d2SGreg Kroah-Hartman 	} else {
1462ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
1463ab4382d2SGreg Kroah-Hartman 	}
1464ab4382d2SGreg Kroah-Hartman }
1465ab4382d2SGreg Kroah-Hartman 
1466ab4382d2SGreg Kroah-Hartman /*
1467ab4382d2SGreg Kroah-Hartman  * Register board-specific modem-control line handlers.
1468ab4382d2SGreg Kroah-Hartman  */
1469ab4382d2SGreg Kroah-Hartman void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1470ab4382d2SGreg Kroah-Hartman {
1471ab4382d2SGreg Kroah-Hartman 	if (fns->enable_ms)
1472ab4382d2SGreg Kroah-Hartman 		atmel_pops.enable_ms = fns->enable_ms;
1473ab4382d2SGreg Kroah-Hartman 	if (fns->get_mctrl)
1474ab4382d2SGreg Kroah-Hartman 		atmel_pops.get_mctrl = fns->get_mctrl;
1475ab4382d2SGreg Kroah-Hartman 	if (fns->set_mctrl)
1476ab4382d2SGreg Kroah-Hartman 		atmel_pops.set_mctrl = fns->set_mctrl;
1477ab4382d2SGreg Kroah-Hartman 	atmel_open_hook		= fns->open;
1478ab4382d2SGreg Kroah-Hartman 	atmel_close_hook	= fns->close;
1479ab4382d2SGreg Kroah-Hartman 	atmel_pops.pm		= fns->pm;
1480ab4382d2SGreg Kroah-Hartman 	atmel_pops.set_wake	= fns->set_wake;
1481ab4382d2SGreg Kroah-Hartman }
1482ab4382d2SGreg Kroah-Hartman 
1483ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1484ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
1485ab4382d2SGreg Kroah-Hartman {
1486ab4382d2SGreg Kroah-Hartman 	while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1487ab4382d2SGreg Kroah-Hartman 		cpu_relax();
1488ab4382d2SGreg Kroah-Hartman 	UART_PUT_CHAR(port, ch);
1489ab4382d2SGreg Kroah-Hartman }
1490ab4382d2SGreg Kroah-Hartman 
1491ab4382d2SGreg Kroah-Hartman /*
1492ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
1493ab4382d2SGreg Kroah-Hartman  */
1494ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
1495ab4382d2SGreg Kroah-Hartman {
1496ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
1497ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1498ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
1499ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
1500ab4382d2SGreg Kroah-Hartman 
1501ab4382d2SGreg Kroah-Hartman 	/*
1502ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
1503ab4382d2SGreg Kroah-Hartman 	 */
1504ab4382d2SGreg Kroah-Hartman 	imr = UART_GET_IMR(port);
1505ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
1506ab4382d2SGreg Kroah-Hartman 
1507ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
1508ab4382d2SGreg Kroah-Hartman 	pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1509ab4382d2SGreg Kroah-Hartman 	UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1510ab4382d2SGreg Kroah-Hartman 
1511ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
1512ab4382d2SGreg Kroah-Hartman 
1513ab4382d2SGreg Kroah-Hartman 	/*
1514ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
1515ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
1516ab4382d2SGreg Kroah-Hartman 	 */
1517ab4382d2SGreg Kroah-Hartman 	do {
1518ab4382d2SGreg Kroah-Hartman 		status = UART_GET_CSR(port);
1519ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
1520ab4382d2SGreg Kroah-Hartman 
1521ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
1522ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
1523ab4382d2SGreg Kroah-Hartman 		UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1524ab4382d2SGreg Kroah-Hartman 
1525ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
1526ab4382d2SGreg Kroah-Hartman 	UART_PUT_IER(port, imr);
1527ab4382d2SGreg Kroah-Hartman }
1528ab4382d2SGreg Kroah-Hartman 
1529ab4382d2SGreg Kroah-Hartman /*
1530ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
1531ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
1532ab4382d2SGreg Kroah-Hartman  */
1533ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1534ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
1535ab4382d2SGreg Kroah-Hartman {
1536ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
1537ab4382d2SGreg Kroah-Hartman 
1538ab4382d2SGreg Kroah-Hartman 	/*
1539ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
1540ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
1541ab4382d2SGreg Kroah-Hartman 	 */
1542ab4382d2SGreg Kroah-Hartman 	quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1543ab4382d2SGreg Kroah-Hartman 	if (!quot)
1544ab4382d2SGreg Kroah-Hartman 		return;
1545ab4382d2SGreg Kroah-Hartman 
1546ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1547ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
1548ab4382d2SGreg Kroah-Hartman 		*bits = 8;
1549ab4382d2SGreg Kroah-Hartman 	else
1550ab4382d2SGreg Kroah-Hartman 		*bits = 7;
1551ab4382d2SGreg Kroah-Hartman 
1552ab4382d2SGreg Kroah-Hartman 	mr = UART_GET_MR(port) & ATMEL_US_PAR;
1553ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
1554ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
1555ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
1556ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
1557ab4382d2SGreg Kroah-Hartman 
1558ab4382d2SGreg Kroah-Hartman 	/*
1559ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
1560ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
1561ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
1562ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
1563ab4382d2SGreg Kroah-Hartman 	 */
1564ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
1565ab4382d2SGreg Kroah-Hartman }
1566ab4382d2SGreg Kroah-Hartman 
1567ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
1568ab4382d2SGreg Kroah-Hartman {
1569ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
1570ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
1571ab4382d2SGreg Kroah-Hartman 	int bits = 8;
1572ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
1573ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
1574ab4382d2SGreg Kroah-Hartman 
1575ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
1576ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
1577ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
1578ab4382d2SGreg Kroah-Hartman 	}
1579ab4382d2SGreg Kroah-Hartman 
1580ab4382d2SGreg Kroah-Hartman 	clk_enable(atmel_ports[co->index].clk);
1581ab4382d2SGreg Kroah-Hartman 
1582ab4382d2SGreg Kroah-Hartman 	UART_PUT_IDR(port, -1);
1583ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1584ab4382d2SGreg Kroah-Hartman 	UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1585ab4382d2SGreg Kroah-Hartman 
1586ab4382d2SGreg Kroah-Hartman 	if (options)
1587ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1588ab4382d2SGreg Kroah-Hartman 	else
1589ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
1590ab4382d2SGreg Kroah-Hartman 
1591ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
1592ab4382d2SGreg Kroah-Hartman }
1593ab4382d2SGreg Kroah-Hartman 
1594ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
1595ab4382d2SGreg Kroah-Hartman 
1596ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
1597ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
1598ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
1599ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
1600ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
1601ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
1602ab4382d2SGreg Kroah-Hartman 	.index		= -1,
1603ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
1604ab4382d2SGreg Kroah-Hartman };
1605ab4382d2SGreg Kroah-Hartman 
1606ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
1607ab4382d2SGreg Kroah-Hartman 
1608ab4382d2SGreg Kroah-Hartman /*
1609ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
1610ab4382d2SGreg Kroah-Hartman  */
1611ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
1612ab4382d2SGreg Kroah-Hartman {
1613ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
1614ab4382d2SGreg Kroah-Hartman 		add_preferred_console(ATMEL_DEVICENAME,
1615ab4382d2SGreg Kroah-Hartman 				      atmel_default_console_device->id, NULL);
1616ab4382d2SGreg Kroah-Hartman 		atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1617ab4382d2SGreg Kroah-Hartman 				atmel_default_console_device);
1618ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
1619ab4382d2SGreg Kroah-Hartman 	}
1620ab4382d2SGreg Kroah-Hartman 
1621ab4382d2SGreg Kroah-Hartman 	return 0;
1622ab4382d2SGreg Kroah-Hartman }
1623ab4382d2SGreg Kroah-Hartman 
1624ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
1625ab4382d2SGreg Kroah-Hartman 
1626ab4382d2SGreg Kroah-Hartman /*
1627ab4382d2SGreg Kroah-Hartman  * Late console initialization.
1628ab4382d2SGreg Kroah-Hartman  */
1629ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
1630ab4382d2SGreg Kroah-Hartman {
1631ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
1632ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
1633ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
1634ab4382d2SGreg Kroah-Hartman 
1635ab4382d2SGreg Kroah-Hartman 	return 0;
1636ab4382d2SGreg Kroah-Hartman }
1637ab4382d2SGreg Kroah-Hartman 
1638ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
1639ab4382d2SGreg Kroah-Hartman 
1640ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
1641ab4382d2SGreg Kroah-Hartman {
1642ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
1643ab4382d2SGreg Kroah-Hartman }
1644ab4382d2SGreg Kroah-Hartman 
1645ab4382d2SGreg Kroah-Hartman #else
1646ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
1647ab4382d2SGreg Kroah-Hartman 
1648ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
1649ab4382d2SGreg Kroah-Hartman {
1650ab4382d2SGreg Kroah-Hartman 	return false;
1651ab4382d2SGreg Kroah-Hartman }
1652ab4382d2SGreg Kroah-Hartman #endif
1653ab4382d2SGreg Kroah-Hartman 
1654ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
1655ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
1656ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
1657ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
1658ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
1659ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
1660ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
1661ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
1662ab4382d2SGreg Kroah-Hartman };
1663ab4382d2SGreg Kroah-Hartman 
1664ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
1665ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
1666ab4382d2SGreg Kroah-Hartman {
1667ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
1668ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
1669ab4382d2SGreg Kroah-Hartman #else
1670ab4382d2SGreg Kroah-Hartman 	return false;
1671ab4382d2SGreg Kroah-Hartman #endif
1672ab4382d2SGreg Kroah-Hartman }
1673ab4382d2SGreg Kroah-Hartman 
1674ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
1675ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
1676ab4382d2SGreg Kroah-Hartman {
1677ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
1678ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1679ab4382d2SGreg Kroah-Hartman 
1680ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
1681ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
1682ab4382d2SGreg Kroah-Hartman 		while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1683ab4382d2SGreg Kroah-Hartman 			cpu_relax();
1684ab4382d2SGreg Kroah-Hartman 	}
1685ab4382d2SGreg Kroah-Hartman 
1686ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
1687ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1688ab4382d2SGreg Kroah-Hartman 	if (atmel_serial_clk_will_stop())
1689ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
1690ab4382d2SGreg Kroah-Hartman 
1691ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
1692ab4382d2SGreg Kroah-Hartman 
1693ab4382d2SGreg Kroah-Hartman 	return 0;
1694ab4382d2SGreg Kroah-Hartman }
1695ab4382d2SGreg Kroah-Hartman 
1696ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
1697ab4382d2SGreg Kroah-Hartman {
1698ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
1699ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1700ab4382d2SGreg Kroah-Hartman 
1701ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
1702ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1703ab4382d2SGreg Kroah-Hartman 
1704ab4382d2SGreg Kroah-Hartman 	return 0;
1705ab4382d2SGreg Kroah-Hartman }
1706ab4382d2SGreg Kroah-Hartman #else
1707ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
1708ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
1709ab4382d2SGreg Kroah-Hartman #endif
1710ab4382d2SGreg Kroah-Hartman 
1711ab4382d2SGreg Kroah-Hartman static int __devinit atmel_serial_probe(struct platform_device *pdev)
1712ab4382d2SGreg Kroah-Hartman {
1713ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *port;
1714ab4382d2SGreg Kroah-Hartman 	void *data;
1715ab4382d2SGreg Kroah-Hartman 	int ret;
1716ab4382d2SGreg Kroah-Hartman 
1717ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1718ab4382d2SGreg Kroah-Hartman 
1719ab4382d2SGreg Kroah-Hartman 	port = &atmel_ports[pdev->id];
1720ab4382d2SGreg Kroah-Hartman 	port->backup_imr = 0;
1721ab4382d2SGreg Kroah-Hartman 
1722ab4382d2SGreg Kroah-Hartman 	atmel_init_port(port, pdev);
1723ab4382d2SGreg Kroah-Hartman 
1724ab4382d2SGreg Kroah-Hartman 	if (!atmel_use_dma_rx(&port->uart)) {
1725ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
1726ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
1727ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1728ab4382d2SGreg Kroah-Hartman 		if (!data)
1729ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
1730ab4382d2SGreg Kroah-Hartman 		port->rx_ring.buf = data;
1731ab4382d2SGreg Kroah-Hartman 	}
1732ab4382d2SGreg Kroah-Hartman 
1733ab4382d2SGreg Kroah-Hartman 	ret = uart_add_one_port(&atmel_uart, &port->uart);
1734ab4382d2SGreg Kroah-Hartman 	if (ret)
1735ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
1736ab4382d2SGreg Kroah-Hartman 
1737ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1738ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(&port->uart)
1739ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1740ab4382d2SGreg Kroah-Hartman 		/*
1741ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
1742ab4382d2SGreg Kroah-Hartman 		 * the clk_enable() in atmel_console_setup()
1743ab4382d2SGreg Kroah-Hartman 		 */
1744ab4382d2SGreg Kroah-Hartman 		clk_disable(port->clk);
1745ab4382d2SGreg Kroah-Hartman 	}
1746ab4382d2SGreg Kroah-Hartman #endif
1747ab4382d2SGreg Kroah-Hartman 
1748ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
1749ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, port);
1750ab4382d2SGreg Kroah-Hartman 
1751fc887b15SLinus Torvalds 	if (port->rs485.flags & SER_RS485_ENABLED) {
1752fc887b15SLinus Torvalds 		UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
1753fc887b15SLinus Torvalds 		UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1754fc887b15SLinus Torvalds 	}
1755fc887b15SLinus Torvalds 
1756ab4382d2SGreg Kroah-Hartman 	return 0;
1757ab4382d2SGreg Kroah-Hartman 
1758ab4382d2SGreg Kroah-Hartman err_add_port:
1759ab4382d2SGreg Kroah-Hartman 	kfree(port->rx_ring.buf);
1760ab4382d2SGreg Kroah-Hartman 	port->rx_ring.buf = NULL;
1761ab4382d2SGreg Kroah-Hartman err_alloc_ring:
1762ab4382d2SGreg Kroah-Hartman 	if (!atmel_is_console_port(&port->uart)) {
1763ab4382d2SGreg Kroah-Hartman 		clk_put(port->clk);
1764ab4382d2SGreg Kroah-Hartman 		port->clk = NULL;
1765ab4382d2SGreg Kroah-Hartman 	}
1766ab4382d2SGreg Kroah-Hartman 
1767ab4382d2SGreg Kroah-Hartman 	return ret;
1768ab4382d2SGreg Kroah-Hartman }
1769ab4382d2SGreg Kroah-Hartman 
1770ab4382d2SGreg Kroah-Hartman static int __devexit atmel_serial_remove(struct platform_device *pdev)
1771ab4382d2SGreg Kroah-Hartman {
1772ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
1773ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1774ab4382d2SGreg Kroah-Hartman 	int ret = 0;
1775ab4382d2SGreg Kroah-Hartman 
1776ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 0);
1777ab4382d2SGreg Kroah-Hartman 	platform_set_drvdata(pdev, NULL);
1778ab4382d2SGreg Kroah-Hartman 
1779ab4382d2SGreg Kroah-Hartman 	ret = uart_remove_one_port(&atmel_uart, port);
1780ab4382d2SGreg Kroah-Hartman 
1781ab4382d2SGreg Kroah-Hartman 	tasklet_kill(&atmel_port->tasklet);
1782ab4382d2SGreg Kroah-Hartman 	kfree(atmel_port->rx_ring.buf);
1783ab4382d2SGreg Kroah-Hartman 
1784ab4382d2SGreg Kroah-Hartman 	/* "port" is allocated statically, so we shouldn't free it */
1785ab4382d2SGreg Kroah-Hartman 
1786ab4382d2SGreg Kroah-Hartman 	clk_put(atmel_port->clk);
1787ab4382d2SGreg Kroah-Hartman 
1788ab4382d2SGreg Kroah-Hartman 	return ret;
1789ab4382d2SGreg Kroah-Hartman }
1790ab4382d2SGreg Kroah-Hartman 
1791ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
1792ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
1793ab4382d2SGreg Kroah-Hartman 	.remove		= __devexit_p(atmel_serial_remove),
1794ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
1795ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
1796ab4382d2SGreg Kroah-Hartman 	.driver		= {
1797ab4382d2SGreg Kroah-Hartman 		.name	= "atmel_usart",
1798ab4382d2SGreg Kroah-Hartman 		.owner	= THIS_MODULE,
1799ab4382d2SGreg Kroah-Hartman 	},
1800ab4382d2SGreg Kroah-Hartman };
1801ab4382d2SGreg Kroah-Hartman 
1802ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
1803ab4382d2SGreg Kroah-Hartman {
1804ab4382d2SGreg Kroah-Hartman 	int ret;
1805ab4382d2SGreg Kroah-Hartman 
1806ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
1807ab4382d2SGreg Kroah-Hartman 	if (ret)
1808ab4382d2SGreg Kroah-Hartman 		return ret;
1809ab4382d2SGreg Kroah-Hartman 
1810ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
1811ab4382d2SGreg Kroah-Hartman 	if (ret)
1812ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
1813ab4382d2SGreg Kroah-Hartman 
1814ab4382d2SGreg Kroah-Hartman 	return ret;
1815ab4382d2SGreg Kroah-Hartman }
1816ab4382d2SGreg Kroah-Hartman 
1817ab4382d2SGreg Kroah-Hartman static void __exit atmel_serial_exit(void)
1818ab4382d2SGreg Kroah-Hartman {
1819ab4382d2SGreg Kroah-Hartman 	platform_driver_unregister(&atmel_serial_driver);
1820ab4382d2SGreg Kroah-Hartman 	uart_unregister_driver(&atmel_uart);
1821ab4382d2SGreg Kroah-Hartman }
1822ab4382d2SGreg Kroah-Hartman 
1823ab4382d2SGreg Kroah-Hartman module_init(atmel_serial_init);
1824ab4382d2SGreg Kroah-Hartman module_exit(atmel_serial_exit);
1825ab4382d2SGreg Kroah-Hartman 
1826ab4382d2SGreg Kroah-Hartman MODULE_AUTHOR("Rick Bronson");
1827ab4382d2SGreg Kroah-Hartman MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1828ab4382d2SGreg Kroah-Hartman MODULE_LICENSE("GPL");
1829ab4382d2SGreg Kroah-Hartman MODULE_ALIAS("platform:atmel_usart");
1830