xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 5be605ac9af979265d7b64c160ad9928088a78be)
1ab4382d2SGreg Kroah-Hartman /*
2ab4382d2SGreg Kroah-Hartman  *  Driver for Atmel AT91 / AT32 Serial ports
3ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
4ab4382d2SGreg Kroah-Hartman  *
5ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7ab4382d2SGreg Kroah-Hartman  *
8ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
9ab4382d2SGreg Kroah-Hartman  *
10ab4382d2SGreg Kroah-Hartman  * This program is free software; you can redistribute it and/or modify
11ab4382d2SGreg Kroah-Hartman  * it under the terms of the GNU General Public License as published by
12ab4382d2SGreg Kroah-Hartman  * the Free Software Foundation; either version 2 of the License, or
13ab4382d2SGreg Kroah-Hartman  * (at your option) any later version.
14ab4382d2SGreg Kroah-Hartman  *
15ab4382d2SGreg Kroah-Hartman  * This program is distributed in the hope that it will be useful,
16ab4382d2SGreg Kroah-Hartman  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ab4382d2SGreg Kroah-Hartman  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ab4382d2SGreg Kroah-Hartman  * GNU General Public License for more details.
19ab4382d2SGreg Kroah-Hartman  *
20ab4382d2SGreg Kroah-Hartman  * You should have received a copy of the GNU General Public License
21ab4382d2SGreg Kroah-Hartman  * along with this program; if not, write to the Free Software
22ab4382d2SGreg Kroah-Hartman  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23ab4382d2SGreg Kroah-Hartman  *
24ab4382d2SGreg Kroah-Hartman  */
25ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
29ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
30ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
31ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
32ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
33ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
34ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
355fbe46b6SNicolas Ferre #include <linux/of.h>
365fbe46b6SNicolas Ferre #include <linux/of_device.h>
37354e57f3SLinus Walleij #include <linux/of_gpio.h>
38ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
396b997babSVinod Koul #include <linux/dmaengine.h>
40ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
41ab4382d2SGreg Kroah-Hartman #include <linux/atmel_serial.h>
42ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
43bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
442e68c22fSElen Song #include <linux/timer.h>
45354e57f3SLinus Walleij #include <linux/gpio.h>
46e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
47e0b0baadSRichard Genoud #include <linux/err.h>
48ab5e4e41SRichard Genoud #include <linux/irq.h>
492c7af5baSBoris BREZILLON #include <linux/suspend.h>
50ab4382d2SGreg Kroah-Hartman 
51ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
52ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
53ab4382d2SGreg Kroah-Hartman 
54ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
55ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
56ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
57ab4382d2SGreg Kroah-Hartman 
58b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
59b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
60b5199d46SCyrille Pitchen /*
61b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
62b5199d46SCyrille Pitchen  * high and low thresholds
63b5199d46SCyrille Pitchen  */
64b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
65b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
66b5199d46SCyrille Pitchen 
67ab4382d2SGreg Kroah-Hartman #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
68ab4382d2SGreg Kroah-Hartman #define SUPPORT_SYSRQ
69ab4382d2SGreg Kroah-Hartman #endif
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
72ab4382d2SGreg Kroah-Hartman 
73e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
74e0b0baadSRichard Genoud 
75ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
76ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
77ab4382d2SGreg Kroah-Hartman 
78ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
79ab4382d2SGreg Kroah-Hartman 
80ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
81ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
82ab4382d2SGreg Kroah-Hartman  * UART. */
83ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
84ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
85ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
86ab4382d2SGreg Kroah-Hartman 
87ab4382d2SGreg Kroah-Hartman #else
88ab4382d2SGreg Kroah-Hartman 
89ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
90ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
91ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
92ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
93ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
94ab4382d2SGreg Kroah-Hartman 
95ab4382d2SGreg Kroah-Hartman #endif
96ab4382d2SGreg Kroah-Hartman 
97ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
98ab4382d2SGreg Kroah-Hartman 
99ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
100ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
101ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
102ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
103ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
104ab4382d2SGreg Kroah-Hartman };
105ab4382d2SGreg Kroah-Hartman 
106ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
107ab4382d2SGreg Kroah-Hartman 	u16		status;
108ab4382d2SGreg Kroah-Hartman 	u16		ch;
109ab4382d2SGreg Kroah-Hartman };
110ab4382d2SGreg Kroah-Hartman 
111ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
112ab4382d2SGreg Kroah-Hartman 
113ab4382d2SGreg Kroah-Hartman /*
1149af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
1159af92fbfSAlexandre Belloni  * avr32: 4
1169af92fbfSAlexandre Belloni  */
1179af92fbfSAlexandre Belloni #define ATMEL_MAX_UART		7
1189af92fbfSAlexandre Belloni 
1199af92fbfSAlexandre Belloni /*
120ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
121ab4382d2SGreg Kroah-Hartman  */
122ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
123ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
124ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
125ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
126ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
127ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
128ab4382d2SGreg Kroah-Hartman 
12934df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
13064e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
131ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
132ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
133ab4382d2SGreg Kroah-Hartman 
13408f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
13564e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
136ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
137ab4382d2SGreg Kroah-Hartman 
13808f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
13934df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
14008f738beSElen Song 	struct dma_chan			*chan_tx;
14134df42f5SElen Song 	struct dma_chan			*chan_rx;
14208f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
14334df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
14408f738beSElen Song 	dma_cookie_t			cookie_tx;
14534df42f5SElen Song 	dma_cookie_t			cookie_rx;
14608f738beSElen Song 	struct scatterlist		sg_tx;
14734df42f5SElen Song 	struct scatterlist		sg_rx;
148ab4382d2SGreg Kroah-Hartman 	struct tasklet_struct	tasklet;
149ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status;
150ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
151d033e82dSLeilei Zhao 	unsigned int		status_change;
1525f258b3eSCyrille Pitchen 	unsigned int		tx_len;
153ab4382d2SGreg Kroah-Hartman 
154ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
155ab4382d2SGreg Kroah-Hartman 
156e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
157ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
158b5199d46SCyrille Pitchen 	u32			fifo_size;
159b5199d46SCyrille Pitchen 	u32			rts_high;
160b5199d46SCyrille Pitchen 	u32			rts_low;
161ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1622958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
1634b769371SNicolas Ferre 	bool			has_hw_timer;
1644b769371SNicolas Ferre 	struct timer_list	uart_timer;
1652c7af5baSBoris BREZILLON 
1662c7af5baSBoris BREZILLON 	bool			suspended;
1672c7af5baSBoris BREZILLON 	unsigned int		pending;
1682c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1692c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1702c7af5baSBoris BREZILLON 
171a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
172a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
173a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
174a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
175a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
176a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
177ab4382d2SGreg Kroah-Hartman };
178ab4382d2SGreg Kroah-Hartman 
179ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
180503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
181ab4382d2SGreg Kroah-Hartman 
182ab4382d2SGreg Kroah-Hartman #ifdef SUPPORT_SYSRQ
183ab4382d2SGreg Kroah-Hartman static struct console atmel_console;
184ab4382d2SGreg Kroah-Hartman #endif
185ab4382d2SGreg Kroah-Hartman 
1865fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1875fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
1885fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91rm9200-usart" },
1895fbe46b6SNicolas Ferre 	{ .compatible = "atmel,at91sam9260-usart" },
1905fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1915fbe46b6SNicolas Ferre };
1925fbe46b6SNicolas Ferre #endif
1935fbe46b6SNicolas Ferre 
194ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
195ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
196ab4382d2SGreg Kroah-Hartman {
197ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
198ab4382d2SGreg Kroah-Hartman }
199ab4382d2SGreg Kroah-Hartman 
2004e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2014e7decdaSCyrille Pitchen {
2024e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2034e7decdaSCyrille Pitchen }
2044e7decdaSCyrille Pitchen 
2054e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2064e7decdaSCyrille Pitchen {
2074e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2084e7decdaSCyrille Pitchen }
2094e7decdaSCyrille Pitchen 
210a6499435SCyrille Pitchen #ifdef CONFIG_AVR32
211a6499435SCyrille Pitchen 
212a6499435SCyrille Pitchen /* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
213a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
214b5199d46SCyrille Pitchen {
215a6499435SCyrille Pitchen 	return __raw_readl(port->membase + ATMEL_US_RHR);
216b5199d46SCyrille Pitchen }
217b5199d46SCyrille Pitchen 
218a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
219b5199d46SCyrille Pitchen {
220a6499435SCyrille Pitchen 	__raw_writel(value, port->membase + ATMEL_US_THR);
221b5199d46SCyrille Pitchen }
222b5199d46SCyrille Pitchen 
223a6499435SCyrille Pitchen #else
224a6499435SCyrille Pitchen 
225a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
226a6499435SCyrille Pitchen {
227a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
228a6499435SCyrille Pitchen }
229a6499435SCyrille Pitchen 
230a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
231a6499435SCyrille Pitchen {
232a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
233a6499435SCyrille Pitchen }
234a6499435SCyrille Pitchen 
235a6499435SCyrille Pitchen #endif
236a6499435SCyrille Pitchen 
237ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
23864e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
239ab4382d2SGreg Kroah-Hartman {
240ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
241ab4382d2SGreg Kroah-Hartman 
24264e22ebeSElen Song 	return atmel_port->use_pdc_rx;
243ab4382d2SGreg Kroah-Hartman }
244ab4382d2SGreg Kroah-Hartman 
24564e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
246ab4382d2SGreg Kroah-Hartman {
247ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
248ab4382d2SGreg Kroah-Hartman 
24964e22ebeSElen Song 	return atmel_port->use_pdc_tx;
250ab4382d2SGreg Kroah-Hartman }
251ab4382d2SGreg Kroah-Hartman #else
25264e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
253ab4382d2SGreg Kroah-Hartman {
254ab4382d2SGreg Kroah-Hartman 	return false;
255ab4382d2SGreg Kroah-Hartman }
256ab4382d2SGreg Kroah-Hartman 
25764e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
258ab4382d2SGreg Kroah-Hartman {
259ab4382d2SGreg Kroah-Hartman 	return false;
260ab4382d2SGreg Kroah-Hartman }
261ab4382d2SGreg Kroah-Hartman #endif
262ab4382d2SGreg Kroah-Hartman 
26308f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26408f738beSElen Song {
26508f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26608f738beSElen Song 
26708f738beSElen Song 	return atmel_port->use_dma_tx;
26808f738beSElen Song }
26908f738beSElen Song 
27034df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27134df42f5SElen Song {
27234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27334df42f5SElen Song 
27434df42f5SElen Song 	return atmel_port->use_dma_rx;
27534df42f5SElen Song }
27634df42f5SElen Song 
277*5be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
278*5be605acSAlexandre Belloni {
279*5be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
280*5be605acSAlexandre Belloni 
281*5be605acSAlexandre Belloni 	return atmel_port->fifo_size;
282*5be605acSAlexandre Belloni }
283*5be605acSAlexandre Belloni 
284e0b0baadSRichard Genoud static unsigned int atmel_get_lines_status(struct uart_port *port)
285e0b0baadSRichard Genoud {
286e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
287e0b0baadSRichard Genoud 	unsigned int status, ret = 0;
288e0b0baadSRichard Genoud 
2894e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
290e0b0baadSRichard Genoud 
291e0b0baadSRichard Genoud 	mctrl_gpio_get(atmel_port->gpios, &ret);
292e0b0baadSRichard Genoud 
293e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
294e0b0baadSRichard Genoud 						UART_GPIO_CTS))) {
295e0b0baadSRichard Genoud 		if (ret & TIOCM_CTS)
296e0b0baadSRichard Genoud 			status &= ~ATMEL_US_CTS;
297e0b0baadSRichard Genoud 		else
298e0b0baadSRichard Genoud 			status |= ATMEL_US_CTS;
299e0b0baadSRichard Genoud 	}
300e0b0baadSRichard Genoud 
301e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
302e0b0baadSRichard Genoud 						UART_GPIO_DSR))) {
303e0b0baadSRichard Genoud 		if (ret & TIOCM_DSR)
304e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DSR;
305e0b0baadSRichard Genoud 		else
306e0b0baadSRichard Genoud 			status |= ATMEL_US_DSR;
307e0b0baadSRichard Genoud 	}
308e0b0baadSRichard Genoud 
309e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
310e0b0baadSRichard Genoud 						UART_GPIO_RI))) {
311e0b0baadSRichard Genoud 		if (ret & TIOCM_RI)
312e0b0baadSRichard Genoud 			status &= ~ATMEL_US_RI;
313e0b0baadSRichard Genoud 		else
314e0b0baadSRichard Genoud 			status |= ATMEL_US_RI;
315e0b0baadSRichard Genoud 	}
316e0b0baadSRichard Genoud 
317e0b0baadSRichard Genoud 	if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
318e0b0baadSRichard Genoud 						UART_GPIO_DCD))) {
319e0b0baadSRichard Genoud 		if (ret & TIOCM_CD)
320e0b0baadSRichard Genoud 			status &= ~ATMEL_US_DCD;
321e0b0baadSRichard Genoud 		else
322e0b0baadSRichard Genoud 			status |= ATMEL_US_DCD;
323e0b0baadSRichard Genoud 	}
324e0b0baadSRichard Genoud 
325e0b0baadSRichard Genoud 	return status;
326e0b0baadSRichard Genoud }
327e0b0baadSRichard Genoud 
328ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
32913bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
33013bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
331ab4382d2SGreg Kroah-Hartman {
332ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
333ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
334ab4382d2SGreg Kroah-Hartman 
335ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
337ab4382d2SGreg Kroah-Hartman 
3384e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
339ab4382d2SGreg Kroah-Hartman 
340ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
341ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
342ab4382d2SGreg Kroah-Hartman 
34313bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
344ab4382d2SGreg Kroah-Hartman 
345ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
346ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
347ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3494e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
350ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
351ab4382d2SGreg Kroah-Hartman 	} else {
352ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
35364e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
354ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
355ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
356ab4382d2SGreg Kroah-Hartman 		else
357ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
358ab4382d2SGreg Kroah-Hartman 	}
3594e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
360ab4382d2SGreg Kroah-Hartman 
361ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3624e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
363ab4382d2SGreg Kroah-Hartman 
36413bd3e6fSRicardo Ribalda Delgado 	return 0;
365ab4382d2SGreg Kroah-Hartman }
366ab4382d2SGreg Kroah-Hartman 
367ab4382d2SGreg Kroah-Hartman /*
368ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
369ab4382d2SGreg Kroah-Hartman  */
370ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
371ab4382d2SGreg Kroah-Hartman {
3724e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
3734e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
3744e7decdaSCyrille Pitchen 		0;
375ab4382d2SGreg Kroah-Hartman }
376ab4382d2SGreg Kroah-Hartman 
377ab4382d2SGreg Kroah-Hartman /*
378ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
379ab4382d2SGreg Kroah-Hartman  */
380ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
381ab4382d2SGreg Kroah-Hartman {
382ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
3834e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
3841cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
385ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
386ab4382d2SGreg Kroah-Hartman 
3871cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
3881cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
3894e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3904e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
3911cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
3921cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
3931cf6e8fcSCyrille Pitchen 	}
3941cf6e8fcSCyrille Pitchen 
3951cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
3961cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
3971cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
3981cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
3991cf6e8fcSCyrille Pitchen 
4001cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4011cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4021cf6e8fcSCyrille Pitchen 	} else {
4031cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4041cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4051cf6e8fcSCyrille Pitchen 
4061cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4071cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4081cf6e8fcSCyrille Pitchen 	}
4091cf6e8fcSCyrille Pitchen 
410ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4111cf6e8fcSCyrille Pitchen 		control |= rts_ready;
412ab4382d2SGreg Kroah-Hartman 	else
4131cf6e8fcSCyrille Pitchen 		control |= rts_paused;
414ab4382d2SGreg Kroah-Hartman 
415ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
416ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
417ab4382d2SGreg Kroah-Hartman 	else
418ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
419ab4382d2SGreg Kroah-Hartman 
4204e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
421ab4382d2SGreg Kroah-Hartman 
422e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
423e0b0baadSRichard Genoud 
424ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
4251cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
426ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
427ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
428ab4382d2SGreg Kroah-Hartman 	else
429ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
430ab4382d2SGreg Kroah-Hartman 
4314e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
432ab4382d2SGreg Kroah-Hartman }
433ab4382d2SGreg Kroah-Hartman 
434ab4382d2SGreg Kroah-Hartman /*
435ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
436ab4382d2SGreg Kroah-Hartman  */
437ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
438ab4382d2SGreg Kroah-Hartman {
439e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
440e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
441ab4382d2SGreg Kroah-Hartman 
4424e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
443ab4382d2SGreg Kroah-Hartman 
444ab4382d2SGreg Kroah-Hartman 	/*
445ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
446ab4382d2SGreg Kroah-Hartman 	 */
447ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
448ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
449ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
450ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
451ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
452ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
453ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
454ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
455ab4382d2SGreg Kroah-Hartman 
456e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
457ab4382d2SGreg Kroah-Hartman }
458ab4382d2SGreg Kroah-Hartman 
459ab4382d2SGreg Kroah-Hartman /*
460ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
461ab4382d2SGreg Kroah-Hartman  */
462ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
463ab4382d2SGreg Kroah-Hartman {
464ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
465ab4382d2SGreg Kroah-Hartman 
46664e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
467ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
4684e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
469ab4382d2SGreg Kroah-Hartman 	}
470ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
4714e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
472ab4382d2SGreg Kroah-Hartman 
47313bd3e6fSRicardo Ribalda Delgado 	if ((port->rs485.flags & SER_RS485_ENABLED) &&
47413bd3e6fSRicardo Ribalda Delgado 	    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
475ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
476ab4382d2SGreg Kroah-Hartman }
477ab4382d2SGreg Kroah-Hartman 
478ab4382d2SGreg Kroah-Hartman /*
479ab4382d2SGreg Kroah-Hartman  * Start transmitting.
480ab4382d2SGreg Kroah-Hartman  */
481ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
482ab4382d2SGreg Kroah-Hartman {
483ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
484ab4382d2SGreg Kroah-Hartman 
48564e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
4864e7decdaSCyrille Pitchen 		if (atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN)
487ab4382d2SGreg Kroah-Hartman 			/* The transmitter is already running.  Yes, we
488ab4382d2SGreg Kroah-Hartman 			   really need this.*/
489ab4382d2SGreg Kroah-Hartman 			return;
490ab4382d2SGreg Kroah-Hartman 
49113bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
49213bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX))
493ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
494ab4382d2SGreg Kroah-Hartman 
495ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
4964e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
497ab4382d2SGreg Kroah-Hartman 	}
498ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
4994e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
500ab4382d2SGreg Kroah-Hartman }
501ab4382d2SGreg Kroah-Hartman 
502ab4382d2SGreg Kroah-Hartman /*
503ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
504ab4382d2SGreg Kroah-Hartman  */
505ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
506ab4382d2SGreg Kroah-Hartman {
5074e7decdaSCyrille Pitchen 	/* reset status and receiver */
5084e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
509ab4382d2SGreg Kroah-Hartman 
5104e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
51157c36868SSiftar, Gabe 
51264e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
513ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
5144e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
5154e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
516ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
5174e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
518ab4382d2SGreg Kroah-Hartman 	} else {
5194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
520ab4382d2SGreg Kroah-Hartman 	}
521ab4382d2SGreg Kroah-Hartman }
522ab4382d2SGreg Kroah-Hartman 
523ab4382d2SGreg Kroah-Hartman /*
524ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
525ab4382d2SGreg Kroah-Hartman  */
526ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
527ab4382d2SGreg Kroah-Hartman {
5284e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
52957c36868SSiftar, Gabe 
53064e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
531ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
5324e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
5334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
5344e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
535ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
536ab4382d2SGreg Kroah-Hartman 	} else {
5374e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
538ab4382d2SGreg Kroah-Hartman 	}
539ab4382d2SGreg Kroah-Hartman }
540ab4382d2SGreg Kroah-Hartman 
541ab4382d2SGreg Kroah-Hartman /*
542ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
543ab4382d2SGreg Kroah-Hartman  */
544ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
545ab4382d2SGreg Kroah-Hartman {
546ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
547ab5e4e41SRichard Genoud 	uint32_t ier = 0;
548ab5e4e41SRichard Genoud 
549ab5e4e41SRichard Genoud 	/*
550ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
551ab5e4e41SRichard Genoud 	 */
552ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
553ab5e4e41SRichard Genoud 		return;
554ab5e4e41SRichard Genoud 
555ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
556ab5e4e41SRichard Genoud 
55718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
558ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
559ab5e4e41SRichard Genoud 
56018dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
561ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
562ab5e4e41SRichard Genoud 
56318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
564ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
565ab5e4e41SRichard Genoud 
56618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
567ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
568ab5e4e41SRichard Genoud 
5694e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
57018dfef9cSUwe Kleine-König 
57118dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
572ab4382d2SGreg Kroah-Hartman }
573ab4382d2SGreg Kroah-Hartman 
574ab4382d2SGreg Kroah-Hartman /*
57535b675b9SRichard Genoud  * Disable modem status interrupts
57635b675b9SRichard Genoud  */
57735b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
57835b675b9SRichard Genoud {
57935b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
58035b675b9SRichard Genoud 	uint32_t idr = 0;
58135b675b9SRichard Genoud 
58235b675b9SRichard Genoud 	/*
58335b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
58435b675b9SRichard Genoud 	 */
58535b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
58635b675b9SRichard Genoud 		return;
58735b675b9SRichard Genoud 
58835b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
58935b675b9SRichard Genoud 
59018dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
59118dfef9cSUwe Kleine-König 
59218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
59335b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
59435b675b9SRichard Genoud 
59518dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
59635b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
59735b675b9SRichard Genoud 
59818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
59935b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
60035b675b9SRichard Genoud 
60118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
60235b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
60335b675b9SRichard Genoud 
6044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
60535b675b9SRichard Genoud }
60635b675b9SRichard Genoud 
60735b675b9SRichard Genoud /*
608ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
609ab4382d2SGreg Kroah-Hartman  */
610ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
611ab4382d2SGreg Kroah-Hartman {
612ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
6134e7decdaSCyrille Pitchen 		/* start break */
6144e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
615ab4382d2SGreg Kroah-Hartman 	else
6164e7decdaSCyrille Pitchen 		/* stop break */
6174e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
618ab4382d2SGreg Kroah-Hartman }
619ab4382d2SGreg Kroah-Hartman 
620ab4382d2SGreg Kroah-Hartman /*
621ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
622ab4382d2SGreg Kroah-Hartman  */
623ab4382d2SGreg Kroah-Hartman static void
624ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
625ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
626ab4382d2SGreg Kroah-Hartman {
627ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
628ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
629ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
630ab4382d2SGreg Kroah-Hartman 
631ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
632ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
633ab4382d2SGreg Kroah-Hartman 		return;
634ab4382d2SGreg Kroah-Hartman 
635ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
636ab4382d2SGreg Kroah-Hartman 	c->status	= status;
637ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
638ab4382d2SGreg Kroah-Hartman 
639ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
640ab4382d2SGreg Kroah-Hartman 	smp_wmb();
641ab4382d2SGreg Kroah-Hartman 
642ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
643ab4382d2SGreg Kroah-Hartman }
644ab4382d2SGreg Kroah-Hartman 
645ab4382d2SGreg Kroah-Hartman /*
646ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
647ab4382d2SGreg Kroah-Hartman  */
648ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
649ab4382d2SGreg Kroah-Hartman {
650ab4382d2SGreg Kroah-Hartman 	/* clear error */
6514e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
652ab4382d2SGreg Kroah-Hartman 
653ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
654ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
655ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
656ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
657ab4382d2SGreg Kroah-Hartman 	}
658ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
659ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
660ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
661ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
662ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
663ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
664ab4382d2SGreg Kroah-Hartman }
665ab4382d2SGreg Kroah-Hartman 
666ab4382d2SGreg Kroah-Hartman /*
667ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
668ab4382d2SGreg Kroah-Hartman  */
669ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
670ab4382d2SGreg Kroah-Hartman {
671ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
672ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
673ab4382d2SGreg Kroah-Hartman 
6744e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
675ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
676a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
677ab4382d2SGreg Kroah-Hartman 
678ab4382d2SGreg Kroah-Hartman 		/*
679ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
680ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
681ab4382d2SGreg Kroah-Hartman 		 */
682ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
683ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
684ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
685ab4382d2SGreg Kroah-Hartman 
686ab4382d2SGreg Kroah-Hartman 			/* clear error */
6874e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
688ab4382d2SGreg Kroah-Hartman 
689ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
690ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
691ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
6924e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
6934e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
694ab4382d2SGreg Kroah-Hartman 			} else {
695ab4382d2SGreg Kroah-Hartman 				/*
696ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
697ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
698ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
699ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
700ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
701ab4382d2SGreg Kroah-Hartman 				 */
7024e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
7034e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
704ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
705ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
706ab4382d2SGreg Kroah-Hartman 			}
707ab4382d2SGreg Kroah-Hartman 		}
708ab4382d2SGreg Kroah-Hartman 
709ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
7104e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
711ab4382d2SGreg Kroah-Hartman 	}
712ab4382d2SGreg Kroah-Hartman 
713ab4382d2SGreg Kroah-Hartman 	tasklet_schedule(&atmel_port->tasklet);
714ab4382d2SGreg Kroah-Hartman }
715ab4382d2SGreg Kroah-Hartman 
716ab4382d2SGreg Kroah-Hartman /*
717ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
718ab4382d2SGreg Kroah-Hartman  * disabled)
719ab4382d2SGreg Kroah-Hartman  */
720ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
721ab4382d2SGreg Kroah-Hartman {
722ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
723ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
724ab4382d2SGreg Kroah-Hartman 
7254e7decdaSCyrille Pitchen 	if (port->x_char &&
7264e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
727a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
728ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
729ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
730ab4382d2SGreg Kroah-Hartman 	}
731ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
732ab4382d2SGreg Kroah-Hartman 		return;
733ab4382d2SGreg Kroah-Hartman 
7344e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
7354e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
736a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
737ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
738ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
739ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
740ab4382d2SGreg Kroah-Hartman 			break;
741ab4382d2SGreg Kroah-Hartman 	}
742ab4382d2SGreg Kroah-Hartman 
743ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
744ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
745ab4382d2SGreg Kroah-Hartman 
746ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
747ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
7484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
7494e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
750ab4382d2SGreg Kroah-Hartman }
751ab4382d2SGreg Kroah-Hartman 
75208f738beSElen Song static void atmel_complete_tx_dma(void *arg)
75308f738beSElen Song {
75408f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
75508f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
75608f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
75708f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
75808f738beSElen Song 	unsigned long flags;
75908f738beSElen Song 
76008f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
76108f738beSElen Song 
76208f738beSElen Song 	if (chan)
76308f738beSElen Song 		dmaengine_terminate_all(chan);
7645f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
76508f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
76608f738beSElen Song 
7675f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
76808f738beSElen Song 
76908f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
77008f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
77108f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
77208f738beSElen Song 	atmel_port->desc_tx = NULL;
77308f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
77408f738beSElen Song 
77508f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
77608f738beSElen Song 		uart_write_wakeup(port);
77708f738beSElen Song 
7781842dc2eSCyrille Pitchen 	/*
7791842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
7801842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
7811842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
7821842dc2eSCyrille Pitchen 	 */
78308f738beSElen Song 	if (!uart_circ_empty(xmit))
78408f738beSElen Song 		tasklet_schedule(&atmel_port->tasklet);
78508f738beSElen Song 
78608f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
78708f738beSElen Song }
78808f738beSElen Song 
78908f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
79008f738beSElen Song {
79108f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
79208f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
79308f738beSElen Song 
79408f738beSElen Song 	if (chan) {
79508f738beSElen Song 		dmaengine_terminate_all(chan);
79608f738beSElen Song 		dma_release_channel(chan);
79708f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
79848479148SWolfram Sang 				DMA_TO_DEVICE);
79908f738beSElen Song 	}
80008f738beSElen Song 
80108f738beSElen Song 	atmel_port->desc_tx = NULL;
80208f738beSElen Song 	atmel_port->chan_tx = NULL;
80308f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
80408f738beSElen Song }
80508f738beSElen Song 
80608f738beSElen Song /*
80708f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
80808f738beSElen Song  */
80908f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
81008f738beSElen Song {
81108f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
81208f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
81308f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
81408f738beSElen Song 	struct dma_async_tx_descriptor *desc;
8155f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
8165f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
8175f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
81808f738beSElen Song 
81908f738beSElen Song 	/* Make sure we have an idle channel */
82008f738beSElen Song 	if (atmel_port->desc_tx != NULL)
82108f738beSElen Song 		return;
82208f738beSElen Song 
82308f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
82408f738beSElen Song 		/*
82508f738beSElen Song 		 * DMA is idle now.
82608f738beSElen Song 		 * Port xmit buffer is already mapped,
82708f738beSElen Song 		 * and it is one page... Just adjust
82808f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
82908f738beSElen Song 		 * we have to transmit till the end, and then the rest.
83008f738beSElen Song 		 * Take the port lock to get a
83108f738beSElen Song 		 * consistent xmit buffer state.
83208f738beSElen Song 		 */
8335f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
83408f738beSElen Song 					 xmit->tail,
83508f738beSElen Song 					 UART_XMIT_SIZE);
8365f258b3eSCyrille Pitchen 
8375f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
8385f258b3eSCyrille Pitchen 			/* multi data mode */
8395f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
8405f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
8415f258b3eSCyrille Pitchen 		} else {
8425f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
8435f258b3eSCyrille Pitchen 			part1_len = 0;
8445f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
8455f258b3eSCyrille Pitchen 		}
8465f258b3eSCyrille Pitchen 
8475f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
8485f258b3eSCyrille Pitchen 		sg_len = 0;
8495f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
8505f258b3eSCyrille Pitchen 		if (part1_len) {
8515f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8525f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8535f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
8545f258b3eSCyrille Pitchen 
8555f258b3eSCyrille Pitchen 			phys_addr += part1_len;
8565f258b3eSCyrille Pitchen 		}
8575f258b3eSCyrille Pitchen 
8585f258b3eSCyrille Pitchen 		if (part2_len) {
8595f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
8605f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
8615f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
8625f258b3eSCyrille Pitchen 		}
8635f258b3eSCyrille Pitchen 
8645f258b3eSCyrille Pitchen 		/*
8655f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
8665f258b3eSCyrille Pitchen 		 * xmit->tail correctly
8675f258b3eSCyrille Pitchen 		 */
8685f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
86908f738beSElen Song 
87008f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
8715f258b3eSCyrille Pitchen 					       sgl,
8725f258b3eSCyrille Pitchen 					       sg_len,
87308f738beSElen Song 					       DMA_MEM_TO_DEV,
87408f738beSElen Song 					       DMA_PREP_INTERRUPT |
87508f738beSElen Song 					       DMA_CTRL_ACK);
87608f738beSElen Song 		if (!desc) {
87708f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
87808f738beSElen Song 			return;
87908f738beSElen Song 		}
88008f738beSElen Song 
8815f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
88208f738beSElen Song 
88308f738beSElen Song 		atmel_port->desc_tx = desc;
88408f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
88508f738beSElen Song 		desc->callback_param = atmel_port;
88608f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
88708f738beSElen Song 
88808f738beSElen Song 	} else {
88913bd3e6fSRicardo Ribalda Delgado 		if (port->rs485.flags & SER_RS485_ENABLED) {
89008f738beSElen Song 			/* DMA done, stop TX, start RX for RS485 */
89108f738beSElen Song 			atmel_start_rx(port);
89208f738beSElen Song 		}
89308f738beSElen Song 	}
89408f738beSElen Song 
89508f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
89608f738beSElen Song 		uart_write_wakeup(port);
89708f738beSElen Song }
89808f738beSElen Song 
89908f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
90008f738beSElen Song {
90108f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
90208f738beSElen Song 	dma_cap_mask_t		mask;
90308f738beSElen Song 	struct dma_slave_config config;
90408f738beSElen Song 	int ret, nent;
90508f738beSElen Song 
90608f738beSElen Song 	dma_cap_zero(mask);
90708f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
90808f738beSElen Song 
90908f738beSElen Song 	atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
91008f738beSElen Song 	if (atmel_port->chan_tx == NULL)
91108f738beSElen Song 		goto chan_err;
91208f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
91308f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
91408f738beSElen Song 
91508f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
91608f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
91708f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
9182c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
91908f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
92008f738beSElen Song 			virt_to_page(port->state->xmit.buf),
92108f738beSElen Song 			UART_XMIT_SIZE,
922c8d1f022SUwe Kleine-König 			(unsigned long)port->state->xmit.buf & ~PAGE_MASK);
92308f738beSElen Song 	nent = dma_map_sg(port->dev,
92408f738beSElen Song 				&atmel_port->sg_tx,
92508f738beSElen Song 				1,
92648479148SWolfram Sang 				DMA_TO_DEVICE);
92708f738beSElen Song 
92808f738beSElen Song 	if (!nent) {
92908f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
93008f738beSElen Song 		goto chan_err;
93108f738beSElen Song 	} else {
932c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
93308f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
93408f738beSElen Song 			port->state->xmit.buf,
935c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
93608f738beSElen Song 	}
93708f738beSElen Song 
93808f738beSElen Song 	/* Configure the slave DMA */
93908f738beSElen Song 	memset(&config, 0, sizeof(config));
94008f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
9415f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
9425f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
9435f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
94408f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
945a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
94608f738beSElen Song 
9475483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
9485483c10eSMaxime Ripard 				     &config);
94908f738beSElen Song 	if (ret) {
95008f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
95108f738beSElen Song 		goto chan_err;
95208f738beSElen Song 	}
95308f738beSElen Song 
95408f738beSElen Song 	return 0;
95508f738beSElen Song 
95608f738beSElen Song chan_err:
95708f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
95808f738beSElen Song 	atmel_port->use_dma_tx = 0;
95908f738beSElen Song 	if (atmel_port->chan_tx)
96008f738beSElen Song 		atmel_release_tx_dma(port);
96108f738beSElen Song 	return -EINVAL;
96208f738beSElen Song }
96308f738beSElen Song 
96434df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
96534df42f5SElen Song {
96634df42f5SElen Song 	struct uart_port *port = arg;
96734df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
96834df42f5SElen Song 
96934df42f5SElen Song 	tasklet_schedule(&atmel_port->tasklet);
97034df42f5SElen Song }
97134df42f5SElen Song 
97234df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
97334df42f5SElen Song {
97434df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
97534df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
97634df42f5SElen Song 
97734df42f5SElen Song 	if (chan) {
97834df42f5SElen Song 		dmaengine_terminate_all(chan);
97934df42f5SElen Song 		dma_release_channel(chan);
98034df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
98148479148SWolfram Sang 				DMA_FROM_DEVICE);
98234df42f5SElen Song 	}
98334df42f5SElen Song 
98434df42f5SElen Song 	atmel_port->desc_rx = NULL;
98534df42f5SElen Song 	atmel_port->chan_rx = NULL;
98634df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
98734df42f5SElen Song }
98834df42f5SElen Song 
98934df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
99034df42f5SElen Song {
99134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
99266f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
99334df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
99434df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
99534df42f5SElen Song 	struct dma_tx_state state;
99634df42f5SElen Song 	enum dma_status dmastat;
99766f37aafSCyrille Pitchen 	size_t count;
99834df42f5SElen Song 
99934df42f5SElen Song 
100034df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
10014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
100234df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
100334df42f5SElen Song 				atmel_port->cookie_rx,
100434df42f5SElen Song 				&state);
100534df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
100634df42f5SElen Song 	if (dmastat == DMA_ERROR) {
100734df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
10084e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
100934df42f5SElen Song 		tasklet_schedule(&atmel_port->tasklet);
101034df42f5SElen Song 		return;
101134df42f5SElen Song 	}
101266f37aafSCyrille Pitchen 
101366f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
101466f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
101566f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
101666f37aafSCyrille Pitchen 			    1,
1017485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
101834df42f5SElen Song 
101934df42f5SElen Song 	/*
102066f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
102166f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
102266f37aafSCyrille Pitchen 	 * framework.
102366f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
102466f37aafSCyrille Pitchen 	 * length.
102534df42f5SElen Song 	 */
102666f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
102766f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
102866f37aafSCyrille Pitchen 	/*
102966f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
103066f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
103166f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
103266f37aafSCyrille Pitchen 	 *
103366f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
103466f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
103566f37aafSCyrille Pitchen 	 *
103666f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
103766f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
103866f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
103966f37aafSCyrille Pitchen 	 */
104066f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
104166f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
104234df42f5SElen Song 
104366f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
104466f37aafSCyrille Pitchen 		ring->tail = 0;
104534df42f5SElen Song 		port->icount.rx += count;
104634df42f5SElen Song 	}
104734df42f5SElen Song 
104866f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
104966f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
105066f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
105166f37aafSCyrille Pitchen 
105266f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
105366f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
105466f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
105566f37aafSCyrille Pitchen 			ring->head = 0;
105666f37aafSCyrille Pitchen 		ring->tail = ring->head;
105766f37aafSCyrille Pitchen 		port->icount.rx += count;
105866f37aafSCyrille Pitchen 	}
105966f37aafSCyrille Pitchen 
106066f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
106166f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
106266f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
106366f37aafSCyrille Pitchen 			       1,
1064485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
106566f37aafSCyrille Pitchen 
106666f37aafSCyrille Pitchen 	/*
106766f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
106866f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
106966f37aafSCyrille Pitchen 	 */
107066f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
107166f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
107266f37aafSCyrille Pitchen 	spin_lock(&port->lock);
107366f37aafSCyrille Pitchen 
10744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
107534df42f5SElen Song }
107634df42f5SElen Song 
107734df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
107834df42f5SElen Song {
107934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
108034df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
108134df42f5SElen Song 	dma_cap_mask_t		mask;
108234df42f5SElen Song 	struct dma_slave_config config;
108334df42f5SElen Song 	struct circ_buf		*ring;
108434df42f5SElen Song 	int ret, nent;
108534df42f5SElen Song 
108634df42f5SElen Song 	ring = &atmel_port->rx_ring;
108734df42f5SElen Song 
108834df42f5SElen Song 	dma_cap_zero(mask);
108934df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
109034df42f5SElen Song 
109134df42f5SElen Song 	atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
109234df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
109334df42f5SElen Song 		goto chan_err;
109434df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
109534df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
109634df42f5SElen Song 
109734df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
109834df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
109934df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
11002c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
110134df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
110234df42f5SElen Song 		    virt_to_page(ring->buf),
1103a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
1104c8d1f022SUwe Kleine-König 		    (unsigned long)ring->buf & ~PAGE_MASK);
110534df42f5SElen Song 	nent = dma_map_sg(port->dev,
110634df42f5SElen Song 			  &atmel_port->sg_rx,
110734df42f5SElen Song 			  1,
110848479148SWolfram Sang 			  DMA_FROM_DEVICE);
110934df42f5SElen Song 
111034df42f5SElen Song 	if (!nent) {
111134df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
111234df42f5SElen Song 		goto chan_err;
111334df42f5SElen Song 	} else {
1114c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
111534df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
111634df42f5SElen Song 			ring->buf,
1117c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
111834df42f5SElen Song 	}
111934df42f5SElen Song 
112034df42f5SElen Song 	/* Configure the slave DMA */
112134df42f5SElen Song 	memset(&config, 0, sizeof(config));
112234df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
112334df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
112434df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1125a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
112634df42f5SElen Song 
11275483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
11285483c10eSMaxime Ripard 				     &config);
112934df42f5SElen Song 	if (ret) {
113034df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
113134df42f5SElen Song 		goto chan_err;
113234df42f5SElen Song 	}
113334df42f5SElen Song 	/*
113434df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
113534df42f5SElen Song 	 * each one is half ring buffer size
113634df42f5SElen Song 	 */
113734df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
113834df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
113934df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
114034df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
114134df42f5SElen Song 					 DMA_DEV_TO_MEM,
114234df42f5SElen Song 					 DMA_PREP_INTERRUPT);
114334df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
114434df42f5SElen Song 	desc->callback_param = port;
114534df42f5SElen Song 	atmel_port->desc_rx = desc;
114634df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
114734df42f5SElen Song 
114834df42f5SElen Song 	return 0;
114934df42f5SElen Song 
115034df42f5SElen Song chan_err:
115134df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
115234df42f5SElen Song 	atmel_port->use_dma_rx = 0;
115334df42f5SElen Song 	if (atmel_port->chan_rx)
115434df42f5SElen Song 		atmel_release_rx_dma(port);
115534df42f5SElen Song 	return -EINVAL;
115634df42f5SElen Song }
115734df42f5SElen Song 
11582e68c22fSElen Song static void atmel_uart_timer_callback(unsigned long data)
11592e68c22fSElen Song {
11602e68c22fSElen Song 	struct uart_port *port = (void *)data;
11612e68c22fSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
11622e68c22fSElen Song 
11632e68c22fSElen Song 	tasklet_schedule(&atmel_port->tasklet);
11642e68c22fSElen Song 	mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
11652e68c22fSElen Song }
11662e68c22fSElen Song 
1167ab4382d2SGreg Kroah-Hartman /*
1168ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1169ab4382d2SGreg Kroah-Hartman  */
1170ab4382d2SGreg Kroah-Hartman static void
1171ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1172ab4382d2SGreg Kroah-Hartman {
1173ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1174ab4382d2SGreg Kroah-Hartman 
117564e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1176ab4382d2SGreg Kroah-Hartman 		/*
1177ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1178ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1179ab4382d2SGreg Kroah-Hartman 		 *
1180ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1181ab4382d2SGreg Kroah-Hartman 		 * the moment.
1182ab4382d2SGreg Kroah-Hartman 		 */
1183ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
11844e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
11854e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
1186ab4382d2SGreg Kroah-Hartman 			tasklet_schedule(&atmel_port->tasklet);
1187ab4382d2SGreg Kroah-Hartman 		}
1188ab4382d2SGreg Kroah-Hartman 
1189ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1190ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1191ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1192ab4382d2SGreg Kroah-Hartman 	}
1193ab4382d2SGreg Kroah-Hartman 
119434df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
119534df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
11964e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
11974e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
119834df42f5SElen Song 			tasklet_schedule(&atmel_port->tasklet);
119934df42f5SElen Song 		}
120034df42f5SElen Song 	}
120134df42f5SElen Song 
1202ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1203ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1204ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1205ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1206ab4382d2SGreg Kroah-Hartman 		/*
1207ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1208ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1209ab4382d2SGreg Kroah-Hartman 		 */
12104e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
12114e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1212ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1213ab4382d2SGreg Kroah-Hartman 	}
1214ab4382d2SGreg Kroah-Hartman }
1215ab4382d2SGreg Kroah-Hartman 
1216ab4382d2SGreg Kroah-Hartman /*
1217ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1218ab4382d2SGreg Kroah-Hartman  */
1219ab4382d2SGreg Kroah-Hartman static void
1220ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1221ab4382d2SGreg Kroah-Hartman {
1222ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1223ab4382d2SGreg Kroah-Hartman 
1224ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
1225ab4382d2SGreg Kroah-Hartman 		/* Either PDC or interrupt transmission */
12264e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
12274e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1228ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1229ab4382d2SGreg Kroah-Hartman 	}
1230ab4382d2SGreg Kroah-Hartman }
1231ab4382d2SGreg Kroah-Hartman 
1232ab4382d2SGreg Kroah-Hartman /*
1233ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1234ab4382d2SGreg Kroah-Hartman  */
1235ab4382d2SGreg Kroah-Hartman static void
1236ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1237ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1238ab4382d2SGreg Kroah-Hartman {
1239ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1240ab4382d2SGreg Kroah-Hartman 
1241ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1242ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
1243ab4382d2SGreg Kroah-Hartman 		atmel_port->irq_status = status;
1244d033e82dSLeilei Zhao 		atmel_port->status_change = atmel_port->irq_status ^
1245d033e82dSLeilei Zhao 					    atmel_port->irq_status_prev;
1246d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
1247ab4382d2SGreg Kroah-Hartman 		tasklet_schedule(&atmel_port->tasklet);
1248ab4382d2SGreg Kroah-Hartman 	}
1249ab4382d2SGreg Kroah-Hartman }
1250ab4382d2SGreg Kroah-Hartman 
1251ab4382d2SGreg Kroah-Hartman /*
1252ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1253ab4382d2SGreg Kroah-Hartman  */
1254ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1255ab4382d2SGreg Kroah-Hartman {
1256ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1257ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
12582c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1259ab4382d2SGreg Kroah-Hartman 
12602c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
12612c7af5baSBoris BREZILLON 
1262ab4382d2SGreg Kroah-Hartman 	do {
1263e0b0baadSRichard Genoud 		status = atmel_get_lines_status(port);
12644e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
12652c7af5baSBoris BREZILLON 		pending = status & mask;
1266ab4382d2SGreg Kroah-Hartman 		if (!pending)
1267ab4382d2SGreg Kroah-Hartman 			break;
1268ab4382d2SGreg Kroah-Hartman 
12692c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
12702c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
12712c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
12724e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
12732c7af5baSBoris BREZILLON 			pm_system_wakeup();
12742c7af5baSBoris BREZILLON 			break;
12752c7af5baSBoris BREZILLON 		}
12762c7af5baSBoris BREZILLON 
1277ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1278ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1279ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1280ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1281ab4382d2SGreg Kroah-Hartman 
12822c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
12832c7af5baSBoris BREZILLON 
1284ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1285ab4382d2SGreg Kroah-Hartman }
1286ab4382d2SGreg Kroah-Hartman 
1287a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1288a930e528SElen Song {
1289a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1290a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1291a930e528SElen Song 
1292a930e528SElen Song 	dma_unmap_single(port->dev,
1293a930e528SElen Song 			 pdc->dma_addr,
1294a930e528SElen Song 			 pdc->dma_size,
1295a930e528SElen Song 			 DMA_TO_DEVICE);
1296a930e528SElen Song }
1297a930e528SElen Song 
1298ab4382d2SGreg Kroah-Hartman /*
1299ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1300ab4382d2SGreg Kroah-Hartman  */
130164e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1302ab4382d2SGreg Kroah-Hartman {
1303ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1304ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1305ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1306ab4382d2SGreg Kroah-Hartman 	int count;
1307ab4382d2SGreg Kroah-Hartman 
1308ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
13094e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1310ab4382d2SGreg Kroah-Hartman 		return;
1311ab4382d2SGreg Kroah-Hartman 
1312ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1313ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1314ab4382d2SGreg Kroah-Hartman 
1315ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1316ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1317ab4382d2SGreg Kroah-Hartman 
1318ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1319ab4382d2SGreg Kroah-Hartman 
1320ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
13214e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1322ab4382d2SGreg Kroah-Hartman 
1323ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1324ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1325ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1326ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1327ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1328ab4382d2SGreg Kroah-Hartman 
1329ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1330ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1331ab4382d2SGreg Kroah-Hartman 
13324e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
13334e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
13344e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1335ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
13364e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1337ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
13384e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
13394e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1340ab4382d2SGreg Kroah-Hartman 	} else {
134113bd3e6fSRicardo Ribalda Delgado 		if ((port->rs485.flags & SER_RS485_ENABLED) &&
134213bd3e6fSRicardo Ribalda Delgado 		    !(port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1343ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1344ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1345ab4382d2SGreg Kroah-Hartman 		}
1346ab4382d2SGreg Kroah-Hartman 	}
1347ab4382d2SGreg Kroah-Hartman 
1348ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1349ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1350ab4382d2SGreg Kroah-Hartman }
1351ab4382d2SGreg Kroah-Hartman 
1352a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1353a930e528SElen Song {
1354a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1355a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1356a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1357a930e528SElen Song 
1358a930e528SElen Song 	pdc->buf = xmit->buf;
1359a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1360a930e528SElen Song 					pdc->buf,
1361a930e528SElen Song 					UART_XMIT_SIZE,
1362a930e528SElen Song 					DMA_TO_DEVICE);
1363a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1364a930e528SElen Song 	pdc->ofs = 0;
1365a930e528SElen Song 
1366a930e528SElen Song 	return 0;
1367a930e528SElen Song }
1368a930e528SElen Song 
1369ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1370ab4382d2SGreg Kroah-Hartman {
1371ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1372ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1373ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1374ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1375ab4382d2SGreg Kroah-Hartman 
1376ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1377ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1378ab4382d2SGreg Kroah-Hartman 
1379ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1380ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1381ab4382d2SGreg Kroah-Hartman 
1382ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1383ab4382d2SGreg Kroah-Hartman 
1384ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1385ab4382d2SGreg Kroah-Hartman 
1386ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1387ab4382d2SGreg Kroah-Hartman 		status = c.status;
1388ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1389ab4382d2SGreg Kroah-Hartman 
1390ab4382d2SGreg Kroah-Hartman 		/*
1391ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1392ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1393ab4382d2SGreg Kroah-Hartman 		 */
1394ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1395ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1396ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1397ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1398ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1399ab4382d2SGreg Kroah-Hartman 
1400ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1401ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1402ab4382d2SGreg Kroah-Hartman 					continue;
1403ab4382d2SGreg Kroah-Hartman 			}
1404ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1405ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1406ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1407ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1408ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1409ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1410ab4382d2SGreg Kroah-Hartman 
1411ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1412ab4382d2SGreg Kroah-Hartman 
1413ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1414ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1415ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1416ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1417ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1418ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1419ab4382d2SGreg Kroah-Hartman 		}
1420ab4382d2SGreg Kroah-Hartman 
1421ab4382d2SGreg Kroah-Hartman 
1422ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1423ab4382d2SGreg Kroah-Hartman 			continue;
1424ab4382d2SGreg Kroah-Hartman 
1425ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1426ab4382d2SGreg Kroah-Hartman 	}
1427ab4382d2SGreg Kroah-Hartman 
1428ab4382d2SGreg Kroah-Hartman 	/*
1429ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1430ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1431ab4382d2SGreg Kroah-Hartman 	 */
1432ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
14332e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1434ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1435ab4382d2SGreg Kroah-Hartman }
1436ab4382d2SGreg Kroah-Hartman 
1437a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1438a930e528SElen Song {
1439a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1440a930e528SElen Song 	int i;
1441a930e528SElen Song 
1442a930e528SElen Song 	for (i = 0; i < 2; i++) {
1443a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1444a930e528SElen Song 
1445a930e528SElen Song 		dma_unmap_single(port->dev,
1446a930e528SElen Song 				 pdc->dma_addr,
1447a930e528SElen Song 				 pdc->dma_size,
1448a930e528SElen Song 				 DMA_FROM_DEVICE);
1449a930e528SElen Song 		kfree(pdc->buf);
1450a930e528SElen Song 	}
1451a930e528SElen Song }
1452a930e528SElen Song 
145364e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1454ab4382d2SGreg Kroah-Hartman {
1455ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
145605c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1457ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1458ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1459ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1460ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1461ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1462ab4382d2SGreg Kroah-Hartman 
1463ab4382d2SGreg Kroah-Hartman 	do {
1464ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
14654e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1466ab4382d2SGreg Kroah-Hartman 
1467ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
14684e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1469ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1470ab4382d2SGreg Kroah-Hartman 
1471ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1472ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1473ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1474ab4382d2SGreg Kroah-Hartman 		 * find out.
1475ab4382d2SGreg Kroah-Hartman 		 *
1476ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1477ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1478ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1479ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1480ab4382d2SGreg Kroah-Hartman 		 */
1481ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1482ab4382d2SGreg Kroah-Hartman 
1483ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1484ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1485ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1486ab4382d2SGreg Kroah-Hartman 
1487ab4382d2SGreg Kroah-Hartman 			/*
1488ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1489ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1490ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1491ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1492ab4382d2SGreg Kroah-Hartman 			 */
1493ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1494ab4382d2SGreg Kroah-Hartman 
149505c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
149605c7cd39SJiri Slaby 						count);
1497ab4382d2SGreg Kroah-Hartman 
1498ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1499ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1500ab4382d2SGreg Kroah-Hartman 
1501ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1502ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1503ab4382d2SGreg Kroah-Hartman 		}
1504ab4382d2SGreg Kroah-Hartman 
1505ab4382d2SGreg Kroah-Hartman 		/*
1506ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1507ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1508ab4382d2SGreg Kroah-Hartman 		 */
1509ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1510ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
15114e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
15124e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1513ab4382d2SGreg Kroah-Hartman 
1514ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1515ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1516ab4382d2SGreg Kroah-Hartman 		}
1517ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1518ab4382d2SGreg Kroah-Hartman 
1519ab4382d2SGreg Kroah-Hartman 	/*
1520ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1521ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1522ab4382d2SGreg Kroah-Hartman 	 */
1523ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15242e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1525ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1526ab4382d2SGreg Kroah-Hartman 
15274e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
15284e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1529ab4382d2SGreg Kroah-Hartman }
1530ab4382d2SGreg Kroah-Hartman 
1531a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1532a930e528SElen Song {
1533a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1534a930e528SElen Song 	int i;
1535a930e528SElen Song 
1536a930e528SElen Song 	for (i = 0; i < 2; i++) {
1537a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1538a930e528SElen Song 
1539a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1540a930e528SElen Song 		if (pdc->buf == NULL) {
1541a930e528SElen Song 			if (i != 0) {
1542a930e528SElen Song 				dma_unmap_single(port->dev,
1543a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1544a930e528SElen Song 					PDC_BUFFER_SIZE,
1545a930e528SElen Song 					DMA_FROM_DEVICE);
1546a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1547a930e528SElen Song 			}
1548a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1549a930e528SElen Song 			return -ENOMEM;
1550a930e528SElen Song 		}
1551a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1552a930e528SElen Song 						pdc->buf,
1553a930e528SElen Song 						PDC_BUFFER_SIZE,
1554a930e528SElen Song 						DMA_FROM_DEVICE);
1555a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1556a930e528SElen Song 		pdc->ofs = 0;
1557a930e528SElen Song 	}
1558a930e528SElen Song 
1559a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1560a930e528SElen Song 
15614e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
15624e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1563a930e528SElen Song 
15644e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
15654e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
15664e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1567a930e528SElen Song 
1568a930e528SElen Song 	return 0;
1569a930e528SElen Song }
1570a930e528SElen Song 
1571ab4382d2SGreg Kroah-Hartman /*
1572ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1573ab4382d2SGreg Kroah-Hartman  */
1574ab4382d2SGreg Kroah-Hartman static void atmel_tasklet_func(unsigned long data)
1575ab4382d2SGreg Kroah-Hartman {
1576ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1577ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1578d033e82dSLeilei Zhao 	unsigned int status = atmel_port->irq_status;
1579d033e82dSLeilei Zhao 	unsigned int status_change = atmel_port->status_change;
1580ab4382d2SGreg Kroah-Hartman 
1581ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1582ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1583ab4382d2SGreg Kroah-Hartman 
1584a930e528SElen Song 	atmel_port->schedule_tx(port);
1585ab4382d2SGreg Kroah-Hartman 
1586ab4382d2SGreg Kroah-Hartman 	if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1587ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_DCD | ATMEL_US_CTS)) {
1588ab4382d2SGreg Kroah-Hartman 		/* TODO: All reads to CSR will clear these interrupts! */
1589ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_RI)
1590ab4382d2SGreg Kroah-Hartman 			port->icount.rng++;
1591ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DSR)
1592ab4382d2SGreg Kroah-Hartman 			port->icount.dsr++;
1593ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_DCD)
1594ab4382d2SGreg Kroah-Hartman 			uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1595ab4382d2SGreg Kroah-Hartman 		if (status_change & ATMEL_US_CTS)
1596ab4382d2SGreg Kroah-Hartman 			uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1597ab4382d2SGreg Kroah-Hartman 
1598ab4382d2SGreg Kroah-Hartman 		wake_up_interruptible(&port->state->port.delta_msr_wait);
1599ab4382d2SGreg Kroah-Hartman 
1600d033e82dSLeilei Zhao 		atmel_port->status_change = 0;
1601ab4382d2SGreg Kroah-Hartman 	}
1602ab4382d2SGreg Kroah-Hartman 
1603a930e528SElen Song 	atmel_port->schedule_rx(port);
1604ab4382d2SGreg Kroah-Hartman 
1605ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1606ab4382d2SGreg Kroah-Hartman }
1607ab4382d2SGreg Kroah-Hartman 
16084a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
160933d64c4fSElen Song 				struct platform_device *pdev)
161033d64c4fSElen Song {
161133d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1612574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
161333d64c4fSElen Song 
161433d64c4fSElen Song 	if (np) {
161533d64c4fSElen Song 		/* DMA/PDC usage specification */
161633d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
161733d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
161833d64c4fSElen Song 				atmel_port->use_dma_rx  = true;
161933d64c4fSElen Song 				atmel_port->use_pdc_rx  = false;
162033d64c4fSElen Song 			} else {
162133d64c4fSElen Song 				atmel_port->use_dma_rx  = false;
162233d64c4fSElen Song 				atmel_port->use_pdc_rx  = true;
162333d64c4fSElen Song 			}
162433d64c4fSElen Song 		} else {
162533d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
162633d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
162733d64c4fSElen Song 		}
162833d64c4fSElen Song 
162933d64c4fSElen Song 		if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
163033d64c4fSElen Song 			if (of_get_property(np, "dmas", NULL)) {
163133d64c4fSElen Song 				atmel_port->use_dma_tx  = true;
163233d64c4fSElen Song 				atmel_port->use_pdc_tx  = false;
163333d64c4fSElen Song 			} else {
163433d64c4fSElen Song 				atmel_port->use_dma_tx  = false;
163533d64c4fSElen Song 				atmel_port->use_pdc_tx  = true;
163633d64c4fSElen Song 			}
163733d64c4fSElen Song 		} else {
163833d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
163933d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
164033d64c4fSElen Song 		}
164133d64c4fSElen Song 
164233d64c4fSElen Song 	} else {
164333d64c4fSElen Song 		atmel_port->use_pdc_rx  = pdata->use_dma_rx;
164433d64c4fSElen Song 		atmel_port->use_pdc_tx  = pdata->use_dma_tx;
164533d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
164633d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
164733d64c4fSElen Song 	}
164833d64c4fSElen Song 
164933d64c4fSElen Song }
165033d64c4fSElen Song 
165113bd3e6fSRicardo Ribalda Delgado static void atmel_init_rs485(struct uart_port *port,
165233d64c4fSElen Song 				struct platform_device *pdev)
165333d64c4fSElen Song {
165433d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
1655574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
165633d64c4fSElen Song 
165733d64c4fSElen Song 	if (np) {
165877bdec6fSJiri Slaby 		struct serial_rs485 *rs485conf = &port->rs485;
165933d64c4fSElen Song 		u32 rs485_delay[2];
166033d64c4fSElen Song 		/* rs485 properties */
166133d64c4fSElen Song 		if (of_property_read_u32_array(np, "rs485-rts-delay",
166233d64c4fSElen Song 					rs485_delay, 2) == 0) {
166333d64c4fSElen Song 			rs485conf->delay_rts_before_send = rs485_delay[0];
166433d64c4fSElen Song 			rs485conf->delay_rts_after_send = rs485_delay[1];
166533d64c4fSElen Song 			rs485conf->flags = 0;
166677bdec6fSJiri Slaby 		}
166733d64c4fSElen Song 
166833d64c4fSElen Song 		if (of_get_property(np, "rs485-rx-during-tx", NULL))
166933d64c4fSElen Song 			rs485conf->flags |= SER_RS485_RX_DURING_TX;
167033d64c4fSElen Song 
167133d64c4fSElen Song 		if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
167233d64c4fSElen Song 								NULL))
167333d64c4fSElen Song 			rs485conf->flags |= SER_RS485_ENABLED;
167433d64c4fSElen Song 	} else {
167513bd3e6fSRicardo Ribalda Delgado 		port->rs485       = pdata->rs485;
167633d64c4fSElen Song 	}
167733d64c4fSElen Song 
167833d64c4fSElen Song }
167933d64c4fSElen Song 
1680a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1681a930e528SElen Song {
1682a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1683a930e528SElen Song 
168434df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
168534df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
168634df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
168734df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
168834df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1689a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1690a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1691a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1692a930e528SElen Song 	} else {
1693a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1694a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1695a930e528SElen Song 		atmel_port->release_rx = NULL;
1696a930e528SElen Song 	}
1697a930e528SElen Song 
169808f738beSElen Song 	if (atmel_use_dma_tx(port)) {
169908f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
170008f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
170108f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
170208f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1703a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1704a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1705a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1706a930e528SElen Song 	} else {
1707a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1708a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1709a930e528SElen Song 		atmel_port->release_tx = NULL;
1710a930e528SElen Song 	}
1711a930e528SElen Song }
1712a930e528SElen Song 
1713ab4382d2SGreg Kroah-Hartman /*
1714055560b0SElen Song  * Get ip name usart or uart
1715055560b0SElen Song  */
1716892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1717055560b0SElen Song {
1718055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
17194e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1720731d9caeSNicolas Ferre 	u32 version;
17211d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
17224b769371SNicolas Ferre 	/* ASCII decoding for IP version */
17234b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
17244b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
17251d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1726055560b0SElen Song 
17274b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
1728055560b0SElen Song 
17292958cceeSLudovic Desroches 	if (name == new_uart) {
17302958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
17314b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
17322958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
17332958cceeSLudovic Desroches 	} else if (name == usart) {
17342958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
17352958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
17362958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
17374b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
17384b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1739055560b0SElen Song 	} else {
1740731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
17414e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1742731d9caeSNicolas Ferre 		switch (version) {
1743731d9caeSNicolas Ferre 		case 0x302:
1744731d9caeSNicolas Ferre 		case 0x10213:
1745731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
17464b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
17472958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1748731d9caeSNicolas Ferre 			break;
1749731d9caeSNicolas Ferre 		case 0x203:
1750731d9caeSNicolas Ferre 		case 0x10202:
1751731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1752731d9caeSNicolas Ferre 			break;
1753731d9caeSNicolas Ferre 		default:
1754731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1755731d9caeSNicolas Ferre 		}
1756055560b0SElen Song 	}
1757055560b0SElen Song }
1758055560b0SElen Song 
1759055560b0SElen Song /*
1760ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1761ab4382d2SGreg Kroah-Hartman  */
1762ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1763ab4382d2SGreg Kroah-Hartman {
176433d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1765ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1766ab4382d2SGreg Kroah-Hartman 	struct tty_struct *tty = port->state->port.tty;
1767ab4382d2SGreg Kroah-Hartman 	int retval;
1768ab4382d2SGreg Kroah-Hartman 
1769ab4382d2SGreg Kroah-Hartman 	/*
1770ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1771ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1772ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1773ab4382d2SGreg Kroah-Hartman 	 */
17744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1775ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1776ab4382d2SGreg Kroah-Hartman 
1777ab4382d2SGreg Kroah-Hartman 	/*
1778ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1779ab4382d2SGreg Kroah-Hartman 	 */
17802c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
17812c7af5baSBoris BREZILLON 			IRQF_SHARED | IRQF_COND_SUSPEND,
1782ab4382d2SGreg Kroah-Hartman 			tty ? tty->name : "atmel_serial", port);
1783ab4382d2SGreg Kroah-Hartman 	if (retval) {
1784ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1785ab4382d2SGreg Kroah-Hartman 		return retval;
1786ab4382d2SGreg Kroah-Hartman 	}
1787ab4382d2SGreg Kroah-Hartman 
17881e125786SLeilei Zhao 	tasklet_enable(&atmel_port->tasklet);
17891e125786SLeilei Zhao 
1790ab5e4e41SRichard Genoud 	/*
1791ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1792ab4382d2SGreg Kroah-Hartman 	 */
179333d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
17944d9628a1SLeilei Zhao 	atmel_set_ops(port);
179533d64c4fSElen Song 
1796a930e528SElen Song 	if (atmel_port->prepare_rx) {
1797a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1798a930e528SElen Song 		if (retval < 0)
1799a930e528SElen Song 			atmel_set_ops(port);
1800ab4382d2SGreg Kroah-Hartman 	}
1801ab4382d2SGreg Kroah-Hartman 
1802a930e528SElen Song 	if (atmel_port->prepare_tx) {
1803a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1804a930e528SElen Song 		if (retval < 0)
1805a930e528SElen Song 			atmel_set_ops(port);
1806ab4382d2SGreg Kroah-Hartman 	}
1807ab4382d2SGreg Kroah-Hartman 
1808b5199d46SCyrille Pitchen 	/*
1809b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1810b5199d46SCyrille Pitchen 	 */
1811b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1812b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1813b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1814b5199d46SCyrille Pitchen 		unsigned int fmr;
1815b5199d46SCyrille Pitchen 
1816b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1817b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1818b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1819b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1820b5199d46SCyrille Pitchen 
18215f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
18225f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
18235f258b3eSCyrille Pitchen 
1824b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1825b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1826b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1827b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1828b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1829b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1830b5199d46SCyrille Pitchen 
1831b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1832b5199d46SCyrille Pitchen 	}
1833b5199d46SCyrille Pitchen 
1834ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1835e0b0baadSRichard Genoud 	atmel_port->irq_status_prev = atmel_get_lines_status(port);
1836ab4382d2SGreg Kroah-Hartman 	atmel_port->irq_status = atmel_port->irq_status_prev;
1837ab4382d2SGreg Kroah-Hartman 
1838ab4382d2SGreg Kroah-Hartman 	/*
1839ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1840ab4382d2SGreg Kroah-Hartman 	 */
18414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1842ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
18434e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1844ab4382d2SGreg Kroah-Hartman 
18452e68c22fSElen Song 	setup_timer(&atmel_port->uart_timer,
18462e68c22fSElen Song 			atmel_uart_timer_callback,
18472e68c22fSElen Song 			(unsigned long)port);
18488bc661bfSMarek Roszko 
18498bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
18508bc661bfSMarek Roszko 		/* set UART timeout */
18514b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18522e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18532e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18542e68c22fSElen Song 		/* set USART timeout */
18552e68c22fSElen Song 		} else {
18562958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18572958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18584e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1859ab4382d2SGreg Kroah-Hartman 
18604e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18614e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
18622e68c22fSElen Song 		}
1863ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
18644e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
186534df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
18662e68c22fSElen Song 		/* set UART timeout */
18674b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
18682e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
18692e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
18702e68c22fSElen Song 		/* set USART timeout */
18712e68c22fSElen Song 		} else {
18722958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
18732958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
18744e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
187534df42f5SElen Song 
18764e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
18774e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
18782e68c22fSElen Song 		}
1879ab4382d2SGreg Kroah-Hartman 	} else {
1880ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
18814e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1882ab4382d2SGreg Kroah-Hartman 	}
1883ab4382d2SGreg Kroah-Hartman 
1884ab4382d2SGreg Kroah-Hartman 	return 0;
1885ab4382d2SGreg Kroah-Hartman }
1886ab4382d2SGreg Kroah-Hartman 
1887ab4382d2SGreg Kroah-Hartman /*
1888479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
1889479e9b94SPeter Hurley  * buffer is reset.
1890479e9b94SPeter Hurley  */
1891479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
1892479e9b94SPeter Hurley {
1893479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1894479e9b94SPeter Hurley 
1895479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
18964e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1897479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
1898479e9b94SPeter Hurley 	}
1899479e9b94SPeter Hurley }
1900479e9b94SPeter Hurley 
1901479e9b94SPeter Hurley /*
1902ab4382d2SGreg Kroah-Hartman  * Disable the port
1903ab4382d2SGreg Kroah-Hartman  */
1904ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
1905ab4382d2SGreg Kroah-Hartman {
1906ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
19070cc7c6c7SMarek Roszko 
1908ab4382d2SGreg Kroah-Hartman 	/*
19098bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
19108bc661bfSMarek Roszko 	 * cleanup
19118bc661bfSMarek Roszko 	 */
19128bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
19138bc661bfSMarek Roszko 
19148bc661bfSMarek Roszko 	/*
19150cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
19160cc7c6c7SMarek Roszko 	 * we destroy the buffers
19170cc7c6c7SMarek Roszko 	 */
19181e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
19190cc7c6c7SMarek Roszko 	tasklet_kill(&atmel_port->tasklet);
19200cc7c6c7SMarek Roszko 
19210cc7c6c7SMarek Roszko 	/*
19220cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
19230cc7c6c7SMarek Roszko 	 * disable all interrupts, port and break condition.
1924ab4382d2SGreg Kroah-Hartman 	 */
1925ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
1926ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
1927ab4382d2SGreg Kroah-Hartman 
19284e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
19294e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
19300cc7c6c7SMarek Roszko 
19310cc7c6c7SMarek Roszko 
1932ab4382d2SGreg Kroah-Hartman 	/*
1933ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
1934ab4382d2SGreg Kroah-Hartman 	 */
1935a930e528SElen Song 	if (atmel_port->release_rx)
1936a930e528SElen Song 		atmel_port->release_rx(port);
1937a930e528SElen Song 	if (atmel_port->release_tx)
1938a930e528SElen Song 		atmel_port->release_tx(port);
1939ab4382d2SGreg Kroah-Hartman 
1940ab4382d2SGreg Kroah-Hartman 	/*
1941bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
1942bb7e73c5SMark Deneen 	 */
1943bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
1944bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
1945bb7e73c5SMark Deneen 
1946bb7e73c5SMark Deneen 	/*
1947ab5e4e41SRichard Genoud 	 * Free the interrupts
1948ab4382d2SGreg Kroah-Hartman 	 */
1949ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
1950ab5e4e41SRichard Genoud 
1951ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1952ab4382d2SGreg Kroah-Hartman 
1953479e9b94SPeter Hurley 	atmel_flush_buffer(port);
1954ab4382d2SGreg Kroah-Hartman }
1955ab4382d2SGreg Kroah-Hartman 
1956ab4382d2SGreg Kroah-Hartman /*
1957ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
1958ab4382d2SGreg Kroah-Hartman  */
1959ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1960ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
1961ab4382d2SGreg Kroah-Hartman {
1962ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1963ab4382d2SGreg Kroah-Hartman 
1964ab4382d2SGreg Kroah-Hartman 	switch (state) {
1965ab4382d2SGreg Kroah-Hartman 	case 0:
1966ab4382d2SGreg Kroah-Hartman 		/*
1967ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
1968ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
1969ab4382d2SGreg Kroah-Hartman 		 */
197091f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
1971ab4382d2SGreg Kroah-Hartman 
1972ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
19734e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
1974ab4382d2SGreg Kroah-Hartman 		break;
1975ab4382d2SGreg Kroah-Hartman 	case 3:
1976ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
19774e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
19784e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
1979ab4382d2SGreg Kroah-Hartman 
1980ab4382d2SGreg Kroah-Hartman 		/*
1981ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
1982ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
1983ab4382d2SGreg Kroah-Hartman 		 */
198491f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
1985ab4382d2SGreg Kroah-Hartman 		break;
1986ab4382d2SGreg Kroah-Hartman 	default:
1987ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
1988ab4382d2SGreg Kroah-Hartman 	}
1989ab4382d2SGreg Kroah-Hartman }
1990ab4382d2SGreg Kroah-Hartman 
1991ab4382d2SGreg Kroah-Hartman /*
1992ab4382d2SGreg Kroah-Hartman  * Change the port parameters
1993ab4382d2SGreg Kroah-Hartman  */
1994ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1995ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
1996ab4382d2SGreg Kroah-Hartman {
1997ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
19981cf6e8fcSCyrille Pitchen 	unsigned int old_mode, mode, imr, quot, baud;
1999ab4382d2SGreg Kroah-Hartman 
20001cf6e8fcSCyrille Pitchen 	/* save the current mode register */
20014e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
20021cf6e8fcSCyrille Pitchen 
20031cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
20041cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
20051cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2006ab4382d2SGreg Kroah-Hartman 
2007ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2008ab4382d2SGreg Kroah-Hartman 	quot = uart_get_divisor(port, baud);
2009ab4382d2SGreg Kroah-Hartman 
2010ab4382d2SGreg Kroah-Hartman 	if (quot > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
2011ab4382d2SGreg Kroah-Hartman 		quot /= 8;
2012ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
2013ab4382d2SGreg Kroah-Hartman 	}
2014ab4382d2SGreg Kroah-Hartman 
2015ab4382d2SGreg Kroah-Hartman 	/* byte size */
2016ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2017ab4382d2SGreg Kroah-Hartman 	case CS5:
2018ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2019ab4382d2SGreg Kroah-Hartman 		break;
2020ab4382d2SGreg Kroah-Hartman 	case CS6:
2021ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2022ab4382d2SGreg Kroah-Hartman 		break;
2023ab4382d2SGreg Kroah-Hartman 	case CS7:
2024ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2025ab4382d2SGreg Kroah-Hartman 		break;
2026ab4382d2SGreg Kroah-Hartman 	default:
2027ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2028ab4382d2SGreg Kroah-Hartman 		break;
2029ab4382d2SGreg Kroah-Hartman 	}
2030ab4382d2SGreg Kroah-Hartman 
2031ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2032ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2033ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2034ab4382d2SGreg Kroah-Hartman 
2035ab4382d2SGreg Kroah-Hartman 	/* parity */
2036ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2037ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2038ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2039ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2040ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2041ab4382d2SGreg Kroah-Hartman 			else
2042ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2043ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2044ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2045ab4382d2SGreg Kroah-Hartman 		else
2046ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2047ab4382d2SGreg Kroah-Hartman 	} else
2048ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2049ab4382d2SGreg Kroah-Hartman 
2050ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2051ab4382d2SGreg Kroah-Hartman 
2052ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2053ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2054ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2055ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2056ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2057ab4382d2SGreg Kroah-Hartman 
205864e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2059ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
20604e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2061ab4382d2SGreg Kroah-Hartman 
2062ab4382d2SGreg Kroah-Hartman 	/*
2063ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2064ab4382d2SGreg Kroah-Hartman 	 */
2065ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2066ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2067ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2068ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2069ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2070ab4382d2SGreg Kroah-Hartman 		/*
2071ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2072ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2073ab4382d2SGreg Kroah-Hartman 		 */
2074ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2075ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2076ab4382d2SGreg Kroah-Hartman 	}
2077ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2078ab4382d2SGreg Kroah-Hartman 
2079ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2080ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2081ab4382d2SGreg Kroah-Hartman 
2082ab4382d2SGreg Kroah-Hartman 	/*
2083ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2084ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2085ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2086ab4382d2SGreg Kroah-Hartman 	 */
20874e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
20884e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2089ab4382d2SGreg Kroah-Hartman 
2090ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
20914e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2092ab4382d2SGreg Kroah-Hartman 
20931cf6e8fcSCyrille Pitchen 	/* mode */
209413bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
20954e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
20964e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2097ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
20981cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
20991cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
2100*5be605acSAlexandre Belloni 		if (atmel_use_dma_rx(port) && !atmel_use_fifo(port)) {
2101*5be605acSAlexandre Belloni 			dev_info(port->dev, "not enabling hardware flow control because DMA is used");
2102*5be605acSAlexandre Belloni 			termios->c_cflag &= ~CRTSCTS;
2103*5be605acSAlexandre Belloni 		} else {
21041cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
2105*5be605acSAlexandre Belloni 		}
21061cf6e8fcSCyrille Pitchen 	} else {
21071cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
21081cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2109ab4382d2SGreg Kroah-Hartman 	}
2110ab4382d2SGreg Kroah-Hartman 
21111cf6e8fcSCyrille Pitchen 	/* set the mode, clock divisor, parity, stop bits and data size */
21124e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2113ab4382d2SGreg Kroah-Hartman 
21141cf6e8fcSCyrille Pitchen 	/*
21151cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
21161cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
21171cf6e8fcSCyrille Pitchen 	 */
21181cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
21191cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
21201cf6e8fcSCyrille Pitchen 
21211cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
21221cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
21231cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
21241cf6e8fcSCyrille Pitchen 		} else {
21251cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
21261cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
21271cf6e8fcSCyrille Pitchen 		}
21281cf6e8fcSCyrille Pitchen 
21294e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
21301cf6e8fcSCyrille Pitchen 	}
21311cf6e8fcSCyrille Pitchen 
2132ab4382d2SGreg Kroah-Hartman 	/* set the baud rate */
21334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_BRGR, quot);
21344e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
21354e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2136ab4382d2SGreg Kroah-Hartman 
2137ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
21384e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2139ab4382d2SGreg Kroah-Hartman 
2140ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2141ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
214235b675b9SRichard Genoud 		atmel_enable_ms(port);
214335b675b9SRichard Genoud 	else
214435b675b9SRichard Genoud 		atmel_disable_ms(port);
2145ab4382d2SGreg Kroah-Hartman 
2146ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2147ab4382d2SGreg Kroah-Hartman }
2148ab4382d2SGreg Kroah-Hartman 
2149732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
215042bd7a4fSViktar Palstsiuk {
2151732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
215242bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2153d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
215442bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2155d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
215642bd7a4fSViktar Palstsiuk 	} else {
215742bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2158cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2159cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2160cab68f89SPeter Hurley 			atmel_disable_ms(port);
2161cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2162cab68f89SPeter Hurley 		}
216342bd7a4fSViktar Palstsiuk 	}
216442bd7a4fSViktar Palstsiuk }
216542bd7a4fSViktar Palstsiuk 
2166ab4382d2SGreg Kroah-Hartman /*
2167ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2168ab4382d2SGreg Kroah-Hartman  */
2169ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2170ab4382d2SGreg Kroah-Hartman {
2171ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2172ab4382d2SGreg Kroah-Hartman }
2173ab4382d2SGreg Kroah-Hartman 
2174ab4382d2SGreg Kroah-Hartman /*
2175ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2176ab4382d2SGreg Kroah-Hartman  */
2177ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2178ab4382d2SGreg Kroah-Hartman {
2179ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2180ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2181ab4382d2SGreg Kroah-Hartman 
2182ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2183ab4382d2SGreg Kroah-Hartman 
2184ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2185ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2186ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2187ab4382d2SGreg Kroah-Hartman 	}
2188ab4382d2SGreg Kroah-Hartman }
2189ab4382d2SGreg Kroah-Hartman 
2190ab4382d2SGreg Kroah-Hartman /*
2191ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2192ab4382d2SGreg Kroah-Hartman  */
2193ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2194ab4382d2SGreg Kroah-Hartman {
2195ab4382d2SGreg Kroah-Hartman 	struct platform_device *pdev = to_platform_device(port->dev);
2196ab4382d2SGreg Kroah-Hartman 	int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2197ab4382d2SGreg Kroah-Hartman 
2198ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2199ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2200ab4382d2SGreg Kroah-Hartman 
2201ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2202ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2203ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2204ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2205ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2206ab4382d2SGreg Kroah-Hartman 		}
2207ab4382d2SGreg Kroah-Hartman 	}
2208ab4382d2SGreg Kroah-Hartman 
2209ab4382d2SGreg Kroah-Hartman 	return 0;
2210ab4382d2SGreg Kroah-Hartman }
2211ab4382d2SGreg Kroah-Hartman 
2212ab4382d2SGreg Kroah-Hartman /*
2213ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2214ab4382d2SGreg Kroah-Hartman  */
2215ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2216ab4382d2SGreg Kroah-Hartman {
2217ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2218ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2219ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2220ab4382d2SGreg Kroah-Hartman 	}
2221ab4382d2SGreg Kroah-Hartman }
2222ab4382d2SGreg Kroah-Hartman 
2223ab4382d2SGreg Kroah-Hartman /*
2224ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2225ab4382d2SGreg Kroah-Hartman  */
2226ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2227ab4382d2SGreg Kroah-Hartman {
2228ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2229ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2230ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2231ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2232ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2233ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2234ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2235ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2236ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2237270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2238ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2239ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2240ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2241ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2242ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2243ab4382d2SGreg Kroah-Hartman 	return ret;
2244ab4382d2SGreg Kroah-Hartman }
2245ab4382d2SGreg Kroah-Hartman 
2246ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2247ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2248ab4382d2SGreg Kroah-Hartman {
22494e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2250ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2251ab4382d2SGreg Kroah-Hartman 
2252a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2253ab4382d2SGreg Kroah-Hartman }
2254ab4382d2SGreg Kroah-Hartman 
2255ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2256ab4382d2SGreg Kroah-Hartman {
22574e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2258ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2259ab4382d2SGreg Kroah-Hartman 
2260a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2261ab4382d2SGreg Kroah-Hartman }
2262ab4382d2SGreg Kroah-Hartman #endif
2263ab4382d2SGreg Kroah-Hartman 
2264ab4382d2SGreg Kroah-Hartman static struct uart_ops atmel_pops = {
2265ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2266ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2267ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2268ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2269ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2270ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2271ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2272ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2273ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2274ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2275ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2276ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
227742bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2278ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2279ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2280ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2281ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2282ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2283ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2284ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2285ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2286ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2287ab4382d2SGreg Kroah-Hartman #endif
2288ab4382d2SGreg Kroah-Hartman };
2289ab4382d2SGreg Kroah-Hartman 
2290ab4382d2SGreg Kroah-Hartman /*
2291ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2292ab4382d2SGreg Kroah-Hartman  */
229391f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2294ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2295ab4382d2SGreg Kroah-Hartman {
229691f8c2d8SBoris BREZILLON 	int ret;
2297ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2298574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2299ab4382d2SGreg Kroah-Hartman 
23004a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2301a930e528SElen Song 	atmel_set_ops(port);
2302a930e528SElen Song 
230313bd3e6fSRicardo Ribalda Delgado 	atmel_init_rs485(port, pdev);
230433d64c4fSElen Song 
2305ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
2306ab4382d2SGreg Kroah-Hartman 	port->flags		= UPF_BOOT_AUTOCONF;
2307ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2308ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2309ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2310ab4382d2SGreg Kroah-Hartman 	port->mapbase	= pdev->resource[0].start;
2311ab4382d2SGreg Kroah-Hartman 	port->irq	= pdev->resource[1].start;
231213bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2313ab4382d2SGreg Kroah-Hartman 
2314ab4382d2SGreg Kroah-Hartman 	tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2315ab4382d2SGreg Kroah-Hartman 			(unsigned long)port);
23161e125786SLeilei Zhao 	tasklet_disable(&atmel_port->tasklet);
2317ab4382d2SGreg Kroah-Hartman 
2318ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2319ab4382d2SGreg Kroah-Hartman 
23205fbe46b6SNicolas Ferre 	if (pdata && pdata->regs) {
2321ab4382d2SGreg Kroah-Hartman 		/* Already mapped by setup code */
23221acfc7ecSNicolas Ferre 		port->membase = pdata->regs;
2323588edbf3SNicolas Ferre 	} else {
2324ab4382d2SGreg Kroah-Hartman 		port->flags	|= UPF_IOREMAP;
2325ab4382d2SGreg Kroah-Hartman 		port->membase	= NULL;
2326ab4382d2SGreg Kroah-Hartman 	}
2327ab4382d2SGreg Kroah-Hartman 
2328ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2329ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2330ab4382d2SGreg Kroah-Hartman 		atmel_port->clk = clk_get(&pdev->dev, "usart");
233191f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
233291f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
233391f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
233491f8c2d8SBoris BREZILLON 			return ret;
233591f8c2d8SBoris BREZILLON 		}
233691f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
233791f8c2d8SBoris BREZILLON 		if (ret) {
233891f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
233991f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
234091f8c2d8SBoris BREZILLON 			return ret;
234191f8c2d8SBoris BREZILLON 		}
2342ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
234391f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2344ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2345ab4382d2SGreg Kroah-Hartman 	}
2346ab4382d2SGreg Kroah-Hartman 
2347ab4382d2SGreg Kroah-Hartman 	/* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
234813bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED)
2349ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
235064e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2351ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2352ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2353ab4382d2SGreg Kroah-Hartman 	} else {
2354ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2355ab4382d2SGreg Kroah-Hartman 	}
235691f8c2d8SBoris BREZILLON 
235791f8c2d8SBoris BREZILLON 	return 0;
2358ab4382d2SGreg Kroah-Hartman }
2359ab4382d2SGreg Kroah-Hartman 
236069f6a27bSJean-Christophe PLAGNIOL-VILLARD struct platform_device *atmel_default_console_device;	/* the serial console device */
236169f6a27bSJean-Christophe PLAGNIOL-VILLARD 
2362ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2363ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2364ab4382d2SGreg Kroah-Hartman {
23654e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2366ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2367a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2368ab4382d2SGreg Kroah-Hartman }
2369ab4382d2SGreg Kroah-Hartman 
2370ab4382d2SGreg Kroah-Hartman /*
2371ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2372ab4382d2SGreg Kroah-Hartman  */
2373ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2374ab4382d2SGreg Kroah-Hartman {
2375ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2376ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2377ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2378ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2379ab4382d2SGreg Kroah-Hartman 
2380ab4382d2SGreg Kroah-Hartman 	/*
2381ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2382ab4382d2SGreg Kroah-Hartman 	 */
23834e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
23844e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
23854e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2386ab4382d2SGreg Kroah-Hartman 
2387ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
23884e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
23894e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2390ab4382d2SGreg Kroah-Hartman 
2391ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2392ab4382d2SGreg Kroah-Hartman 
2393ab4382d2SGreg Kroah-Hartman 	/*
2394ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2395ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2396ab4382d2SGreg Kroah-Hartman 	 */
2397ab4382d2SGreg Kroah-Hartman 	do {
23984e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2399ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2400ab4382d2SGreg Kroah-Hartman 
2401ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2402ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
24034e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2404ab4382d2SGreg Kroah-Hartman 
2405ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
24064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2407ab4382d2SGreg Kroah-Hartman }
2408ab4382d2SGreg Kroah-Hartman 
2409ab4382d2SGreg Kroah-Hartman /*
2410ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2411ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2412ab4382d2SGreg Kroah-Hartman  */
2413ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2414ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2415ab4382d2SGreg Kroah-Hartman {
2416ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2417ab4382d2SGreg Kroah-Hartman 
2418ab4382d2SGreg Kroah-Hartman 	/*
2419ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2420ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2421ab4382d2SGreg Kroah-Hartman 	 */
24224e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2423ab4382d2SGreg Kroah-Hartman 	if (!quot)
2424ab4382d2SGreg Kroah-Hartman 		return;
2425ab4382d2SGreg Kroah-Hartman 
24264e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2427ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2428ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2429ab4382d2SGreg Kroah-Hartman 	else
2430ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2431ab4382d2SGreg Kroah-Hartman 
24324e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2433ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2434ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2435ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2436ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2437ab4382d2SGreg Kroah-Hartman 
2438ab4382d2SGreg Kroah-Hartman 	/*
2439ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2440ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2441ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2442ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2443ab4382d2SGreg Kroah-Hartman 	 */
2444ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2445ab4382d2SGreg Kroah-Hartman }
2446ab4382d2SGreg Kroah-Hartman 
2447ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2448ab4382d2SGreg Kroah-Hartman {
244991f8c2d8SBoris BREZILLON 	int ret;
2450ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2451ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2452ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2453ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2454ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2455ab4382d2SGreg Kroah-Hartman 
2456ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2457ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2458ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2459ab4382d2SGreg Kroah-Hartman 	}
2460ab4382d2SGreg Kroah-Hartman 
246191f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
246291f8c2d8SBoris BREZILLON 	if (ret)
246391f8c2d8SBoris BREZILLON 		return ret;
2464ab4382d2SGreg Kroah-Hartman 
24654e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
24664e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
24674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2468ab4382d2SGreg Kroah-Hartman 
2469ab4382d2SGreg Kroah-Hartman 	if (options)
2470ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2471ab4382d2SGreg Kroah-Hartman 	else
2472ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2473ab4382d2SGreg Kroah-Hartman 
2474ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2475ab4382d2SGreg Kroah-Hartman }
2476ab4382d2SGreg Kroah-Hartman 
2477ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2478ab4382d2SGreg Kroah-Hartman 
2479ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2480ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2481ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2482ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2483ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2484ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2485ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2486ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2487ab4382d2SGreg Kroah-Hartman };
2488ab4382d2SGreg Kroah-Hartman 
2489ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2490ab4382d2SGreg Kroah-Hartman 
2491ab4382d2SGreg Kroah-Hartman /*
2492ab4382d2SGreg Kroah-Hartman  * Early console initialization (before VM subsystem initialized).
2493ab4382d2SGreg Kroah-Hartman  */
2494ab4382d2SGreg Kroah-Hartman static int __init atmel_console_init(void)
2495ab4382d2SGreg Kroah-Hartman {
249691f8c2d8SBoris BREZILLON 	int ret;
2497ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device) {
24980d0a3cc1SVoss, Nikolaus 		struct atmel_uart_data *pdata =
2499574de559SJingoo Han 			dev_get_platdata(&atmel_default_console_device->dev);
2500efb8d21bSLinus Torvalds 		int id = pdata->num;
2501b78cd169SJaeden Amero 		struct atmel_uart_port *atmel_port = &atmel_ports[id];
25020d0a3cc1SVoss, Nikolaus 
2503b78cd169SJaeden Amero 		atmel_port->backup_imr = 0;
2504b78cd169SJaeden Amero 		atmel_port->uart.line = id;
25054cbf9f48SNicolas Ferre 
25064cbf9f48SNicolas Ferre 		add_preferred_console(ATMEL_DEVICENAME, id, NULL);
2507b78cd169SJaeden Amero 		ret = atmel_init_port(atmel_port, atmel_default_console_device);
250891f8c2d8SBoris BREZILLON 		if (ret)
250991f8c2d8SBoris BREZILLON 			return ret;
2510ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2511ab4382d2SGreg Kroah-Hartman 	}
2512ab4382d2SGreg Kroah-Hartman 
2513ab4382d2SGreg Kroah-Hartman 	return 0;
2514ab4382d2SGreg Kroah-Hartman }
2515ab4382d2SGreg Kroah-Hartman 
2516ab4382d2SGreg Kroah-Hartman console_initcall(atmel_console_init);
2517ab4382d2SGreg Kroah-Hartman 
2518ab4382d2SGreg Kroah-Hartman /*
2519ab4382d2SGreg Kroah-Hartman  * Late console initialization.
2520ab4382d2SGreg Kroah-Hartman  */
2521ab4382d2SGreg Kroah-Hartman static int __init atmel_late_console_init(void)
2522ab4382d2SGreg Kroah-Hartman {
2523ab4382d2SGreg Kroah-Hartman 	if (atmel_default_console_device
2524ab4382d2SGreg Kroah-Hartman 	    && !(atmel_console.flags & CON_ENABLED))
2525ab4382d2SGreg Kroah-Hartman 		register_console(&atmel_console);
2526ab4382d2SGreg Kroah-Hartman 
2527ab4382d2SGreg Kroah-Hartman 	return 0;
2528ab4382d2SGreg Kroah-Hartman }
2529ab4382d2SGreg Kroah-Hartman 
2530ab4382d2SGreg Kroah-Hartman core_initcall(atmel_late_console_init);
2531ab4382d2SGreg Kroah-Hartman 
2532ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2533ab4382d2SGreg Kroah-Hartman {
2534ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2535ab4382d2SGreg Kroah-Hartman }
2536ab4382d2SGreg Kroah-Hartman 
2537ab4382d2SGreg Kroah-Hartman #else
2538ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2539ab4382d2SGreg Kroah-Hartman 
2540ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2541ab4382d2SGreg Kroah-Hartman {
2542ab4382d2SGreg Kroah-Hartman 	return false;
2543ab4382d2SGreg Kroah-Hartman }
2544ab4382d2SGreg Kroah-Hartman #endif
2545ab4382d2SGreg Kroah-Hartman 
2546ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2547ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2548ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2549ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2550ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2551ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2552ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2553ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2554ab4382d2SGreg Kroah-Hartman };
2555ab4382d2SGreg Kroah-Hartman 
2556ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2557ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2558ab4382d2SGreg Kroah-Hartman {
2559ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2560ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2561ab4382d2SGreg Kroah-Hartman #else
2562ab4382d2SGreg Kroah-Hartman 	return false;
2563ab4382d2SGreg Kroah-Hartman #endif
2564ab4382d2SGreg Kroah-Hartman }
2565ab4382d2SGreg Kroah-Hartman 
2566ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2567ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2568ab4382d2SGreg Kroah-Hartman {
2569ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2570ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2571ab4382d2SGreg Kroah-Hartman 
2572ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2573ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
25744e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
25754e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2576ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2577ab4382d2SGreg Kroah-Hartman 	}
2578ab4382d2SGreg Kroah-Hartman 
2579ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2580ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
25812c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
25822c7af5baSBoris BREZILLON 		unsigned long flags;
25832c7af5baSBoris BREZILLON 
25842c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
25852c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
25862c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2587ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
25882c7af5baSBoris BREZILLON 	}
2589ab4382d2SGreg Kroah-Hartman 
2590ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2591ab4382d2SGreg Kroah-Hartman 
2592ab4382d2SGreg Kroah-Hartman 	return 0;
2593ab4382d2SGreg Kroah-Hartman }
2594ab4382d2SGreg Kroah-Hartman 
2595ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2596ab4382d2SGreg Kroah-Hartman {
2597ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2598ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
25992c7af5baSBoris BREZILLON 	unsigned long flags;
26002c7af5baSBoris BREZILLON 
26012c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
26022c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
26032c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
26042c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
26052c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
26062c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
26072c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
26082c7af5baSBoris BREZILLON 	}
26092c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
26102c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2611ab4382d2SGreg Kroah-Hartman 
2612ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2613ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2614ab4382d2SGreg Kroah-Hartman 
2615ab4382d2SGreg Kroah-Hartman 	return 0;
2616ab4382d2SGreg Kroah-Hartman }
2617ab4382d2SGreg Kroah-Hartman #else
2618ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2619ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2620ab4382d2SGreg Kroah-Hartman #endif
2621ab4382d2SGreg Kroah-Hartman 
2622b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2623b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2624b5199d46SCyrille Pitchen {
2625b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2626b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2627b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2628b5199d46SCyrille Pitchen 
2629b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2630b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2631b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2632b5199d46SCyrille Pitchen 		return;
2633b5199d46SCyrille Pitchen 
2634b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2635b5199d46SCyrille Pitchen 		return;
2636b5199d46SCyrille Pitchen 
2637b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2638b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2639b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2640b5199d46SCyrille Pitchen 		return;
2641b5199d46SCyrille Pitchen 	}
2642b5199d46SCyrille Pitchen 
2643b5199d46SCyrille Pitchen 	/*
2644b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2645b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2646b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2647b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2648b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2649b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2650b5199d46SCyrille Pitchen 	 */
2651b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2652b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2653b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2654b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2655b5199d46SCyrille Pitchen 
2656b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2657b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2658b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2659b78cd169SJaeden Amero 		atmel_port->rts_high);
2660b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2661b78cd169SJaeden Amero 		atmel_port->rts_low);
2662b5199d46SCyrille Pitchen }
2663b5199d46SCyrille Pitchen 
26649671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2665ab4382d2SGreg Kroah-Hartman {
2666b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
26675fbe46b6SNicolas Ferre 	struct device_node *np = pdev->dev.of_node;
2668574de559SJingoo Han 	struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2669ab4382d2SGreg Kroah-Hartman 	void *data;
26704cbf9f48SNicolas Ferre 	int ret = -ENODEV;
2671bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2672ab4382d2SGreg Kroah-Hartman 
2673ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2674ab4382d2SGreg Kroah-Hartman 
26755fbe46b6SNicolas Ferre 	if (np)
26765fbe46b6SNicolas Ferre 		ret = of_alias_get_id(np, "serial");
26775fbe46b6SNicolas Ferre 	else
26784cbf9f48SNicolas Ferre 		if (pdata)
26794cbf9f48SNicolas Ferre 			ret = pdata->num;
26804cbf9f48SNicolas Ferre 
26814cbf9f48SNicolas Ferre 	if (ret < 0)
26825fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
26834cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2684503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
26854cbf9f48SNicolas Ferre 
2686503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
26874cbf9f48SNicolas Ferre 		ret = -ENODEV;
26884cbf9f48SNicolas Ferre 		goto err;
26894cbf9f48SNicolas Ferre 	}
26904cbf9f48SNicolas Ferre 
2691503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
26924cbf9f48SNicolas Ferre 		/* port already in use */
26934cbf9f48SNicolas Ferre 		ret = -EBUSY;
26944cbf9f48SNicolas Ferre 		goto err;
26954cbf9f48SNicolas Ferre 	}
26964cbf9f48SNicolas Ferre 
2697b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2698b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2699b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2700b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2701354e57f3SLinus Walleij 
2702b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
27032c7af5baSBoris BREZILLON 
2704b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
270591f8c2d8SBoris BREZILLON 	if (ret)
27066fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2707ab4382d2SGreg Kroah-Hartman 
2708b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2709b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2710b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
271118dfef9cSUwe Kleine-König 		goto err_clear_bit;
271218dfef9cSUwe Kleine-König 	}
271318dfef9cSUwe Kleine-König 
2714b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2715ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
2716ab4382d2SGreg Kroah-Hartman 		data = kmalloc(sizeof(struct atmel_uart_char)
2717ab4382d2SGreg Kroah-Hartman 				* ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2718ab4382d2SGreg Kroah-Hartman 		if (!data)
2719ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2720b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2721ab4382d2SGreg Kroah-Hartman 	}
2722ab4382d2SGreg Kroah-Hartman 
2723b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2724bd737f87SRicardo Ribalda Delgado 
2725b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2726ab4382d2SGreg Kroah-Hartman 	if (ret)
2727ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2728ab4382d2SGreg Kroah-Hartman 
2729ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2730b78cd169SJaeden Amero 	if (atmel_is_console_port(&atmel_port->uart)
2731ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2732ab4382d2SGreg Kroah-Hartman 		/*
2733ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
273491f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2735ab4382d2SGreg Kroah-Hartman 		 */
2736b78cd169SJaeden Amero 		clk_disable_unprepare(atmel_port->clk);
2737ab4382d2SGreg Kroah-Hartman 	}
2738ab4382d2SGreg Kroah-Hartman #endif
2739ab4382d2SGreg Kroah-Hartman 
2740ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2741b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2742ab4382d2SGreg Kroah-Hartman 
2743d4f64187SCyrille Pitchen 	/*
2744d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2745d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2746d4f64187SCyrille Pitchen 	 */
2747b78cd169SJaeden Amero 	clk_prepare_enable(atmel_port->clk);
2748d4f64187SCyrille Pitchen 
2749bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2750b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
27514e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2752b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2753b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2754fc887b15SLinus Torvalds 	}
2755fc887b15SLinus Torvalds 
2756055560b0SElen Song 	/*
2757055560b0SElen Song 	 * Get port name of usart or uart
2758055560b0SElen Song 	 */
2759b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2760055560b0SElen Song 
2761d4f64187SCyrille Pitchen 	/*
2762d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2763d4f64187SCyrille Pitchen 	 * is used
2764d4f64187SCyrille Pitchen 	 */
2765b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2766d4f64187SCyrille Pitchen 
2767ab4382d2SGreg Kroah-Hartman 	return 0;
2768ab4382d2SGreg Kroah-Hartman 
2769ab4382d2SGreg Kroah-Hartman err_add_port:
2770b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2771b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
2772ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2773b78cd169SJaeden Amero 	if (!atmel_is_console_port(&atmel_port->uart)) {
2774b78cd169SJaeden Amero 		clk_put(atmel_port->clk);
2775b78cd169SJaeden Amero 		atmel_port->clk = NULL;
2776ab4382d2SGreg Kroah-Hartman 	}
27776fbb9bdfSCyrille Pitchen err_clear_bit:
2778b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
27794cbf9f48SNicolas Ferre err:
2780ab4382d2SGreg Kroah-Hartman 	return ret;
2781ab4382d2SGreg Kroah-Hartman }
2782ab4382d2SGreg Kroah-Hartman 
2783f4a8ab04SRomain Izard /*
2784f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2785f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2786f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2787f4a8ab04SRomain Izard  *
2788f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
2789f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
2790f4a8ab04SRomain Izard  * port in the normal case.
2791f4a8ab04SRomain Izard  */
2792f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
2793f4a8ab04SRomain Izard {
2794f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
2795f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2796f4a8ab04SRomain Izard 	int ret = 0;
2797f4a8ab04SRomain Izard 
2798f4a8ab04SRomain Izard 	tasklet_kill(&atmel_port->tasklet);
2799f4a8ab04SRomain Izard 
2800f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
2801f4a8ab04SRomain Izard 
2802f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
2803f4a8ab04SRomain Izard 
2804f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
2805f4a8ab04SRomain Izard 
2806f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
2807f4a8ab04SRomain Izard 
2808f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
2809f4a8ab04SRomain Izard 
2810f4a8ab04SRomain Izard 	clk_put(atmel_port->clk);
2811f4a8ab04SRomain Izard 	atmel_port->clk = NULL;
2812f4a8ab04SRomain Izard 
2813f4a8ab04SRomain Izard 	return ret;
2814f4a8ab04SRomain Izard }
2815f4a8ab04SRomain Izard 
2816ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2817ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
2818f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
2819ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2820ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2821ab4382d2SGreg Kroah-Hartman 	.driver		= {
2822ab4382d2SGreg Kroah-Hartman 		.name			= "atmel_usart",
28235fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
2824ab4382d2SGreg Kroah-Hartman 	},
2825ab4382d2SGreg Kroah-Hartman };
2826ab4382d2SGreg Kroah-Hartman 
2827ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
2828ab4382d2SGreg Kroah-Hartman {
2829ab4382d2SGreg Kroah-Hartman 	int ret;
2830ab4382d2SGreg Kroah-Hartman 
2831ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
2832ab4382d2SGreg Kroah-Hartman 	if (ret)
2833ab4382d2SGreg Kroah-Hartman 		return ret;
2834ab4382d2SGreg Kroah-Hartman 
2835ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
2836ab4382d2SGreg Kroah-Hartman 	if (ret)
2837ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
2838ab4382d2SGreg Kroah-Hartman 
2839ab4382d2SGreg Kroah-Hartman 	return ret;
2840ab4382d2SGreg Kroah-Hartman }
2841c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
2842