xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision c24d25317a7c6bb3053d4c193b3cf57d1e9a3e4b)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2ab4382d2SGreg Kroah-Hartman /*
372ce5732SAndy Shevchenko  *  Driver for Atmel AT91 Serial ports
4ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
5ab4382d2SGreg Kroah-Hartman  *
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
7ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8ab4382d2SGreg Kroah-Hartman  *
9ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
10ab4382d2SGreg Kroah-Hartman  */
11ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
12ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
13ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
14ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
15ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
16ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
17ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
18ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
19ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
20ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
215fbe46b6SNicolas Ferre #include <linux/of.h>
225fbe46b6SNicolas Ferre #include <linux/of_device.h>
23354e57f3SLinus Walleij #include <linux/of_gpio.h>
24ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
256b997babSVinod Koul #include <linux/dmaengine.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
28bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
292e68c22fSElen Song #include <linux/timer.h>
30354e57f3SLinus Walleij #include <linux/gpio.h>
31e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
32e0b0baadSRichard Genoud #include <linux/err.h>
33ab5e4e41SRichard Genoud #include <linux/irq.h>
342c7af5baSBoris BREZILLON #include <linux/suspend.h>
352b5cf14bSGeliang Tang #include <linux/mm.h>
36ab4382d2SGreg Kroah-Hartman 
37ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
38ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
39ab4382d2SGreg Kroah-Hartman 
40ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
41ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
42ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
43ab4382d2SGreg Kroah-Hartman 
44b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
45b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
46b5199d46SCyrille Pitchen /*
47b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
48b5199d46SCyrille Pitchen  * high and low thresholds
49b5199d46SCyrille Pitchen  */
50b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
51b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
52b5199d46SCyrille Pitchen 
53ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
55ab4382d2SGreg Kroah-Hartman #endif
56ab4382d2SGreg Kroah-Hartman 
57ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
58ab4382d2SGreg Kroah-Hartman 
59e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
608961df89SRichard Genoud #include "atmel_serial.h"
61e0b0baadSRichard Genoud 
62ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
63ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
64ab4382d2SGreg Kroah-Hartman 
65ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
66ab4382d2SGreg Kroah-Hartman 
67ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
68ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
69ab4382d2SGreg Kroah-Hartman  * UART. */
70ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
71ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
72ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
73ab4382d2SGreg Kroah-Hartman 
74ab4382d2SGreg Kroah-Hartman #else
75ab4382d2SGreg Kroah-Hartman 
76ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
77ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
78ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
79ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
80ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
81ab4382d2SGreg Kroah-Hartman 
82ab4382d2SGreg Kroah-Hartman #endif
83ab4382d2SGreg Kroah-Hartman 
84ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
85ab4382d2SGreg Kroah-Hartman 
86ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
87ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
88ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
89ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
90ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
91ab4382d2SGreg Kroah-Hartman };
92ab4382d2SGreg Kroah-Hartman 
93ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
94ab4382d2SGreg Kroah-Hartman 	u16		status;
95ab4382d2SGreg Kroah-Hartman 	u16		ch;
96ab4382d2SGreg Kroah-Hartman };
97ab4382d2SGreg Kroah-Hartman 
98637ba54fSLudovic Desroches /*
99637ba54fSLudovic Desroches  * Be careful, the real size of the ring buffer is
100637ba54fSLudovic Desroches  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
101637ba54fSLudovic Desroches  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
102637ba54fSLudovic Desroches  * DMA mode.
103637ba54fSLudovic Desroches  */
104ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
105ab4382d2SGreg Kroah-Hartman 
106ab4382d2SGreg Kroah-Hartman /*
1079af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
108432f9748SAlexandre Belloni  * samx7: 3 USARTs and 5 UARTs
1099af92fbfSAlexandre Belloni  */
110432f9748SAlexandre Belloni #define ATMEL_MAX_UART		8
1119af92fbfSAlexandre Belloni 
1129af92fbfSAlexandre Belloni /*
113ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
114ab4382d2SGreg Kroah-Hartman  */
115ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
116ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
117ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
118ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
119ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
120ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
121ab4382d2SGreg Kroah-Hartman 
12234df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
12364e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
124ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
125ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
126ab4382d2SGreg Kroah-Hartman 
12708f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
12864e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
129ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
130ab4382d2SGreg Kroah-Hartman 
13108f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
13234df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
13308f738beSElen Song 	struct dma_chan			*chan_tx;
13434df42f5SElen Song 	struct dma_chan			*chan_rx;
13508f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
13634df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
13708f738beSElen Song 	dma_cookie_t			cookie_tx;
13834df42f5SElen Song 	dma_cookie_t			cookie_rx;
13908f738beSElen Song 	struct scatterlist		sg_tx;
14034df42f5SElen Song 	struct scatterlist		sg_rx;
14100e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_rx;
14200e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_tx;
14398f2082cSNicolas Ferre 	atomic_t		tasklet_shutdown;
144ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
1455f258b3eSCyrille Pitchen 	unsigned int		tx_len;
146ab4382d2SGreg Kroah-Hartman 
147ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
148ab4382d2SGreg Kroah-Hartman 
149e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
150ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
151b5199d46SCyrille Pitchen 	u32			fifo_size;
152b5199d46SCyrille Pitchen 	u32			rts_high;
153b5199d46SCyrille Pitchen 	u32			rts_low;
154ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1552958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
1565bf5635aSLudovic Desroches 	bool			has_frac_baudrate;
1574b769371SNicolas Ferre 	bool			has_hw_timer;
1584b769371SNicolas Ferre 	struct timer_list	uart_timer;
1592c7af5baSBoris BREZILLON 
160ea04f82aSRomain Izard 	bool			tx_stopped;
1612c7af5baSBoris BREZILLON 	bool			suspended;
1622c7af5baSBoris BREZILLON 	unsigned int		pending;
1632c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1642c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1652c7af5baSBoris BREZILLON 
166488ae82dSAlexandre Belloni #ifdef CONFIG_PM
1676a5f0e2fSAlexandre Belloni 	struct {
1686a5f0e2fSAlexandre Belloni 		u32		cr;
1696a5f0e2fSAlexandre Belloni 		u32		mr;
1706a5f0e2fSAlexandre Belloni 		u32		imr;
1716a5f0e2fSAlexandre Belloni 		u32		brgr;
1726a5f0e2fSAlexandre Belloni 		u32		rtor;
1736a5f0e2fSAlexandre Belloni 		u32		ttgr;
1746a5f0e2fSAlexandre Belloni 		u32		fmr;
1756a5f0e2fSAlexandre Belloni 		u32		fimr;
1766a5f0e2fSAlexandre Belloni 	} cache;
177488ae82dSAlexandre Belloni #endif
1786a5f0e2fSAlexandre Belloni 
179a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
180a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
181a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
182a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
183a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
184a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
185ab4382d2SGreg Kroah-Hartman };
186ab4382d2SGreg Kroah-Hartman 
187ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
188503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
189ab4382d2SGreg Kroah-Hartman 
190ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
191ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
192ab4382d2SGreg Kroah-Hartman #endif
193ab4382d2SGreg Kroah-Hartman 
1945fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1955fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
196*c24d2531SRadu Pirea 	{ .compatible = "atmel,at91rm9200-usart-serial" },
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 static inline u8 atmel_uart_read_char(struct uart_port *port)
218a6499435SCyrille Pitchen {
219a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
220a6499435SCyrille Pitchen }
221a6499435SCyrille Pitchen 
222a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
223a6499435SCyrille Pitchen {
224a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
225a6499435SCyrille Pitchen }
226a6499435SCyrille Pitchen 
227ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
22864e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
229ab4382d2SGreg Kroah-Hartman {
230ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
231ab4382d2SGreg Kroah-Hartman 
23264e22ebeSElen Song 	return atmel_port->use_pdc_rx;
233ab4382d2SGreg Kroah-Hartman }
234ab4382d2SGreg Kroah-Hartman 
23564e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
236ab4382d2SGreg Kroah-Hartman {
237ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
238ab4382d2SGreg Kroah-Hartman 
23964e22ebeSElen Song 	return atmel_port->use_pdc_tx;
240ab4382d2SGreg Kroah-Hartman }
241ab4382d2SGreg Kroah-Hartman #else
24264e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
243ab4382d2SGreg Kroah-Hartman {
244ab4382d2SGreg Kroah-Hartman 	return false;
245ab4382d2SGreg Kroah-Hartman }
246ab4382d2SGreg Kroah-Hartman 
24764e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
248ab4382d2SGreg Kroah-Hartman {
249ab4382d2SGreg Kroah-Hartman 	return false;
250ab4382d2SGreg Kroah-Hartman }
251ab4382d2SGreg Kroah-Hartman #endif
252ab4382d2SGreg Kroah-Hartman 
25308f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
25408f738beSElen Song {
25508f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
25608f738beSElen Song 
25708f738beSElen Song 	return atmel_port->use_dma_tx;
25808f738beSElen Song }
25908f738beSElen Song 
26034df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
26134df42f5SElen Song {
26234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26334df42f5SElen Song 
26434df42f5SElen Song 	return atmel_port->use_dma_rx;
26534df42f5SElen Song }
26634df42f5SElen Song 
2675be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2685be605acSAlexandre Belloni {
2695be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2705be605acSAlexandre Belloni 
2715be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2725be605acSAlexandre Belloni }
2735be605acSAlexandre Belloni 
27498f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
27598f2082cSNicolas Ferre 				   struct tasklet_struct *t)
27698f2082cSNicolas Ferre {
27798f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown))
27898f2082cSNicolas Ferre 		tasklet_schedule(t);
27998f2082cSNicolas Ferre }
28098f2082cSNicolas Ferre 
281e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
282e0b0baadSRichard Genoud {
283e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
284e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
285e0b0baadSRichard Genoud 
2864e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
287e0b0baadSRichard Genoud 
288e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
289e0b0baadSRichard Genoud 
290e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
291e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
292e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
293e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
294e0b0baadSRichard Genoud 		else
295e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
296e0b0baadSRichard Genoud 	}
297e0b0baadSRichard Genoud 
298e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
299e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
300e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
301e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
302e0b0baadSRichard Genoud 		else
303e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
304e0b0baadSRichard Genoud 	}
305e0b0baadSRichard Genoud 
306e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
307e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
308e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
309e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
310e0b0baadSRichard Genoud 		else
311e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
312e0b0baadSRichard Genoud 	}
313e0b0baadSRichard Genoud 
314e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
315e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
316e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
317e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
318e0b0baadSRichard Genoud 		else
319e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
320e0b0baadSRichard Genoud 	}
321e0b0baadSRichard Genoud 
322e0b0baadSRichard Genoud 	return status;
323e0b0baadSRichard Genoud }
324e0b0baadSRichard Genoud 
325ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
32613bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
32713bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
328ab4382d2SGreg Kroah-Hartman {
329ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
330ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
331ab4382d2SGreg Kroah-Hartman 
332ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
334ab4382d2SGreg Kroah-Hartman 
3354e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
336ab4382d2SGreg Kroah-Hartman 
337ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
338ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
339ab4382d2SGreg Kroah-Hartman 
34013bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
341ab4382d2SGreg Kroah-Hartman 
342ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
343ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
344ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3454e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3464e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
347ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
348ab4382d2SGreg Kroah-Hartman 	} else {
349ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
35064e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
351ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
352ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
353ab4382d2SGreg Kroah-Hartman 		else
354ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
355ab4382d2SGreg Kroah-Hartman 	}
3564e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
357ab4382d2SGreg Kroah-Hartman 
358ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3594e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
360ab4382d2SGreg Kroah-Hartman 
36113bd3e6fSRicardo Ribalda Delgado 	return 0;
362ab4382d2SGreg Kroah-Hartman }
363ab4382d2SGreg Kroah-Hartman 
364ab4382d2SGreg Kroah-Hartman /*
365ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
366ab4382d2SGreg Kroah-Hartman  */
367ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
368ab4382d2SGreg Kroah-Hartman {
369ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
370ea04f82aSRomain Izard 
371ea04f82aSRomain Izard 	if (atmel_port->tx_stopped)
372ea04f82aSRomain Izard 		return TIOCSER_TEMT;
3734e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3744e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3754e7decdaSCyrille Pitchen 		0;
376ab4382d2SGreg Kroah-Hartman }
377ab4382d2SGreg Kroah-Hartman 
378ab4382d2SGreg Kroah-Hartman /*
379ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
380ab4382d2SGreg Kroah-Hartman  */
381ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
382ab4382d2SGreg Kroah-Hartman {
383ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3844e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3851cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
386ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
387ab4382d2SGreg Kroah-Hartman 
3881cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3891cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
3904e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3914e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
3921cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3931cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3941cf6e8fcSCyrille Pitchen 	}
3951cf6e8fcSCyrille Pitchen 
3961cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
3971cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
3981cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3991cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4001cf6e8fcSCyrille Pitchen 
4011cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4021cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4031cf6e8fcSCyrille Pitchen 	} else {
4041cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4051cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4061cf6e8fcSCyrille Pitchen 
4071cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4081cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4091cf6e8fcSCyrille Pitchen 	}
4101cf6e8fcSCyrille Pitchen 
411ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4121cf6e8fcSCyrille Pitchen 		control |= rts_ready;
413ab4382d2SGreg Kroah-Hartman 	else
4141cf6e8fcSCyrille Pitchen 		control |= rts_paused;
415ab4382d2SGreg Kroah-Hartman 
416ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
417ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
418ab4382d2SGreg Kroah-Hartman 	else
419ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
420ab4382d2SGreg Kroah-Hartman 
4214e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
422ab4382d2SGreg Kroah-Hartman 
423e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
424e0b0baadSRichard Genoud 
425ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
4261cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
427ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
428ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
429ab4382d2SGreg Kroah-Hartman 	else
430ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
431ab4382d2SGreg Kroah-Hartman 
4324e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
433ab4382d2SGreg Kroah-Hartman }
434ab4382d2SGreg Kroah-Hartman 
435ab4382d2SGreg Kroah-Hartman /*
436ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
437ab4382d2SGreg Kroah-Hartman  */
438ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
439ab4382d2SGreg Kroah-Hartman {
440e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
441e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
442ab4382d2SGreg Kroah-Hartman 
4434e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
444ab4382d2SGreg Kroah-Hartman 
445ab4382d2SGreg Kroah-Hartman 	/*
446ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
447ab4382d2SGreg Kroah-Hartman 	 */
448ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
449ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
450ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
451ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
452ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
453ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
454ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
455ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
456ab4382d2SGreg Kroah-Hartman 
457e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
458ab4382d2SGreg Kroah-Hartman }
459ab4382d2SGreg Kroah-Hartman 
460ab4382d2SGreg Kroah-Hartman /*
461ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
462ab4382d2SGreg Kroah-Hartman  */
463ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
464ab4382d2SGreg Kroah-Hartman {
465ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
466ab4382d2SGreg Kroah-Hartman 
46764e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
468ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4694e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
470ab4382d2SGreg Kroah-Hartman 	}
47189d82324SRichard Genoud 
47289d82324SRichard Genoud 	/*
47389d82324SRichard Genoud 	 * Disable the transmitter.
47489d82324SRichard Genoud 	 * This is mandatory when DMA is used, otherwise the DMA buffer
47589d82324SRichard Genoud 	 * is fully transmitted.
47689d82324SRichard Genoud 	 */
47789d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
478ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
47989d82324SRichard Genoud 
480ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4814e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
482ab4382d2SGreg Kroah-Hartman 
48313bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
48413bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
485ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
486ab4382d2SGreg Kroah-Hartman }
487ab4382d2SGreg Kroah-Hartman 
488ab4382d2SGreg Kroah-Hartman /*
489ab4382d2SGreg Kroah-Hartman  * Start transmitting.
490ab4382d2SGreg Kroah-Hartman  */
491ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
492ab4382d2SGreg Kroah-Hartman {
493ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
494ab4382d2SGreg Kroah-Hartman 
4950058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
4960058f087SAlexandre Belloni 				       & ATMEL_PDC_TXTEN))
497ab4382d2SGreg Kroah-Hartman 		/* The transmitter is already running.  Yes, we
498ab4382d2SGreg Kroah-Hartman 		   really need this.*/
499ab4382d2SGreg Kroah-Hartman 		return;
500ab4382d2SGreg Kroah-Hartman 
5010058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
50213bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
50313bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
504ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
505ab4382d2SGreg Kroah-Hartman 
5060058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port))
507ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
5084e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
5090058f087SAlexandre Belloni 
510ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
5114e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
51289d82324SRichard Genoud 
51389d82324SRichard Genoud 	/* re-enable the transmitter */
51489d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
515ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
516ab4382d2SGreg Kroah-Hartman }
517ab4382d2SGreg Kroah-Hartman 
518ab4382d2SGreg Kroah-Hartman /*
519ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
520ab4382d2SGreg Kroah-Hartman  */
521ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
522ab4382d2SGreg Kroah-Hartman {
5234e7decdaSCyrille Pitchen 	/* reset status and receiver */
5244e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
525ab4382d2SGreg Kroah-Hartman 
5264e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
52757c36868SSiftar, Gabe 
52864e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
529ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
5304e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
5314e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
532ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
5334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
534ab4382d2SGreg Kroah-Hartman 	} else {
5354e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
536ab4382d2SGreg Kroah-Hartman 	}
537ab4382d2SGreg Kroah-Hartman }
538ab4382d2SGreg Kroah-Hartman 
539ab4382d2SGreg Kroah-Hartman /*
540ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
541ab4382d2SGreg Kroah-Hartman  */
542ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
543ab4382d2SGreg Kroah-Hartman {
5444e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
54557c36868SSiftar, Gabe 
54664e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
547ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5494e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5504e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
551ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
552ab4382d2SGreg Kroah-Hartman 	} else {
5534e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
554ab4382d2SGreg Kroah-Hartman 	}
555ab4382d2SGreg Kroah-Hartman }
556ab4382d2SGreg Kroah-Hartman 
557ab4382d2SGreg Kroah-Hartman /*
558ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
559ab4382d2SGreg Kroah-Hartman  */
560ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
561ab4382d2SGreg Kroah-Hartman {
562ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
563ab5e4e41SRichard Genoud 	uint32_t ier = 0;
564ab5e4e41SRichard Genoud 
565ab5e4e41SRichard Genoud 	/*
566ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
567ab5e4e41SRichard Genoud 	 */
568ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
569ab5e4e41SRichard Genoud 		return;
570ab5e4e41SRichard Genoud 
571ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
572ab5e4e41SRichard Genoud 
57318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
574ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
575ab5e4e41SRichard Genoud 
57618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
577ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
578ab5e4e41SRichard Genoud 
57918dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
580ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
581ab5e4e41SRichard Genoud 
58218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
583ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
584ab5e4e41SRichard Genoud 
5854e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
58618dfef9cSUwe Kleine-König 
58718dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
588ab4382d2SGreg Kroah-Hartman }
589ab4382d2SGreg Kroah-Hartman 
590ab4382d2SGreg Kroah-Hartman /*
59135b675b9SRichard Genoud  * Disable modem status interrupts
59235b675b9SRichard Genoud  */
59335b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
59435b675b9SRichard Genoud {
59535b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
59635b675b9SRichard Genoud 	uint32_t idr = 0;
59735b675b9SRichard Genoud 
59835b675b9SRichard Genoud 	/*
59935b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
60035b675b9SRichard Genoud 	 */
60135b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
60235b675b9SRichard Genoud 		return;
60335b675b9SRichard Genoud 
60435b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
60535b675b9SRichard Genoud 
60618dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
60718dfef9cSUwe Kleine-König 
60818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
60935b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
61035b675b9SRichard Genoud 
61118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
61235b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
61335b675b9SRichard Genoud 
61418dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
61535b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
61635b675b9SRichard Genoud 
61718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
61835b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
61935b675b9SRichard Genoud 
6204e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
62135b675b9SRichard Genoud }
62235b675b9SRichard Genoud 
62335b675b9SRichard Genoud /*
624ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
625ab4382d2SGreg Kroah-Hartman  */
626ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
627ab4382d2SGreg Kroah-Hartman {
628ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
6294e7decdaSCyrille Pitchen 		/* start break */
6304e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
631ab4382d2SGreg Kroah-Hartman 	else
6324e7decdaSCyrille Pitchen 		/* stop break */
6334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
634ab4382d2SGreg Kroah-Hartman }
635ab4382d2SGreg Kroah-Hartman 
636ab4382d2SGreg Kroah-Hartman /*
637ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
638ab4382d2SGreg Kroah-Hartman  */
639ab4382d2SGreg Kroah-Hartman static void
640ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
641ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
642ab4382d2SGreg Kroah-Hartman {
643ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
644ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
645ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
646ab4382d2SGreg Kroah-Hartman 
647ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
648ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
649ab4382d2SGreg Kroah-Hartman 		return;
650ab4382d2SGreg Kroah-Hartman 
651ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
652ab4382d2SGreg Kroah-Hartman 	c->status	= status;
653ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
654ab4382d2SGreg Kroah-Hartman 
655ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
656ab4382d2SGreg Kroah-Hartman 	smp_wmb();
657ab4382d2SGreg Kroah-Hartman 
658ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
659ab4382d2SGreg Kroah-Hartman }
660ab4382d2SGreg Kroah-Hartman 
661ab4382d2SGreg Kroah-Hartman /*
662ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
663ab4382d2SGreg Kroah-Hartman  */
664ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
665ab4382d2SGreg Kroah-Hartman {
666ab4382d2SGreg Kroah-Hartman 	/* clear error */
6674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
668ab4382d2SGreg Kroah-Hartman 
669ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
670ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
671ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
672ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
673ab4382d2SGreg Kroah-Hartman 	}
674ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
675ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
676ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
677ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
678ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
679ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
680ab4382d2SGreg Kroah-Hartman }
681ab4382d2SGreg Kroah-Hartman 
682ab4382d2SGreg Kroah-Hartman /*
683ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
684ab4382d2SGreg Kroah-Hartman  */
685ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
686ab4382d2SGreg Kroah-Hartman {
687ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
688ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
689ab4382d2SGreg Kroah-Hartman 
6904e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
691ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
692a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
693ab4382d2SGreg Kroah-Hartman 
694ab4382d2SGreg Kroah-Hartman 		/*
695ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
696ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
697ab4382d2SGreg Kroah-Hartman 		 */
698ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
699ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
700ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
701ab4382d2SGreg Kroah-Hartman 
702ab4382d2SGreg Kroah-Hartman 			/* clear error */
7034e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
704ab4382d2SGreg Kroah-Hartman 
705ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
706ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
707ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
7084e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
7094e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
710ab4382d2SGreg Kroah-Hartman 			} else {
711ab4382d2SGreg Kroah-Hartman 				/*
712ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
713ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
714ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
715ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
716ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
717ab4382d2SGreg Kroah-Hartman 				 */
7184e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
7194e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
720ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
721ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
722ab4382d2SGreg Kroah-Hartman 			}
723ab4382d2SGreg Kroah-Hartman 		}
724ab4382d2SGreg Kroah-Hartman 
725ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
7264e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
727ab4382d2SGreg Kroah-Hartman 	}
728ab4382d2SGreg Kroah-Hartman 
72998f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
730ab4382d2SGreg Kroah-Hartman }
731ab4382d2SGreg Kroah-Hartman 
732ab4382d2SGreg Kroah-Hartman /*
733ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
734ab4382d2SGreg Kroah-Hartman  * disabled)
735ab4382d2SGreg Kroah-Hartman  */
736ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
737ab4382d2SGreg Kroah-Hartman {
738ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
739ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
740ab4382d2SGreg Kroah-Hartman 
7414e7decdaSCyrille Pitchen 	if (port->x_char &&
7424e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
743a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
744ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
745ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
746ab4382d2SGreg Kroah-Hartman 	}
747ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
748ab4382d2SGreg Kroah-Hartman 		return;
749ab4382d2SGreg Kroah-Hartman 
7504e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7514e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
752a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
753ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
754ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
755ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
756ab4382d2SGreg Kroah-Hartman 			break;
757ab4382d2SGreg Kroah-Hartman 	}
758ab4382d2SGreg Kroah-Hartman 
759ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
760ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
761ab4382d2SGreg Kroah-Hartman 
762ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
763ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7644e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7654e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
766ab4382d2SGreg Kroah-Hartman }
767ab4382d2SGreg Kroah-Hartman 
76808f738beSElen Song static void atmel_complete_tx_dma(void *arg)
76908f738beSElen Song {
77008f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
77108f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
77208f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
77308f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
77408f738beSElen Song 	unsigned long flags;
77508f738beSElen Song 
77608f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
77708f738beSElen Song 
77808f738beSElen Song 	if (chan)
77908f738beSElen Song 		dmaengine_terminate_all(chan);
7805f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
78108f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
78208f738beSElen Song 
7835f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
78408f738beSElen Song 
78508f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
78608f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
78708f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
78808f738beSElen Song 	atmel_port->desc_tx = NULL;
78908f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
79008f738beSElen Song 
79108f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
79208f738beSElen Song 		uart_write_wakeup(port);
79308f738beSElen Song 
7941842dc2eSCyrille Pitchen 	/*
7951842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7961842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7971842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7981842dc2eSCyrille Pitchen 	 */
79908f738beSElen Song 	if (!uart_circ_empty(xmit))
80098f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
801b389f173SRichard Genoud 	else if ((port->rs485.flags & SER_RS485_ENABLED) &&
802b389f173SRichard Genoud 		 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
803b389f173SRichard Genoud 		/* DMA done, stop TX, start RX for RS485 */
804b389f173SRichard Genoud 		atmel_start_rx(port);
805b389f173SRichard Genoud 	}
80608f738beSElen Song 
80708f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
80808f738beSElen Song }
80908f738beSElen Song 
81008f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
81108f738beSElen Song {
81208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
81308f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
81408f738beSElen Song 
81508f738beSElen Song 	if (chan) {
81608f738beSElen Song 		dmaengine_terminate_all(chan);
81708f738beSElen Song 		dma_release_channel(chan);
81808f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
81948479148SWolfram Sang 				DMA_TO_DEVICE);
82008f738beSElen Song 	}
82108f738beSElen Song 
82208f738beSElen Song 	atmel_port->desc_tx = NULL;
82308f738beSElen Song 	atmel_port->chan_tx = NULL;
82408f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
82508f738beSElen Song }
82608f738beSElen Song 
82708f738beSElen Song /*
82808f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
82908f738beSElen Song  */
83008f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
83108f738beSElen Song {
83208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
83308f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
83408f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
83508f738beSElen Song 	struct dma_async_tx_descriptor *desc;
8365f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
8375f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
8385f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
83908f738beSElen Song 
84008f738beSElen Song 	/* Make sure we have an idle channel */
84108f738beSElen Song 	if (atmel_port->desc_tx != NULL)
84208f738beSElen Song 		return;
84308f738beSElen Song 
84408f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
84508f738beSElen Song 		/*
84608f738beSElen Song 		 * DMA is idle now.
84708f738beSElen Song 		 * Port xmit buffer is already mapped,
84808f738beSElen Song 		 * and it is one page... Just adjust
84908f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
85008f738beSElen Song 		 * we have to transmit till the end, and then the rest.
85108f738beSElen Song 		 * Take the port lock to get a
85208f738beSElen Song 		 * consistent xmit buffer state.
85308f738beSElen Song 		 */
8545f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
85508f738beSElen Song 					 xmit->tail,
85608f738beSElen Song 					 UART_XMIT_SIZE);
8575f258b3eSCyrille Pitchen 
8585f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
8595f258b3eSCyrille Pitchen 			/* multi data mode */
8605f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
8615f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
8625f258b3eSCyrille Pitchen 		} else {
8635f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
8645f258b3eSCyrille Pitchen 			part1_len = 0;
8655f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
8665f258b3eSCyrille Pitchen 		}
8675f258b3eSCyrille Pitchen 
8685f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
8695f258b3eSCyrille Pitchen 		sg_len = 0;
8705f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
8715f258b3eSCyrille Pitchen 		if (part1_len) {
8725f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8735f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8745f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
8755f258b3eSCyrille Pitchen 
8765f258b3eSCyrille Pitchen 			phys_addr += part1_len;
8775f258b3eSCyrille Pitchen 		}
8785f258b3eSCyrille Pitchen 
8795f258b3eSCyrille Pitchen 		if (part2_len) {
8805f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8815f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8825f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
8835f258b3eSCyrille Pitchen 		}
8845f258b3eSCyrille Pitchen 
8855f258b3eSCyrille Pitchen 		/*
8865f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
8875f258b3eSCyrille Pitchen 		 * xmit->tail correctly
8885f258b3eSCyrille Pitchen 		 */
8895f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
89008f738beSElen Song 
89108f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
8925f258b3eSCyrille Pitchen 					       sgl,
8935f258b3eSCyrille Pitchen 					       sg_len,
89408f738beSElen Song 					       DMA_MEM_TO_DEV,
89508f738beSElen Song 					       DMA_PREP_INTERRUPT |
89608f738beSElen Song 					       DMA_CTRL_ACK);
89708f738beSElen Song 		if (!desc) {
89808f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
89908f738beSElen Song 			return;
90008f738beSElen Song 		}
90108f738beSElen Song 
9025f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
90308f738beSElen Song 
90408f738beSElen Song 		atmel_port->desc_tx = desc;
90508f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
90608f738beSElen Song 		desc->callback_param = atmel_port;
90708f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
90808f738beSElen Song 	}
90908f738beSElen Song 
91008f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
91108f738beSElen Song 		uart_write_wakeup(port);
91208f738beSElen Song }
91308f738beSElen Song 
91408f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
91508f738beSElen Song {
91608f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
917*c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
91808f738beSElen Song 	dma_cap_mask_t		mask;
91908f738beSElen Song 	struct dma_slave_config config;
92008f738beSElen Song 	int ret, nent;
92108f738beSElen Song 
92208f738beSElen Song 	dma_cap_zero(mask);
92308f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
92408f738beSElen Song 
925*c24d2531SRadu Pirea 	atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx");
92608f738beSElen Song 	if (atmel_port->chan_tx == NULL)
92708f738beSElen Song 		goto chan_err;
92808f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
92908f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
93008f738beSElen Song 
93108f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
93208f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
93308f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
9342c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
93508f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
93608f738beSElen Song 			virt_to_page(port->state->xmit.buf),
93708f738beSElen Song 			UART_XMIT_SIZE,
9382b5cf14bSGeliang Tang 			offset_in_page(port->state->xmit.buf));
93908f738beSElen Song 	nent = dma_map_sg(port->dev,
94008f738beSElen Song 				&atmel_port->sg_tx,
94108f738beSElen Song 				1,
94248479148SWolfram Sang 				DMA_TO_DEVICE);
94308f738beSElen Song 
94408f738beSElen Song 	if (!nent) {
94508f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
94608f738beSElen Song 		goto chan_err;
94708f738beSElen Song 	} else {
948c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
94908f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
95008f738beSElen Song 			port->state->xmit.buf,
951c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
95208f738beSElen Song 	}
95308f738beSElen Song 
95408f738beSElen Song 	/* Configure the slave DMA */
95508f738beSElen Song 	memset(&config, 0, sizeof(config));
95608f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
9575f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
9585f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
9595f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
96008f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
961a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
96208f738beSElen Song 
9635483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9645483c10eSMaxime Ripard 				     &config);
96508f738beSElen Song 	if (ret) {
96608f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
96708f738beSElen Song 		goto chan_err;
96808f738beSElen Song 	}
96908f738beSElen Song 
97008f738beSElen Song 	return 0;
97108f738beSElen Song 
97208f738beSElen Song chan_err:
97308f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
97408f738beSElen Song 	atmel_port->use_dma_tx = 0;
97508f738beSElen Song 	if (atmel_port->chan_tx)
97608f738beSElen Song 		atmel_release_tx_dma(port);
97708f738beSElen Song 	return -EINVAL;
97808f738beSElen Song }
97908f738beSElen Song 
98034df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
98134df42f5SElen Song {
98234df42f5SElen Song 	struct uart_port *port = arg;
98334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
98434df42f5SElen Song 
98598f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
98634df42f5SElen Song }
98734df42f5SElen Song 
98834df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
98934df42f5SElen Song {
99034df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
99134df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
99234df42f5SElen Song 
99334df42f5SElen Song 	if (chan) {
99434df42f5SElen Song 		dmaengine_terminate_all(chan);
99534df42f5SElen Song 		dma_release_channel(chan);
99634df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
99748479148SWolfram Sang 				DMA_FROM_DEVICE);
99834df42f5SElen Song 	}
99934df42f5SElen Song 
100034df42f5SElen Song 	atmel_port->desc_rx = NULL;
100134df42f5SElen Song 	atmel_port->chan_rx = NULL;
100234df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
100334df42f5SElen Song }
100434df42f5SElen Song 
100534df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
100634df42f5SElen Song {
100734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
100866f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
100934df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
101034df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
101134df42f5SElen Song 	struct dma_tx_state state;
101234df42f5SElen Song 	enum dma_status dmastat;
101366f37aafSCyrille Pitchen 	size_t count;
101434df42f5SElen Song 
101534df42f5SElen Song 
101634df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
10174e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
101834df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
101934df42f5SElen Song 				atmel_port->cookie_rx,
102034df42f5SElen Song 				&state);
102134df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
102234df42f5SElen Song 	if (dmastat == DMA_ERROR) {
102334df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
10244e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
102598f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
102634df42f5SElen Song 		return;
102734df42f5SElen Song 	}
102866f37aafSCyrille Pitchen 
102966f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
103066f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
103166f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
103266f37aafSCyrille Pitchen 			    1,
1033485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
103434df42f5SElen Song 
103534df42f5SElen Song 	/*
103666f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
103766f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
103866f37aafSCyrille Pitchen 	 * framework.
103966f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
104066f37aafSCyrille Pitchen 	 * length.
104134df42f5SElen Song 	 */
104266f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
104366f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
104466f37aafSCyrille Pitchen 	/*
104566f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
104666f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
104766f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
104866f37aafSCyrille Pitchen 	 *
104966f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
105066f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
105166f37aafSCyrille Pitchen 	 *
105266f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
105366f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
105466f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
105566f37aafSCyrille Pitchen 	 */
105666f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
105766f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
105834df42f5SElen Song 
105966f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
106066f37aafSCyrille Pitchen 		ring->tail = 0;
106134df42f5SElen Song 		port->icount.rx += count;
106234df42f5SElen Song 	}
106334df42f5SElen Song 
106466f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
106566f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
106666f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
106766f37aafSCyrille Pitchen 
106866f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
106966f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
107066f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
107166f37aafSCyrille Pitchen 			ring->head = 0;
107266f37aafSCyrille Pitchen 		ring->tail = ring->head;
107366f37aafSCyrille Pitchen 		port->icount.rx += count;
107466f37aafSCyrille Pitchen 	}
107566f37aafSCyrille Pitchen 
107666f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
107766f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
107866f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
107966f37aafSCyrille Pitchen 			       1,
1080485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
108166f37aafSCyrille Pitchen 
108266f37aafSCyrille Pitchen 	/*
108366f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
108466f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
108566f37aafSCyrille Pitchen 	 */
108666f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
108766f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
108866f37aafSCyrille Pitchen 	spin_lock(&port->lock);
108966f37aafSCyrille Pitchen 
10904e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
109134df42f5SElen Song }
109234df42f5SElen Song 
109334df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
109434df42f5SElen Song {
109534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1096*c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
109734df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
109834df42f5SElen Song 	dma_cap_mask_t		mask;
109934df42f5SElen Song 	struct dma_slave_config config;
110034df42f5SElen Song 	struct circ_buf		*ring;
110134df42f5SElen Song 	int ret, nent;
110234df42f5SElen Song 
110334df42f5SElen Song 	ring = &atmel_port->rx_ring;
110434df42f5SElen Song 
110534df42f5SElen Song 	dma_cap_zero(mask);
110634df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
110734df42f5SElen Song 
1108*c24d2531SRadu Pirea 	atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx");
110934df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
111034df42f5SElen Song 		goto chan_err;
111134df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
111234df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
111334df42f5SElen Song 
111434df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
111534df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
111634df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
11172c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
111834df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
111934df42f5SElen Song 		    virt_to_page(ring->buf),
1120a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
11212b5cf14bSGeliang Tang 		    offset_in_page(ring->buf));
112234df42f5SElen Song 	nent = dma_map_sg(port->dev,
112334df42f5SElen Song 			  &atmel_port->sg_rx,
112434df42f5SElen Song 			  1,
112548479148SWolfram Sang 			  DMA_FROM_DEVICE);
112634df42f5SElen Song 
112734df42f5SElen Song 	if (!nent) {
112834df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
112934df42f5SElen Song 		goto chan_err;
113034df42f5SElen Song 	} else {
1131c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
113234df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
113334df42f5SElen Song 			ring->buf,
1134c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
113534df42f5SElen Song 	}
113634df42f5SElen Song 
113734df42f5SElen Song 	/* Configure the slave DMA */
113834df42f5SElen Song 	memset(&config, 0, sizeof(config));
113934df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
114034df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
114134df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1142a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
114334df42f5SElen Song 
11445483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
11455483c10eSMaxime Ripard 				     &config);
114634df42f5SElen Song 	if (ret) {
114734df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
114834df42f5SElen Song 		goto chan_err;
114934df42f5SElen Song 	}
115034df42f5SElen Song 	/*
115134df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
115234df42f5SElen Song 	 * each one is half ring buffer size
115334df42f5SElen Song 	 */
115434df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
115534df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
115634df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
115734df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
115834df42f5SElen Song 					 DMA_DEV_TO_MEM,
115934df42f5SElen Song 					 DMA_PREP_INTERRUPT);
116034df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
116134df42f5SElen Song 	desc->callback_param = port;
116234df42f5SElen Song 	atmel_port->desc_rx = desc;
116334df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
116434df42f5SElen Song 
116534df42f5SElen Song 	return 0;
116634df42f5SElen Song 
116734df42f5SElen Song chan_err:
116834df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
116934df42f5SElen Song 	atmel_port->use_dma_rx = 0;
117034df42f5SElen Song 	if (atmel_port->chan_rx)
117134df42f5SElen Song 		atmel_release_rx_dma(port);
117234df42f5SElen Song 	return -EINVAL;
117334df42f5SElen Song }
117434df42f5SElen Song 
1175026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t)
11762e68c22fSElen Song {
1177026cb432SKees Cook 	struct atmel_uart_port *atmel_port = from_timer(atmel_port, t,
1178026cb432SKees Cook 							uart_timer);
1179026cb432SKees Cook 	struct uart_port *port = &atmel_port->uart;
11802e68c22fSElen Song 
118198f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown)) {
118200e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
118398f2082cSNicolas Ferre 		mod_timer(&atmel_port->uart_timer,
118498f2082cSNicolas Ferre 			  jiffies + uart_poll_timeout(port));
118598f2082cSNicolas Ferre 	}
11862e68c22fSElen Song }
11872e68c22fSElen Song 
1188ab4382d2SGreg Kroah-Hartman /*
1189ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1190ab4382d2SGreg Kroah-Hartman  */
1191ab4382d2SGreg Kroah-Hartman static void
1192ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1193ab4382d2SGreg Kroah-Hartman {
1194ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1195ab4382d2SGreg Kroah-Hartman 
119664e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1197ab4382d2SGreg Kroah-Hartman 		/*
1198ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1199ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1200ab4382d2SGreg Kroah-Hartman 		 *
1201ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1202ab4382d2SGreg Kroah-Hartman 		 * the moment.
1203ab4382d2SGreg Kroah-Hartman 		 */
1204ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
12054e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12064e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
120798f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
120898f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
1209ab4382d2SGreg Kroah-Hartman 		}
1210ab4382d2SGreg Kroah-Hartman 
1211ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1212ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1213ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1214ab4382d2SGreg Kroah-Hartman 	}
1215ab4382d2SGreg Kroah-Hartman 
121634df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
121734df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
12184e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12194e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
122098f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
122198f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
122234df42f5SElen Song 		}
122334df42f5SElen Song 	}
122434df42f5SElen Song 
1225ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1226ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1227ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1228ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1229ab4382d2SGreg Kroah-Hartman 		/*
1230ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1231ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1232ab4382d2SGreg Kroah-Hartman 		 */
12334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
12344e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1235ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1236ab4382d2SGreg Kroah-Hartman 	}
1237ab4382d2SGreg Kroah-Hartman }
1238ab4382d2SGreg Kroah-Hartman 
1239ab4382d2SGreg Kroah-Hartman /*
1240ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1241ab4382d2SGreg Kroah-Hartman  */
1242ab4382d2SGreg Kroah-Hartman static void
1243ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1244ab4382d2SGreg Kroah-Hartman {
1245ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1246ab4382d2SGreg Kroah-Hartman 
1247ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1248ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
12494e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
12504e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
125198f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1252ab4382d2SGreg Kroah-Hartman 	}
1253ab4382d2SGreg Kroah-Hartman }
1254ab4382d2SGreg Kroah-Hartman 
1255ab4382d2SGreg Kroah-Hartman /*
1256ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1257ab4382d2SGreg Kroah-Hartman  */
1258ab4382d2SGreg Kroah-Hartman static void
1259ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1260ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1261ab4382d2SGreg Kroah-Hartman {
1262ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12639205218eSNicolas Ferre 	unsigned int status_change;
1264ab4382d2SGreg Kroah-Hartman 
1265ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1266ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
12679205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1268d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
12699205218eSNicolas Ferre 
12709205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
12719205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
12729205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
12739205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
12749205218eSNicolas Ferre 				port->icount.rng++;
12759205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
12769205218eSNicolas Ferre 				port->icount.dsr++;
12779205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
12789205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
12799205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
12809205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
12819205218eSNicolas Ferre 
12829205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
12839205218eSNicolas Ferre 		}
1284ab4382d2SGreg Kroah-Hartman 	}
1285ab4382d2SGreg Kroah-Hartman }
1286ab4382d2SGreg Kroah-Hartman 
1287ab4382d2SGreg Kroah-Hartman /*
1288ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1289ab4382d2SGreg Kroah-Hartman  */
1290ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1291ab4382d2SGreg Kroah-Hartman {
1292ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1293ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12942c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1295ab4382d2SGreg Kroah-Hartman 
12962c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
12972c7af5baSBoris BREZILLON 
1298ab4382d2SGreg Kroah-Hartman 	do {
1299e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
13004e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
13012c7af5baSBoris BREZILLON 		pending = status & mask;
1302ab4382d2SGreg Kroah-Hartman 		if (!pending)
1303ab4382d2SGreg Kroah-Hartman 			break;
1304ab4382d2SGreg Kroah-Hartman 
13052c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
13062c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
13072c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
13084e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
13092c7af5baSBoris BREZILLON 			pm_system_wakeup();
13102c7af5baSBoris BREZILLON 			break;
13112c7af5baSBoris BREZILLON 		}
13122c7af5baSBoris BREZILLON 
1313ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1314ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1315ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1316ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1317ab4382d2SGreg Kroah-Hartman 
13182c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
13192c7af5baSBoris BREZILLON 
1320ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1321ab4382d2SGreg Kroah-Hartman }
1322ab4382d2SGreg Kroah-Hartman 
1323a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1324a930e528SElen Song {
1325a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1326a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1327a930e528SElen Song 
1328a930e528SElen Song 	dma_unmap_single(port->dev,
1329a930e528SElen Song 			 pdc->dma_addr,
1330a930e528SElen Song 			 pdc->dma_size,
1331a930e528SElen Song 			 DMA_TO_DEVICE);
1332a930e528SElen Song }
1333a930e528SElen Song 
1334ab4382d2SGreg Kroah-Hartman /*
1335ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1336ab4382d2SGreg Kroah-Hartman  */
133764e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1338ab4382d2SGreg Kroah-Hartman {
1339ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1340ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1341ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1342ab4382d2SGreg Kroah-Hartman 	int count;
1343ab4382d2SGreg Kroah-Hartman 
1344ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
13454e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1346ab4382d2SGreg Kroah-Hartman 		return;
1347ab4382d2SGreg Kroah-Hartman 
1348ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1349ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1350ab4382d2SGreg Kroah-Hartman 
1351ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1352ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1353ab4382d2SGreg Kroah-Hartman 
1354ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1355ab4382d2SGreg Kroah-Hartman 
1356ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
13574e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1358ab4382d2SGreg Kroah-Hartman 
1359ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1360ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1361ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1362ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1363ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1364ab4382d2SGreg Kroah-Hartman 
1365ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1366ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1367ab4382d2SGreg Kroah-Hartman 
13684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13694e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13704e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1371ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13724e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1373ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13744e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13754e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1376ab4382d2SGreg Kroah-Hartman 	} else {
137713bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
137813bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1379ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1380ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1381ab4382d2SGreg Kroah-Hartman 		}
1382ab4382d2SGreg Kroah-Hartman 	}
1383ab4382d2SGreg Kroah-Hartman 
1384ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1385ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1386ab4382d2SGreg Kroah-Hartman }
1387ab4382d2SGreg Kroah-Hartman 
1388a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1389a930e528SElen Song {
1390a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1391a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1392a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1393a930e528SElen Song 
1394a930e528SElen Song 	pdc->buf = xmit->buf;
1395a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1396a930e528SElen Song 					pdc->buf,
1397a930e528SElen Song 					UART_XMIT_SIZE,
1398a930e528SElen Song 					DMA_TO_DEVICE);
1399a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1400a930e528SElen Song 	pdc->ofs = 0;
1401a930e528SElen Song 
1402a930e528SElen Song 	return 0;
1403a930e528SElen Song }
1404a930e528SElen Song 
1405ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1406ab4382d2SGreg Kroah-Hartman {
1407ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1408ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1409ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1410ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1411ab4382d2SGreg Kroah-Hartman 
1412ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1413ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1414ab4382d2SGreg Kroah-Hartman 
1415ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1416ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1417ab4382d2SGreg Kroah-Hartman 
1418ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1419ab4382d2SGreg Kroah-Hartman 
1420ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1421ab4382d2SGreg Kroah-Hartman 
1422ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1423ab4382d2SGreg Kroah-Hartman 		status = c.status;
1424ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1425ab4382d2SGreg Kroah-Hartman 
1426ab4382d2SGreg Kroah-Hartman 		/*
1427ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1428ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1429ab4382d2SGreg Kroah-Hartman 		 */
1430ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1431ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1432ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1433ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1434ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1435ab4382d2SGreg Kroah-Hartman 
1436ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1437ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1438ab4382d2SGreg Kroah-Hartman 					continue;
1439ab4382d2SGreg Kroah-Hartman 			}
1440ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1441ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1442ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1443ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1444ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1445ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1446ab4382d2SGreg Kroah-Hartman 
1447ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1448ab4382d2SGreg Kroah-Hartman 
1449ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1450ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1451ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1452ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1453ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1454ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1455ab4382d2SGreg Kroah-Hartman 		}
1456ab4382d2SGreg Kroah-Hartman 
1457ab4382d2SGreg Kroah-Hartman 
1458ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1459ab4382d2SGreg Kroah-Hartman 			continue;
1460ab4382d2SGreg Kroah-Hartman 
1461ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1462ab4382d2SGreg Kroah-Hartman 	}
1463ab4382d2SGreg Kroah-Hartman 
1464ab4382d2SGreg Kroah-Hartman 	/*
1465ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1466ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1467ab4382d2SGreg Kroah-Hartman 	 */
1468ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14692e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1470ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1471ab4382d2SGreg Kroah-Hartman }
1472ab4382d2SGreg Kroah-Hartman 
1473a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1474a930e528SElen Song {
1475a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1476a930e528SElen Song 	int i;
1477a930e528SElen Song 
1478a930e528SElen Song 	for (i = 0; i < 2; i++) {
1479a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1480a930e528SElen Song 
1481a930e528SElen Song 		dma_unmap_single(port->dev,
1482a930e528SElen Song 				 pdc->dma_addr,
1483a930e528SElen Song 				 pdc->dma_size,
1484a930e528SElen Song 				 DMA_FROM_DEVICE);
1485a930e528SElen Song 		kfree(pdc->buf);
1486a930e528SElen Song 	}
1487a930e528SElen Song }
1488a930e528SElen Song 
148964e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1490ab4382d2SGreg Kroah-Hartman {
1491ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
149205c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1493ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1494ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1495ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1496ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1497ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1498ab4382d2SGreg Kroah-Hartman 
1499ab4382d2SGreg Kroah-Hartman 	do {
1500ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
15014e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1502ab4382d2SGreg Kroah-Hartman 
1503ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
15044e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1505ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1506ab4382d2SGreg Kroah-Hartman 
1507ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1508ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1509ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1510ab4382d2SGreg Kroah-Hartman 		 * find out.
1511ab4382d2SGreg Kroah-Hartman 		 *
1512ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1513ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1514ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1515ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1516ab4382d2SGreg Kroah-Hartman 		 */
1517ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1518ab4382d2SGreg Kroah-Hartman 
1519ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1520ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1521ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1522ab4382d2SGreg Kroah-Hartman 
1523ab4382d2SGreg Kroah-Hartman 			/*
1524ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1525ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1526ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1527ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1528ab4382d2SGreg Kroah-Hartman 			 */
1529ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1530ab4382d2SGreg Kroah-Hartman 
153105c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
153205c7cd39SJiri Slaby 						count);
1533ab4382d2SGreg Kroah-Hartman 
1534ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1535ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1536ab4382d2SGreg Kroah-Hartman 
1537ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1538ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1539ab4382d2SGreg Kroah-Hartman 		}
1540ab4382d2SGreg Kroah-Hartman 
1541ab4382d2SGreg Kroah-Hartman 		/*
1542ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1543ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1544ab4382d2SGreg Kroah-Hartman 		 */
1545ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1546ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
15474e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
15484e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1549ab4382d2SGreg Kroah-Hartman 
1550ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1551ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1552ab4382d2SGreg Kroah-Hartman 		}
1553ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1554ab4382d2SGreg Kroah-Hartman 
1555ab4382d2SGreg Kroah-Hartman 	/*
1556ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1557ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1558ab4382d2SGreg Kroah-Hartman 	 */
1559ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15602e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1561ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1562ab4382d2SGreg Kroah-Hartman 
15634e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
15644e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1565ab4382d2SGreg Kroah-Hartman }
1566ab4382d2SGreg Kroah-Hartman 
1567a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1568a930e528SElen Song {
1569a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1570a930e528SElen Song 	int i;
1571a930e528SElen Song 
1572a930e528SElen Song 	for (i = 0; i < 2; i++) {
1573a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1574a930e528SElen Song 
1575a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1576a930e528SElen Song 		if (pdc->buf == NULL) {
1577a930e528SElen Song 			if (i != 0) {
1578a930e528SElen Song 				dma_unmap_single(port->dev,
1579a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1580a930e528SElen Song 					PDC_BUFFER_SIZE,
1581a930e528SElen Song 					DMA_FROM_DEVICE);
1582a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1583a930e528SElen Song 			}
1584a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1585a930e528SElen Song 			return -ENOMEM;
1586a930e528SElen Song 		}
1587a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1588a930e528SElen Song 						pdc->buf,
1589a930e528SElen Song 						PDC_BUFFER_SIZE,
1590a930e528SElen Song 						DMA_FROM_DEVICE);
1591a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1592a930e528SElen Song 		pdc->ofs = 0;
1593a930e528SElen Song 	}
1594a930e528SElen Song 
1595a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1596a930e528SElen Song 
15974e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
15984e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1599a930e528SElen Song 
16004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
16014e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
16024e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1603a930e528SElen Song 
1604a930e528SElen Song 	return 0;
1605a930e528SElen Song }
1606a930e528SElen Song 
1607ab4382d2SGreg Kroah-Hartman /*
1608ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1609ab4382d2SGreg Kroah-Hartman  */
161000e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data)
1611ab4382d2SGreg Kroah-Hartman {
1612ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1613ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1614ab4382d2SGreg Kroah-Hartman 
1615ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1616ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1617a930e528SElen Song 	atmel_port->schedule_rx(port);
161800e8e658SNicolas Ferre 	spin_unlock(&port->lock);
161900e8e658SNicolas Ferre }
1620ab4382d2SGreg Kroah-Hartman 
162100e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data)
162200e8e658SNicolas Ferre {
162300e8e658SNicolas Ferre 	struct uart_port *port = (struct uart_port *)data;
162400e8e658SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
162500e8e658SNicolas Ferre 
162600e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
162700e8e658SNicolas Ferre 	spin_lock(&port->lock);
162800e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1629ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1630ab4382d2SGreg Kroah-Hartman }
1631ab4382d2SGreg Kroah-Hartman 
16324a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
163333d64c4fSElen Song 				struct platform_device *pdev)
163433d64c4fSElen Song {
163533d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
163633d64c4fSElen Song 
163733d64c4fSElen Song 	/* DMA/PDC usage specification */
1638490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1639490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
164033d64c4fSElen Song 			atmel_port->use_dma_rx  = true;
164133d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
164233d64c4fSElen Song 		} else {
164333d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
164433d64c4fSElen Song 			atmel_port->use_pdc_rx  = true;
164533d64c4fSElen Song 		}
164633d64c4fSElen Song 	} else {
164733d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
164833d64c4fSElen Song 		atmel_port->use_pdc_rx  = false;
164933d64c4fSElen Song 	}
165033d64c4fSElen Song 
1651490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1652490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
165333d64c4fSElen Song 			atmel_port->use_dma_tx  = true;
165433d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
165533d64c4fSElen Song 		} else {
165633d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
165733d64c4fSElen Song 			atmel_port->use_pdc_tx  = true;
165833d64c4fSElen Song 		}
165933d64c4fSElen Song 	} else {
166033d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
166133d64c4fSElen Song 		atmel_port->use_pdc_tx  = false;
166233d64c4fSElen Song 	}
166333d64c4fSElen Song }
166433d64c4fSElen Song 
1665a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1666a930e528SElen Song {
1667a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1668a930e528SElen Song 
166934df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
167034df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
167134df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
167234df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
167334df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1674a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1675a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1676a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1677a930e528SElen Song 	} else {
1678a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1679a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1680a930e528SElen Song 		atmel_port->release_rx = NULL;
1681a930e528SElen Song 	}
1682a930e528SElen Song 
168308f738beSElen Song 	if (atmel_use_dma_tx(port)) {
168408f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
168508f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
168608f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
168708f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1688a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1689a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1690a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1691a930e528SElen Song 	} else {
1692a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1693a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1694a930e528SElen Song 		atmel_port->release_tx = NULL;
1695a930e528SElen Song 	}
1696a930e528SElen Song }
1697a930e528SElen Song 
1698ab4382d2SGreg Kroah-Hartman /*
1699055560b0SElen Song  * Get ip name usart or uart
1700055560b0SElen Song  */
1701892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1702055560b0SElen Song {
1703055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
17044e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1705731d9caeSNicolas Ferre 	u32 version;
17061d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
17074b769371SNicolas Ferre 	/* ASCII decoding for IP version */
17084b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
17094b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
17101d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1711055560b0SElen Song 
17125bf5635aSLudovic Desroches 	/*
17135bf5635aSLudovic Desroches 	 * Only USART devices from at91sam9260 SOC implement fractional
17142867af2dSRomain Izard 	 * baudrate. It is available for all asynchronous modes, with the
17152867af2dSRomain Izard 	 * following restriction: the sampling clock's duty cycle is not
17162867af2dSRomain Izard 	 * constant.
17175bf5635aSLudovic Desroches 	 */
17185bf5635aSLudovic Desroches 	atmel_port->has_frac_baudrate = false;
17194b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
1720055560b0SElen Song 
17212958cceeSLudovic Desroches 	if (name == new_uart) {
17222958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
17234b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
17242958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
17252958cceeSLudovic Desroches 	} else if (name == usart) {
17262958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
17275bf5635aSLudovic Desroches 		atmel_port->has_frac_baudrate = true;
17282958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
17292958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
17304b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
17314b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1732055560b0SElen Song 	} else {
1733731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17344e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1735731d9caeSNicolas Ferre 		switch (version) {
1736731d9caeSNicolas Ferre 		case 0x302:
1737731d9caeSNicolas Ferre 		case 0x10213:
1738fd63a890SJonas Danielsson 		case 0x10302:
1739731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
17405bf5635aSLudovic Desroches 			atmel_port->has_frac_baudrate = true;
17414b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
17422958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1743731d9caeSNicolas Ferre 			break;
1744731d9caeSNicolas Ferre 		case 0x203:
1745731d9caeSNicolas Ferre 		case 0x10202:
1746731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1747731d9caeSNicolas Ferre 			break;
1748731d9caeSNicolas Ferre 		default:
1749731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1750731d9caeSNicolas Ferre 		}
1751055560b0SElen Song 	}
1752055560b0SElen Song }
1753055560b0SElen Song 
1754055560b0SElen Song /*
1755ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1756ab4382d2SGreg Kroah-Hartman  */
1757ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1758ab4382d2SGreg Kroah-Hartman {
175933d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1760ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1761ab4382d2SGreg Kroah-Hartman 	int retval;
1762ab4382d2SGreg Kroah-Hartman 
1763ab4382d2SGreg Kroah-Hartman 	/*
1764ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1765ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1766ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1767ab4382d2SGreg Kroah-Hartman 	 */
17684e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1769ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1770ab4382d2SGreg Kroah-Hartman 
1771ab4382d2SGreg Kroah-Hartman 	/*
1772ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1773ab4382d2SGreg Kroah-Hartman 	 */
17742c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
17752c7af5baSBoris BREZILLON 			     IRQF_SHARED | IRQF_COND_SUSPEND,
17769594b5beSSebastian Andrzej Siewior 			     dev_name(&pdev->dev), port);
1777ab4382d2SGreg Kroah-Hartman 	if (retval) {
1778ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1779ab4382d2SGreg Kroah-Hartman 		return retval;
1780ab4382d2SGreg Kroah-Hartman 	}
1781ab4382d2SGreg Kroah-Hartman 
178298f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
178398f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
178498f2082cSNicolas Ferre 			(unsigned long)port);
178598f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
178698f2082cSNicolas Ferre 			(unsigned long)port);
17871e125786SLeilei Zhao 
1788ab5e4e41SRichard Genoud 	/*
1789ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1790ab4382d2SGreg Kroah-Hartman 	 */
179133d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
17924d9628a1SLeilei Zhao 	atmel_set_ops(port);
179333d64c4fSElen Song 
1794a930e528SElen Song 	if (atmel_port->prepare_rx) {
1795a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1796a930e528SElen Song 		if (retval < 0)
1797a930e528SElen Song 			atmel_set_ops(port);
1798ab4382d2SGreg Kroah-Hartman 	}
1799ab4382d2SGreg Kroah-Hartman 
1800a930e528SElen Song 	if (atmel_port->prepare_tx) {
1801a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1802a930e528SElen Song 		if (retval < 0)
1803a930e528SElen Song 			atmel_set_ops(port);
1804ab4382d2SGreg Kroah-Hartman 	}
1805ab4382d2SGreg Kroah-Hartman 
1806b5199d46SCyrille Pitchen 	/*
1807b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1808b5199d46SCyrille Pitchen 	 */
1809b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1810b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1811b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1812b5199d46SCyrille Pitchen 		unsigned int fmr;
1813b5199d46SCyrille Pitchen 
1814b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1815b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1816b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1817b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1818b5199d46SCyrille Pitchen 
18195f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
18205f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
18215f258b3eSCyrille Pitchen 
1822b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1823b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1824b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1825b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1826b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1827b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1828b5199d46SCyrille Pitchen 
1829b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1830b5199d46SCyrille Pitchen 	}
1831b5199d46SCyrille Pitchen 
1832ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1833e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1834ab4382d2SGreg Kroah-Hartman 
1835ab4382d2SGreg Kroah-Hartman 	/*
1836ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1837ab4382d2SGreg Kroah-Hartman 	 */
18384e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1839ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
18404e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1841ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
1842ab4382d2SGreg Kroah-Hartman 
1843026cb432SKees Cook 	timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0);
18448bc661bfSMarek Roszko 
18458bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18468bc661bfSMarek Roszko 		/* set UART timeout */
18474b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18482e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18492e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18502e68c22fSElen Song 		/* set USART timeout */
18512e68c22fSElen Song 		} else {
18522958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18532958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18544e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1855ab4382d2SGreg Kroah-Hartman 
18564e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18574e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18582e68c22fSElen Song 		}
1859ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
18604e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
186134df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18622e68c22fSElen Song 		/* set UART timeout */
18634b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18642e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18652e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18662e68c22fSElen Song 		/* set USART timeout */
18672e68c22fSElen Song 		} else {
18682958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18692958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18704e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
187134df42f5SElen Song 
18724e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18734e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
18742e68c22fSElen Song 		}
1875ab4382d2SGreg Kroah-Hartman 	} else {
1876ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
18774e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1878ab4382d2SGreg Kroah-Hartman 	}
1879ab4382d2SGreg Kroah-Hartman 
1880ab4382d2SGreg Kroah-Hartman 	return 0;
1881ab4382d2SGreg Kroah-Hartman }
1882ab4382d2SGreg Kroah-Hartman 
1883ab4382d2SGreg Kroah-Hartman /*
1884479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1885479e9b94SPeter Hurley  * buffer is reset.
1886479e9b94SPeter Hurley  */
1887479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1888479e9b94SPeter Hurley {
1889479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1890479e9b94SPeter Hurley 
1891479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
18924e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1893479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1894479e9b94SPeter Hurley 	}
189531ca2c63SRichard Genoud 	/*
189631ca2c63SRichard Genoud 	 * in uart_flush_buffer(), the xmit circular buffer has just
189731ca2c63SRichard Genoud 	 * been cleared, so we have to reset tx_len accordingly.
189831ca2c63SRichard Genoud 	 */
189931ca2c63SRichard Genoud 	atmel_port->tx_len = 0;
1900479e9b94SPeter Hurley }
1901479e9b94SPeter Hurley 
1902479e9b94SPeter Hurley /*
1903ab4382d2SGreg Kroah-Hartman  * Disable the port
1904ab4382d2SGreg Kroah-Hartman  */
1905ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1906ab4382d2SGreg Kroah-Hartman {
1907ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19080cc7c6c7SMarek Roszko 
19090ae9fdefSRichard Genoud 	/* Disable modem control lines interrupts */
19100ae9fdefSRichard Genoud 	atmel_disable_ms(port);
19110ae9fdefSRichard Genoud 
191298f2082cSNicolas Ferre 	/* Disable interrupts at device level */
191398f2082cSNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
191498f2082cSNicolas Ferre 
191598f2082cSNicolas Ferre 	/* Prevent spurious interrupts from scheduling the tasklet */
191698f2082cSNicolas Ferre 	atomic_inc(&atmel_port->tasklet_shutdown);
191798f2082cSNicolas Ferre 
1918ab4382d2SGreg Kroah-Hartman 	/*
19198bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19208bc661bfSMarek Roszko 	 * cleanup
19218bc661bfSMarek Roszko 	 */
19228bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19238bc661bfSMarek Roszko 
192498f2082cSNicolas Ferre 	/* Make sure that no interrupt is on the fly */
192598f2082cSNicolas Ferre 	synchronize_irq(port->irq);
192698f2082cSNicolas Ferre 
19278bc661bfSMarek Roszko 	/*
19280cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19290cc7c6c7SMarek Roszko 	 * we destroy the buffers
19300cc7c6c7SMarek Roszko 	 */
193100e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
193200e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
19330cc7c6c7SMarek Roszko 
19340cc7c6c7SMarek Roszko 	/*
19350cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
193698f2082cSNicolas Ferre 	 * disable port and break condition.
1937ab4382d2SGreg Kroah-Hartman 	 */
1938ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1939ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1940ab4382d2SGreg Kroah-Hartman 
19414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
19420cc7c6c7SMarek Roszko 
1943ab4382d2SGreg Kroah-Hartman 	/*
1944ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1945ab4382d2SGreg Kroah-Hartman 	 */
1946a930e528SElen Song 	if (atmel_port->release_rx)
1947a930e528SElen Song 		atmel_port->release_rx(port);
1948a930e528SElen Song 	if (atmel_port->release_tx)
1949a930e528SElen Song 		atmel_port->release_tx(port);
1950ab4382d2SGreg Kroah-Hartman 
1951ab4382d2SGreg Kroah-Hartman 	/*
1952bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1953bb7e73c5SMark Deneen 	 */
1954bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1955bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1956bb7e73c5SMark Deneen 
1957bb7e73c5SMark Deneen 	/*
1958ab5e4e41SRichard Genoud 	 * Free the interrupts
1959ab4382d2SGreg Kroah-Hartman 	 */
1960ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1961ab5e4e41SRichard Genoud 
1962479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1963ab4382d2SGreg Kroah-Hartman }
1964ab4382d2SGreg Kroah-Hartman 
1965ab4382d2SGreg Kroah-Hartman /*
1966ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1967ab4382d2SGreg Kroah-Hartman  */
1968ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1969ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1970ab4382d2SGreg Kroah-Hartman {
1971ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1972ab4382d2SGreg Kroah-Hartman 
1973ab4382d2SGreg Kroah-Hartman 	switch (state) {
1974ab4382d2SGreg Kroah-Hartman 	case 0:
1975ab4382d2SGreg Kroah-Hartman 		/*
1976ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1977ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1978ab4382d2SGreg Kroah-Hartman 		 */
197991f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1980ab4382d2SGreg Kroah-Hartman 
1981ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
19824e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
1983ab4382d2SGreg Kroah-Hartman 		break;
1984ab4382d2SGreg Kroah-Hartman 	case 3:
1985ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
19864e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
19874e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
1988ab4382d2SGreg Kroah-Hartman 
1989ab4382d2SGreg Kroah-Hartman 		/*
1990ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1991ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1992ab4382d2SGreg Kroah-Hartman 		 */
199391f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1994ab4382d2SGreg Kroah-Hartman 		break;
1995ab4382d2SGreg Kroah-Hartman 	default:
1996ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1997ab4382d2SGreg Kroah-Hartman 	}
1998ab4382d2SGreg Kroah-Hartman }
1999ab4382d2SGreg Kroah-Hartman 
2000ab4382d2SGreg Kroah-Hartman /*
2001ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2002ab4382d2SGreg Kroah-Hartman  */
2003ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2004ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2005ab4382d2SGreg Kroah-Hartman {
20065bf5635aSLudovic Desroches 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2007ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
20085bf5635aSLudovic Desroches 	unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
2009ab4382d2SGreg Kroah-Hartman 
20101cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20114e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20121cf6e8fcSCyrille Pitchen 
20131cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20141cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20151cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2016ab4382d2SGreg Kroah-Hartman 
2017ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2018ab4382d2SGreg Kroah-Hartman 
2019ab4382d2SGreg Kroah-Hartman 	/* byte size */
2020ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2021ab4382d2SGreg Kroah-Hartman 	case CS5:
2022ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2023ab4382d2SGreg Kroah-Hartman 		break;
2024ab4382d2SGreg Kroah-Hartman 	case CS6:
2025ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2026ab4382d2SGreg Kroah-Hartman 		break;
2027ab4382d2SGreg Kroah-Hartman 	case CS7:
2028ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2029ab4382d2SGreg Kroah-Hartman 		break;
2030ab4382d2SGreg Kroah-Hartman 	default:
2031ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2032ab4382d2SGreg Kroah-Hartman 		break;
2033ab4382d2SGreg Kroah-Hartman 	}
2034ab4382d2SGreg Kroah-Hartman 
2035ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2036ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2037ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2038ab4382d2SGreg Kroah-Hartman 
2039ab4382d2SGreg Kroah-Hartman 	/* parity */
2040ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2041ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2042ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2043ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2044ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2045ab4382d2SGreg Kroah-Hartman 			else
2046ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2047ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2048ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2049ab4382d2SGreg Kroah-Hartman 		else
2050ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2051ab4382d2SGreg Kroah-Hartman 	} else
2052ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2053ab4382d2SGreg Kroah-Hartman 
2054ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2055ab4382d2SGreg Kroah-Hartman 
2056ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2057ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2058ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2059ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2060ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2061ab4382d2SGreg Kroah-Hartman 
206264e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2063ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
20644e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2065ab4382d2SGreg Kroah-Hartman 
2066ab4382d2SGreg Kroah-Hartman 	/*
2067ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2068ab4382d2SGreg Kroah-Hartman 	 */
2069ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2070ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2071ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2072ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2073ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2074ab4382d2SGreg Kroah-Hartman 		/*
2075ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2076ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2077ab4382d2SGreg Kroah-Hartman 		 */
2078ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2079ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2080ab4382d2SGreg Kroah-Hartman 	}
2081ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2082ab4382d2SGreg Kroah-Hartman 
2083ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2084ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2085ab4382d2SGreg Kroah-Hartman 
2086ab4382d2SGreg Kroah-Hartman 	/*
2087ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2088ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2089ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2090ab4382d2SGreg Kroah-Hartman 	 */
20914e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
20924e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2093ab4382d2SGreg Kroah-Hartman 
2094ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
20954e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2096ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
2097ab4382d2SGreg Kroah-Hartman 
20981cf6e8fcSCyrille Pitchen 	/* mode */
209913bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
21004e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
21014e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2102ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
21031cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
21041cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
21059bcffe75SRichard Genoud 		if (atmel_use_fifo(port) &&
21069bcffe75SRichard Genoud 		    !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
21079bcffe75SRichard Genoud 			/*
21089bcffe75SRichard Genoud 			 * with ATMEL_US_USMODE_HWHS set, the controller will
21099bcffe75SRichard Genoud 			 * be able to drive the RTS pin high/low when the RX
21109bcffe75SRichard Genoud 			 * FIFO is above RXFTHRES/below RXFTHRES2.
21119bcffe75SRichard Genoud 			 * It will also disable the transmitter when the CTS
21129bcffe75SRichard Genoud 			 * pin is high.
21139bcffe75SRichard Genoud 			 * This mode is not activated if CTS pin is a GPIO
21149bcffe75SRichard Genoud 			 * because in this case, the transmitter is always
21159bcffe75SRichard Genoud 			 * disabled (there must be an internal pull-up
21169bcffe75SRichard Genoud 			 * responsible for this behaviour).
21179bcffe75SRichard Genoud 			 * If the RTS pin is a GPIO, the controller won't be
21189bcffe75SRichard Genoud 			 * able to drive it according to the FIFO thresholds,
21199bcffe75SRichard Genoud 			 * but it will be handled by the driver.
21209bcffe75SRichard Genoud 			 */
21211cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
21229bcffe75SRichard Genoud 		} else {
21239bcffe75SRichard Genoud 			/*
21249bcffe75SRichard Genoud 			 * For platforms without FIFO, the flow control is
21259bcffe75SRichard Genoud 			 * handled by the driver.
21269bcffe75SRichard Genoud 			 */
21279bcffe75SRichard Genoud 			mode |= ATMEL_US_USMODE_NORMAL;
21285be605acSAlexandre Belloni 		}
21291cf6e8fcSCyrille Pitchen 	} else {
21301cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21311cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2132ab4382d2SGreg Kroah-Hartman 	}
2133ab4382d2SGreg Kroah-Hartman 
21341cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2136ab4382d2SGreg Kroah-Hartman 
21371cf6e8fcSCyrille Pitchen 	/*
21381cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21391cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21401cf6e8fcSCyrille Pitchen 	 */
21411cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21421cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21431cf6e8fcSCyrille Pitchen 
21441cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21451cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21461cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21471cf6e8fcSCyrille Pitchen 		} else {
21481cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21491cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21501cf6e8fcSCyrille Pitchen 		}
21511cf6e8fcSCyrille Pitchen 
21524e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21531cf6e8fcSCyrille Pitchen 	}
21541cf6e8fcSCyrille Pitchen 
21555bf5635aSLudovic Desroches 	/*
21565bf5635aSLudovic Desroches 	 * Set the baud rate:
21575bf5635aSLudovic Desroches 	 * Fractional baudrate allows to setup output frequency more
21585bf5635aSLudovic Desroches 	 * accurately. This feature is enabled only when using normal mode.
21595bf5635aSLudovic Desroches 	 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
21605bf5635aSLudovic Desroches 	 * Currently, OVER is always set to 0 so we get
216136131cdfSAlexey Starikovskiy 	 * baudrate = selected clock / (16 * (CD + FP / 8))
216236131cdfSAlexey Starikovskiy 	 * then
216336131cdfSAlexey Starikovskiy 	 * 8 CD + FP = selected clock / (2 * baudrate)
21645bf5635aSLudovic Desroches 	 */
21652867af2dSRomain Izard 	if (atmel_port->has_frac_baudrate) {
216636131cdfSAlexey Starikovskiy 		div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
216736131cdfSAlexey Starikovskiy 		cd = div >> 3;
216836131cdfSAlexey Starikovskiy 		fp = div & ATMEL_US_FP_MASK;
21695bf5635aSLudovic Desroches 	} else {
21705bf5635aSLudovic Desroches 		cd = uart_get_divisor(port, baud);
21715bf5635aSLudovic Desroches 	}
21725bf5635aSLudovic Desroches 
21735bf5635aSLudovic Desroches 	if (cd > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
21745bf5635aSLudovic Desroches 		cd /= 8;
21755bf5635aSLudovic Desroches 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
21765bf5635aSLudovic Desroches 	}
21775bf5635aSLudovic Desroches 	quot = cd | fp << ATMEL_US_FP_OFFSET;
21785bf5635aSLudovic Desroches 
21794e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
21804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
21814e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2182ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2183ab4382d2SGreg Kroah-Hartman 
2184ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
21854e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2186ab4382d2SGreg Kroah-Hartman 
2187ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2188ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
218935b675b9SRichard Genoud 		atmel_enable_ms(port);
219035b675b9SRichard Genoud 	else
219135b675b9SRichard Genoud 		atmel_disable_ms(port);
2192ab4382d2SGreg Kroah-Hartman 
2193ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2194ab4382d2SGreg Kroah-Hartman }
2195ab4382d2SGreg Kroah-Hartman 
2196732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
219742bd7a4fSViktar Palstsiuk {
2198732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
219942bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2200d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
220142bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2202d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
220342bd7a4fSViktar Palstsiuk 	} else {
220442bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2205cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2206cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2207cab68f89SPeter Hurley 			atmel_disable_ms(port);
2208cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2209cab68f89SPeter Hurley 		}
221042bd7a4fSViktar Palstsiuk 	}
221142bd7a4fSViktar Palstsiuk }
221242bd7a4fSViktar Palstsiuk 
2213ab4382d2SGreg Kroah-Hartman /*
2214ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2215ab4382d2SGreg Kroah-Hartman  */
2216ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2217ab4382d2SGreg Kroah-Hartman {
2218ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2219ab4382d2SGreg Kroah-Hartman }
2220ab4382d2SGreg Kroah-Hartman 
2221ab4382d2SGreg Kroah-Hartman /*
2222ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2223ab4382d2SGreg Kroah-Hartman  */
2224ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2225ab4382d2SGreg Kroah-Hartman {
2226*c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2227*c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2228ab4382d2SGreg Kroah-Hartman 
2229ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2230ab4382d2SGreg Kroah-Hartman 
2231ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2232ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2233ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2234ab4382d2SGreg Kroah-Hartman 	}
2235ab4382d2SGreg Kroah-Hartman }
2236ab4382d2SGreg Kroah-Hartman 
2237ab4382d2SGreg Kroah-Hartman /*
2238ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2239ab4382d2SGreg Kroah-Hartman  */
2240ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2241ab4382d2SGreg Kroah-Hartman {
2242*c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2243*c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2244ab4382d2SGreg Kroah-Hartman 
2245ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2246ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2247ab4382d2SGreg Kroah-Hartman 
2248ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2249ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2250ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2251ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2252ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2253ab4382d2SGreg Kroah-Hartman 		}
2254ab4382d2SGreg Kroah-Hartman 	}
2255ab4382d2SGreg Kroah-Hartman 
2256ab4382d2SGreg Kroah-Hartman 	return 0;
2257ab4382d2SGreg Kroah-Hartman }
2258ab4382d2SGreg Kroah-Hartman 
2259ab4382d2SGreg Kroah-Hartman /*
2260ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2261ab4382d2SGreg Kroah-Hartman  */
2262ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2263ab4382d2SGreg Kroah-Hartman {
2264ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2265ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2266ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2267ab4382d2SGreg Kroah-Hartman 	}
2268ab4382d2SGreg Kroah-Hartman }
2269ab4382d2SGreg Kroah-Hartman 
2270ab4382d2SGreg Kroah-Hartman /*
2271ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2272ab4382d2SGreg Kroah-Hartman  */
2273ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2274ab4382d2SGreg Kroah-Hartman {
2275ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2276ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2277ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2278ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2279ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2280ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2281ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2282ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2283ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2284270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2285ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2286ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2287ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2288ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2289ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2290ab4382d2SGreg Kroah-Hartman 	return ret;
2291ab4382d2SGreg Kroah-Hartman }
2292ab4382d2SGreg Kroah-Hartman 
2293ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2294ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2295ab4382d2SGreg Kroah-Hartman {
22964e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2297ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2298ab4382d2SGreg Kroah-Hartman 
2299a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2300ab4382d2SGreg Kroah-Hartman }
2301ab4382d2SGreg Kroah-Hartman 
2302ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2303ab4382d2SGreg Kroah-Hartman {
23044e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2305ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2306ab4382d2SGreg Kroah-Hartman 
2307a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2308ab4382d2SGreg Kroah-Hartman }
2309ab4382d2SGreg Kroah-Hartman #endif
2310ab4382d2SGreg Kroah-Hartman 
23115c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = {
2312ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2313ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2314ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2315ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2316ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2317ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2318ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2319ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2320ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2321ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2322ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2323ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
232442bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2325ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2326ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2327ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2328ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2329ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2330ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2331ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2332ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2333ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2334ab4382d2SGreg Kroah-Hartman #endif
2335ab4382d2SGreg Kroah-Hartman };
2336ab4382d2SGreg Kroah-Hartman 
2337ab4382d2SGreg Kroah-Hartman /*
2338ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2339ab4382d2SGreg Kroah-Hartman  */
234091f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2341ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2342ab4382d2SGreg Kroah-Hartman {
234391f8c2d8SBoris BREZILLON 	int ret;
2344ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2345*c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(pdev->dev.parent);
2346ab4382d2SGreg Kroah-Hartman 
23474a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2348a930e528SElen Song 	atmel_set_ops(port);
2349a930e528SElen Song 
2350*c24d2531SRadu Pirea 	uart_get_rs485_mode(&mpdev->dev, &port->rs485);
235133d64c4fSElen Song 
2352ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
235392c8f7c0SAlexandre Belloni 	port->flags		= UPF_BOOT_AUTOCONF | UPF_IOREMAP;
2354ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2355ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2356ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2357*c24d2531SRadu Pirea 	port->mapbase		= mpdev->resource[0].start;
2358*c24d2531SRadu Pirea 	port->irq		= mpdev->resource[1].start;
235913bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
236092c8f7c0SAlexandre Belloni 	port->membase		= NULL;
2361ab4382d2SGreg Kroah-Hartman 
2362ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2363ab4382d2SGreg Kroah-Hartman 
2364ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2365ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2366*c24d2531SRadu Pirea 		atmel_port->clk = clk_get(&mpdev->dev, "usart");
236791f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
236891f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
236991f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
237091f8c2d8SBoris BREZILLON 			return ret;
237191f8c2d8SBoris BREZILLON 		}
237291f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
237391f8c2d8SBoris BREZILLON 		if (ret) {
237491f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
237591f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
237691f8c2d8SBoris BREZILLON 			return ret;
237791f8c2d8SBoris BREZILLON 		}
2378ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
237991f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2380ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2381ab4382d2SGreg Kroah-Hartman 	}
2382ab4382d2SGreg Kroah-Hartman 
2383ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
238413bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2385ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
238664e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2387ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2388ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2389ab4382d2SGreg Kroah-Hartman 	} else {
2390ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2391ab4382d2SGreg Kroah-Hartman 	}
239291f8c2d8SBoris BREZILLON 
239391f8c2d8SBoris BREZILLON 	return 0;
2394ab4382d2SGreg Kroah-Hartman }
2395ab4382d2SGreg Kroah-Hartman 
2396ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2397ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2398ab4382d2SGreg Kroah-Hartman {
23994e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2400ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2401a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2402ab4382d2SGreg Kroah-Hartman }
2403ab4382d2SGreg Kroah-Hartman 
2404ab4382d2SGreg Kroah-Hartman /*
2405ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2406ab4382d2SGreg Kroah-Hartman  */
2407ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2408ab4382d2SGreg Kroah-Hartman {
2409ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2410ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2411ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2412ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2413ab4382d2SGreg Kroah-Hartman 
2414ab4382d2SGreg Kroah-Hartman 	/*
2415ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2416ab4382d2SGreg Kroah-Hartman 	 */
24174e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
24184e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
24194e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2420ab4382d2SGreg Kroah-Hartman 
2421ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
24224e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
24234e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2424ab4382d2SGreg Kroah-Hartman 
2425497e1e16SNicolas Ferre 	/* Make sure that tx path is actually able to send characters */
2426497e1e16SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2427ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2428497e1e16SNicolas Ferre 
2429ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2430ab4382d2SGreg Kroah-Hartman 
2431ab4382d2SGreg Kroah-Hartman 	/*
2432ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2433ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2434ab4382d2SGreg Kroah-Hartman 	 */
2435ab4382d2SGreg Kroah-Hartman 	do {
24364e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2437ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2438ab4382d2SGreg Kroah-Hartman 
2439ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2440ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24414e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24444e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2445ab4382d2SGreg Kroah-Hartman }
2446ab4382d2SGreg Kroah-Hartman 
2447ab4382d2SGreg Kroah-Hartman /*
2448ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2449ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2450ab4382d2SGreg Kroah-Hartman  */
2451ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2452ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2453ab4382d2SGreg Kroah-Hartman {
2454ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2455ab4382d2SGreg Kroah-Hartman 
2456ab4382d2SGreg Kroah-Hartman 	/*
2457ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2458ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2459ab4382d2SGreg Kroah-Hartman 	 */
24604e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2461ab4382d2SGreg Kroah-Hartman 	if (!quot)
2462ab4382d2SGreg Kroah-Hartman 		return;
2463ab4382d2SGreg Kroah-Hartman 
24644e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2465ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2466ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2467ab4382d2SGreg Kroah-Hartman 	else
2468ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2469ab4382d2SGreg Kroah-Hartman 
24704e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2471ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2472ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2473ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2474ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2475ab4382d2SGreg Kroah-Hartman 
2476ab4382d2SGreg Kroah-Hartman 	/*
2477ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2478ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2479ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2480ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2481ab4382d2SGreg Kroah-Hartman 	 */
2482ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2483ab4382d2SGreg Kroah-Hartman }
2484ab4382d2SGreg Kroah-Hartman 
2485ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2486ab4382d2SGreg Kroah-Hartman {
248791f8c2d8SBoris BREZILLON 	int ret;
2488ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2489ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2490ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2491ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2492ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2493ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2494ab4382d2SGreg Kroah-Hartman 
2495ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2496ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2497ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2498ab4382d2SGreg Kroah-Hartman 	}
2499ab4382d2SGreg Kroah-Hartman 
250091f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
250191f8c2d8SBoris BREZILLON 	if (ret)
250291f8c2d8SBoris BREZILLON 		return ret;
2503ab4382d2SGreg Kroah-Hartman 
25044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
25054e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
25064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2507ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2508ab4382d2SGreg Kroah-Hartman 
2509ab4382d2SGreg Kroah-Hartman 	if (options)
2510ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2511ab4382d2SGreg Kroah-Hartman 	else
2512ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2513ab4382d2SGreg Kroah-Hartman 
2514ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2515ab4382d2SGreg Kroah-Hartman }
2516ab4382d2SGreg Kroah-Hartman 
2517ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2518ab4382d2SGreg Kroah-Hartman 
2519ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2520ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2521ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2522ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2523ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2524ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2525ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2526ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2527ab4382d2SGreg Kroah-Hartman };
2528ab4382d2SGreg Kroah-Hartman 
2529ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2530ab4382d2SGreg Kroah-Hartman 
2531ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2532ab4382d2SGreg Kroah-Hartman {
2533ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2534ab4382d2SGreg Kroah-Hartman }
2535ab4382d2SGreg Kroah-Hartman 
2536ab4382d2SGreg Kroah-Hartman #else
2537ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2538ab4382d2SGreg Kroah-Hartman 
2539ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2540ab4382d2SGreg Kroah-Hartman {
2541ab4382d2SGreg Kroah-Hartman 	return false;
2542ab4382d2SGreg Kroah-Hartman }
2543ab4382d2SGreg Kroah-Hartman #endif
2544ab4382d2SGreg Kroah-Hartman 
2545ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2546ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2547ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2548ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2549ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2550ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2551ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2552ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2553ab4382d2SGreg Kroah-Hartman };
2554ab4382d2SGreg Kroah-Hartman 
2555ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2556ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2557ab4382d2SGreg Kroah-Hartman {
2558ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2559ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2560ab4382d2SGreg Kroah-Hartman #else
2561ab4382d2SGreg Kroah-Hartman 	return false;
2562ab4382d2SGreg Kroah-Hartman #endif
2563ab4382d2SGreg Kroah-Hartman }
2564ab4382d2SGreg Kroah-Hartman 
2565ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2566ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2567ab4382d2SGreg Kroah-Hartman {
2568ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2569ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2570ab4382d2SGreg Kroah-Hartman 
2571ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2572ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
25734e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
25744e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2575ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2576ab4382d2SGreg Kroah-Hartman 	}
2577ab4382d2SGreg Kroah-Hartman 
25786a5f0e2fSAlexandre Belloni 	if (atmel_is_console_port(port) && !console_suspend_enabled) {
25796a5f0e2fSAlexandre Belloni 		/* Cache register values as we won't get a full shutdown/startup
25806a5f0e2fSAlexandre Belloni 		 * cycle
25816a5f0e2fSAlexandre Belloni 		 */
25826a5f0e2fSAlexandre Belloni 		atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR);
25836a5f0e2fSAlexandre Belloni 		atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR);
25846a5f0e2fSAlexandre Belloni 		atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
25856a5f0e2fSAlexandre Belloni 		atmel_port->cache.rtor = atmel_uart_readl(port,
25866a5f0e2fSAlexandre Belloni 							  atmel_port->rtor);
25876a5f0e2fSAlexandre Belloni 		atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR);
25886a5f0e2fSAlexandre Belloni 		atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR);
25896a5f0e2fSAlexandre Belloni 		atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR);
25906a5f0e2fSAlexandre Belloni 	}
25916a5f0e2fSAlexandre Belloni 
2592ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2593ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
25942c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
25952c7af5baSBoris BREZILLON 		unsigned long flags;
25962c7af5baSBoris BREZILLON 
25972c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25982c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
25992c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2600ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
26012c7af5baSBoris BREZILLON 	}
2602ab4382d2SGreg Kroah-Hartman 
2603ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2604ab4382d2SGreg Kroah-Hartman 
2605ab4382d2SGreg Kroah-Hartman 	return 0;
2606ab4382d2SGreg Kroah-Hartman }
2607ab4382d2SGreg Kroah-Hartman 
2608ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2609ab4382d2SGreg Kroah-Hartman {
2610ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2611ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26122c7af5baSBoris BREZILLON 	unsigned long flags;
26132c7af5baSBoris BREZILLON 
26146a5f0e2fSAlexandre Belloni 	if (atmel_is_console_port(port) && !console_suspend_enabled) {
26156a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
26166a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
26176a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
26186a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, atmel_port->rtor,
26196a5f0e2fSAlexandre Belloni 				  atmel_port->cache.rtor);
26206a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr);
26216a5f0e2fSAlexandre Belloni 
26226a5f0e2fSAlexandre Belloni 		if (atmel_port->fifo_size) {
26236a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN |
26246a5f0e2fSAlexandre Belloni 					  ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR);
26256a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FMR,
26266a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fmr);
26276a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FIER,
26286a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fimr);
26296a5f0e2fSAlexandre Belloni 		}
26306a5f0e2fSAlexandre Belloni 		atmel_start_rx(port);
26316a5f0e2fSAlexandre Belloni 	}
26326a5f0e2fSAlexandre Belloni 
26332c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26342c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26352c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26362c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26372c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26382c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26392c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26402c7af5baSBoris BREZILLON 	}
26412c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26422c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2643ab4382d2SGreg Kroah-Hartman 
2644ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2645ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2646ab4382d2SGreg Kroah-Hartman 
2647ab4382d2SGreg Kroah-Hartman 	return 0;
2648ab4382d2SGreg Kroah-Hartman }
2649ab4382d2SGreg Kroah-Hartman #else
2650ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2651ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2652ab4382d2SGreg Kroah-Hartman #endif
2653ab4382d2SGreg Kroah-Hartman 
2654b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2655b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2656b5199d46SCyrille Pitchen {
2657b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2658b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2659b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2660b5199d46SCyrille Pitchen 
2661b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2662b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2663b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2664b5199d46SCyrille Pitchen 		return;
2665b5199d46SCyrille Pitchen 
2666b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2667b5199d46SCyrille Pitchen 		return;
2668b5199d46SCyrille Pitchen 
2669b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2670b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2671b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2672b5199d46SCyrille Pitchen 		return;
2673b5199d46SCyrille Pitchen 	}
2674b5199d46SCyrille Pitchen 
2675b5199d46SCyrille Pitchen 	/*
2676b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2677b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2678b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2679b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2680b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2681b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2682b5199d46SCyrille Pitchen 	 */
2683b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2684b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2685b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2686b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2687b5199d46SCyrille Pitchen 
2688b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2689b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2690b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2691b78cd169SJaeden Amero 		atmel_port->rts_high);
2692b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2693b78cd169SJaeden Amero 		atmel_port->rts_low);
2694b5199d46SCyrille Pitchen }
2695b5199d46SCyrille Pitchen 
26969671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2697ab4382d2SGreg Kroah-Hartman {
2698b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
2699*c24d2531SRadu Pirea 	struct device_node *np = pdev->dev.parent->of_node;
2700ab4382d2SGreg Kroah-Hartman 	void *data;
27014cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2702bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2703ab4382d2SGreg Kroah-Hartman 
2704ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2705ab4382d2SGreg Kroah-Hartman 
2706*c24d2531SRadu Pirea 	/*
2707*c24d2531SRadu Pirea 	 * In device tree there is no node with "atmel,at91rm9200-usart-serial"
2708*c24d2531SRadu Pirea 	 * as compatible string. This driver is probed by at91-usart mfd driver
2709*c24d2531SRadu Pirea 	 * which is just a wrapper over the atmel_serial driver and
2710*c24d2531SRadu Pirea 	 * spi-at91-usart driver. All attributes needed by this driver are
2711*c24d2531SRadu Pirea 	 * found in of_node of parent.
2712*c24d2531SRadu Pirea 	 */
2713*c24d2531SRadu Pirea 	pdev->dev.of_node = np;
2714*c24d2531SRadu Pirea 
27155fbe46b6SNicolas Ferre 	ret = of_alias_get_id(np, "serial");
27164cbf9f48SNicolas Ferre 	if (ret < 0)
27175fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
27184cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2719503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
27204cbf9f48SNicolas Ferre 
2721503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
27224cbf9f48SNicolas Ferre 		ret = -ENODEV;
27234cbf9f48SNicolas Ferre 		goto err;
27244cbf9f48SNicolas Ferre 	}
27254cbf9f48SNicolas Ferre 
2726503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
27274cbf9f48SNicolas Ferre 		/* port already in use */
27284cbf9f48SNicolas Ferre 		ret = -EBUSY;
27294cbf9f48SNicolas Ferre 		goto err;
27304cbf9f48SNicolas Ferre 	}
27314cbf9f48SNicolas Ferre 
2732b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2733b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2734b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2735b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2736354e57f3SLinus Walleij 
273798f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
2738b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
27392c7af5baSBoris BREZILLON 
2740b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
274191f8c2d8SBoris BREZILLON 	if (ret)
27426fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2743ab4382d2SGreg Kroah-Hartman 
2744b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2745b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2746b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
274718dfef9cSUwe Kleine-König 		goto err_clear_bit;
274818dfef9cSUwe Kleine-König 	}
274918dfef9cSUwe Kleine-König 
2750b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2751ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
27526da2ec56SKees Cook 		data = kmalloc_array(ATMEL_SERIAL_RINGSIZE,
27536da2ec56SKees Cook 				     sizeof(struct atmel_uart_char),
27546da2ec56SKees Cook 				     GFP_KERNEL);
2755ab4382d2SGreg Kroah-Hartman 		if (!data)
2756ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2757b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2758ab4382d2SGreg Kroah-Hartman 	}
2759ab4382d2SGreg Kroah-Hartman 
2760b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2761bd737f87SRicardo Ribalda Delgado 
2762b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2763ab4382d2SGreg Kroah-Hartman 	if (ret)
2764ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2765ab4382d2SGreg Kroah-Hartman 
2766ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2767b78cd169SJaeden Amero 	if (atmel_is_console_port(&atmel_port->uart)
2768ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2769ab4382d2SGreg Kroah-Hartman 		/*
2770ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
277191f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2772ab4382d2SGreg Kroah-Hartman 		 */
2773b78cd169SJaeden Amero 		clk_disable_unprepare(atmel_port->clk);
2774ab4382d2SGreg Kroah-Hartman 	}
2775ab4382d2SGreg Kroah-Hartman #endif
2776ab4382d2SGreg Kroah-Hartman 
2777ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2778b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2779ab4382d2SGreg Kroah-Hartman 
2780d4f64187SCyrille Pitchen 	/*
2781d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2782d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2783d4f64187SCyrille Pitchen 	 */
2784b78cd169SJaeden Amero 	clk_prepare_enable(atmel_port->clk);
2785d4f64187SCyrille Pitchen 
2786bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2787b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
27884e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2789b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2790b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2791fc887b15SLinus Torvalds 	}
2792fc887b15SLinus Torvalds 
2793055560b0SElen Song 	/*
2794055560b0SElen Song 	 * Get port name of usart or uart
2795055560b0SElen Song 	 */
2796b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2797055560b0SElen Song 
2798d4f64187SCyrille Pitchen 	/*
2799d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2800d4f64187SCyrille Pitchen 	 * is used
2801d4f64187SCyrille Pitchen 	 */
2802b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2803d4f64187SCyrille Pitchen 
2804ab4382d2SGreg Kroah-Hartman 	return 0;
2805ab4382d2SGreg Kroah-Hartman 
2806ab4382d2SGreg Kroah-Hartman err_add_port:
2807b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2808b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
2809ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2810b78cd169SJaeden Amero 	if (!atmel_is_console_port(&atmel_port->uart)) {
2811b78cd169SJaeden Amero 		clk_put(atmel_port->clk);
2812b78cd169SJaeden Amero 		atmel_port->clk = NULL;
2813ab4382d2SGreg Kroah-Hartman 	}
28146fbb9bdfSCyrille Pitchen err_clear_bit:
2815b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
28164cbf9f48SNicolas Ferre err:
2817ab4382d2SGreg Kroah-Hartman 	return ret;
2818ab4382d2SGreg Kroah-Hartman }
2819ab4382d2SGreg Kroah-Hartman 
2820f4a8ab04SRomain Izard /*
2821f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2822f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2823f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2824f4a8ab04SRomain Izard  *
2825f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
2826f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
2827f4a8ab04SRomain Izard  * port in the normal case.
2828f4a8ab04SRomain Izard  */
2829f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
2830f4a8ab04SRomain Izard {
2831f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
2832f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2833f4a8ab04SRomain Izard 	int ret = 0;
2834f4a8ab04SRomain Izard 
283500e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
283600e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
2837f4a8ab04SRomain Izard 
2838f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
2839f4a8ab04SRomain Izard 
2840f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
2841f4a8ab04SRomain Izard 
2842f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
2843f4a8ab04SRomain Izard 
2844f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
2845f4a8ab04SRomain Izard 
2846f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
2847f4a8ab04SRomain Izard 
2848f4a8ab04SRomain Izard 	clk_put(atmel_port->clk);
2849f4a8ab04SRomain Izard 	atmel_port->clk = NULL;
2850*c24d2531SRadu Pirea 	pdev->dev.of_node = NULL;
2851f4a8ab04SRomain Izard 
2852f4a8ab04SRomain Izard 	return ret;
2853f4a8ab04SRomain Izard }
2854f4a8ab04SRomain Izard 
2855ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2856ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
2857f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
2858ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2859ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2860ab4382d2SGreg Kroah-Hartman 	.driver		= {
2861*c24d2531SRadu Pirea 		.name			= "atmel_usart_serial",
28625fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
2863ab4382d2SGreg Kroah-Hartman 	},
2864ab4382d2SGreg Kroah-Hartman };
2865ab4382d2SGreg Kroah-Hartman 
2866ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2867ab4382d2SGreg Kroah-Hartman {
2868ab4382d2SGreg Kroah-Hartman 	int ret;
2869ab4382d2SGreg Kroah-Hartman 
2870ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2871ab4382d2SGreg Kroah-Hartman 	if (ret)
2872ab4382d2SGreg Kroah-Hartman 		return ret;
2873ab4382d2SGreg Kroah-Hartman 
2874ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2875ab4382d2SGreg Kroah-Hartman 	if (ret)
2876ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2877ab4382d2SGreg Kroah-Hartman 
2878ab4382d2SGreg Kroah-Hartman 	return ret;
2879ab4382d2SGreg Kroah-Hartman }
2880c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
2881