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