xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision b389f173aaa1204d6dc1f299082a162eb0491545)
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/tty.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
355fbe46b6SNicolas Ferre #include <linux/of.h>
365fbe46b6SNicolas Ferre #include <linux/of_device.h>
37354e57f3SLinus Walleij #include <linux/of_gpio.h>
38ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
396b997babSVinod Koul #include <linux/dmaengine.h>
40ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
43bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
442e68c22fSElen Song #include <linux/timer.h>
45354e57f3SLinus Walleij #include <linux/gpio.h>
46e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
47e0b0baadSRichard Genoud #include <linux/err.h>
48ab5e4e41SRichard Genoud #include <linux/irq.h>
492c7af5baSBoris BREZILLON #include <linux/suspend.h>
50ab4382d2SGreg Kroah-Hartman 
51ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
52ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
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 
58b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
59b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
60b5199d46SCyrille Pitchen /*
61b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
62b5199d46SCyrille Pitchen  * high and low thresholds
63b5199d46SCyrille Pitchen  */
64b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
65b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
66b5199d46SCyrille Pitchen 
67ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
68ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
69ab4382d2SGreg Kroah-Hartman #endif
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
72ab4382d2SGreg Kroah-Hartman 
73e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
74e0b0baadSRichard Genoud 
75ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
76ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
77ab4382d2SGreg Kroah-Hartman 
78ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
79ab4382d2SGreg Kroah-Hartman 
80ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
81ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
82ab4382d2SGreg Kroah-Hartman  * UART. */
83ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
84ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
85ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
86ab4382d2SGreg Kroah-Hartman 
87ab4382d2SGreg Kroah-Hartman #else
88ab4382d2SGreg Kroah-Hartman 
89ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
90ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
91ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
92ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
93ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
94ab4382d2SGreg Kroah-Hartman 
95ab4382d2SGreg Kroah-Hartman #endif
96ab4382d2SGreg Kroah-Hartman 
97ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
98ab4382d2SGreg Kroah-Hartman 
99ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
100ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
101ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
102ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
103ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
104ab4382d2SGreg Kroah-Hartman };
105ab4382d2SGreg Kroah-Hartman 
106ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
107ab4382d2SGreg Kroah-Hartman 	u16		status;
108ab4382d2SGreg Kroah-Hartman 	u16		ch;
109ab4382d2SGreg Kroah-Hartman };
110ab4382d2SGreg Kroah-Hartman 
111637ba54fSLudovic Desroches /*
112637ba54fSLudovic Desroches  * Be careful, the real size of the ring buffer is
113637ba54fSLudovic Desroches  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
114637ba54fSLudovic Desroches  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
115637ba54fSLudovic Desroches  * DMA mode.
116637ba54fSLudovic Desroches  */
117ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
118ab4382d2SGreg Kroah-Hartman 
119ab4382d2SGreg Kroah-Hartman /*
1209af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
1219af92fbfSAlexandre Belloni  * avr32: 4
1229af92fbfSAlexandre Belloni  */
1239af92fbfSAlexandre Belloni #define ATMEL_MAX_UART		7
1249af92fbfSAlexandre Belloni 
1259af92fbfSAlexandre Belloni /*
126ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
127ab4382d2SGreg Kroah-Hartman  */
128ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
129ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
130ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
131ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
132ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
133ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
134ab4382d2SGreg Kroah-Hartman 
13534df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
13664e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
137ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
138ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
139ab4382d2SGreg Kroah-Hartman 
14008f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
14164e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
142ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
143ab4382d2SGreg Kroah-Hartman 
14408f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
14534df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
14608f738beSElen Song 	struct dma_chan			*chan_tx;
14734df42f5SElen Song 	struct dma_chan			*chan_rx;
14808f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
14934df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
15008f738beSElen Song 	dma_cookie_t			cookie_tx;
15134df42f5SElen Song 	dma_cookie_t			cookie_rx;
15208f738beSElen Song 	struct scatterlist		sg_tx;
15334df42f5SElen Song 	struct scatterlist		sg_rx;
15400e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_rx;
15500e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_tx;
15698f2082cSNicolas Ferre 	atomic_t		tasklet_shutdown;
157ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
1585f258b3eSCyrille Pitchen 	unsigned int		tx_len;
159ab4382d2SGreg Kroah-Hartman 
160ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
161ab4382d2SGreg Kroah-Hartman 
162e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
163ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
164b5199d46SCyrille Pitchen 	u32			fifo_size;
165b5199d46SCyrille Pitchen 	u32			rts_high;
166b5199d46SCyrille Pitchen 	u32			rts_low;
167ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1682958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
1695bf5635aSLudovic Desroches 	bool			has_frac_baudrate;
1704b769371SNicolas Ferre 	bool			has_hw_timer;
1714b769371SNicolas Ferre 	struct timer_list	uart_timer;
1722c7af5baSBoris BREZILLON 
1732c7af5baSBoris BREZILLON 	bool			suspended;
1742c7af5baSBoris BREZILLON 	unsigned int		pending;
1752c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1762c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1772c7af5baSBoris BREZILLON 
178a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
179a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
180a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
181a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
182a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
183a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
184ab4382d2SGreg Kroah-Hartman };
185ab4382d2SGreg Kroah-Hartman 
186ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
187503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
188ab4382d2SGreg Kroah-Hartman 
189ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
190ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
191ab4382d2SGreg Kroah-Hartman #endif
192ab4382d2SGreg Kroah-Hartman 
1935fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1945fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1955fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1965fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1975fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1985fbe46b6SNicolas Ferre };
1995fbe46b6SNicolas Ferre #endif
2005fbe46b6SNicolas Ferre 
201ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
202ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
203ab4382d2SGreg Kroah-Hartman {
204ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
205ab4382d2SGreg Kroah-Hartman }
206ab4382d2SGreg Kroah-Hartman 
2074e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2084e7decdaSCyrille Pitchen {
2094e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2104e7decdaSCyrille Pitchen }
2114e7decdaSCyrille Pitchen 
2124e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2134e7decdaSCyrille Pitchen {
2144e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2154e7decdaSCyrille Pitchen }
2164e7decdaSCyrille Pitchen 
217a6499435SCyrille Pitchen #ifdef CONFIG_AVR32
218a6499435SCyrille Pitchen 
219a6499435SCyrille Pitchen /* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
220a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
221b5199d46SCyrille Pitchen {
222a6499435SCyrille Pitchen 	return __raw_readl(port->membase + ATMEL_US_RHR);
223b5199d46SCyrille Pitchen }
224b5199d46SCyrille Pitchen 
225a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
226b5199d46SCyrille Pitchen {
227a6499435SCyrille Pitchen 	__raw_writel(value, port->membase + ATMEL_US_THR);
228b5199d46SCyrille Pitchen }
229b5199d46SCyrille Pitchen 
230a6499435SCyrille Pitchen #else
231a6499435SCyrille Pitchen 
232a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
233a6499435SCyrille Pitchen {
234a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
235a6499435SCyrille Pitchen }
236a6499435SCyrille Pitchen 
237a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
238a6499435SCyrille Pitchen {
239a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
240a6499435SCyrille Pitchen }
241a6499435SCyrille Pitchen 
242a6499435SCyrille Pitchen #endif
243a6499435SCyrille Pitchen 
244ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
24564e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
246ab4382d2SGreg Kroah-Hartman {
247ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
248ab4382d2SGreg Kroah-Hartman 
24964e22ebeSElen Song 	return atmel_port->use_pdc_rx;
250ab4382d2SGreg Kroah-Hartman }
251ab4382d2SGreg Kroah-Hartman 
25264e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
253ab4382d2SGreg Kroah-Hartman {
254ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
255ab4382d2SGreg Kroah-Hartman 
25664e22ebeSElen Song 	return atmel_port->use_pdc_tx;
257ab4382d2SGreg Kroah-Hartman }
258ab4382d2SGreg Kroah-Hartman #else
25964e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
260ab4382d2SGreg Kroah-Hartman {
261ab4382d2SGreg Kroah-Hartman 	return false;
262ab4382d2SGreg Kroah-Hartman }
263ab4382d2SGreg Kroah-Hartman 
26464e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
265ab4382d2SGreg Kroah-Hartman {
266ab4382d2SGreg Kroah-Hartman 	return false;
267ab4382d2SGreg Kroah-Hartman }
268ab4382d2SGreg Kroah-Hartman #endif
269ab4382d2SGreg Kroah-Hartman 
27008f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
27108f738beSElen Song {
27208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27308f738beSElen Song 
27408f738beSElen Song 	return atmel_port->use_dma_tx;
27508f738beSElen Song }
27608f738beSElen Song 
27734df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27834df42f5SElen Song {
27934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
28034df42f5SElen Song 
28134df42f5SElen Song 	return atmel_port->use_dma_rx;
28234df42f5SElen Song }
28334df42f5SElen Song 
2845be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2855be605acSAlexandre Belloni {
2865be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2875be605acSAlexandre Belloni 
2885be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2895be605acSAlexandre Belloni }
2905be605acSAlexandre Belloni 
29198f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
29298f2082cSNicolas Ferre 				   struct tasklet_struct *t)
29398f2082cSNicolas Ferre {
29498f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown))
29598f2082cSNicolas Ferre 		tasklet_schedule(t);
29698f2082cSNicolas Ferre }
29798f2082cSNicolas Ferre 
298e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
299e0b0baadSRichard Genoud {
300e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
301e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
302e0b0baadSRichard Genoud 
3034e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
304e0b0baadSRichard Genoud 
305e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
306e0b0baadSRichard Genoud 
307e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
308e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
309e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
310e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
311e0b0baadSRichard Genoud 		else
312e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
313e0b0baadSRichard Genoud 	}
314e0b0baadSRichard Genoud 
315e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
316e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
317e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
318e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
319e0b0baadSRichard Genoud 		else
320e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
321e0b0baadSRichard Genoud 	}
322e0b0baadSRichard Genoud 
323e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
324e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
325e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
326e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
327e0b0baadSRichard Genoud 		else
328e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
329e0b0baadSRichard Genoud 	}
330e0b0baadSRichard Genoud 
331e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
332e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
333e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
334e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
335e0b0baadSRichard Genoud 		else
336e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
337e0b0baadSRichard Genoud 	}
338e0b0baadSRichard Genoud 
339e0b0baadSRichard Genoud 	return status;
340e0b0baadSRichard Genoud }
341e0b0baadSRichard Genoud 
342ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
34313bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
34413bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
345ab4382d2SGreg Kroah-Hartman {
346ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
347ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
348ab4382d2SGreg Kroah-Hartman 
349ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3504e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
351ab4382d2SGreg Kroah-Hartman 
3524e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
353ab4382d2SGreg Kroah-Hartman 
354ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
355ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
356ab4382d2SGreg Kroah-Hartman 
35713bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
358ab4382d2SGreg Kroah-Hartman 
359ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
360ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
361ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3624e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3634e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
364ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
365ab4382d2SGreg Kroah-Hartman 	} else {
366ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
36764e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
368ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
369ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
370ab4382d2SGreg Kroah-Hartman 		else
371ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
372ab4382d2SGreg Kroah-Hartman 	}
3734e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
374ab4382d2SGreg Kroah-Hartman 
375ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3764e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
377ab4382d2SGreg Kroah-Hartman 
37813bd3e6fSRicardo Ribalda Delgado 	return 0;
379ab4382d2SGreg Kroah-Hartman }
380ab4382d2SGreg Kroah-Hartman 
381ab4382d2SGreg Kroah-Hartman /*
382ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
383ab4382d2SGreg Kroah-Hartman  */
384ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
385ab4382d2SGreg Kroah-Hartman {
3864e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3874e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3884e7decdaSCyrille Pitchen 		0;
389ab4382d2SGreg Kroah-Hartman }
390ab4382d2SGreg Kroah-Hartman 
391ab4382d2SGreg Kroah-Hartman /*
392ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
393ab4382d2SGreg Kroah-Hartman  */
394ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
395ab4382d2SGreg Kroah-Hartman {
396ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3974e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3981cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
399ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
400ab4382d2SGreg Kroah-Hartman 
4011cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
4021cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
4034e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
4044e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
4051cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
4061cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
4071cf6e8fcSCyrille Pitchen 	}
4081cf6e8fcSCyrille Pitchen 
4091cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
4101cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
4111cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4121cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4131cf6e8fcSCyrille Pitchen 
4141cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4151cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4161cf6e8fcSCyrille Pitchen 	} else {
4171cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4181cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4191cf6e8fcSCyrille Pitchen 
4201cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4211cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4221cf6e8fcSCyrille Pitchen 	}
4231cf6e8fcSCyrille Pitchen 
424ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4251cf6e8fcSCyrille Pitchen 		control |= rts_ready;
426ab4382d2SGreg Kroah-Hartman 	else
4271cf6e8fcSCyrille Pitchen 		control |= rts_paused;
428ab4382d2SGreg Kroah-Hartman 
429ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
430ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
431ab4382d2SGreg Kroah-Hartman 	else
432ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
433ab4382d2SGreg Kroah-Hartman 
4344e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
435ab4382d2SGreg Kroah-Hartman 
436e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
437e0b0baadSRichard Genoud 
438ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
4391cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
440ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
441ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
442ab4382d2SGreg Kroah-Hartman 	else
443ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
444ab4382d2SGreg Kroah-Hartman 
4454e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
446ab4382d2SGreg Kroah-Hartman }
447ab4382d2SGreg Kroah-Hartman 
448ab4382d2SGreg Kroah-Hartman /*
449ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
450ab4382d2SGreg Kroah-Hartman  */
451ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
452ab4382d2SGreg Kroah-Hartman {
453e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
454e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
455ab4382d2SGreg Kroah-Hartman 
4564e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
457ab4382d2SGreg Kroah-Hartman 
458ab4382d2SGreg Kroah-Hartman 	/*
459ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
460ab4382d2SGreg Kroah-Hartman 	 */
461ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
462ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
463ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
464ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
465ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
466ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
467ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
468ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
469ab4382d2SGreg Kroah-Hartman 
470e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
471ab4382d2SGreg Kroah-Hartman }
472ab4382d2SGreg Kroah-Hartman 
473ab4382d2SGreg Kroah-Hartman /*
474ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
475ab4382d2SGreg Kroah-Hartman  */
476ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
477ab4382d2SGreg Kroah-Hartman {
478ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
479ab4382d2SGreg Kroah-Hartman 
48064e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
481ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4824e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
483ab4382d2SGreg Kroah-Hartman 	}
48489d82324SRichard Genoud 
48589d82324SRichard Genoud 	/*
48689d82324SRichard Genoud 	 * Disable the transmitter.
48789d82324SRichard Genoud 	 * This is mandatory when DMA is used, otherwise the DMA buffer
48889d82324SRichard Genoud 	 * is fully transmitted.
48989d82324SRichard Genoud 	 */
49089d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
49189d82324SRichard Genoud 
492ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4934e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
494ab4382d2SGreg Kroah-Hartman 
49513bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
49613bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
497ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
498ab4382d2SGreg Kroah-Hartman }
499ab4382d2SGreg Kroah-Hartman 
500ab4382d2SGreg Kroah-Hartman /*
501ab4382d2SGreg Kroah-Hartman  * Start transmitting.
502ab4382d2SGreg Kroah-Hartman  */
503ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
504ab4382d2SGreg Kroah-Hartman {
505ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
506ab4382d2SGreg Kroah-Hartman 
5070058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
5080058f087SAlexandre Belloni 				       & ATMEL_PDC_TXTEN))
509ab4382d2SGreg Kroah-Hartman 		/* The transmitter is already running.  Yes, we
510ab4382d2SGreg Kroah-Hartman 		   really need this.*/
511ab4382d2SGreg Kroah-Hartman 		return;
512ab4382d2SGreg Kroah-Hartman 
5130058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
51413bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
51513bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
516ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
517ab4382d2SGreg Kroah-Hartman 
5180058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port))
519ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
5204e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
5210058f087SAlexandre Belloni 
522ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
5234e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
52489d82324SRichard Genoud 
52589d82324SRichard Genoud 	/* re-enable the transmitter */
52689d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
527ab4382d2SGreg Kroah-Hartman }
528ab4382d2SGreg Kroah-Hartman 
529ab4382d2SGreg Kroah-Hartman /*
530ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
531ab4382d2SGreg Kroah-Hartman  */
532ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
533ab4382d2SGreg Kroah-Hartman {
5344e7decdaSCyrille Pitchen 	/* reset status and receiver */
5354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
536ab4382d2SGreg Kroah-Hartman 
5374e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
53857c36868SSiftar, Gabe 
53964e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
540ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
5414e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
5424e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
543ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
5444e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
545ab4382d2SGreg Kroah-Hartman 	} else {
5464e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
547ab4382d2SGreg Kroah-Hartman 	}
548ab4382d2SGreg Kroah-Hartman }
549ab4382d2SGreg Kroah-Hartman 
550ab4382d2SGreg Kroah-Hartman /*
551ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
552ab4382d2SGreg Kroah-Hartman  */
553ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
554ab4382d2SGreg Kroah-Hartman {
5554e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
55657c36868SSiftar, Gabe 
55764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
558ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5594e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5604e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5614e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
562ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
563ab4382d2SGreg Kroah-Hartman 	} else {
5644e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
565ab4382d2SGreg Kroah-Hartman 	}
566ab4382d2SGreg Kroah-Hartman }
567ab4382d2SGreg Kroah-Hartman 
568ab4382d2SGreg Kroah-Hartman /*
569ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
570ab4382d2SGreg Kroah-Hartman  */
571ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
572ab4382d2SGreg Kroah-Hartman {
573ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
574ab5e4e41SRichard Genoud 	uint32_t ier = 0;
575ab5e4e41SRichard Genoud 
576ab5e4e41SRichard Genoud 	/*
577ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
578ab5e4e41SRichard Genoud 	 */
579ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
580ab5e4e41SRichard Genoud 		return;
581ab5e4e41SRichard Genoud 
582ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
583ab5e4e41SRichard Genoud 
58418dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
585ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
586ab5e4e41SRichard Genoud 
58718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
588ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
589ab5e4e41SRichard Genoud 
59018dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
591ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
592ab5e4e41SRichard Genoud 
59318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
594ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
595ab5e4e41SRichard Genoud 
5964e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
59718dfef9cSUwe Kleine-König 
59818dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
599ab4382d2SGreg Kroah-Hartman }
600ab4382d2SGreg Kroah-Hartman 
601ab4382d2SGreg Kroah-Hartman /*
60235b675b9SRichard Genoud  * Disable modem status interrupts
60335b675b9SRichard Genoud  */
60435b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
60535b675b9SRichard Genoud {
60635b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
60735b675b9SRichard Genoud 	uint32_t idr = 0;
60835b675b9SRichard Genoud 
60935b675b9SRichard Genoud 	/*
61035b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
61135b675b9SRichard Genoud 	 */
61235b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
61335b675b9SRichard Genoud 		return;
61435b675b9SRichard Genoud 
61535b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
61635b675b9SRichard Genoud 
61718dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
61818dfef9cSUwe Kleine-König 
61918dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
62035b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
62135b675b9SRichard Genoud 
62218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
62335b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
62435b675b9SRichard Genoud 
62518dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
62635b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
62735b675b9SRichard Genoud 
62818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
62935b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
63035b675b9SRichard Genoud 
6314e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
63235b675b9SRichard Genoud }
63335b675b9SRichard Genoud 
63435b675b9SRichard Genoud /*
635ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
636ab4382d2SGreg Kroah-Hartman  */
637ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
638ab4382d2SGreg Kroah-Hartman {
639ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
6404e7decdaSCyrille Pitchen 		/* start break */
6414e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
642ab4382d2SGreg Kroah-Hartman 	else
6434e7decdaSCyrille Pitchen 		/* stop break */
6444e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
645ab4382d2SGreg Kroah-Hartman }
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman /*
648ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
649ab4382d2SGreg Kroah-Hartman  */
650ab4382d2SGreg Kroah-Hartman static void
651ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
652ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
653ab4382d2SGreg Kroah-Hartman {
654ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
655ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
656ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
657ab4382d2SGreg Kroah-Hartman 
658ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
659ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
660ab4382d2SGreg Kroah-Hartman 		return;
661ab4382d2SGreg Kroah-Hartman 
662ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
663ab4382d2SGreg Kroah-Hartman 	c->status	= status;
664ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
665ab4382d2SGreg Kroah-Hartman 
666ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
667ab4382d2SGreg Kroah-Hartman 	smp_wmb();
668ab4382d2SGreg Kroah-Hartman 
669ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
670ab4382d2SGreg Kroah-Hartman }
671ab4382d2SGreg Kroah-Hartman 
672ab4382d2SGreg Kroah-Hartman /*
673ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
674ab4382d2SGreg Kroah-Hartman  */
675ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
676ab4382d2SGreg Kroah-Hartman {
677ab4382d2SGreg Kroah-Hartman 	/* clear error */
6784e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
679ab4382d2SGreg Kroah-Hartman 
680ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
681ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
682ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
683ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
684ab4382d2SGreg Kroah-Hartman 	}
685ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
686ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
687ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
688ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
689ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
690ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
691ab4382d2SGreg Kroah-Hartman }
692ab4382d2SGreg Kroah-Hartman 
693ab4382d2SGreg Kroah-Hartman /*
694ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
695ab4382d2SGreg Kroah-Hartman  */
696ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
697ab4382d2SGreg Kroah-Hartman {
698ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
699ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
700ab4382d2SGreg Kroah-Hartman 
7014e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
702ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
703a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
704ab4382d2SGreg Kroah-Hartman 
705ab4382d2SGreg Kroah-Hartman 		/*
706ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
707ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
708ab4382d2SGreg Kroah-Hartman 		 */
709ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
710ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
711ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
712ab4382d2SGreg Kroah-Hartman 
713ab4382d2SGreg Kroah-Hartman 			/* clear error */
7144e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
715ab4382d2SGreg Kroah-Hartman 
716ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
717ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
718ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
7194e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
7204e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
721ab4382d2SGreg Kroah-Hartman 			} else {
722ab4382d2SGreg Kroah-Hartman 				/*
723ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
724ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
725ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
726ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
727ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
728ab4382d2SGreg Kroah-Hartman 				 */
7294e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
7304e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
731ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
732ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
733ab4382d2SGreg Kroah-Hartman 			}
734ab4382d2SGreg Kroah-Hartman 		}
735ab4382d2SGreg Kroah-Hartman 
736ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
7374e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
738ab4382d2SGreg Kroah-Hartman 	}
739ab4382d2SGreg Kroah-Hartman 
74098f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
741ab4382d2SGreg Kroah-Hartman }
742ab4382d2SGreg Kroah-Hartman 
743ab4382d2SGreg Kroah-Hartman /*
744ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
745ab4382d2SGreg Kroah-Hartman  * disabled)
746ab4382d2SGreg Kroah-Hartman  */
747ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
748ab4382d2SGreg Kroah-Hartman {
749ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
750ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
751ab4382d2SGreg Kroah-Hartman 
7524e7decdaSCyrille Pitchen 	if (port->x_char &&
7534e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
754a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
755ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
756ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
757ab4382d2SGreg Kroah-Hartman 	}
758ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
759ab4382d2SGreg Kroah-Hartman 		return;
760ab4382d2SGreg Kroah-Hartman 
7614e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7624e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
763a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
764ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
765ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
766ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
767ab4382d2SGreg Kroah-Hartman 			break;
768ab4382d2SGreg Kroah-Hartman 	}
769ab4382d2SGreg Kroah-Hartman 
770ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
771ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
772ab4382d2SGreg Kroah-Hartman 
773ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
774ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7754e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7764e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
777ab4382d2SGreg Kroah-Hartman }
778ab4382d2SGreg Kroah-Hartman 
77908f738beSElen Song static void atmel_complete_tx_dma(void *arg)
78008f738beSElen Song {
78108f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
78208f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
78308f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
78408f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
78508f738beSElen Song 	unsigned long flags;
78608f738beSElen Song 
78708f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
78808f738beSElen Song 
78908f738beSElen Song 	if (chan)
79008f738beSElen Song 		dmaengine_terminate_all(chan);
7915f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
79208f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
79308f738beSElen Song 
7945f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
79508f738beSElen Song 
79608f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
79708f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
79808f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
79908f738beSElen Song 	atmel_port->desc_tx = NULL;
80008f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
80108f738beSElen Song 
80208f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
80308f738beSElen Song 		uart_write_wakeup(port);
80408f738beSElen Song 
8051842dc2eSCyrille Pitchen 	/*
8061842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
8071842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
8081842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
8091842dc2eSCyrille Pitchen 	 */
81008f738beSElen Song 	if (!uart_circ_empty(xmit))
81198f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
812*b389f173SRichard Genoud 	else if ((port->rs485.flags & SER_RS485_ENABLED) &&
813*b389f173SRichard Genoud 		 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
814*b389f173SRichard Genoud 		/* DMA done, stop TX, start RX for RS485 */
815*b389f173SRichard Genoud 		atmel_start_rx(port);
816*b389f173SRichard Genoud 	}
81708f738beSElen Song 
81808f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
81908f738beSElen Song }
82008f738beSElen Song 
82108f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
82208f738beSElen Song {
82308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
82408f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
82508f738beSElen Song 
82608f738beSElen Song 	if (chan) {
82708f738beSElen Song 		dmaengine_terminate_all(chan);
82808f738beSElen Song 		dma_release_channel(chan);
82908f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
83048479148SWolfram Sang 				DMA_TO_DEVICE);
83108f738beSElen Song 	}
83208f738beSElen Song 
83308f738beSElen Song 	atmel_port->desc_tx = NULL;
83408f738beSElen Song 	atmel_port->chan_tx = NULL;
83508f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
83608f738beSElen Song }
83708f738beSElen Song 
83808f738beSElen Song /*
83908f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
84008f738beSElen Song  */
84108f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
84208f738beSElen Song {
84308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
84408f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
84508f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
84608f738beSElen Song 	struct dma_async_tx_descriptor *desc;
8475f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
8485f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
8495f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
85008f738beSElen Song 
85108f738beSElen Song 	/* Make sure we have an idle channel */
85208f738beSElen Song 	if (atmel_port->desc_tx != NULL)
85308f738beSElen Song 		return;
85408f738beSElen Song 
85508f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
85608f738beSElen Song 		/*
85708f738beSElen Song 		 * DMA is idle now.
85808f738beSElen Song 		 * Port xmit buffer is already mapped,
85908f738beSElen Song 		 * and it is one page... Just adjust
86008f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
86108f738beSElen Song 		 * we have to transmit till the end, and then the rest.
86208f738beSElen Song 		 * Take the port lock to get a
86308f738beSElen Song 		 * consistent xmit buffer state.
86408f738beSElen Song 		 */
8655f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
86608f738beSElen Song 					 xmit->tail,
86708f738beSElen Song 					 UART_XMIT_SIZE);
8685f258b3eSCyrille Pitchen 
8695f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
8705f258b3eSCyrille Pitchen 			/* multi data mode */
8715f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
8725f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
8735f258b3eSCyrille Pitchen 		} else {
8745f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
8755f258b3eSCyrille Pitchen 			part1_len = 0;
8765f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
8775f258b3eSCyrille Pitchen 		}
8785f258b3eSCyrille Pitchen 
8795f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
8805f258b3eSCyrille Pitchen 		sg_len = 0;
8815f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
8825f258b3eSCyrille Pitchen 		if (part1_len) {
8835f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8845f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8855f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
8865f258b3eSCyrille Pitchen 
8875f258b3eSCyrille Pitchen 			phys_addr += part1_len;
8885f258b3eSCyrille Pitchen 		}
8895f258b3eSCyrille Pitchen 
8905f258b3eSCyrille Pitchen 		if (part2_len) {
8915f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8925f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8935f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
8945f258b3eSCyrille Pitchen 		}
8955f258b3eSCyrille Pitchen 
8965f258b3eSCyrille Pitchen 		/*
8975f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
8985f258b3eSCyrille Pitchen 		 * xmit->tail correctly
8995f258b3eSCyrille Pitchen 		 */
9005f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
90108f738beSElen Song 
90208f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
9035f258b3eSCyrille Pitchen 					       sgl,
9045f258b3eSCyrille Pitchen 					       sg_len,
90508f738beSElen Song 					       DMA_MEM_TO_DEV,
90608f738beSElen Song 					       DMA_PREP_INTERRUPT |
90708f738beSElen Song 					       DMA_CTRL_ACK);
90808f738beSElen Song 		if (!desc) {
90908f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
91008f738beSElen Song 			return;
91108f738beSElen Song 		}
91208f738beSElen Song 
9135f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
91408f738beSElen Song 
91508f738beSElen Song 		atmel_port->desc_tx = desc;
91608f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
91708f738beSElen Song 		desc->callback_param = atmel_port;
91808f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
91908f738beSElen Song 	}
92008f738beSElen Song 
92108f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
92208f738beSElen Song 		uart_write_wakeup(port);
92308f738beSElen Song }
92408f738beSElen Song 
92508f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
92608f738beSElen Song {
92708f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
92808f738beSElen Song 	dma_cap_mask_t		mask;
92908f738beSElen Song 	struct dma_slave_config config;
93008f738beSElen Song 	int ret, nent;
93108f738beSElen Song 
93208f738beSElen Song 	dma_cap_zero(mask);
93308f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
93408f738beSElen Song 
93508f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
93608f738beSElen Song 	if (atmel_port->chan_tx == NULL)
93708f738beSElen Song 		goto chan_err;
93808f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
93908f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
94008f738beSElen Song 
94108f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
94208f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
94308f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
9442c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
94508f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
94608f738beSElen Song 			virt_to_page(port->state->xmit.buf),
94708f738beSElen Song 			UART_XMIT_SIZE,
948c8d1f022SUwe Kleine-König 			(unsigned long)port->state->xmit.buf & ~PAGE_MASK);
94908f738beSElen Song 	nent = dma_map_sg(port->dev,
95008f738beSElen Song 				&atmel_port->sg_tx,
95108f738beSElen Song 				1,
95248479148SWolfram Sang 				DMA_TO_DEVICE);
95308f738beSElen Song 
95408f738beSElen Song 	if (!nent) {
95508f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
95608f738beSElen Song 		goto chan_err;
95708f738beSElen Song 	} else {
958c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
95908f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
96008f738beSElen Song 			port->state->xmit.buf,
961c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
96208f738beSElen Song 	}
96308f738beSElen Song 
96408f738beSElen Song 	/* Configure the slave DMA */
96508f738beSElen Song 	memset(&config, 0, sizeof(config));
96608f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
9675f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
9685f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
9695f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
97008f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
971a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
97208f738beSElen Song 
9735483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9745483c10eSMaxime Ripard 				     &config);
97508f738beSElen Song 	if (ret) {
97608f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
97708f738beSElen Song 		goto chan_err;
97808f738beSElen Song 	}
97908f738beSElen Song 
98008f738beSElen Song 	return 0;
98108f738beSElen Song 
98208f738beSElen Song chan_err:
98308f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
98408f738beSElen Song 	atmel_port->use_dma_tx = 0;
98508f738beSElen Song 	if (atmel_port->chan_tx)
98608f738beSElen Song 		atmel_release_tx_dma(port);
98708f738beSElen Song 	return -EINVAL;
98808f738beSElen Song }
98908f738beSElen Song 
99034df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
99134df42f5SElen Song {
99234df42f5SElen Song 	struct uart_port *port = arg;
99334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
99434df42f5SElen Song 
99598f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
99634df42f5SElen Song }
99734df42f5SElen Song 
99834df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
99934df42f5SElen Song {
100034df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
100134df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
100234df42f5SElen Song 
100334df42f5SElen Song 	if (chan) {
100434df42f5SElen Song 		dmaengine_terminate_all(chan);
100534df42f5SElen Song 		dma_release_channel(chan);
100634df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
100748479148SWolfram Sang 				DMA_FROM_DEVICE);
100834df42f5SElen Song 	}
100934df42f5SElen Song 
101034df42f5SElen Song 	atmel_port->desc_rx = NULL;
101134df42f5SElen Song 	atmel_port->chan_rx = NULL;
101234df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
101334df42f5SElen Song }
101434df42f5SElen Song 
101534df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
101634df42f5SElen Song {
101734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
101866f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
101934df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
102034df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
102134df42f5SElen Song 	struct dma_tx_state state;
102234df42f5SElen Song 	enum dma_status dmastat;
102366f37aafSCyrille Pitchen 	size_t count;
102434df42f5SElen Song 
102534df42f5SElen Song 
102634df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
10274e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
102834df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
102934df42f5SElen Song 				atmel_port->cookie_rx,
103034df42f5SElen Song 				&state);
103134df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
103234df42f5SElen Song 	if (dmastat == DMA_ERROR) {
103334df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
10344e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
103598f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
103634df42f5SElen Song 		return;
103734df42f5SElen Song 	}
103866f37aafSCyrille Pitchen 
103966f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
104066f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
104166f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
104266f37aafSCyrille Pitchen 			    1,
1043485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
104434df42f5SElen Song 
104534df42f5SElen Song 	/*
104666f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
104766f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
104866f37aafSCyrille Pitchen 	 * framework.
104966f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
105066f37aafSCyrille Pitchen 	 * length.
105134df42f5SElen Song 	 */
105266f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
105366f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
105466f37aafSCyrille Pitchen 	/*
105566f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
105666f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
105766f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
105866f37aafSCyrille Pitchen 	 *
105966f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
106066f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
106166f37aafSCyrille Pitchen 	 *
106266f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
106366f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
106466f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
106566f37aafSCyrille Pitchen 	 */
106666f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
106766f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
106834df42f5SElen Song 
106966f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
107066f37aafSCyrille Pitchen 		ring->tail = 0;
107134df42f5SElen Song 		port->icount.rx += count;
107234df42f5SElen Song 	}
107334df42f5SElen Song 
107466f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
107566f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
107666f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
107766f37aafSCyrille Pitchen 
107866f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
107966f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
108066f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
108166f37aafSCyrille Pitchen 			ring->head = 0;
108266f37aafSCyrille Pitchen 		ring->tail = ring->head;
108366f37aafSCyrille Pitchen 		port->icount.rx += count;
108466f37aafSCyrille Pitchen 	}
108566f37aafSCyrille Pitchen 
108666f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
108766f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
108866f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
108966f37aafSCyrille Pitchen 			       1,
1090485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
109166f37aafSCyrille Pitchen 
109266f37aafSCyrille Pitchen 	/*
109366f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
109466f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
109566f37aafSCyrille Pitchen 	 */
109666f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
109766f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
109866f37aafSCyrille Pitchen 	spin_lock(&port->lock);
109966f37aafSCyrille Pitchen 
11004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
110134df42f5SElen Song }
110234df42f5SElen Song 
110334df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
110434df42f5SElen Song {
110534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
110634df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
110734df42f5SElen Song 	dma_cap_mask_t		mask;
110834df42f5SElen Song 	struct dma_slave_config config;
110934df42f5SElen Song 	struct circ_buf		*ring;
111034df42f5SElen Song 	int ret, nent;
111134df42f5SElen Song 
111234df42f5SElen Song 	ring = &atmel_port->rx_ring;
111334df42f5SElen Song 
111434df42f5SElen Song 	dma_cap_zero(mask);
111534df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
111634df42f5SElen Song 
111734df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
111834df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
111934df42f5SElen Song 		goto chan_err;
112034df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
112134df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
112234df42f5SElen Song 
112334df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
112434df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
112534df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
11262c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
112734df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
112834df42f5SElen Song 		    virt_to_page(ring->buf),
1129a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
1130c8d1f022SUwe Kleine-König 		    (unsigned long)ring->buf & ~PAGE_MASK);
113134df42f5SElen Song 	nent = dma_map_sg(port->dev,
113234df42f5SElen Song 			  &atmel_port->sg_rx,
113334df42f5SElen Song 			  1,
113448479148SWolfram Sang 			  DMA_FROM_DEVICE);
113534df42f5SElen Song 
113634df42f5SElen Song 	if (!nent) {
113734df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
113834df42f5SElen Song 		goto chan_err;
113934df42f5SElen Song 	} else {
1140c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
114134df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
114234df42f5SElen Song 			ring->buf,
1143c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
114434df42f5SElen Song 	}
114534df42f5SElen Song 
114634df42f5SElen Song 	/* Configure the slave DMA */
114734df42f5SElen Song 	memset(&config, 0, sizeof(config));
114834df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
114934df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
115034df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1151a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
115234df42f5SElen Song 
11535483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
11545483c10eSMaxime Ripard 				     &config);
115534df42f5SElen Song 	if (ret) {
115634df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
115734df42f5SElen Song 		goto chan_err;
115834df42f5SElen Song 	}
115934df42f5SElen Song 	/*
116034df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
116134df42f5SElen Song 	 * each one is half ring buffer size
116234df42f5SElen Song 	 */
116334df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
116434df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
116534df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
116634df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
116734df42f5SElen Song 					 DMA_DEV_TO_MEM,
116834df42f5SElen Song 					 DMA_PREP_INTERRUPT);
116934df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
117034df42f5SElen Song 	desc->callback_param = port;
117134df42f5SElen Song 	atmel_port->desc_rx = desc;
117234df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
117334df42f5SElen Song 
117434df42f5SElen Song 	return 0;
117534df42f5SElen Song 
117634df42f5SElen Song chan_err:
117734df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
117834df42f5SElen Song 	atmel_port->use_dma_rx = 0;
117934df42f5SElen Song 	if (atmel_port->chan_rx)
118034df42f5SElen Song 		atmel_release_rx_dma(port);
118134df42f5SElen Song 	return -EINVAL;
118234df42f5SElen Song }
118334df42f5SElen Song 
11842e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
11852e68c22fSElen Song {
11862e68c22fSElen Song 	struct uart_port *port = (void *)data;
11872e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11882e68c22fSElen Song 
118998f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown)) {
119000e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
119198f2082cSNicolas Ferre 		mod_timer(&atmel_port->uart_timer,
119298f2082cSNicolas Ferre 			  jiffies + uart_poll_timeout(port));
119398f2082cSNicolas Ferre 	}
11942e68c22fSElen Song }
11952e68c22fSElen Song 
1196ab4382d2SGreg Kroah-Hartman /*
1197ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1198ab4382d2SGreg Kroah-Hartman  */
1199ab4382d2SGreg Kroah-Hartman static void
1200ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1201ab4382d2SGreg Kroah-Hartman {
1202ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1203ab4382d2SGreg Kroah-Hartman 
120464e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1205ab4382d2SGreg Kroah-Hartman 		/*
1206ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1207ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1208ab4382d2SGreg Kroah-Hartman 		 *
1209ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1210ab4382d2SGreg Kroah-Hartman 		 * the moment.
1211ab4382d2SGreg Kroah-Hartman 		 */
1212ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
12134e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12144e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
121598f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
121698f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
1217ab4382d2SGreg Kroah-Hartman 		}
1218ab4382d2SGreg Kroah-Hartman 
1219ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1220ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1221ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1222ab4382d2SGreg Kroah-Hartman 	}
1223ab4382d2SGreg Kroah-Hartman 
122434df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
122534df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
12264e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12274e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
122898f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
122998f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
123034df42f5SElen Song 		}
123134df42f5SElen Song 	}
123234df42f5SElen Song 
1233ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1234ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1235ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1236ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1237ab4382d2SGreg Kroah-Hartman 		/*
1238ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1239ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1240ab4382d2SGreg Kroah-Hartman 		 */
12414e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
12424e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1243ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1244ab4382d2SGreg Kroah-Hartman 	}
1245ab4382d2SGreg Kroah-Hartman }
1246ab4382d2SGreg Kroah-Hartman 
1247ab4382d2SGreg Kroah-Hartman /*
1248ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1249ab4382d2SGreg Kroah-Hartman  */
1250ab4382d2SGreg Kroah-Hartman static void
1251ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1252ab4382d2SGreg Kroah-Hartman {
1253ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1254ab4382d2SGreg Kroah-Hartman 
1255ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1256ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
12574e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
12584e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
125998f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1260ab4382d2SGreg Kroah-Hartman 	}
1261ab4382d2SGreg Kroah-Hartman }
1262ab4382d2SGreg Kroah-Hartman 
1263ab4382d2SGreg Kroah-Hartman /*
1264ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1265ab4382d2SGreg Kroah-Hartman  */
1266ab4382d2SGreg Kroah-Hartman static void
1267ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1268ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1269ab4382d2SGreg Kroah-Hartman {
1270ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12719205218eSNicolas Ferre 	unsigned int status_change;
1272ab4382d2SGreg Kroah-Hartman 
1273ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1274ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
12759205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1276d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
12779205218eSNicolas Ferre 
12789205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
12799205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
12809205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
12819205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
12829205218eSNicolas Ferre 				port->icount.rng++;
12839205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
12849205218eSNicolas Ferre 				port->icount.dsr++;
12859205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
12869205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
12879205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
12889205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
12899205218eSNicolas Ferre 
12909205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
12919205218eSNicolas Ferre 		}
1292ab4382d2SGreg Kroah-Hartman 	}
1293ab4382d2SGreg Kroah-Hartman }
1294ab4382d2SGreg Kroah-Hartman 
1295ab4382d2SGreg Kroah-Hartman /*
1296ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1297ab4382d2SGreg Kroah-Hartman  */
1298ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1299ab4382d2SGreg Kroah-Hartman {
1300ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1301ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
13022c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1303ab4382d2SGreg Kroah-Hartman 
13042c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
13052c7af5baSBoris BREZILLON 
1306ab4382d2SGreg Kroah-Hartman 	do {
1307e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
13084e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
13092c7af5baSBoris BREZILLON 		pending = status & mask;
1310ab4382d2SGreg Kroah-Hartman 		if (!pending)
1311ab4382d2SGreg Kroah-Hartman 			break;
1312ab4382d2SGreg Kroah-Hartman 
13132c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
13142c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
13152c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
13164e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
13172c7af5baSBoris BREZILLON 			pm_system_wakeup();
13182c7af5baSBoris BREZILLON 			break;
13192c7af5baSBoris BREZILLON 		}
13202c7af5baSBoris BREZILLON 
1321ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1322ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1323ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1324ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1325ab4382d2SGreg Kroah-Hartman 
13262c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
13272c7af5baSBoris BREZILLON 
1328ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1329ab4382d2SGreg Kroah-Hartman }
1330ab4382d2SGreg Kroah-Hartman 
1331a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1332a930e528SElen Song {
1333a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1334a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1335a930e528SElen Song 
1336a930e528SElen Song 	dma_unmap_single(port->dev,
1337a930e528SElen Song 			 pdc->dma_addr,
1338a930e528SElen Song 			 pdc->dma_size,
1339a930e528SElen Song 			 DMA_TO_DEVICE);
1340a930e528SElen Song }
1341a930e528SElen Song 
1342ab4382d2SGreg Kroah-Hartman /*
1343ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1344ab4382d2SGreg Kroah-Hartman  */
134564e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1346ab4382d2SGreg Kroah-Hartman {
1347ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1348ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1349ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1350ab4382d2SGreg Kroah-Hartman 	int count;
1351ab4382d2SGreg Kroah-Hartman 
1352ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
13534e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1354ab4382d2SGreg Kroah-Hartman 		return;
1355ab4382d2SGreg Kroah-Hartman 
1356ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1357ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1358ab4382d2SGreg Kroah-Hartman 
1359ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1360ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1361ab4382d2SGreg Kroah-Hartman 
1362ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1363ab4382d2SGreg Kroah-Hartman 
1364ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
13654e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1366ab4382d2SGreg Kroah-Hartman 
1367ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1368ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1369ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1370ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1371ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1372ab4382d2SGreg Kroah-Hartman 
1373ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1374ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1375ab4382d2SGreg Kroah-Hartman 
13764e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13774e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13784e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1379ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13804e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1381ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13824e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13834e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1384ab4382d2SGreg Kroah-Hartman 	} else {
138513bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
138613bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1387ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1388ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1389ab4382d2SGreg Kroah-Hartman 		}
1390ab4382d2SGreg Kroah-Hartman 	}
1391ab4382d2SGreg Kroah-Hartman 
1392ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1393ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1394ab4382d2SGreg Kroah-Hartman }
1395ab4382d2SGreg Kroah-Hartman 
1396a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1397a930e528SElen Song {
1398a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1399a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1400a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1401a930e528SElen Song 
1402a930e528SElen Song 	pdc->buf = xmit->buf;
1403a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1404a930e528SElen Song 					pdc->buf,
1405a930e528SElen Song 					UART_XMIT_SIZE,
1406a930e528SElen Song 					DMA_TO_DEVICE);
1407a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1408a930e528SElen Song 	pdc->ofs = 0;
1409a930e528SElen Song 
1410a930e528SElen Song 	return 0;
1411a930e528SElen Song }
1412a930e528SElen Song 
1413ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1414ab4382d2SGreg Kroah-Hartman {
1415ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1416ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1417ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1418ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1419ab4382d2SGreg Kroah-Hartman 
1420ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1421ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1422ab4382d2SGreg Kroah-Hartman 
1423ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1424ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1425ab4382d2SGreg Kroah-Hartman 
1426ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1427ab4382d2SGreg Kroah-Hartman 
1428ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1429ab4382d2SGreg Kroah-Hartman 
1430ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1431ab4382d2SGreg Kroah-Hartman 		status = c.status;
1432ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1433ab4382d2SGreg Kroah-Hartman 
1434ab4382d2SGreg Kroah-Hartman 		/*
1435ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1436ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1437ab4382d2SGreg Kroah-Hartman 		 */
1438ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1439ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1440ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1441ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1442ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1443ab4382d2SGreg Kroah-Hartman 
1444ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1445ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1446ab4382d2SGreg Kroah-Hartman 					continue;
1447ab4382d2SGreg Kroah-Hartman 			}
1448ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1449ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1450ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1451ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1452ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1453ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1454ab4382d2SGreg Kroah-Hartman 
1455ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1456ab4382d2SGreg Kroah-Hartman 
1457ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1458ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1459ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1460ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1461ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1462ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1463ab4382d2SGreg Kroah-Hartman 		}
1464ab4382d2SGreg Kroah-Hartman 
1465ab4382d2SGreg Kroah-Hartman 
1466ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1467ab4382d2SGreg Kroah-Hartman 			continue;
1468ab4382d2SGreg Kroah-Hartman 
1469ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1470ab4382d2SGreg Kroah-Hartman 	}
1471ab4382d2SGreg Kroah-Hartman 
1472ab4382d2SGreg Kroah-Hartman 	/*
1473ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1474ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1475ab4382d2SGreg Kroah-Hartman 	 */
1476ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14772e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1478ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1479ab4382d2SGreg Kroah-Hartman }
1480ab4382d2SGreg Kroah-Hartman 
1481a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1482a930e528SElen Song {
1483a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1484a930e528SElen Song 	int i;
1485a930e528SElen Song 
1486a930e528SElen Song 	for (i = 0; i < 2; i++) {
1487a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1488a930e528SElen Song 
1489a930e528SElen Song 		dma_unmap_single(port->dev,
1490a930e528SElen Song 				 pdc->dma_addr,
1491a930e528SElen Song 				 pdc->dma_size,
1492a930e528SElen Song 				 DMA_FROM_DEVICE);
1493a930e528SElen Song 		kfree(pdc->buf);
1494a930e528SElen Song 	}
1495a930e528SElen Song }
1496a930e528SElen Song 
149764e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1498ab4382d2SGreg Kroah-Hartman {
1499ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
150005c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1501ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1502ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1503ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1504ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1505ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1506ab4382d2SGreg Kroah-Hartman 
1507ab4382d2SGreg Kroah-Hartman 	do {
1508ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
15094e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1510ab4382d2SGreg Kroah-Hartman 
1511ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
15124e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1513ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1514ab4382d2SGreg Kroah-Hartman 
1515ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1516ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1517ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1518ab4382d2SGreg Kroah-Hartman 		 * find out.
1519ab4382d2SGreg Kroah-Hartman 		 *
1520ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1521ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1522ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1523ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1524ab4382d2SGreg Kroah-Hartman 		 */
1525ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1526ab4382d2SGreg Kroah-Hartman 
1527ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1528ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1529ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1530ab4382d2SGreg Kroah-Hartman 
1531ab4382d2SGreg Kroah-Hartman 			/*
1532ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1533ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1534ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1535ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1536ab4382d2SGreg Kroah-Hartman 			 */
1537ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1538ab4382d2SGreg Kroah-Hartman 
153905c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
154005c7cd39SJiri Slaby 						count);
1541ab4382d2SGreg Kroah-Hartman 
1542ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1543ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1544ab4382d2SGreg Kroah-Hartman 
1545ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1546ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1547ab4382d2SGreg Kroah-Hartman 		}
1548ab4382d2SGreg Kroah-Hartman 
1549ab4382d2SGreg Kroah-Hartman 		/*
1550ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1551ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1552ab4382d2SGreg Kroah-Hartman 		 */
1553ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1554ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
15554e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
15564e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1557ab4382d2SGreg Kroah-Hartman 
1558ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1559ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1560ab4382d2SGreg Kroah-Hartman 		}
1561ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1562ab4382d2SGreg Kroah-Hartman 
1563ab4382d2SGreg Kroah-Hartman 	/*
1564ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1565ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1566ab4382d2SGreg Kroah-Hartman 	 */
1567ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15682e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1569ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1570ab4382d2SGreg Kroah-Hartman 
15714e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
15724e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1573ab4382d2SGreg Kroah-Hartman }
1574ab4382d2SGreg Kroah-Hartman 
1575a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1576a930e528SElen Song {
1577a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1578a930e528SElen Song 	int i;
1579a930e528SElen Song 
1580a930e528SElen Song 	for (i = 0; i < 2; i++) {
1581a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1582a930e528SElen Song 
1583a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1584a930e528SElen Song 		if (pdc->buf == NULL) {
1585a930e528SElen Song 			if (i != 0) {
1586a930e528SElen Song 				dma_unmap_single(port->dev,
1587a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1588a930e528SElen Song 					PDC_BUFFER_SIZE,
1589a930e528SElen Song 					DMA_FROM_DEVICE);
1590a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1591a930e528SElen Song 			}
1592a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1593a930e528SElen Song 			return -ENOMEM;
1594a930e528SElen Song 		}
1595a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1596a930e528SElen Song 						pdc->buf,
1597a930e528SElen Song 						PDC_BUFFER_SIZE,
1598a930e528SElen Song 						DMA_FROM_DEVICE);
1599a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1600a930e528SElen Song 		pdc->ofs = 0;
1601a930e528SElen Song 	}
1602a930e528SElen Song 
1603a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1604a930e528SElen Song 
16054e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
16064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1607a930e528SElen Song 
16084e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
16094e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
16104e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1611a930e528SElen Song 
1612a930e528SElen Song 	return 0;
1613a930e528SElen Song }
1614a930e528SElen Song 
1615ab4382d2SGreg Kroah-Hartman /*
1616ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1617ab4382d2SGreg Kroah-Hartman  */
161800e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data)
1619ab4382d2SGreg Kroah-Hartman {
1620ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1621ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1622ab4382d2SGreg Kroah-Hartman 
1623ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1624ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1625a930e528SElen Song 	atmel_port->schedule_rx(port);
162600e8e658SNicolas Ferre 	spin_unlock(&port->lock);
162700e8e658SNicolas Ferre }
1628ab4382d2SGreg Kroah-Hartman 
162900e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data)
163000e8e658SNicolas Ferre {
163100e8e658SNicolas Ferre 	struct uart_port *port = (struct uart_port *)data;
163200e8e658SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
163300e8e658SNicolas Ferre 
163400e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
163500e8e658SNicolas Ferre 	spin_lock(&port->lock);
163600e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1637ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1638ab4382d2SGreg Kroah-Hartman }
1639ab4382d2SGreg Kroah-Hartman 
16404a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
164133d64c4fSElen Song 				struct platform_device *pdev)
164233d64c4fSElen Song {
164333d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1644574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
164533d64c4fSElen Song 
164633d64c4fSElen Song 	if (np) {
164733d64c4fSElen Song 		/* DMA/PDC usage specification */
1648490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1649490d5ce2SJulia Lawall 			if (of_property_read_bool(np, "dmas")) {
165033d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
165133d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
165233d64c4fSElen Song 			} else {
165333d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
165433d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
165533d64c4fSElen Song 			}
165633d64c4fSElen Song 		} else {
165733d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
165833d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
165933d64c4fSElen Song 		}
166033d64c4fSElen Song 
1661490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1662490d5ce2SJulia Lawall 			if (of_property_read_bool(np, "dmas")) {
166333d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
166433d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
166533d64c4fSElen Song 			} else {
166633d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
166733d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
166833d64c4fSElen Song 			}
166933d64c4fSElen Song 		} else {
167033d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
167133d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
167233d64c4fSElen Song 		}
167333d64c4fSElen Song 
167433d64c4fSElen Song 	} else {
167533d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
167633d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
167733d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
167833d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
167933d64c4fSElen Song 	}
168033d64c4fSElen Song 
168133d64c4fSElen Song }
168233d64c4fSElen Song 
168313bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
168433d64c4fSElen Song 				struct platform_device *pdev)
168533d64c4fSElen Song {
168633d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1687574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
168833d64c4fSElen Song 
168933d64c4fSElen Song 	if (np) {
169077bdec6fSJiri Slaby 		struct serial_rs485 *rs485conf = &port->rs485;
169133d64c4fSElen Song 		u32 rs485_delay[2];
169233d64c4fSElen Song 		/* rs485 properties */
169333d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
169433d64c4fSElen Song 					rs485_delay, 2) == 0) {
169533d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
169633d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
169733d64c4fSElen Song 			rs485conf->flags = 0;
169877bdec6fSJiri Slaby 		}
169933d64c4fSElen Song 
170033d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
170133d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
170233d64c4fSElen Song 
170333d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
170433d64c4fSElen Song 								NULL))
170533d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
170633d64c4fSElen Song 	} else {
170713bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
170833d64c4fSElen Song 	}
170933d64c4fSElen Song 
171033d64c4fSElen Song }
171133d64c4fSElen Song 
1712a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1713a930e528SElen Song {
1714a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1715a930e528SElen Song 
171634df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
171734df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
171834df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
171934df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
172034df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1721a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1722a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1723a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1724a930e528SElen Song 	} else {
1725a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1726a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1727a930e528SElen Song 		atmel_port->release_rx = NULL;
1728a930e528SElen Song 	}
1729a930e528SElen Song 
173008f738beSElen Song 	if (atmel_use_dma_tx(port)) {
173108f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
173208f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
173308f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
173408f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1735a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1736a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1737a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1738a930e528SElen Song 	} else {
1739a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1740a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1741a930e528SElen Song 		atmel_port->release_tx = NULL;
1742a930e528SElen Song 	}
1743a930e528SElen Song }
1744a930e528SElen Song 
1745ab4382d2SGreg Kroah-Hartman /*
1746055560b0SElen Song  * Get ip name usart or uart
1747055560b0SElen Song  */
1748892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1749055560b0SElen Song {
1750055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
17514e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1752731d9caeSNicolas Ferre 	u32 version;
17531d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
17544b769371SNicolas Ferre 	/* ASCII decoding for IP version */
17554b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
17564b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
17571d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1758055560b0SElen Song 
17595bf5635aSLudovic Desroches 	/*
17605bf5635aSLudovic Desroches 	 * Only USART devices from at91sam9260 SOC implement fractional
17615bf5635aSLudovic Desroches 	 * baudrate.
17625bf5635aSLudovic Desroches 	 */
17635bf5635aSLudovic Desroches 	atmel_port->has_frac_baudrate = false;
17644b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
1765055560b0SElen Song 
17662958cceeSLudovic Desroches 	if (name == new_uart) {
17672958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
17684b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
17692958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
17702958cceeSLudovic Desroches 	} else if (name == usart) {
17712958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
17725bf5635aSLudovic Desroches 		atmel_port->has_frac_baudrate = true;
17732958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
17742958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
17754b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
17764b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1777055560b0SElen Song 	} else {
1778731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17794e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1780731d9caeSNicolas Ferre 		switch (version) {
1781731d9caeSNicolas Ferre 		case 0x302:
1782731d9caeSNicolas Ferre 		case 0x10213:
1783731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
17845bf5635aSLudovic Desroches 			atmel_port->has_frac_baudrate = true;
17854b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
17862958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1787731d9caeSNicolas Ferre 			break;
1788731d9caeSNicolas Ferre 		case 0x203:
1789731d9caeSNicolas Ferre 		case 0x10202:
1790731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1791731d9caeSNicolas Ferre 			break;
1792731d9caeSNicolas Ferre 		default:
1793731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1794731d9caeSNicolas Ferre 		}
1795055560b0SElen Song 	}
1796055560b0SElen Song }
1797055560b0SElen Song 
1798055560b0SElen Song /*
1799ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1800ab4382d2SGreg Kroah-Hartman  */
1801ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1802ab4382d2SGreg Kroah-Hartman {
180333d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1804ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1805ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1806ab4382d2SGreg Kroah-Hartman 	int retval;
1807ab4382d2SGreg Kroah-Hartman 
1808ab4382d2SGreg Kroah-Hartman 	/*
1809ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1810ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1811ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1812ab4382d2SGreg Kroah-Hartman 	 */
18134e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1814ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1815ab4382d2SGreg Kroah-Hartman 
1816ab4382d2SGreg Kroah-Hartman 	/*
1817ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1818ab4382d2SGreg Kroah-Hartman 	 */
18192c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
18202c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1821ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1822ab4382d2SGreg Kroah-Hartman 	if (retval) {
1823ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1824ab4382d2SGreg Kroah-Hartman 		return retval;
1825ab4382d2SGreg Kroah-Hartman 	}
1826ab4382d2SGreg Kroah-Hartman 
182798f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
182898f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
182998f2082cSNicolas Ferre 			(unsigned long)port);
183098f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
183198f2082cSNicolas Ferre 			(unsigned long)port);
18321e125786SLeilei Zhao 
1833ab5e4e41SRichard Genoud 	/*
1834ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1835ab4382d2SGreg Kroah-Hartman 	 */
183633d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
18374d9628a1SLeilei Zhao 	atmel_set_ops(port);
183833d64c4fSElen Song 
1839a930e528SElen Song 	if (atmel_port->prepare_rx) {
1840a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1841a930e528SElen Song 		if (retval < 0)
1842a930e528SElen Song 			atmel_set_ops(port);
1843ab4382d2SGreg Kroah-Hartman 	}
1844ab4382d2SGreg Kroah-Hartman 
1845a930e528SElen Song 	if (atmel_port->prepare_tx) {
1846a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1847a930e528SElen Song 		if (retval < 0)
1848a930e528SElen Song 			atmel_set_ops(port);
1849ab4382d2SGreg Kroah-Hartman 	}
1850ab4382d2SGreg Kroah-Hartman 
1851b5199d46SCyrille Pitchen 	/*
1852b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1853b5199d46SCyrille Pitchen 	 */
1854b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1855b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1856b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1857b5199d46SCyrille Pitchen 		unsigned int fmr;
1858b5199d46SCyrille Pitchen 
1859b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1860b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1861b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1862b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1863b5199d46SCyrille Pitchen 
18645f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
18655f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
18665f258b3eSCyrille Pitchen 
1867b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1868b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1869b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1870b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1871b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1872b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1873b5199d46SCyrille Pitchen 
1874b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1875b5199d46SCyrille Pitchen 	}
1876b5199d46SCyrille Pitchen 
1877ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1878e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1879ab4382d2SGreg Kroah-Hartman 
1880ab4382d2SGreg Kroah-Hartman 	/*
1881ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1882ab4382d2SGreg Kroah-Hartman 	 */
18834e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1884ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
18854e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1886ab4382d2SGreg Kroah-Hartman 
18872e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
18882e68c22fSElen Song 			atmel_uart_timer_callback,
18892e68c22fSElen Song 			(unsigned long)port);
18908bc661bfSMarek Roszko 
18918bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18928bc661bfSMarek Roszko 		/* set UART timeout */
18934b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18942e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18952e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18962e68c22fSElen Song 		/* set USART timeout */
18972e68c22fSElen Song 		} else {
18982958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18992958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19004e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1901ab4382d2SGreg Kroah-Hartman 
19024e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19034e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
19042e68c22fSElen Song 		}
1905ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
19064e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
190734df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
19082e68c22fSElen Song 		/* set UART timeout */
19094b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19102e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19112e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19122e68c22fSElen Song 		/* set USART timeout */
19132e68c22fSElen Song 		} else {
19142958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
19152958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19164e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
191734df42f5SElen Song 
19184e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19194e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
19202e68c22fSElen Song 		}
1921ab4382d2SGreg Kroah-Hartman 	} else {
1922ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
19234e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1924ab4382d2SGreg Kroah-Hartman 	}
1925ab4382d2SGreg Kroah-Hartman 
1926ab4382d2SGreg Kroah-Hartman 	return 0;
1927ab4382d2SGreg Kroah-Hartman }
1928ab4382d2SGreg Kroah-Hartman 
1929ab4382d2SGreg Kroah-Hartman /*
1930479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1931479e9b94SPeter Hurley  * buffer is reset.
1932479e9b94SPeter Hurley  */
1933479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1934479e9b94SPeter Hurley {
1935479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1936479e9b94SPeter Hurley 
1937479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
19384e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1939479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1940479e9b94SPeter Hurley 	}
1941479e9b94SPeter Hurley }
1942479e9b94SPeter Hurley 
1943479e9b94SPeter Hurley /*
1944ab4382d2SGreg Kroah-Hartman  * Disable the port
1945ab4382d2SGreg Kroah-Hartman  */
1946ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1947ab4382d2SGreg Kroah-Hartman {
1948ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19490cc7c6c7SMarek Roszko 
19500ae9fdefSRichard Genoud 	/* Disable modem control lines interrupts */
19510ae9fdefSRichard Genoud 	atmel_disable_ms(port);
19520ae9fdefSRichard Genoud 
195398f2082cSNicolas Ferre 	/* Disable interrupts at device level */
195498f2082cSNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
195598f2082cSNicolas Ferre 
195698f2082cSNicolas Ferre 	/* Prevent spurious interrupts from scheduling the tasklet */
195798f2082cSNicolas Ferre 	atomic_inc(&atmel_port->tasklet_shutdown);
195898f2082cSNicolas Ferre 
1959ab4382d2SGreg Kroah-Hartman 	/*
19608bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19618bc661bfSMarek Roszko 	 * cleanup
19628bc661bfSMarek Roszko 	 */
19638bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19648bc661bfSMarek Roszko 
196598f2082cSNicolas Ferre 	/* Make sure that no interrupt is on the fly */
196698f2082cSNicolas Ferre 	synchronize_irq(port->irq);
196798f2082cSNicolas Ferre 
19688bc661bfSMarek Roszko 	/*
19690cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19700cc7c6c7SMarek Roszko 	 * we destroy the buffers
19710cc7c6c7SMarek Roszko 	 */
197200e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
197300e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
19740cc7c6c7SMarek Roszko 
19750cc7c6c7SMarek Roszko 	/*
19760cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
197798f2082cSNicolas Ferre 	 * disable port and break condition.
1978ab4382d2SGreg Kroah-Hartman 	 */
1979ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1980ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1981ab4382d2SGreg Kroah-Hartman 
19824e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
19830cc7c6c7SMarek Roszko 
1984ab4382d2SGreg Kroah-Hartman 	/*
1985ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1986ab4382d2SGreg Kroah-Hartman 	 */
1987a930e528SElen Song 	if (atmel_port->release_rx)
1988a930e528SElen Song 		atmel_port->release_rx(port);
1989a930e528SElen Song 	if (atmel_port->release_tx)
1990a930e528SElen Song 		atmel_port->release_tx(port);
1991ab4382d2SGreg Kroah-Hartman 
1992ab4382d2SGreg Kroah-Hartman 	/*
1993bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1994bb7e73c5SMark Deneen 	 */
1995bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1996bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1997bb7e73c5SMark Deneen 
1998bb7e73c5SMark Deneen 	/*
1999ab5e4e41SRichard Genoud 	 * Free the interrupts
2000ab4382d2SGreg Kroah-Hartman 	 */
2001ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
2002ab5e4e41SRichard Genoud 
2003479e9b94SPeter Hurley 	atmel_flush_buffer(port);
2004ab4382d2SGreg Kroah-Hartman }
2005ab4382d2SGreg Kroah-Hartman 
2006ab4382d2SGreg Kroah-Hartman /*
2007ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
2008ab4382d2SGreg Kroah-Hartman  */
2009ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2010ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
2011ab4382d2SGreg Kroah-Hartman {
2012ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2013ab4382d2SGreg Kroah-Hartman 
2014ab4382d2SGreg Kroah-Hartman 	switch (state) {
2015ab4382d2SGreg Kroah-Hartman 	case 0:
2016ab4382d2SGreg Kroah-Hartman 		/*
2017ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
2018ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
2019ab4382d2SGreg Kroah-Hartman 		 */
202091f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
2021ab4382d2SGreg Kroah-Hartman 
2022ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
20234e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
2024ab4382d2SGreg Kroah-Hartman 		break;
2025ab4382d2SGreg Kroah-Hartman 	case 3:
2026ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
20274e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
20284e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
2029ab4382d2SGreg Kroah-Hartman 
2030ab4382d2SGreg Kroah-Hartman 		/*
2031ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
2032ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
2033ab4382d2SGreg Kroah-Hartman 		 */
203491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2035ab4382d2SGreg Kroah-Hartman 		break;
2036ab4382d2SGreg Kroah-Hartman 	default:
2037ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2038ab4382d2SGreg Kroah-Hartman 	}
2039ab4382d2SGreg Kroah-Hartman }
2040ab4382d2SGreg Kroah-Hartman 
2041ab4382d2SGreg Kroah-Hartman /*
2042ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2043ab4382d2SGreg Kroah-Hartman  */
2044ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2045ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2046ab4382d2SGreg Kroah-Hartman {
20475bf5635aSLudovic Desroches 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2048ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
20495bf5635aSLudovic Desroches 	unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
2050ab4382d2SGreg Kroah-Hartman 
20511cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20524e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20531cf6e8fcSCyrille Pitchen 
20541cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20551cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20561cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2057ab4382d2SGreg Kroah-Hartman 
2058ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2059ab4382d2SGreg Kroah-Hartman 
2060ab4382d2SGreg Kroah-Hartman 	/* byte size */
2061ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2062ab4382d2SGreg Kroah-Hartman 	case CS5:
2063ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2064ab4382d2SGreg Kroah-Hartman 		break;
2065ab4382d2SGreg Kroah-Hartman 	case CS6:
2066ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2067ab4382d2SGreg Kroah-Hartman 		break;
2068ab4382d2SGreg Kroah-Hartman 	case CS7:
2069ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2070ab4382d2SGreg Kroah-Hartman 		break;
2071ab4382d2SGreg Kroah-Hartman 	default:
2072ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2073ab4382d2SGreg Kroah-Hartman 		break;
2074ab4382d2SGreg Kroah-Hartman 	}
2075ab4382d2SGreg Kroah-Hartman 
2076ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2077ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2078ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2079ab4382d2SGreg Kroah-Hartman 
2080ab4382d2SGreg Kroah-Hartman 	/* parity */
2081ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2082ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2083ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2084ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2085ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2086ab4382d2SGreg Kroah-Hartman 			else
2087ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2088ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2089ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2090ab4382d2SGreg Kroah-Hartman 		else
2091ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2092ab4382d2SGreg Kroah-Hartman 	} else
2093ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2094ab4382d2SGreg Kroah-Hartman 
2095ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2096ab4382d2SGreg Kroah-Hartman 
2097ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2098ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2099ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2100ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2101ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2102ab4382d2SGreg Kroah-Hartman 
210364e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2104ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
21054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2106ab4382d2SGreg Kroah-Hartman 
2107ab4382d2SGreg Kroah-Hartman 	/*
2108ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2109ab4382d2SGreg Kroah-Hartman 	 */
2110ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2111ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2112ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2113ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2114ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2115ab4382d2SGreg Kroah-Hartman 		/*
2116ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2117ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2118ab4382d2SGreg Kroah-Hartman 		 */
2119ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2120ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2121ab4382d2SGreg Kroah-Hartman 	}
2122ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2123ab4382d2SGreg Kroah-Hartman 
2124ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2125ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2126ab4382d2SGreg Kroah-Hartman 
2127ab4382d2SGreg Kroah-Hartman 	/*
2128ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2129ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2130ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2131ab4382d2SGreg Kroah-Hartman 	 */
21324e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
21334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2134ab4382d2SGreg Kroah-Hartman 
2135ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
21364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2137ab4382d2SGreg Kroah-Hartman 
21381cf6e8fcSCyrille Pitchen 	/* mode */
213913bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
21404e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
21414e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2142ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
21431cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
21441cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
21459bcffe75SRichard Genoud 		if (atmel_use_fifo(port) &&
21469bcffe75SRichard Genoud 		    !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
21479bcffe75SRichard Genoud 			/*
21489bcffe75SRichard Genoud 			 * with ATMEL_US_USMODE_HWHS set, the controller will
21499bcffe75SRichard Genoud 			 * be able to drive the RTS pin high/low when the RX
21509bcffe75SRichard Genoud 			 * FIFO is above RXFTHRES/below RXFTHRES2.
21519bcffe75SRichard Genoud 			 * It will also disable the transmitter when the CTS
21529bcffe75SRichard Genoud 			 * pin is high.
21539bcffe75SRichard Genoud 			 * This mode is not activated if CTS pin is a GPIO
21549bcffe75SRichard Genoud 			 * because in this case, the transmitter is always
21559bcffe75SRichard Genoud 			 * disabled (there must be an internal pull-up
21569bcffe75SRichard Genoud 			 * responsible for this behaviour).
21579bcffe75SRichard Genoud 			 * If the RTS pin is a GPIO, the controller won't be
21589bcffe75SRichard Genoud 			 * able to drive it according to the FIFO thresholds,
21599bcffe75SRichard Genoud 			 * but it will be handled by the driver.
21609bcffe75SRichard Genoud 			 */
21611cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
21629bcffe75SRichard Genoud 		} else {
21639bcffe75SRichard Genoud 			/*
21649bcffe75SRichard Genoud 			 * For platforms without FIFO, the flow control is
21659bcffe75SRichard Genoud 			 * handled by the driver.
21669bcffe75SRichard Genoud 			 */
21679bcffe75SRichard Genoud 			mode |= ATMEL_US_USMODE_NORMAL;
21685be605acSAlexandre Belloni 		}
21691cf6e8fcSCyrille Pitchen 	} else {
21701cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21711cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2172ab4382d2SGreg Kroah-Hartman 	}
2173ab4382d2SGreg Kroah-Hartman 
21741cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21754e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2176ab4382d2SGreg Kroah-Hartman 
21771cf6e8fcSCyrille Pitchen 	/*
21781cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21791cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21801cf6e8fcSCyrille Pitchen 	 */
21811cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21821cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21831cf6e8fcSCyrille Pitchen 
21841cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21851cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21861cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21871cf6e8fcSCyrille Pitchen 		} else {
21881cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21891cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21901cf6e8fcSCyrille Pitchen 		}
21911cf6e8fcSCyrille Pitchen 
21924e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21931cf6e8fcSCyrille Pitchen 	}
21941cf6e8fcSCyrille Pitchen 
21955bf5635aSLudovic Desroches 	/*
21965bf5635aSLudovic Desroches 	 * Set the baud rate:
21975bf5635aSLudovic Desroches 	 * Fractional baudrate allows to setup output frequency more
21985bf5635aSLudovic Desroches 	 * accurately. This feature is enabled only when using normal mode.
21995bf5635aSLudovic Desroches 	 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
22005bf5635aSLudovic Desroches 	 * Currently, OVER is always set to 0 so we get
220136131cdfSAlexey Starikovskiy 	 * baudrate = selected clock / (16 * (CD + FP / 8))
220236131cdfSAlexey Starikovskiy 	 * then
220336131cdfSAlexey Starikovskiy 	 * 8 CD + FP = selected clock / (2 * baudrate)
22045bf5635aSLudovic Desroches 	 */
22055bf5635aSLudovic Desroches 	if (atmel_port->has_frac_baudrate &&
22065bf5635aSLudovic Desroches 	    (mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_NORMAL) {
220736131cdfSAlexey Starikovskiy 		div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
220836131cdfSAlexey Starikovskiy 		cd = div >> 3;
220936131cdfSAlexey Starikovskiy 		fp = div & ATMEL_US_FP_MASK;
22105bf5635aSLudovic Desroches 	} else {
22115bf5635aSLudovic Desroches 		cd = uart_get_divisor(port, baud);
22125bf5635aSLudovic Desroches 	}
22135bf5635aSLudovic Desroches 
22145bf5635aSLudovic Desroches 	if (cd > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
22155bf5635aSLudovic Desroches 		cd /= 8;
22165bf5635aSLudovic Desroches 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
22175bf5635aSLudovic Desroches 	}
22185bf5635aSLudovic Desroches 	quot = cd | fp << ATMEL_US_FP_OFFSET;
22195bf5635aSLudovic Desroches 
22204e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
22214e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
22224e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2223ab4382d2SGreg Kroah-Hartman 
2224ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
22254e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2226ab4382d2SGreg Kroah-Hartman 
2227ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2228ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
222935b675b9SRichard Genoud 		atmel_enable_ms(port);
223035b675b9SRichard Genoud 	else
223135b675b9SRichard Genoud 		atmel_disable_ms(port);
2232ab4382d2SGreg Kroah-Hartman 
2233ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2234ab4382d2SGreg Kroah-Hartman }
2235ab4382d2SGreg Kroah-Hartman 
2236732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
223742bd7a4fSViktar Palstsiuk {
2238732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
223942bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2240d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
224142bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2242d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
224342bd7a4fSViktar Palstsiuk 	} else {
224442bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2245cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2246cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2247cab68f89SPeter Hurley 			atmel_disable_ms(port);
2248cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2249cab68f89SPeter Hurley 		}
225042bd7a4fSViktar Palstsiuk 	}
225142bd7a4fSViktar Palstsiuk }
225242bd7a4fSViktar Palstsiuk 
2253ab4382d2SGreg Kroah-Hartman /*
2254ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2255ab4382d2SGreg Kroah-Hartman  */
2256ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2257ab4382d2SGreg Kroah-Hartman {
2258ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2259ab4382d2SGreg Kroah-Hartman }
2260ab4382d2SGreg Kroah-Hartman 
2261ab4382d2SGreg Kroah-Hartman /*
2262ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2263ab4382d2SGreg Kroah-Hartman  */
2264ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2265ab4382d2SGreg Kroah-Hartman {
2266ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2267ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2268ab4382d2SGreg Kroah-Hartman 
2269ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2270ab4382d2SGreg Kroah-Hartman 
2271ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2272ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2273ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2274ab4382d2SGreg Kroah-Hartman 	}
2275ab4382d2SGreg Kroah-Hartman }
2276ab4382d2SGreg Kroah-Hartman 
2277ab4382d2SGreg Kroah-Hartman /*
2278ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2279ab4382d2SGreg Kroah-Hartman  */
2280ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2281ab4382d2SGreg Kroah-Hartman {
2282ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2283ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2284ab4382d2SGreg Kroah-Hartman 
2285ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2286ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2287ab4382d2SGreg Kroah-Hartman 
2288ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2289ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2290ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2291ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2292ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2293ab4382d2SGreg Kroah-Hartman 		}
2294ab4382d2SGreg Kroah-Hartman 	}
2295ab4382d2SGreg Kroah-Hartman 
2296ab4382d2SGreg Kroah-Hartman 	return 0;
2297ab4382d2SGreg Kroah-Hartman }
2298ab4382d2SGreg Kroah-Hartman 
2299ab4382d2SGreg Kroah-Hartman /*
2300ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2301ab4382d2SGreg Kroah-Hartman  */
2302ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2303ab4382d2SGreg Kroah-Hartman {
2304ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2305ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2306ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2307ab4382d2SGreg Kroah-Hartman 	}
2308ab4382d2SGreg Kroah-Hartman }
2309ab4382d2SGreg Kroah-Hartman 
2310ab4382d2SGreg Kroah-Hartman /*
2311ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2312ab4382d2SGreg Kroah-Hartman  */
2313ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2314ab4382d2SGreg Kroah-Hartman {
2315ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2316ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2317ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2318ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2319ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2320ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2321ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2322ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2323ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2324270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2325ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2326ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2327ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2328ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2329ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2330ab4382d2SGreg Kroah-Hartman 	return ret;
2331ab4382d2SGreg Kroah-Hartman }
2332ab4382d2SGreg Kroah-Hartman 
2333ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2334ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2335ab4382d2SGreg Kroah-Hartman {
23364e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2337ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2338ab4382d2SGreg Kroah-Hartman 
2339a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2340ab4382d2SGreg Kroah-Hartman }
2341ab4382d2SGreg Kroah-Hartman 
2342ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2343ab4382d2SGreg Kroah-Hartman {
23444e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2345ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2346ab4382d2SGreg Kroah-Hartman 
2347a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2348ab4382d2SGreg Kroah-Hartman }
2349ab4382d2SGreg Kroah-Hartman #endif
2350ab4382d2SGreg Kroah-Hartman 
23515c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = {
2352ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2353ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2354ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2355ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2356ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2357ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2358ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2359ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2360ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2361ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2362ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2363ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
236442bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2365ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2366ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2367ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2368ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2369ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2370ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2371ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2372ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2373ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2374ab4382d2SGreg Kroah-Hartman #endif
2375ab4382d2SGreg Kroah-Hartman };
2376ab4382d2SGreg Kroah-Hartman 
2377ab4382d2SGreg Kroah-Hartman /*
2378ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2379ab4382d2SGreg Kroah-Hartman  */
238091f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2381ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2382ab4382d2SGreg Kroah-Hartman {
238391f8c2d8SBoris BREZILLON 	int ret;
2384ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2385574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2386ab4382d2SGreg Kroah-Hartman 
23874a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2388a930e528SElen Song 	atmel_set_ops(port);
2389a930e528SElen Song 
239013bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
239133d64c4fSElen Song 
2392ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2393ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2394ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2395ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2396ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2397ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2398ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
239913bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2400ab4382d2SGreg Kroah-Hartman 
2401ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2402ab4382d2SGreg Kroah-Hartman 
24035fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2404ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
24051acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2406588edbf3SNicolas Ferre 	} else {
2407ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2408ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2409ab4382d2SGreg Kroah-Hartman 	}
2410ab4382d2SGreg Kroah-Hartman 
2411ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2412ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2413ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
241491f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
241591f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
241691f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
241791f8c2d8SBoris BREZILLON 			return ret;
241891f8c2d8SBoris BREZILLON 		}
241991f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
242091f8c2d8SBoris BREZILLON 		if (ret) {
242191f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
242291f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
242391f8c2d8SBoris BREZILLON 			return ret;
242491f8c2d8SBoris BREZILLON 		}
2425ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
242691f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2427ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2428ab4382d2SGreg Kroah-Hartman 	}
2429ab4382d2SGreg Kroah-Hartman 
2430ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
243113bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2432ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
243364e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2434ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2435ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2436ab4382d2SGreg Kroah-Hartman 	} else {
2437ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2438ab4382d2SGreg Kroah-Hartman 	}
243991f8c2d8SBoris BREZILLON 
244091f8c2d8SBoris BREZILLON 	return 0;
2441ab4382d2SGreg Kroah-Hartman }
2442ab4382d2SGreg Kroah-Hartman 
244369f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
244469f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2445ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2446ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2447ab4382d2SGreg Kroah-Hartman {
24484e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2449ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2450a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2451ab4382d2SGreg Kroah-Hartman }
2452ab4382d2SGreg Kroah-Hartman 
2453ab4382d2SGreg Kroah-Hartman /*
2454ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2455ab4382d2SGreg Kroah-Hartman  */
2456ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2457ab4382d2SGreg Kroah-Hartman {
2458ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2459ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2460ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2461ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2462ab4382d2SGreg Kroah-Hartman 
2463ab4382d2SGreg Kroah-Hartman 	/*
2464ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2465ab4382d2SGreg Kroah-Hartman 	 */
24664e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
24674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
24684e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2469ab4382d2SGreg Kroah-Hartman 
2470ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
24714e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
24724e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2473ab4382d2SGreg Kroah-Hartman 
2474ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2475ab4382d2SGreg Kroah-Hartman 
2476ab4382d2SGreg Kroah-Hartman 	/*
2477ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2478ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2479ab4382d2SGreg Kroah-Hartman 	 */
2480ab4382d2SGreg Kroah-Hartman 	do {
24814e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2482ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2483ab4382d2SGreg Kroah-Hartman 
2484ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2485ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24864e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2487ab4382d2SGreg Kroah-Hartman 
2488ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24894e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2490ab4382d2SGreg Kroah-Hartman }
2491ab4382d2SGreg Kroah-Hartman 
2492ab4382d2SGreg Kroah-Hartman /*
2493ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2494ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2495ab4382d2SGreg Kroah-Hartman  */
2496ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2497ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2498ab4382d2SGreg Kroah-Hartman {
2499ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2500ab4382d2SGreg Kroah-Hartman 
2501ab4382d2SGreg Kroah-Hartman 	/*
2502ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2503ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2504ab4382d2SGreg Kroah-Hartman 	 */
25054e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2506ab4382d2SGreg Kroah-Hartman 	if (!quot)
2507ab4382d2SGreg Kroah-Hartman 		return;
2508ab4382d2SGreg Kroah-Hartman 
25094e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2510ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2511ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2512ab4382d2SGreg Kroah-Hartman 	else
2513ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2514ab4382d2SGreg Kroah-Hartman 
25154e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2516ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2517ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2518ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2519ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2520ab4382d2SGreg Kroah-Hartman 
2521ab4382d2SGreg Kroah-Hartman 	/*
2522ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2523ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2524ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2525ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2526ab4382d2SGreg Kroah-Hartman 	 */
2527ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2528ab4382d2SGreg Kroah-Hartman }
2529ab4382d2SGreg Kroah-Hartman 
2530ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2531ab4382d2SGreg Kroah-Hartman {
253291f8c2d8SBoris BREZILLON 	int ret;
2533ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2534ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2535ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2536ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2537ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2538ab4382d2SGreg Kroah-Hartman 
2539ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2540ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2541ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2542ab4382d2SGreg Kroah-Hartman 	}
2543ab4382d2SGreg Kroah-Hartman 
254491f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
254591f8c2d8SBoris BREZILLON 	if (ret)
254691f8c2d8SBoris BREZILLON 		return ret;
2547ab4382d2SGreg Kroah-Hartman 
25484e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
25494e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
25504e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2551ab4382d2SGreg Kroah-Hartman 
2552ab4382d2SGreg Kroah-Hartman 	if (options)
2553ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2554ab4382d2SGreg Kroah-Hartman 	else
2555ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2556ab4382d2SGreg Kroah-Hartman 
2557ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2558ab4382d2SGreg Kroah-Hartman }
2559ab4382d2SGreg Kroah-Hartman 
2560ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2561ab4382d2SGreg Kroah-Hartman 
2562ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2563ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2564ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2565ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2566ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2567ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2568ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2569ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2570ab4382d2SGreg Kroah-Hartman };
2571ab4382d2SGreg Kroah-Hartman 
2572ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2573ab4382d2SGreg Kroah-Hartman 
2574ab4382d2SGreg Kroah-Hartman /*
2575ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2576ab4382d2SGreg Kroah-Hartman  */
2577ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2578ab4382d2SGreg Kroah-Hartman {
257991f8c2d8SBoris BREZILLON 	int ret;
2580ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
25810d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2582574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2583efb8d21bSLinus Torvalds 		int id = pdata->num;
2584b78cd169SJaeden Amero 		struct atmel_uart_port *atmel_port = &atmel_ports[id];
25850d0a3cc1SVoss, Nikolaus 
2586b78cd169SJaeden Amero 		atmel_port->backup_imr = 0;
2587b78cd169SJaeden Amero 		atmel_port->uart.line = id;
25884cbf9f48SNicolas Ferre 
25894cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
2590b78cd169SJaeden Amero 		ret = atmel_init_port(atmel_port, atmel_default_console_device);
259191f8c2d8SBoris BREZILLON 		if (ret)
259291f8c2d8SBoris BREZILLON 			return ret;
2593ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2594ab4382d2SGreg Kroah-Hartman 	}
2595ab4382d2SGreg Kroah-Hartman 
2596ab4382d2SGreg Kroah-Hartman 	return 0;
2597ab4382d2SGreg Kroah-Hartman }
2598ab4382d2SGreg Kroah-Hartman 
2599ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2600ab4382d2SGreg Kroah-Hartman 
2601ab4382d2SGreg Kroah-Hartman /*
2602ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2603ab4382d2SGreg Kroah-Hartman  */
2604ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2605ab4382d2SGreg Kroah-Hartman {
2606ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2607ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2608ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2609ab4382d2SGreg Kroah-Hartman 
2610ab4382d2SGreg Kroah-Hartman 	return 0;
2611ab4382d2SGreg Kroah-Hartman }
2612ab4382d2SGreg Kroah-Hartman 
2613ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2614ab4382d2SGreg Kroah-Hartman 
2615ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2616ab4382d2SGreg Kroah-Hartman {
2617ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2618ab4382d2SGreg Kroah-Hartman }
2619ab4382d2SGreg Kroah-Hartman 
2620ab4382d2SGreg Kroah-Hartman #else
2621ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2622ab4382d2SGreg Kroah-Hartman 
2623ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2624ab4382d2SGreg Kroah-Hartman {
2625ab4382d2SGreg Kroah-Hartman 	return false;
2626ab4382d2SGreg Kroah-Hartman }
2627ab4382d2SGreg Kroah-Hartman #endif
2628ab4382d2SGreg Kroah-Hartman 
2629ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2630ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2631ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2632ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2633ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2634ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2635ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2636ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2637ab4382d2SGreg Kroah-Hartman };
2638ab4382d2SGreg Kroah-Hartman 
2639ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2640ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2641ab4382d2SGreg Kroah-Hartman {
2642ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2643ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2644ab4382d2SGreg Kroah-Hartman #else
2645ab4382d2SGreg Kroah-Hartman 	return false;
2646ab4382d2SGreg Kroah-Hartman #endif
2647ab4382d2SGreg Kroah-Hartman }
2648ab4382d2SGreg Kroah-Hartman 
2649ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2650ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2651ab4382d2SGreg Kroah-Hartman {
2652ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2653ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2654ab4382d2SGreg Kroah-Hartman 
2655ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2656ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
26574e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
26584e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2659ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2660ab4382d2SGreg Kroah-Hartman 	}
2661ab4382d2SGreg Kroah-Hartman 
2662ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2663ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
26642c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
26652c7af5baSBoris BREZILLON 		unsigned long flags;
26662c7af5baSBoris BREZILLON 
26672c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26682c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
26692c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2670ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
26712c7af5baSBoris BREZILLON 	}
2672ab4382d2SGreg Kroah-Hartman 
2673ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2674ab4382d2SGreg Kroah-Hartman 
2675ab4382d2SGreg Kroah-Hartman 	return 0;
2676ab4382d2SGreg Kroah-Hartman }
2677ab4382d2SGreg Kroah-Hartman 
2678ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2679ab4382d2SGreg Kroah-Hartman {
2680ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2681ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26822c7af5baSBoris BREZILLON 	unsigned long flags;
26832c7af5baSBoris BREZILLON 
26842c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26852c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26862c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26872c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26882c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26892c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26902c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26912c7af5baSBoris BREZILLON 	}
26922c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26932c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2694ab4382d2SGreg Kroah-Hartman 
2695ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2696ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2697ab4382d2SGreg Kroah-Hartman 
2698ab4382d2SGreg Kroah-Hartman 	return 0;
2699ab4382d2SGreg Kroah-Hartman }
2700ab4382d2SGreg Kroah-Hartman #else
2701ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2702ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2703ab4382d2SGreg Kroah-Hartman #endif
2704ab4382d2SGreg Kroah-Hartman 
2705b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2706b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2707b5199d46SCyrille Pitchen {
2708b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2709b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2710b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2711b5199d46SCyrille Pitchen 
2712b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2713b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2714b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2715b5199d46SCyrille Pitchen 		return;
2716b5199d46SCyrille Pitchen 
2717b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2718b5199d46SCyrille Pitchen 		return;
2719b5199d46SCyrille Pitchen 
2720b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2721b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2722b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2723b5199d46SCyrille Pitchen 		return;
2724b5199d46SCyrille Pitchen 	}
2725b5199d46SCyrille Pitchen 
2726b5199d46SCyrille Pitchen 	/*
2727b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2728b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2729b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2730b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2731b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2732b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2733b5199d46SCyrille Pitchen 	 */
2734b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2735b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2736b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2737b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2738b5199d46SCyrille Pitchen 
2739b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2740b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2741b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2742b78cd169SJaeden Amero 		atmel_port->rts_high);
2743b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2744b78cd169SJaeden Amero 		atmel_port->rts_low);
2745b5199d46SCyrille Pitchen }
2746b5199d46SCyrille Pitchen 
27479671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2748ab4382d2SGreg Kroah-Hartman {
2749b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
27505fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2751574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2752ab4382d2SGreg Kroah-Hartman 	void *data;
27534cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2754bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2755ab4382d2SGreg Kroah-Hartman 
2756ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2757ab4382d2SGreg Kroah-Hartman 
27585fbe46b6SNicolas Ferre 	if (np)
27595fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
27605fbe46b6SNicolas Ferre 	else
27614cbf9f48SNicolas Ferre 		if (pdata)
27624cbf9f48SNicolas Ferre 			ret = pdata->num;
27634cbf9f48SNicolas Ferre 
27644cbf9f48SNicolas Ferre 	if (ret < 0)
27655fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
27664cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2767503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
27684cbf9f48SNicolas Ferre 
2769503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
27704cbf9f48SNicolas Ferre 		ret = -ENODEV;
27714cbf9f48SNicolas Ferre 		goto err;
27724cbf9f48SNicolas Ferre 	}
27734cbf9f48SNicolas Ferre 
2774503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
27754cbf9f48SNicolas Ferre 		/* port already in use */
27764cbf9f48SNicolas Ferre 		ret = -EBUSY;
27774cbf9f48SNicolas Ferre 		goto err;
27784cbf9f48SNicolas Ferre 	}
27794cbf9f48SNicolas Ferre 
2780b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2781b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2782b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2783b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2784354e57f3SLinus Walleij 
278598f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
2786b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
27872c7af5baSBoris BREZILLON 
2788b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
278991f8c2d8SBoris BREZILLON 	if (ret)
27906fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2791ab4382d2SGreg Kroah-Hartman 
2792b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2793b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2794b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
279518dfef9cSUwe Kleine-König 		goto err_clear_bit;
279618dfef9cSUwe Kleine-König 	}
279718dfef9cSUwe Kleine-König 
2798b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2799ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2800ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2801ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2802ab4382d2SGreg Kroah-Hartman 		if (!data)
2803ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2804b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2805ab4382d2SGreg Kroah-Hartman 	}
2806ab4382d2SGreg Kroah-Hartman 
2807b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2808bd737f87SRicardo Ribalda Delgado 
2809b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2810ab4382d2SGreg Kroah-Hartman 	if (ret)
2811ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2812ab4382d2SGreg Kroah-Hartman 
2813ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2814b78cd169SJaeden Amero 	if (atmel_is_console_port(&atmel_port->uart)
2815ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2816ab4382d2SGreg Kroah-Hartman 		/*
2817ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
281891f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2819ab4382d2SGreg Kroah-Hartman 		 */
2820b78cd169SJaeden Amero 		clk_disable_unprepare(atmel_port->clk);
2821ab4382d2SGreg Kroah-Hartman 	}
2822ab4382d2SGreg Kroah-Hartman #endif
2823ab4382d2SGreg Kroah-Hartman 
2824ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2825b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2826ab4382d2SGreg Kroah-Hartman 
2827d4f64187SCyrille Pitchen 	/*
2828d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2829d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2830d4f64187SCyrille Pitchen 	 */
2831b78cd169SJaeden Amero 	clk_prepare_enable(atmel_port->clk);
2832d4f64187SCyrille Pitchen 
2833bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2834b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
28354e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2836b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2837b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2838fc887b15SLinus Torvalds 	}
2839fc887b15SLinus Torvalds 
2840055560b0SElen Song 	/*
2841055560b0SElen Song 	 * Get port name of usart or uart
2842055560b0SElen Song 	 */
2843b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2844055560b0SElen Song 
2845d4f64187SCyrille Pitchen 	/*
2846d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2847d4f64187SCyrille Pitchen 	 * is used
2848d4f64187SCyrille Pitchen 	 */
2849b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2850d4f64187SCyrille Pitchen 
2851ab4382d2SGreg Kroah-Hartman 	return 0;
2852ab4382d2SGreg Kroah-Hartman 
2853ab4382d2SGreg Kroah-Hartman err_add_port:
2854b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2855b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
2856ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2857b78cd169SJaeden Amero 	if (!atmel_is_console_port(&atmel_port->uart)) {
2858b78cd169SJaeden Amero 		clk_put(atmel_port->clk);
2859b78cd169SJaeden Amero 		atmel_port->clk = NULL;
2860ab4382d2SGreg Kroah-Hartman 	}
28616fbb9bdfSCyrille Pitchen err_clear_bit:
2862b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
28634cbf9f48SNicolas Ferre err:
2864ab4382d2SGreg Kroah-Hartman 	return ret;
2865ab4382d2SGreg Kroah-Hartman }
2866ab4382d2SGreg Kroah-Hartman 
2867f4a8ab04SRomain Izard /*
2868f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2869f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2870f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2871f4a8ab04SRomain Izard  *
2872f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
2873f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
2874f4a8ab04SRomain Izard  * port in the normal case.
2875f4a8ab04SRomain Izard  */
2876f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
2877f4a8ab04SRomain Izard {
2878f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
2879f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2880f4a8ab04SRomain Izard 	int ret = 0;
2881f4a8ab04SRomain Izard 
288200e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
288300e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
2884f4a8ab04SRomain Izard 
2885f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
2886f4a8ab04SRomain Izard 
2887f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
2888f4a8ab04SRomain Izard 
2889f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
2890f4a8ab04SRomain Izard 
2891f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
2892f4a8ab04SRomain Izard 
2893f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
2894f4a8ab04SRomain Izard 
2895f4a8ab04SRomain Izard 	clk_put(atmel_port->clk);
2896f4a8ab04SRomain Izard 	atmel_port->clk = NULL;
2897f4a8ab04SRomain Izard 
2898f4a8ab04SRomain Izard 	return ret;
2899f4a8ab04SRomain Izard }
2900f4a8ab04SRomain Izard 
2901ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2902ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
2903f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
2904ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2905ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2906ab4382d2SGreg Kroah-Hartman 	.driver		= {
2907ab4382d2SGreg Kroah-Hartman 		.name			= "atmel_usart",
29085fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
2909ab4382d2SGreg Kroah-Hartman 	},
2910ab4382d2SGreg Kroah-Hartman };
2911ab4382d2SGreg Kroah-Hartman 
2912ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2913ab4382d2SGreg Kroah-Hartman {
2914ab4382d2SGreg Kroah-Hartman 	int ret;
2915ab4382d2SGreg Kroah-Hartman 
2916ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2917ab4382d2SGreg Kroah-Hartman 	if (ret)
2918ab4382d2SGreg Kroah-Hartman 		return ret;
2919ab4382d2SGreg Kroah-Hartman 
2920ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2921ab4382d2SGreg Kroah-Hartman 	if (ret)
2922ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2923ab4382d2SGreg Kroah-Hartman 
2924ab4382d2SGreg Kroah-Hartman 	return ret;
2925ab4382d2SGreg Kroah-Hartman }
2926c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
2927