xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 5e3ce1f261296bfe4bb59a98c82f4959d214a4f7)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2ab4382d2SGreg Kroah-Hartman /*
372ce5732SAndy Shevchenko  *  Driver for Atmel AT91 Serial ports
4ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
5ab4382d2SGreg Kroah-Hartman  *
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
7ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8ab4382d2SGreg Kroah-Hartman  *
9ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
10ab4382d2SGreg Kroah-Hartman  */
11702d10a0SJiri Slaby #include <linux/circ_buf.h>
12ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
13ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
14ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
15ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
16ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
17ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
18*5e3ce1f2SSergiu Moga #include <linux/clk-provider.h>
19ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
20ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
21ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
22ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
235fbe46b6SNicolas Ferre #include <linux/of.h>
245fbe46b6SNicolas Ferre #include <linux/of_device.h>
25ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
266b997babSVinod Koul #include <linux/dmaengine.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
28ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
29bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
302e68c22fSElen Song #include <linux/timer.h>
31e0b0baadSRichard Genoud #include <linux/err.h>
32ab5e4e41SRichard Genoud #include <linux/irq.h>
332c7af5baSBoris BREZILLON #include <linux/suspend.h>
342b5cf14bSGeliang Tang #include <linux/mm.h>
35635b2589SZihao Tang #include <linux/io.h>
36ab4382d2SGreg Kroah-Hartman 
37377fedd1SNicolas Ferre #include <asm/div64.h>
38ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
39ab4382d2SGreg Kroah-Hartman 
40ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
41ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
42ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
43ab4382d2SGreg Kroah-Hartman 
44b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
45b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
46b5199d46SCyrille Pitchen /*
47b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
48b5199d46SCyrille Pitchen  * high and low thresholds
49b5199d46SCyrille Pitchen  */
50b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
51b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
52b5199d46SCyrille Pitchen 
53ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
54ab4382d2SGreg Kroah-Hartman 
55e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
568961df89SRichard Genoud #include "atmel_serial.h"
57e0b0baadSRichard Genoud 
58ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
59ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
60ab4382d2SGreg Kroah-Hartman 
61ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
62ab4382d2SGreg Kroah-Hartman 
63ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
64ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
65ab4382d2SGreg Kroah-Hartman  * UART. */
66ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
67ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
68ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
69ab4382d2SGreg Kroah-Hartman 
70ab4382d2SGreg Kroah-Hartman #else
71ab4382d2SGreg Kroah-Hartman 
72ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
73ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
74ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
75ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
76ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
77ab4382d2SGreg Kroah-Hartman 
78ab4382d2SGreg Kroah-Hartman #endif
79ab4382d2SGreg Kroah-Hartman 
80ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
81ab4382d2SGreg Kroah-Hartman 
82ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
83ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
84ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
85ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
86ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
87ab4382d2SGreg Kroah-Hartman };
88ab4382d2SGreg Kroah-Hartman 
89ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
90ab4382d2SGreg Kroah-Hartman 	u16		status;
91ab4382d2SGreg Kroah-Hartman 	u16		ch;
92ab4382d2SGreg Kroah-Hartman };
93ab4382d2SGreg Kroah-Hartman 
94637ba54fSLudovic Desroches /*
95637ba54fSLudovic Desroches  * Be careful, the real size of the ring buffer is
96637ba54fSLudovic Desroches  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
97637ba54fSLudovic Desroches  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
98637ba54fSLudovic Desroches  * DMA mode.
99637ba54fSLudovic Desroches  */
100ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
101ab4382d2SGreg Kroah-Hartman 
102ab4382d2SGreg Kroah-Hartman /*
1039af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
104432f9748SAlexandre Belloni  * samx7: 3 USARTs and 5 UARTs
1059af92fbfSAlexandre Belloni  */
106432f9748SAlexandre Belloni #define ATMEL_MAX_UART		8
1079af92fbfSAlexandre Belloni 
1089af92fbfSAlexandre Belloni /*
109ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
110ab4382d2SGreg Kroah-Hartman  */
111ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
112ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
113ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
114*5e3ce1f2SSergiu Moga 	struct clk		*gclk;		/* uart generic clock */
115ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
116ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
117ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
118ab4382d2SGreg Kroah-Hartman 
11934df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
12064e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
121ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
122ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
123ab4382d2SGreg Kroah-Hartman 
12408f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
12564e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
126ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
127ab4382d2SGreg Kroah-Hartman 
12808f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
12934df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
13008f738beSElen Song 	struct dma_chan			*chan_tx;
13134df42f5SElen Song 	struct dma_chan			*chan_rx;
13208f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
13334df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
13408f738beSElen Song 	dma_cookie_t			cookie_tx;
13534df42f5SElen Song 	dma_cookie_t			cookie_rx;
13608f738beSElen Song 	struct scatterlist		sg_tx;
13734df42f5SElen Song 	struct scatterlist		sg_rx;
13800e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_rx;
13900e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_tx;
14098f2082cSNicolas Ferre 	atomic_t		tasklet_shutdown;
141ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
1425f258b3eSCyrille Pitchen 	unsigned int		tx_len;
143ab4382d2SGreg Kroah-Hartman 
144ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
145ab4382d2SGreg Kroah-Hartman 
146e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
147377fedd1SNicolas Ferre 	u32			backup_mode;	/* MR saved during iso7816 operations */
148377fedd1SNicolas Ferre 	u32			backup_brgr;	/* BRGR saved during iso7816 operations */
149ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
150b5199d46SCyrille Pitchen 	u32			fifo_size;
151b5199d46SCyrille Pitchen 	u32			rts_high;
152b5199d46SCyrille Pitchen 	u32			rts_low;
153ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1542958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
1555644bf18SSergiu Moga 	bool			is_usart;
1565bf5635aSLudovic Desroches 	bool			has_frac_baudrate;
1574b769371SNicolas Ferre 	bool			has_hw_timer;
1584b769371SNicolas Ferre 	struct timer_list	uart_timer;
1592c7af5baSBoris BREZILLON 
160ea04f82aSRomain Izard 	bool			tx_stopped;
1612c7af5baSBoris BREZILLON 	bool			suspended;
1622c7af5baSBoris BREZILLON 	unsigned int		pending;
1632c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1642c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1652c7af5baSBoris BREZILLON 
16669646d7aSRazvan Stefanescu 	bool			hd_start_rx;	/* can start RX during half-duplex operation */
16769646d7aSRazvan Stefanescu 
168377fedd1SNicolas Ferre 	/* ISO7816 */
169377fedd1SNicolas Ferre 	unsigned int		fidi_min;
170377fedd1SNicolas Ferre 	unsigned int		fidi_max;
171377fedd1SNicolas Ferre 
1726a5f0e2fSAlexandre Belloni 	struct {
1736a5f0e2fSAlexandre Belloni 		u32		cr;
1746a5f0e2fSAlexandre Belloni 		u32		mr;
1756a5f0e2fSAlexandre Belloni 		u32		imr;
1766a5f0e2fSAlexandre Belloni 		u32		brgr;
1776a5f0e2fSAlexandre Belloni 		u32		rtor;
1786a5f0e2fSAlexandre Belloni 		u32		ttgr;
1796a5f0e2fSAlexandre Belloni 		u32		fmr;
1806a5f0e2fSAlexandre Belloni 		u32		fimr;
1816a5f0e2fSAlexandre Belloni 	} cache;
1826a5f0e2fSAlexandre Belloni 
183a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
184a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
185a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
186a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
187a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
188a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
189ab4382d2SGreg Kroah-Hartman };
190ab4382d2SGreg Kroah-Hartman 
191ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
192503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
193ab4382d2SGreg Kroah-Hartman 
1945fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1955fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
196c24d2531SRadu Pirea 	{ .compatible = "atmel,at91rm9200-usart-serial" },
1975fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1985fbe46b6SNicolas Ferre };
1995fbe46b6SNicolas Ferre #endif
2005fbe46b6SNicolas Ferre 
201ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
202ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
203ab4382d2SGreg Kroah-Hartman {
204ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
205ab4382d2SGreg Kroah-Hartman }
206ab4382d2SGreg Kroah-Hartman 
2074e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2084e7decdaSCyrille Pitchen {
2094e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2104e7decdaSCyrille Pitchen }
2114e7decdaSCyrille Pitchen 
2124e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2134e7decdaSCyrille Pitchen {
2144e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2154e7decdaSCyrille Pitchen }
2164e7decdaSCyrille Pitchen 
217a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
218a6499435SCyrille Pitchen {
219a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
220a6499435SCyrille Pitchen }
221a6499435SCyrille Pitchen 
222a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
223a6499435SCyrille Pitchen {
224a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
225a6499435SCyrille Pitchen }
226a6499435SCyrille Pitchen 
227f3040983SRazvan Stefanescu static inline int atmel_uart_is_half_duplex(struct uart_port *port)
228f3040983SRazvan Stefanescu {
229f3040983SRazvan Stefanescu 	return ((port->rs485.flags & SER_RS485_ENABLED) &&
230f3040983SRazvan Stefanescu 		!(port->rs485.flags & SER_RS485_RX_DURING_TX)) ||
231f3040983SRazvan Stefanescu 		(port->iso7816.flags & SER_ISO7816_ENABLED);
232f3040983SRazvan Stefanescu }
233f3040983SRazvan Stefanescu 
234*5e3ce1f2SSergiu Moga static inline int atmel_error_rate(int desired_value, int actual_value)
235*5e3ce1f2SSergiu Moga {
236*5e3ce1f2SSergiu Moga 	return 100 - (desired_value * 100) / actual_value;
237*5e3ce1f2SSergiu Moga }
238*5e3ce1f2SSergiu Moga 
239ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
24064e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
241ab4382d2SGreg Kroah-Hartman {
242ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
243ab4382d2SGreg Kroah-Hartman 
24464e22ebeSElen Song 	return atmel_port->use_pdc_rx;
245ab4382d2SGreg Kroah-Hartman }
246ab4382d2SGreg Kroah-Hartman 
24764e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
248ab4382d2SGreg Kroah-Hartman {
249ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
250ab4382d2SGreg Kroah-Hartman 
25164e22ebeSElen Song 	return atmel_port->use_pdc_tx;
252ab4382d2SGreg Kroah-Hartman }
253ab4382d2SGreg Kroah-Hartman #else
25464e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
255ab4382d2SGreg Kroah-Hartman {
256ab4382d2SGreg Kroah-Hartman 	return false;
257ab4382d2SGreg Kroah-Hartman }
258ab4382d2SGreg Kroah-Hartman 
25964e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
260ab4382d2SGreg Kroah-Hartman {
261ab4382d2SGreg Kroah-Hartman 	return false;
262ab4382d2SGreg Kroah-Hartman }
263ab4382d2SGreg Kroah-Hartman #endif
264ab4382d2SGreg Kroah-Hartman 
26508f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26608f738beSElen Song {
26708f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26808f738beSElen Song 
26908f738beSElen Song 	return atmel_port->use_dma_tx;
27008f738beSElen Song }
27108f738beSElen Song 
27234df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27334df42f5SElen Song {
27434df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27534df42f5SElen Song 
27634df42f5SElen Song 	return atmel_port->use_dma_rx;
27734df42f5SElen Song }
27834df42f5SElen Song 
2795be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2805be605acSAlexandre Belloni {
2815be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2825be605acSAlexandre Belloni 
2835be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2845be605acSAlexandre Belloni }
2855be605acSAlexandre Belloni 
28698f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
28798f2082cSNicolas Ferre 				   struct tasklet_struct *t)
28898f2082cSNicolas Ferre {
28998f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown))
29098f2082cSNicolas Ferre 		tasklet_schedule(t);
29198f2082cSNicolas Ferre }
29298f2082cSNicolas Ferre 
293ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
294ae50bb27SIlpo Järvinen static int atmel_config_rs485(struct uart_port *port, struct ktermios *termios,
29513bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
296ab4382d2SGreg Kroah-Hartman {
297ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
298ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
299ab4382d2SGreg Kroah-Hartman 
300ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
302ab4382d2SGreg Kroah-Hartman 
3034e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
304ab4382d2SGreg Kroah-Hartman 
305ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
306ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
30760efd051SLino Sanfilippo 		if (rs485conf->flags & SER_RS485_RX_DURING_TX)
308477b8383SCodrin.Ciubotariu@microchip.com 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
309477b8383SCodrin.Ciubotariu@microchip.com 		else
310ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
311477b8383SCodrin.Ciubotariu@microchip.com 
3124e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3134e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
314692a8ebcSSergiu Moga 		mode &= ~ATMEL_US_USMODE;
315ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
316ab4382d2SGreg Kroah-Hartman 	} else {
317ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
31864e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
319ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
320ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
321ab4382d2SGreg Kroah-Hartman 		else
322ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
323ab4382d2SGreg Kroah-Hartman 	}
3244e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
325ab4382d2SGreg Kroah-Hartman 
326ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3274e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
328ab4382d2SGreg Kroah-Hartman 
32913bd3e6fSRicardo Ribalda Delgado 	return 0;
330ab4382d2SGreg Kroah-Hartman }
331ab4382d2SGreg Kroah-Hartman 
332377fedd1SNicolas Ferre static unsigned int atmel_calc_cd(struct uart_port *port,
333377fedd1SNicolas Ferre 				  struct serial_iso7816 *iso7816conf)
334377fedd1SNicolas Ferre {
335377fedd1SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
336377fedd1SNicolas Ferre 	unsigned int cd;
337377fedd1SNicolas Ferre 	u64 mck_rate;
338377fedd1SNicolas Ferre 
339377fedd1SNicolas Ferre 	mck_rate = (u64)clk_get_rate(atmel_port->clk);
340377fedd1SNicolas Ferre 	do_div(mck_rate, iso7816conf->clk);
341377fedd1SNicolas Ferre 	cd = mck_rate;
342377fedd1SNicolas Ferre 	return cd;
343377fedd1SNicolas Ferre }
344377fedd1SNicolas Ferre 
345377fedd1SNicolas Ferre static unsigned int atmel_calc_fidi(struct uart_port *port,
346377fedd1SNicolas Ferre 				    struct serial_iso7816 *iso7816conf)
347377fedd1SNicolas Ferre {
348377fedd1SNicolas Ferre 	u64 fidi = 0;
349377fedd1SNicolas Ferre 
350377fedd1SNicolas Ferre 	if (iso7816conf->sc_fi && iso7816conf->sc_di) {
351377fedd1SNicolas Ferre 		fidi = (u64)iso7816conf->sc_fi;
352377fedd1SNicolas Ferre 		do_div(fidi, iso7816conf->sc_di);
353377fedd1SNicolas Ferre 	}
354377fedd1SNicolas Ferre 	return (u32)fidi;
355377fedd1SNicolas Ferre }
356377fedd1SNicolas Ferre 
357377fedd1SNicolas Ferre /* Enable or disable the iso7816 support */
358377fedd1SNicolas Ferre /* Called with interrupts disabled */
359377fedd1SNicolas Ferre static int atmel_config_iso7816(struct uart_port *port,
360377fedd1SNicolas Ferre 				struct serial_iso7816 *iso7816conf)
361377fedd1SNicolas Ferre {
362377fedd1SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
363377fedd1SNicolas Ferre 	unsigned int mode;
364377fedd1SNicolas Ferre 	unsigned int cd, fidi;
365377fedd1SNicolas Ferre 	int ret = 0;
366377fedd1SNicolas Ferre 
367377fedd1SNicolas Ferre 	/* Disable interrupts */
368377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
369377fedd1SNicolas Ferre 
370377fedd1SNicolas Ferre 	mode = atmel_uart_readl(port, ATMEL_US_MR);
371377fedd1SNicolas Ferre 
372377fedd1SNicolas Ferre 	if (iso7816conf->flags & SER_ISO7816_ENABLED) {
373377fedd1SNicolas Ferre 		mode &= ~ATMEL_US_USMODE;
374377fedd1SNicolas Ferre 
375377fedd1SNicolas Ferre 		if (iso7816conf->tg > 255) {
376377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n");
377377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
378377fedd1SNicolas Ferre 			ret = -EINVAL;
379377fedd1SNicolas Ferre 			goto err_out;
380377fedd1SNicolas Ferre 		}
381377fedd1SNicolas Ferre 
382377fedd1SNicolas Ferre 		if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
383377fedd1SNicolas Ferre 		    == SER_ISO7816_T(0)) {
384377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T0 | ATMEL_US_DSNACK;
385377fedd1SNicolas Ferre 		} else if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
386377fedd1SNicolas Ferre 			   == SER_ISO7816_T(1)) {
387377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T1 | ATMEL_US_INACK;
388377fedd1SNicolas Ferre 		} else {
389377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816: Type not supported\n");
390377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
391377fedd1SNicolas Ferre 			ret = -EINVAL;
392377fedd1SNicolas Ferre 			goto err_out;
393377fedd1SNicolas Ferre 		}
394377fedd1SNicolas Ferre 
395377fedd1SNicolas Ferre 		mode &= ~(ATMEL_US_USCLKS | ATMEL_US_NBSTOP | ATMEL_US_PAR);
396377fedd1SNicolas Ferre 
397377fedd1SNicolas Ferre 		/* select mck clock, and output  */
398377fedd1SNicolas Ferre 		mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
399377fedd1SNicolas Ferre 		/* set parity for normal/inverse mode + max iterations */
400377fedd1SNicolas Ferre 		mode |= ATMEL_US_PAR_EVEN | ATMEL_US_NBSTOP_1 | ATMEL_US_MAX_ITER(3);
401377fedd1SNicolas Ferre 
402377fedd1SNicolas Ferre 		cd = atmel_calc_cd(port, iso7816conf);
403377fedd1SNicolas Ferre 		fidi = atmel_calc_fidi(port, iso7816conf);
404377fedd1SNicolas Ferre 		if (fidi == 0) {
405377fedd1SNicolas Ferre 			dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n");
406377fedd1SNicolas Ferre 		} else if (fidi < atmel_port->fidi_min
407377fedd1SNicolas Ferre 			   || fidi > atmel_port->fidi_max) {
408377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi);
409377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
410377fedd1SNicolas Ferre 			ret = -EINVAL;
411377fedd1SNicolas Ferre 			goto err_out;
412377fedd1SNicolas Ferre 		}
413377fedd1SNicolas Ferre 
414377fedd1SNicolas Ferre 		if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) {
415377fedd1SNicolas Ferre 			/* port not yet in iso7816 mode: store configuration */
416377fedd1SNicolas Ferre 			atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR);
417377fedd1SNicolas Ferre 			atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
418377fedd1SNicolas Ferre 		}
419377fedd1SNicolas Ferre 
420377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg);
421377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_BRGR, cd);
422377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_FIDI, fidi);
423377fedd1SNicolas Ferre 
424377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXEN);
425377fedd1SNicolas Ferre 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION;
426377fedd1SNicolas Ferre 	} else {
427377fedd1SNicolas Ferre 		dev_dbg(port->dev, "Setting UART back to RS232\n");
428377fedd1SNicolas Ferre 		/* back to last RS232 settings */
429377fedd1SNicolas Ferre 		mode = atmel_port->backup_mode;
430377fedd1SNicolas Ferre 		memset(iso7816conf, 0, sizeof(struct serial_iso7816));
431377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, 0);
432377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr);
433377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_FIDI, 0x174);
434377fedd1SNicolas Ferre 
435377fedd1SNicolas Ferre 		if (atmel_use_pdc_tx(port))
436377fedd1SNicolas Ferre 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
437377fedd1SNicolas Ferre 						   ATMEL_US_TXBUFE;
438377fedd1SNicolas Ferre 		else
439377fedd1SNicolas Ferre 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
440377fedd1SNicolas Ferre 	}
441377fedd1SNicolas Ferre 
442377fedd1SNicolas Ferre 	port->iso7816 = *iso7816conf;
443377fedd1SNicolas Ferre 
444377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_MR, mode);
445377fedd1SNicolas Ferre 
446377fedd1SNicolas Ferre err_out:
447377fedd1SNicolas Ferre 	/* Enable interrupts */
448377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
449377fedd1SNicolas Ferre 
450377fedd1SNicolas Ferre 	return ret;
451377fedd1SNicolas Ferre }
452377fedd1SNicolas Ferre 
453ab4382d2SGreg Kroah-Hartman /*
454ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
455ab4382d2SGreg Kroah-Hartman  */
456ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
457ab4382d2SGreg Kroah-Hartman {
458ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
459ea04f82aSRomain Izard 
460ea04f82aSRomain Izard 	if (atmel_port->tx_stopped)
461ea04f82aSRomain Izard 		return TIOCSER_TEMT;
4624e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
4634e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
4644e7decdaSCyrille Pitchen 		0;
465ab4382d2SGreg Kroah-Hartman }
466ab4382d2SGreg Kroah-Hartman 
467ab4382d2SGreg Kroah-Hartman /*
468ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
469ab4382d2SGreg Kroah-Hartman  */
470ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
471ab4382d2SGreg Kroah-Hartman {
472ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
4734e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
4741cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
475ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
476ab4382d2SGreg Kroah-Hartman 
4771cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
4781cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
4794e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
4804e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
4811cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
4821cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
4831cf6e8fcSCyrille Pitchen 	}
4841cf6e8fcSCyrille Pitchen 
4851cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
4861cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
4871cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4881cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4891cf6e8fcSCyrille Pitchen 
4901cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4911cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4921cf6e8fcSCyrille Pitchen 	} else {
4931cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4941cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4951cf6e8fcSCyrille Pitchen 
4961cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4971cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4981cf6e8fcSCyrille Pitchen 	}
4991cf6e8fcSCyrille Pitchen 
500ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
5011cf6e8fcSCyrille Pitchen 		control |= rts_ready;
502ab4382d2SGreg Kroah-Hartman 	else
5031cf6e8fcSCyrille Pitchen 		control |= rts_paused;
504ab4382d2SGreg Kroah-Hartman 
505ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
506ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
507ab4382d2SGreg Kroah-Hartman 	else
508ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
509ab4382d2SGreg Kroah-Hartman 
5104e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
511ab4382d2SGreg Kroah-Hartman 
512e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
513e0b0baadSRichard Genoud 
514ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
5151cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
516ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
517ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
518ab4382d2SGreg Kroah-Hartman 	else
519ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
520ab4382d2SGreg Kroah-Hartman 
5214e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
522ab4382d2SGreg Kroah-Hartman }
523ab4382d2SGreg Kroah-Hartman 
524ab4382d2SGreg Kroah-Hartman /*
525ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
526ab4382d2SGreg Kroah-Hartman  */
527ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
528ab4382d2SGreg Kroah-Hartman {
529e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
530e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
531ab4382d2SGreg Kroah-Hartman 
5324e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
533ab4382d2SGreg Kroah-Hartman 
534ab4382d2SGreg Kroah-Hartman 	/*
535ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
536ab4382d2SGreg Kroah-Hartman 	 */
537ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
538ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
539ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
540ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
541ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
542ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
543ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
544ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
545ab4382d2SGreg Kroah-Hartman 
546e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
547ab4382d2SGreg Kroah-Hartman }
548ab4382d2SGreg Kroah-Hartman 
549ab4382d2SGreg Kroah-Hartman /*
550ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
551ab4382d2SGreg Kroah-Hartman  */
552ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
553ab4382d2SGreg Kroah-Hartman {
554ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
555ab4382d2SGreg Kroah-Hartman 
55664e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
557ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
5584e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
559ab4382d2SGreg Kroah-Hartman 	}
56089d82324SRichard Genoud 
56189d82324SRichard Genoud 	/*
56289d82324SRichard Genoud 	 * Disable the transmitter.
56389d82324SRichard Genoud 	 * This is mandatory when DMA is used, otherwise the DMA buffer
56489d82324SRichard Genoud 	 * is fully transmitted.
56589d82324SRichard Genoud 	 */
56689d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
567ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
56889d82324SRichard Genoud 
569ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
5704e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
571ab4382d2SGreg Kroah-Hartman 
572f3040983SRazvan Stefanescu 	if (atmel_uart_is_half_duplex(port))
57304b5bfe3SNicolas Ferre 		if (!atomic_read(&atmel_port->tasklet_shutdown))
574ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
575f3040983SRazvan Stefanescu 
576ab4382d2SGreg Kroah-Hartman }
577ab4382d2SGreg Kroah-Hartman 
578ab4382d2SGreg Kroah-Hartman /*
579ab4382d2SGreg Kroah-Hartman  * Start transmitting.
580ab4382d2SGreg Kroah-Hartman  */
581ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
582ab4382d2SGreg Kroah-Hartman {
583ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
584ab4382d2SGreg Kroah-Hartman 
5850058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
5860058f087SAlexandre Belloni 				       & ATMEL_PDC_TXTEN))
587ab4382d2SGreg Kroah-Hartman 		/* The transmitter is already running.  Yes, we
588ab4382d2SGreg Kroah-Hartman 		   really need this.*/
589ab4382d2SGreg Kroah-Hartman 		return;
590ab4382d2SGreg Kroah-Hartman 
5910058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
592f3040983SRazvan Stefanescu 		if (atmel_uart_is_half_duplex(port))
593ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
594ab4382d2SGreg Kroah-Hartman 
5950058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port))
596ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
5974e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
5980058f087SAlexandre Belloni 
599ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
6004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
60189d82324SRichard Genoud 
60289d82324SRichard Genoud 	/* re-enable the transmitter */
60389d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
604ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
605ab4382d2SGreg Kroah-Hartman }
606ab4382d2SGreg Kroah-Hartman 
607ab4382d2SGreg Kroah-Hartman /*
608ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
609ab4382d2SGreg Kroah-Hartman  */
610ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
611ab4382d2SGreg Kroah-Hartman {
6124e7decdaSCyrille Pitchen 	/* reset status and receiver */
6134e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
614ab4382d2SGreg Kroah-Hartman 
6154e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
61657c36868SSiftar, Gabe 
61764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
618ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
6194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
6204e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
621ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
6224e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
623ab4382d2SGreg Kroah-Hartman 	} else {
6244e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
625ab4382d2SGreg Kroah-Hartman 	}
626ab4382d2SGreg Kroah-Hartman }
627ab4382d2SGreg Kroah-Hartman 
628ab4382d2SGreg Kroah-Hartman /*
629ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
630ab4382d2SGreg Kroah-Hartman  */
631ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
632ab4382d2SGreg Kroah-Hartman {
6334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
63457c36868SSiftar, Gabe 
63564e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
636ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
6374e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
6384e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
6394e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
640ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
641ab4382d2SGreg Kroah-Hartman 	} else {
6424e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
643ab4382d2SGreg Kroah-Hartman 	}
644ab4382d2SGreg Kroah-Hartman }
645ab4382d2SGreg Kroah-Hartman 
646ab4382d2SGreg Kroah-Hartman /*
647ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
648ab4382d2SGreg Kroah-Hartman  */
649ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
650ab4382d2SGreg Kroah-Hartman {
651ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
652ab5e4e41SRichard Genoud 	uint32_t ier = 0;
653ab5e4e41SRichard Genoud 
654ab5e4e41SRichard Genoud 	/*
655ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
656ab5e4e41SRichard Genoud 	 */
657ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
658ab5e4e41SRichard Genoud 		return;
659ab5e4e41SRichard Genoud 
660ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
661ab5e4e41SRichard Genoud 
66218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
663ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
664ab5e4e41SRichard Genoud 
66518dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
666ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
667ab5e4e41SRichard Genoud 
66818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
669ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
670ab5e4e41SRichard Genoud 
67118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
672ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
673ab5e4e41SRichard Genoud 
6744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
67518dfef9cSUwe Kleine-König 
67618dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
677ab4382d2SGreg Kroah-Hartman }
678ab4382d2SGreg Kroah-Hartman 
679ab4382d2SGreg Kroah-Hartman /*
68035b675b9SRichard Genoud  * Disable modem status interrupts
68135b675b9SRichard Genoud  */
68235b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
68335b675b9SRichard Genoud {
68435b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
68535b675b9SRichard Genoud 	uint32_t idr = 0;
68635b675b9SRichard Genoud 
68735b675b9SRichard Genoud 	/*
68835b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
68935b675b9SRichard Genoud 	 */
69035b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
69135b675b9SRichard Genoud 		return;
69235b675b9SRichard Genoud 
69335b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
69435b675b9SRichard Genoud 
69518dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
69618dfef9cSUwe Kleine-König 
69718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
69835b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
69935b675b9SRichard Genoud 
70018dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
70135b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
70235b675b9SRichard Genoud 
70318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
70435b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
70535b675b9SRichard Genoud 
70618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
70735b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
70835b675b9SRichard Genoud 
7094e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
71035b675b9SRichard Genoud }
71135b675b9SRichard Genoud 
71235b675b9SRichard Genoud /*
713ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
714ab4382d2SGreg Kroah-Hartman  */
715ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
716ab4382d2SGreg Kroah-Hartman {
717ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
7184e7decdaSCyrille Pitchen 		/* start break */
7194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
720ab4382d2SGreg Kroah-Hartman 	else
7214e7decdaSCyrille Pitchen 		/* stop break */
7224e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
723ab4382d2SGreg Kroah-Hartman }
724ab4382d2SGreg Kroah-Hartman 
725ab4382d2SGreg Kroah-Hartman /*
726ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
727ab4382d2SGreg Kroah-Hartman  */
728ab4382d2SGreg Kroah-Hartman static void
729ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
730ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
731ab4382d2SGreg Kroah-Hartman {
732ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
733ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
734ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
735ab4382d2SGreg Kroah-Hartman 
736ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
737ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
738ab4382d2SGreg Kroah-Hartman 		return;
739ab4382d2SGreg Kroah-Hartman 
740ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
741ab4382d2SGreg Kroah-Hartman 	c->status	= status;
742ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
743ab4382d2SGreg Kroah-Hartman 
744ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
745ab4382d2SGreg Kroah-Hartman 	smp_wmb();
746ab4382d2SGreg Kroah-Hartman 
747ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
748ab4382d2SGreg Kroah-Hartman }
749ab4382d2SGreg Kroah-Hartman 
750ab4382d2SGreg Kroah-Hartman /*
751ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
752ab4382d2SGreg Kroah-Hartman  */
753ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
754ab4382d2SGreg Kroah-Hartman {
755ab4382d2SGreg Kroah-Hartman 	/* clear error */
7564e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
757ab4382d2SGreg Kroah-Hartman 
758ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
759ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
760ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
761ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
762ab4382d2SGreg Kroah-Hartman 	}
763ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
764ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
765ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
766ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
767ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
768ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
769ab4382d2SGreg Kroah-Hartman }
770ab4382d2SGreg Kroah-Hartman 
771ab4382d2SGreg Kroah-Hartman /*
772ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
773ab4382d2SGreg Kroah-Hartman  */
774ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
775ab4382d2SGreg Kroah-Hartman {
776ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
777ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
778ab4382d2SGreg Kroah-Hartman 
7794e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
780ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
781a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
782ab4382d2SGreg Kroah-Hartman 
783ab4382d2SGreg Kroah-Hartman 		/*
784ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
785ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
786ab4382d2SGreg Kroah-Hartman 		 */
787ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
788ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
789ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
790ab4382d2SGreg Kroah-Hartman 
791ab4382d2SGreg Kroah-Hartman 			/* clear error */
7924e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
793ab4382d2SGreg Kroah-Hartman 
794ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
795ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
796ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
7974e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
7984e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
799ab4382d2SGreg Kroah-Hartman 			} else {
800ab4382d2SGreg Kroah-Hartman 				/*
801ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
802ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
803ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
804ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
805ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
806ab4382d2SGreg Kroah-Hartman 				 */
8074e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
8084e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
809ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
810ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
811ab4382d2SGreg Kroah-Hartman 			}
812ab4382d2SGreg Kroah-Hartman 		}
813ab4382d2SGreg Kroah-Hartman 
814ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
8154e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
816ab4382d2SGreg Kroah-Hartman 	}
817ab4382d2SGreg Kroah-Hartman 
81898f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
819ab4382d2SGreg Kroah-Hartman }
820ab4382d2SGreg Kroah-Hartman 
821ab4382d2SGreg Kroah-Hartman /*
822ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
823ab4382d2SGreg Kroah-Hartman  * disabled)
824ab4382d2SGreg Kroah-Hartman  */
825ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
826ab4382d2SGreg Kroah-Hartman {
827ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
828ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
829ab4382d2SGreg Kroah-Hartman 
8304e7decdaSCyrille Pitchen 	if (port->x_char &&
831477b8383SCodrin.Ciubotariu@microchip.com 	    (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) {
832a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
833ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
834ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
835ab4382d2SGreg Kroah-Hartman 	}
836ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
837ab4382d2SGreg Kroah-Hartman 		return;
838ab4382d2SGreg Kroah-Hartman 
839477b8383SCodrin.Ciubotariu@microchip.com 	while (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY) {
840a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
841ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
842ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
843ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
844ab4382d2SGreg Kroah-Hartman 			break;
845ab4382d2SGreg Kroah-Hartman 	}
846ab4382d2SGreg Kroah-Hartman 
847ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
848ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
849ab4382d2SGreg Kroah-Hartman 
850477b8383SCodrin.Ciubotariu@microchip.com 	if (!uart_circ_empty(xmit)) {
851477b8383SCodrin.Ciubotariu@microchip.com 		/* we still have characters to transmit, so we should continue
852477b8383SCodrin.Ciubotariu@microchip.com 		 * transmitting them when TX is ready, regardless of
853477b8383SCodrin.Ciubotariu@microchip.com 		 * mode or duplexity
854477b8383SCodrin.Ciubotariu@microchip.com 		 */
855477b8383SCodrin.Ciubotariu@microchip.com 		atmel_port->tx_done_mask |= ATMEL_US_TXRDY;
856477b8383SCodrin.Ciubotariu@microchip.com 
857ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
8584e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
8594e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
860477b8383SCodrin.Ciubotariu@microchip.com 	} else {
861477b8383SCodrin.Ciubotariu@microchip.com 		if (atmel_uart_is_half_duplex(port))
862477b8383SCodrin.Ciubotariu@microchip.com 			atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY;
863477b8383SCodrin.Ciubotariu@microchip.com 	}
864ab4382d2SGreg Kroah-Hartman }
865ab4382d2SGreg Kroah-Hartman 
86608f738beSElen Song static void atmel_complete_tx_dma(void *arg)
86708f738beSElen Song {
86808f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
86908f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
87008f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
87108f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
87208f738beSElen Song 	unsigned long flags;
87308f738beSElen Song 
87408f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
87508f738beSElen Song 
87608f738beSElen Song 	if (chan)
87708f738beSElen Song 		dmaengine_terminate_all(chan);
8785f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
87908f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
88008f738beSElen Song 
8815f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
88208f738beSElen Song 
88308f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
88408f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
88508f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
88608f738beSElen Song 	atmel_port->desc_tx = NULL;
88708f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
88808f738beSElen Song 
88908f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
89008f738beSElen Song 		uart_write_wakeup(port);
89108f738beSElen Song 
8921842dc2eSCyrille Pitchen 	/*
8931842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
8941842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
8951842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
8961842dc2eSCyrille Pitchen 	 */
89708f738beSElen Song 	if (!uart_circ_empty(xmit))
89898f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
899f3040983SRazvan Stefanescu 	else if (atmel_uart_is_half_duplex(port)) {
90069646d7aSRazvan Stefanescu 		/*
90169646d7aSRazvan Stefanescu 		 * DMA done, re-enable TXEMPTY and signal that we can stop
90269646d7aSRazvan Stefanescu 		 * TX and start RX for RS485
90369646d7aSRazvan Stefanescu 		 */
90469646d7aSRazvan Stefanescu 		atmel_port->hd_start_rx = true;
90569646d7aSRazvan Stefanescu 		atmel_uart_writel(port, ATMEL_US_IER,
90669646d7aSRazvan Stefanescu 				  atmel_port->tx_done_mask);
907b389f173SRichard Genoud 	}
90808f738beSElen Song 
90908f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
91008f738beSElen Song }
91108f738beSElen Song 
91208f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
91308f738beSElen Song {
91408f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
91508f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
91608f738beSElen Song 
91708f738beSElen Song 	if (chan) {
91808f738beSElen Song 		dmaengine_terminate_all(chan);
91908f738beSElen Song 		dma_release_channel(chan);
92008f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
92148479148SWolfram Sang 				DMA_TO_DEVICE);
92208f738beSElen Song 	}
92308f738beSElen Song 
92408f738beSElen Song 	atmel_port->desc_tx = NULL;
92508f738beSElen Song 	atmel_port->chan_tx = NULL;
92608f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
92708f738beSElen Song }
92808f738beSElen Song 
92908f738beSElen Song /*
93008f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
93108f738beSElen Song  */
93208f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
93308f738beSElen Song {
93408f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
93508f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
93608f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
93708f738beSElen Song 	struct dma_async_tx_descriptor *desc;
9385f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
9395f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
9405f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
94108f738beSElen Song 
94208f738beSElen Song 	/* Make sure we have an idle channel */
94308f738beSElen Song 	if (atmel_port->desc_tx != NULL)
94408f738beSElen Song 		return;
94508f738beSElen Song 
94608f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
94708f738beSElen Song 		/*
94808f738beSElen Song 		 * DMA is idle now.
94908f738beSElen Song 		 * Port xmit buffer is already mapped,
95008f738beSElen Song 		 * and it is one page... Just adjust
95108f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
95208f738beSElen Song 		 * we have to transmit till the end, and then the rest.
95308f738beSElen Song 		 * Take the port lock to get a
95408f738beSElen Song 		 * consistent xmit buffer state.
95508f738beSElen Song 		 */
9565f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
95708f738beSElen Song 					 xmit->tail,
95808f738beSElen Song 					 UART_XMIT_SIZE);
9595f258b3eSCyrille Pitchen 
9605f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
9615f258b3eSCyrille Pitchen 			/* multi data mode */
9625f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
9635f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
9645f258b3eSCyrille Pitchen 		} else {
9655f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
9665f258b3eSCyrille Pitchen 			part1_len = 0;
9675f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
9685f258b3eSCyrille Pitchen 		}
9695f258b3eSCyrille Pitchen 
9705f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
9715f258b3eSCyrille Pitchen 		sg_len = 0;
9725f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
9735f258b3eSCyrille Pitchen 		if (part1_len) {
9745f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
9755f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
9765f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
9775f258b3eSCyrille Pitchen 
9785f258b3eSCyrille Pitchen 			phys_addr += part1_len;
9795f258b3eSCyrille Pitchen 		}
9805f258b3eSCyrille Pitchen 
9815f258b3eSCyrille Pitchen 		if (part2_len) {
9825f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
9835f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
9845f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
9855f258b3eSCyrille Pitchen 		}
9865f258b3eSCyrille Pitchen 
9875f258b3eSCyrille Pitchen 		/*
9885f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
9895f258b3eSCyrille Pitchen 		 * xmit->tail correctly
9905f258b3eSCyrille Pitchen 		 */
9915f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
99208f738beSElen Song 
99308f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
9945f258b3eSCyrille Pitchen 					       sgl,
9955f258b3eSCyrille Pitchen 					       sg_len,
99608f738beSElen Song 					       DMA_MEM_TO_DEV,
99708f738beSElen Song 					       DMA_PREP_INTERRUPT |
99808f738beSElen Song 					       DMA_CTRL_ACK);
99908f738beSElen Song 		if (!desc) {
100008f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
100108f738beSElen Song 			return;
100208f738beSElen Song 		}
100308f738beSElen Song 
10045f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
100508f738beSElen Song 
100608f738beSElen Song 		atmel_port->desc_tx = desc;
100708f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
100808f738beSElen Song 		desc->callback_param = atmel_port;
100908f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
10101e67bd2bSTudor Ambarus 		if (dma_submit_error(atmel_port->cookie_tx)) {
10111e67bd2bSTudor Ambarus 			dev_err(port->dev, "dma_submit_error %d\n",
10121e67bd2bSTudor Ambarus 				atmel_port->cookie_tx);
10131e67bd2bSTudor Ambarus 			return;
10141e67bd2bSTudor Ambarus 		}
10154f4b9b58STudor Ambarus 
10164f4b9b58STudor Ambarus 		dma_async_issue_pending(chan);
101708f738beSElen Song 	}
101808f738beSElen Song 
101908f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
102008f738beSElen Song 		uart_write_wakeup(port);
102108f738beSElen Song }
102208f738beSElen Song 
102308f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
102408f738beSElen Song {
102508f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1026c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
102708f738beSElen Song 	dma_cap_mask_t		mask;
102808f738beSElen Song 	struct dma_slave_config config;
102908f738beSElen Song 	int ret, nent;
103008f738beSElen Song 
103108f738beSElen Song 	dma_cap_zero(mask);
103208f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
103308f738beSElen Song 
1034c24d2531SRadu Pirea 	atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx");
103508f738beSElen Song 	if (atmel_port->chan_tx == NULL)
103608f738beSElen Song 		goto chan_err;
103708f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
103808f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
103908f738beSElen Song 
104008f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
104108f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
104208f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
10432c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
104408f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
104508f738beSElen Song 			virt_to_page(port->state->xmit.buf),
104608f738beSElen Song 			UART_XMIT_SIZE,
10472b5cf14bSGeliang Tang 			offset_in_page(port->state->xmit.buf));
104808f738beSElen Song 	nent = dma_map_sg(port->dev,
104908f738beSElen Song 				&atmel_port->sg_tx,
105008f738beSElen Song 				1,
105148479148SWolfram Sang 				DMA_TO_DEVICE);
105208f738beSElen Song 
105308f738beSElen Song 	if (!nent) {
105408f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
105508f738beSElen Song 		goto chan_err;
105608f738beSElen Song 	} else {
1057c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
105808f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
105908f738beSElen Song 			port->state->xmit.buf,
1060c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
106108f738beSElen Song 	}
106208f738beSElen Song 
106308f738beSElen Song 	/* Configure the slave DMA */
106408f738beSElen Song 	memset(&config, 0, sizeof(config));
106508f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
10665f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
10675f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
10685f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
106908f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
1070a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
107108f738beSElen Song 
10725483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
10735483c10eSMaxime Ripard 				     &config);
107408f738beSElen Song 	if (ret) {
107508f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
107608f738beSElen Song 		goto chan_err;
107708f738beSElen Song 	}
107808f738beSElen Song 
107908f738beSElen Song 	return 0;
108008f738beSElen Song 
108108f738beSElen Song chan_err:
108208f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
108336ce7cffSZheng Bin 	atmel_port->use_dma_tx = false;
108408f738beSElen Song 	if (atmel_port->chan_tx)
108508f738beSElen Song 		atmel_release_tx_dma(port);
108608f738beSElen Song 	return -EINVAL;
108708f738beSElen Song }
108808f738beSElen Song 
108934df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
109034df42f5SElen Song {
109134df42f5SElen Song 	struct uart_port *port = arg;
109234df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
109334df42f5SElen Song 
109498f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
109534df42f5SElen Song }
109634df42f5SElen Song 
109734df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
109834df42f5SElen Song {
109934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
110034df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
110134df42f5SElen Song 
110234df42f5SElen Song 	if (chan) {
110334df42f5SElen Song 		dmaengine_terminate_all(chan);
110434df42f5SElen Song 		dma_release_channel(chan);
110534df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
110648479148SWolfram Sang 				DMA_FROM_DEVICE);
110734df42f5SElen Song 	}
110834df42f5SElen Song 
110934df42f5SElen Song 	atmel_port->desc_rx = NULL;
111034df42f5SElen Song 	atmel_port->chan_rx = NULL;
111134df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
111234df42f5SElen Song }
111334df42f5SElen Song 
111434df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
111534df42f5SElen Song {
111634df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
111766f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
111834df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
111934df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
112034df42f5SElen Song 	struct dma_tx_state state;
112134df42f5SElen Song 	enum dma_status dmastat;
112266f37aafSCyrille Pitchen 	size_t count;
112334df42f5SElen Song 
112434df42f5SElen Song 
112534df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
11264e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
112734df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
112834df42f5SElen Song 				atmel_port->cookie_rx,
112934df42f5SElen Song 				&state);
113034df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
113134df42f5SElen Song 	if (dmastat == DMA_ERROR) {
113234df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
11334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
113498f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
113534df42f5SElen Song 		return;
113634df42f5SElen Song 	}
113766f37aafSCyrille Pitchen 
113866f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
113966f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
114066f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
114166f37aafSCyrille Pitchen 			    1,
1142485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
114334df42f5SElen Song 
114434df42f5SElen Song 	/*
114566f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
114666f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
114766f37aafSCyrille Pitchen 	 * framework.
114866f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
114966f37aafSCyrille Pitchen 	 * length.
115034df42f5SElen Song 	 */
115166f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
115266f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
115366f37aafSCyrille Pitchen 	/*
115466f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
115566f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
115666f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
115766f37aafSCyrille Pitchen 	 *
115866f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
115966f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
116066f37aafSCyrille Pitchen 	 *
116166f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
116266f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
116366f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
116466f37aafSCyrille Pitchen 	 */
116566f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
116666f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
116734df42f5SElen Song 
116866f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
116966f37aafSCyrille Pitchen 		ring->tail = 0;
117034df42f5SElen Song 		port->icount.rx += count;
117134df42f5SElen Song 	}
117234df42f5SElen Song 
117366f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
117466f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
117566f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
117666f37aafSCyrille Pitchen 
117766f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
117866f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
117966f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
118066f37aafSCyrille Pitchen 			ring->head = 0;
118166f37aafSCyrille Pitchen 		ring->tail = ring->head;
118266f37aafSCyrille Pitchen 		port->icount.rx += count;
118366f37aafSCyrille Pitchen 	}
118466f37aafSCyrille Pitchen 
118566f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
118666f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
118766f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
118866f37aafSCyrille Pitchen 			       1,
1189485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
119066f37aafSCyrille Pitchen 
119166f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
119266f37aafSCyrille Pitchen 
11934e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
119434df42f5SElen Song }
119534df42f5SElen Song 
119634df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
119734df42f5SElen Song {
119834df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1199c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
120034df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
120134df42f5SElen Song 	dma_cap_mask_t		mask;
120234df42f5SElen Song 	struct dma_slave_config config;
120334df42f5SElen Song 	struct circ_buf		*ring;
120434df42f5SElen Song 	int ret, nent;
120534df42f5SElen Song 
120634df42f5SElen Song 	ring = &atmel_port->rx_ring;
120734df42f5SElen Song 
120834df42f5SElen Song 	dma_cap_zero(mask);
120934df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
121034df42f5SElen Song 
1211c24d2531SRadu Pirea 	atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx");
121234df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
121334df42f5SElen Song 		goto chan_err;
121434df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
121534df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
121634df42f5SElen Song 
121734df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
121834df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
121934df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
12202c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
122134df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
122234df42f5SElen Song 		    virt_to_page(ring->buf),
1223a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
12242b5cf14bSGeliang Tang 		    offset_in_page(ring->buf));
122534df42f5SElen Song 	nent = dma_map_sg(port->dev,
122634df42f5SElen Song 			  &atmel_port->sg_rx,
122734df42f5SElen Song 			  1,
122848479148SWolfram Sang 			  DMA_FROM_DEVICE);
122934df42f5SElen Song 
123034df42f5SElen Song 	if (!nent) {
123134df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
123234df42f5SElen Song 		goto chan_err;
123334df42f5SElen Song 	} else {
1234c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
123534df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
123634df42f5SElen Song 			ring->buf,
1237c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
123834df42f5SElen Song 	}
123934df42f5SElen Song 
124034df42f5SElen Song 	/* Configure the slave DMA */
124134df42f5SElen Song 	memset(&config, 0, sizeof(config));
124234df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
124334df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
124434df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1245a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
124634df42f5SElen Song 
12475483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
12485483c10eSMaxime Ripard 				     &config);
124934df42f5SElen Song 	if (ret) {
125034df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
125134df42f5SElen Song 		goto chan_err;
125234df42f5SElen Song 	}
125334df42f5SElen Song 	/*
125434df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
125534df42f5SElen Song 	 * each one is half ring buffer size
125634df42f5SElen Song 	 */
125734df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
125834df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
125934df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
126034df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
126134df42f5SElen Song 					 DMA_DEV_TO_MEM,
126234df42f5SElen Song 					 DMA_PREP_INTERRUPT);
1263c85be041SKangjie Lu 	if (!desc) {
1264c85be041SKangjie Lu 		dev_err(port->dev, "Preparing DMA cyclic failed\n");
1265c85be041SKangjie Lu 		goto chan_err;
1266c85be041SKangjie Lu 	}
126734df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
126834df42f5SElen Song 	desc->callback_param = port;
126934df42f5SElen Song 	atmel_port->desc_rx = desc;
127034df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
12711e67bd2bSTudor Ambarus 	if (dma_submit_error(atmel_port->cookie_rx)) {
12721e67bd2bSTudor Ambarus 		dev_err(port->dev, "dma_submit_error %d\n",
12731e67bd2bSTudor Ambarus 			atmel_port->cookie_rx);
12741e67bd2bSTudor Ambarus 		goto chan_err;
12751e67bd2bSTudor Ambarus 	}
127634df42f5SElen Song 
12774f4b9b58STudor Ambarus 	dma_async_issue_pending(atmel_port->chan_rx);
12784f4b9b58STudor Ambarus 
127934df42f5SElen Song 	return 0;
128034df42f5SElen Song 
128134df42f5SElen Song chan_err:
128234df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
128336ce7cffSZheng Bin 	atmel_port->use_dma_rx = false;
128434df42f5SElen Song 	if (atmel_port->chan_rx)
128534df42f5SElen Song 		atmel_release_rx_dma(port);
128634df42f5SElen Song 	return -EINVAL;
128734df42f5SElen Song }
128834df42f5SElen Song 
1289026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t)
12902e68c22fSElen Song {
1291026cb432SKees Cook 	struct atmel_uart_port *atmel_port = from_timer(atmel_port, t,
1292026cb432SKees Cook 							uart_timer);
1293026cb432SKees Cook 	struct uart_port *port = &atmel_port->uart;
12942e68c22fSElen Song 
129598f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown)) {
129600e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
129798f2082cSNicolas Ferre 		mod_timer(&atmel_port->uart_timer,
129898f2082cSNicolas Ferre 			  jiffies + uart_poll_timeout(port));
129998f2082cSNicolas Ferre 	}
13002e68c22fSElen Song }
13012e68c22fSElen Song 
1302ab4382d2SGreg Kroah-Hartman /*
1303ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1304ab4382d2SGreg Kroah-Hartman  */
1305ab4382d2SGreg Kroah-Hartman static void
1306ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1307ab4382d2SGreg Kroah-Hartman {
1308ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1309ab4382d2SGreg Kroah-Hartman 
131064e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1311ab4382d2SGreg Kroah-Hartman 		/*
1312ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1313ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1314ab4382d2SGreg Kroah-Hartman 		 *
1315ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1316ab4382d2SGreg Kroah-Hartman 		 * the moment.
1317ab4382d2SGreg Kroah-Hartman 		 */
1318ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
13194e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
13204e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
132198f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
132298f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
1323ab4382d2SGreg Kroah-Hartman 		}
1324ab4382d2SGreg Kroah-Hartman 
1325ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1326ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1327ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1328ab4382d2SGreg Kroah-Hartman 	}
1329ab4382d2SGreg Kroah-Hartman 
133034df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
133134df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
13324e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
13334e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
133498f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
133598f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
133634df42f5SElen Song 		}
133734df42f5SElen Song 	}
133834df42f5SElen Song 
1339ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1340ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1341ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1342ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1343ab4382d2SGreg Kroah-Hartman 		/*
1344ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1345ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1346ab4382d2SGreg Kroah-Hartman 		 */
13474e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
13484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1349ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1350ab4382d2SGreg Kroah-Hartman 	}
1351ab4382d2SGreg Kroah-Hartman }
1352ab4382d2SGreg Kroah-Hartman 
1353ab4382d2SGreg Kroah-Hartman /*
1354ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1355ab4382d2SGreg Kroah-Hartman  */
1356ab4382d2SGreg Kroah-Hartman static void
1357ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1358ab4382d2SGreg Kroah-Hartman {
1359ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1360ab4382d2SGreg Kroah-Hartman 
1361ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
13624e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
13634e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
136469646d7aSRazvan Stefanescu 
136569646d7aSRazvan Stefanescu 		/* Start RX if flag was set and FIFO is empty */
136669646d7aSRazvan Stefanescu 		if (atmel_port->hd_start_rx) {
136769646d7aSRazvan Stefanescu 			if (!(atmel_uart_readl(port, ATMEL_US_CSR)
136869646d7aSRazvan Stefanescu 					& ATMEL_US_TXEMPTY))
136969646d7aSRazvan Stefanescu 				dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n");
137069646d7aSRazvan Stefanescu 
137169646d7aSRazvan Stefanescu 			atmel_port->hd_start_rx = false;
137269646d7aSRazvan Stefanescu 			atmel_start_rx(port);
137369646d7aSRazvan Stefanescu 		}
137469646d7aSRazvan Stefanescu 
137598f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1376ab4382d2SGreg Kroah-Hartman 	}
1377ab4382d2SGreg Kroah-Hartman }
1378ab4382d2SGreg Kroah-Hartman 
1379ab4382d2SGreg Kroah-Hartman /*
1380ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1381ab4382d2SGreg Kroah-Hartman  */
1382ab4382d2SGreg Kroah-Hartman static void
1383ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1384ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1385ab4382d2SGreg Kroah-Hartman {
1386ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
13879205218eSNicolas Ferre 	unsigned int status_change;
1388ab4382d2SGreg Kroah-Hartman 
1389ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1390ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
13919205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1392d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
13939205218eSNicolas Ferre 
13949205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
13959205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
13969205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
13979205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
13989205218eSNicolas Ferre 				port->icount.rng++;
13999205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
14009205218eSNicolas Ferre 				port->icount.dsr++;
14019205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
14029205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
14039205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
14049205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
14059205218eSNicolas Ferre 
14069205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
14079205218eSNicolas Ferre 		}
1408ab4382d2SGreg Kroah-Hartman 	}
1409377fedd1SNicolas Ferre 
1410377fedd1SNicolas Ferre 	if (pending & (ATMEL_US_NACK | ATMEL_US_ITERATION))
1411377fedd1SNicolas Ferre 		dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending);
1412ab4382d2SGreg Kroah-Hartman }
1413ab4382d2SGreg Kroah-Hartman 
1414ab4382d2SGreg Kroah-Hartman /*
1415ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1416ab4382d2SGreg Kroah-Hartman  */
1417ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1418ab4382d2SGreg Kroah-Hartman {
1419ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1420ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
14212c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1422ab4382d2SGreg Kroah-Hartman 
14232c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
14242c7af5baSBoris BREZILLON 
1425ab4382d2SGreg Kroah-Hartman 	do {
1426d2d8d4c0SRichard Genoud 		status = atmel_uart_readl(port, ATMEL_US_CSR);
14274e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
14282c7af5baSBoris BREZILLON 		pending = status & mask;
1429ab4382d2SGreg Kroah-Hartman 		if (!pending)
1430ab4382d2SGreg Kroah-Hartman 			break;
1431ab4382d2SGreg Kroah-Hartman 
14322c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
14332c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
14342c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
14354e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
14362c7af5baSBoris BREZILLON 			pm_system_wakeup();
14372c7af5baSBoris BREZILLON 			break;
14382c7af5baSBoris BREZILLON 		}
14392c7af5baSBoris BREZILLON 
1440ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1441ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1442ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1443ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1444ab4382d2SGreg Kroah-Hartman 
14452c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
14462c7af5baSBoris BREZILLON 
1447ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1448ab4382d2SGreg Kroah-Hartman }
1449ab4382d2SGreg Kroah-Hartman 
1450a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1451a930e528SElen Song {
1452a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1453a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1454a930e528SElen Song 
1455a930e528SElen Song 	dma_unmap_single(port->dev,
1456a930e528SElen Song 			 pdc->dma_addr,
1457a930e528SElen Song 			 pdc->dma_size,
1458a930e528SElen Song 			 DMA_TO_DEVICE);
1459a930e528SElen Song }
1460a930e528SElen Song 
1461ab4382d2SGreg Kroah-Hartman /*
1462ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1463ab4382d2SGreg Kroah-Hartman  */
146464e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1465ab4382d2SGreg Kroah-Hartman {
1466ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1467ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1468ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1469ab4382d2SGreg Kroah-Hartman 	int count;
1470ab4382d2SGreg Kroah-Hartman 
1471ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
14724e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1473ab4382d2SGreg Kroah-Hartman 		return;
1474ab4382d2SGreg Kroah-Hartman 
1475ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1476ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1477ab4382d2SGreg Kroah-Hartman 
1478ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1479ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1480ab4382d2SGreg Kroah-Hartman 
1481ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1482ab4382d2SGreg Kroah-Hartman 
1483ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
14844e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1485ab4382d2SGreg Kroah-Hartman 
1486ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1487ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1488ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1489ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1490ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1491ab4382d2SGreg Kroah-Hartman 
1492ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1493ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1494ab4382d2SGreg Kroah-Hartman 
14954e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
14964e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
14974e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1498ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
14994e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1500ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
15014e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
15024e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1503ab4382d2SGreg Kroah-Hartman 	} else {
1504f3040983SRazvan Stefanescu 		if (atmel_uart_is_half_duplex(port)) {
1505ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1506ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1507ab4382d2SGreg Kroah-Hartman 		}
1508ab4382d2SGreg Kroah-Hartman 	}
1509ab4382d2SGreg Kroah-Hartman 
1510ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1511ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1512ab4382d2SGreg Kroah-Hartman }
1513ab4382d2SGreg Kroah-Hartman 
1514a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1515a930e528SElen Song {
1516a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1517a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1518a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1519a930e528SElen Song 
1520a930e528SElen Song 	pdc->buf = xmit->buf;
1521a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1522a930e528SElen Song 					pdc->buf,
1523a930e528SElen Song 					UART_XMIT_SIZE,
1524a930e528SElen Song 					DMA_TO_DEVICE);
1525a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1526a930e528SElen Song 	pdc->ofs = 0;
1527a930e528SElen Song 
1528a930e528SElen Song 	return 0;
1529a930e528SElen Song }
1530a930e528SElen Song 
1531ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1532ab4382d2SGreg Kroah-Hartman {
1533ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1534ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1535ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1536ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1537ab4382d2SGreg Kroah-Hartman 
1538ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1539ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1540ab4382d2SGreg Kroah-Hartman 
1541ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1542ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1543ab4382d2SGreg Kroah-Hartman 
1544ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1545ab4382d2SGreg Kroah-Hartman 
1546ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1547ab4382d2SGreg Kroah-Hartman 
1548ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1549ab4382d2SGreg Kroah-Hartman 		status = c.status;
1550ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1551ab4382d2SGreg Kroah-Hartman 
1552ab4382d2SGreg Kroah-Hartman 		/*
1553ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1554ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1555ab4382d2SGreg Kroah-Hartman 		 */
1556ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1557ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1558ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1559ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1560ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1561ab4382d2SGreg Kroah-Hartman 
1562ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1563ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1564ab4382d2SGreg Kroah-Hartman 					continue;
1565ab4382d2SGreg Kroah-Hartman 			}
1566ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1567ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1568ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1569ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1570ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1571ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1572ab4382d2SGreg Kroah-Hartman 
1573ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1574ab4382d2SGreg Kroah-Hartman 
1575ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1576ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1577ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1578ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1579ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1580ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1581ab4382d2SGreg Kroah-Hartman 		}
1582ab4382d2SGreg Kroah-Hartman 
1583ab4382d2SGreg Kroah-Hartman 
1584ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1585ab4382d2SGreg Kroah-Hartman 			continue;
1586ab4382d2SGreg Kroah-Hartman 
1587ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1588ab4382d2SGreg Kroah-Hartman 	}
1589ab4382d2SGreg Kroah-Hartman 
15902e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1591ab4382d2SGreg Kroah-Hartman }
1592ab4382d2SGreg Kroah-Hartman 
1593a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1594a930e528SElen Song {
1595a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1596a930e528SElen Song 	int i;
1597a930e528SElen Song 
1598a930e528SElen Song 	for (i = 0; i < 2; i++) {
1599a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1600a930e528SElen Song 
1601a930e528SElen Song 		dma_unmap_single(port->dev,
1602a930e528SElen Song 				 pdc->dma_addr,
1603a930e528SElen Song 				 pdc->dma_size,
1604a930e528SElen Song 				 DMA_FROM_DEVICE);
1605a930e528SElen Song 		kfree(pdc->buf);
1606a930e528SElen Song 	}
1607a930e528SElen Song }
1608a930e528SElen Song 
160964e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1610ab4382d2SGreg Kroah-Hartman {
1611ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
161205c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1613ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1614ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1615ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1616ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1617ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1618ab4382d2SGreg Kroah-Hartman 
1619ab4382d2SGreg Kroah-Hartman 	do {
1620ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
16214e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1622ab4382d2SGreg Kroah-Hartman 
1623ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
16244e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1625ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1626ab4382d2SGreg Kroah-Hartman 
1627ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1628ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1629ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1630ab4382d2SGreg Kroah-Hartman 		 * find out.
1631ab4382d2SGreg Kroah-Hartman 		 *
1632ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1633ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1634ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1635ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1636ab4382d2SGreg Kroah-Hartman 		 */
1637ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1638ab4382d2SGreg Kroah-Hartman 
1639ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1640ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1641ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1642ab4382d2SGreg Kroah-Hartman 
1643ab4382d2SGreg Kroah-Hartman 			/*
1644ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1645ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1646ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1647ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1648ab4382d2SGreg Kroah-Hartman 			 */
1649ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1650ab4382d2SGreg Kroah-Hartman 
165105c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
165205c7cd39SJiri Slaby 						count);
1653ab4382d2SGreg Kroah-Hartman 
1654ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1655ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1656ab4382d2SGreg Kroah-Hartman 
1657ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1658ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1659ab4382d2SGreg Kroah-Hartman 		}
1660ab4382d2SGreg Kroah-Hartman 
1661ab4382d2SGreg Kroah-Hartman 		/*
1662ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1663ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1664ab4382d2SGreg Kroah-Hartman 		 */
1665ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1666ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
16674e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
16684e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1669ab4382d2SGreg Kroah-Hartman 
1670ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1671ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1672ab4382d2SGreg Kroah-Hartman 		}
1673ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1674ab4382d2SGreg Kroah-Hartman 
16752e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1676ab4382d2SGreg Kroah-Hartman 
16774e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
16784e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1679ab4382d2SGreg Kroah-Hartman }
1680ab4382d2SGreg Kroah-Hartman 
1681a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1682a930e528SElen Song {
1683a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1684a930e528SElen Song 	int i;
1685a930e528SElen Song 
1686a930e528SElen Song 	for (i = 0; i < 2; i++) {
1687a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1688a930e528SElen Song 
1689a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1690a930e528SElen Song 		if (pdc->buf == NULL) {
1691a930e528SElen Song 			if (i != 0) {
1692a930e528SElen Song 				dma_unmap_single(port->dev,
1693a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1694a930e528SElen Song 					PDC_BUFFER_SIZE,
1695a930e528SElen Song 					DMA_FROM_DEVICE);
1696a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1697a930e528SElen Song 			}
169836ce7cffSZheng Bin 			atmel_port->use_pdc_rx = false;
1699a930e528SElen Song 			return -ENOMEM;
1700a930e528SElen Song 		}
1701a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1702a930e528SElen Song 						pdc->buf,
1703a930e528SElen Song 						PDC_BUFFER_SIZE,
1704a930e528SElen Song 						DMA_FROM_DEVICE);
1705a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1706a930e528SElen Song 		pdc->ofs = 0;
1707a930e528SElen Song 	}
1708a930e528SElen Song 
1709a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1710a930e528SElen Song 
17114e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
17124e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1713a930e528SElen Song 
17144e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
17154e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
17164e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1717a930e528SElen Song 
1718a930e528SElen Song 	return 0;
1719a930e528SElen Song }
1720a930e528SElen Song 
1721ab4382d2SGreg Kroah-Hartman /*
1722ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1723ab4382d2SGreg Kroah-Hartman  */
172441e85e44SAllen Pais static void atmel_tasklet_rx_func(struct tasklet_struct *t)
1725ab4382d2SGreg Kroah-Hartman {
172641e85e44SAllen Pais 	struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t,
172741e85e44SAllen Pais 							  tasklet_rx);
172841e85e44SAllen Pais 	struct uart_port *port = &atmel_port->uart;
1729ab4382d2SGreg Kroah-Hartman 
1730ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1731ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1732a930e528SElen Song 	atmel_port->schedule_rx(port);
173300e8e658SNicolas Ferre 	spin_unlock(&port->lock);
173400e8e658SNicolas Ferre }
1735ab4382d2SGreg Kroah-Hartman 
173641e85e44SAllen Pais static void atmel_tasklet_tx_func(struct tasklet_struct *t)
173700e8e658SNicolas Ferre {
173841e85e44SAllen Pais 	struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t,
173941e85e44SAllen Pais 							  tasklet_tx);
174041e85e44SAllen Pais 	struct uart_port *port = &atmel_port->uart;
174100e8e658SNicolas Ferre 
174200e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
174300e8e658SNicolas Ferre 	spin_lock(&port->lock);
174400e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1745ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1746ab4382d2SGreg Kroah-Hartman }
1747ab4382d2SGreg Kroah-Hartman 
17484a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
174933d64c4fSElen Song 				struct platform_device *pdev)
175033d64c4fSElen Song {
175133d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
175233d64c4fSElen Song 
175333d64c4fSElen Song 	/* DMA/PDC usage specification */
1754490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1755490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
175633d64c4fSElen Song 			atmel_port->use_dma_rx  = true;
175733d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
175833d64c4fSElen Song 		} else {
175933d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
176033d64c4fSElen Song 			atmel_port->use_pdc_rx  = true;
176133d64c4fSElen Song 		}
176233d64c4fSElen Song 	} else {
176333d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
176433d64c4fSElen Song 		atmel_port->use_pdc_rx  = false;
176533d64c4fSElen Song 	}
176633d64c4fSElen Song 
1767490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1768490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
176933d64c4fSElen Song 			atmel_port->use_dma_tx  = true;
177033d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
177133d64c4fSElen Song 		} else {
177233d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
177333d64c4fSElen Song 			atmel_port->use_pdc_tx  = true;
177433d64c4fSElen Song 		}
177533d64c4fSElen Song 	} else {
177633d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
177733d64c4fSElen Song 		atmel_port->use_pdc_tx  = false;
177833d64c4fSElen Song 	}
177933d64c4fSElen Song }
178033d64c4fSElen Song 
1781a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1782a930e528SElen Song {
1783a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1784a930e528SElen Song 
178534df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
178634df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
178734df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
178834df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
178934df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1790a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1791a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1792a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1793a930e528SElen Song 	} else {
1794a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1795a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1796a930e528SElen Song 		atmel_port->release_rx = NULL;
1797a930e528SElen Song 	}
1798a930e528SElen Song 
179908f738beSElen Song 	if (atmel_use_dma_tx(port)) {
180008f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
180108f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
180208f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
180308f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1804a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1805a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1806a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1807a930e528SElen Song 	} else {
1808a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1809a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1810a930e528SElen Song 		atmel_port->release_tx = NULL;
1811a930e528SElen Song 	}
1812a930e528SElen Song }
1813a930e528SElen Song 
1814ab4382d2SGreg Kroah-Hartman /*
1815055560b0SElen Song  * Get ip name usart or uart
1816055560b0SElen Song  */
1817892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1818055560b0SElen Song {
1819055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
18204e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1821731d9caeSNicolas Ferre 	u32 version;
18221d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
18234b769371SNicolas Ferre 	/* ASCII decoding for IP version */
18244b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
18254b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
18261d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1827055560b0SElen Song 
18285bf5635aSLudovic Desroches 	/*
18295bf5635aSLudovic Desroches 	 * Only USART devices from at91sam9260 SOC implement fractional
18302867af2dSRomain Izard 	 * baudrate. It is available for all asynchronous modes, with the
18312867af2dSRomain Izard 	 * following restriction: the sampling clock's duty cycle is not
18322867af2dSRomain Izard 	 * constant.
18335bf5635aSLudovic Desroches 	 */
18345bf5635aSLudovic Desroches 	atmel_port->has_frac_baudrate = false;
18354b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
18365644bf18SSergiu Moga 	atmel_port->is_usart = false;
1837055560b0SElen Song 
18382958cceeSLudovic Desroches 	if (name == new_uart) {
18392958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
18404b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
18412958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
18422958cceeSLudovic Desroches 	} else if (name == usart) {
18432958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
18445bf5635aSLudovic Desroches 		atmel_port->has_frac_baudrate = true;
18452958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
18465644bf18SSergiu Moga 		atmel_port->is_usart = true;
18472958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
1848377fedd1SNicolas Ferre 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1849377fedd1SNicolas Ferre 		switch (version) {
1850377fedd1SNicolas Ferre 		case 0x814:	/* sama5d2 */
1851df561f66SGustavo A. R. Silva 			fallthrough;
1852377fedd1SNicolas Ferre 		case 0x701:	/* sama5d4 */
1853377fedd1SNicolas Ferre 			atmel_port->fidi_min = 3;
1854377fedd1SNicolas Ferre 			atmel_port->fidi_max = 65535;
1855377fedd1SNicolas Ferre 			break;
1856377fedd1SNicolas Ferre 		case 0x502:	/* sam9x5, sama5d3 */
1857377fedd1SNicolas Ferre 			atmel_port->fidi_min = 3;
1858377fedd1SNicolas Ferre 			atmel_port->fidi_max = 2047;
1859377fedd1SNicolas Ferre 			break;
1860377fedd1SNicolas Ferre 		default:
1861377fedd1SNicolas Ferre 			atmel_port->fidi_min = 1;
1862377fedd1SNicolas Ferre 			atmel_port->fidi_max = 2047;
1863377fedd1SNicolas Ferre 		}
18644b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
18654b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1866055560b0SElen Song 	} else {
1867731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
18684e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1869731d9caeSNicolas Ferre 		switch (version) {
1870731d9caeSNicolas Ferre 		case 0x302:
1871731d9caeSNicolas Ferre 		case 0x10213:
1872fd63a890SJonas Danielsson 		case 0x10302:
1873731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
18745bf5635aSLudovic Desroches 			atmel_port->has_frac_baudrate = true;
18754b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
18765644bf18SSergiu Moga 			atmel_port->is_usart = true;
18772958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1878731d9caeSNicolas Ferre 			break;
1879731d9caeSNicolas Ferre 		case 0x203:
1880731d9caeSNicolas Ferre 		case 0x10202:
1881731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1882731d9caeSNicolas Ferre 			break;
1883731d9caeSNicolas Ferre 		default:
1884731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1885731d9caeSNicolas Ferre 		}
1886055560b0SElen Song 	}
1887055560b0SElen Song }
1888055560b0SElen Song 
1889055560b0SElen Song /*
1890ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1891ab4382d2SGreg Kroah-Hartman  */
1892ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1893ab4382d2SGreg Kroah-Hartman {
189433d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1895ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1896ab4382d2SGreg Kroah-Hartman 	int retval;
1897ab4382d2SGreg Kroah-Hartman 
1898ab4382d2SGreg Kroah-Hartman 	/*
1899ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1900ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1901ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1902ab4382d2SGreg Kroah-Hartman 	 */
19034e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1904ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1905ab4382d2SGreg Kroah-Hartman 
1906ab4382d2SGreg Kroah-Hartman 	/*
1907ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1908ab4382d2SGreg Kroah-Hartman 	 */
19092c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
19102c7af5baSBoris BREZILLON 			     IRQF_SHARED | IRQF_COND_SUSPEND,
19119594b5beSSebastian Andrzej Siewior 			     dev_name(&pdev->dev), port);
1912ab4382d2SGreg Kroah-Hartman 	if (retval) {
1913ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1914ab4382d2SGreg Kroah-Hartman 		return retval;
1915ab4382d2SGreg Kroah-Hartman 	}
1916ab4382d2SGreg Kroah-Hartman 
191798f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
191841e85e44SAllen Pais 	tasklet_setup(&atmel_port->tasklet_rx, atmel_tasklet_rx_func);
191941e85e44SAllen Pais 	tasklet_setup(&atmel_port->tasklet_tx, atmel_tasklet_tx_func);
19201e125786SLeilei Zhao 
1921ab5e4e41SRichard Genoud 	/*
1922ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1923ab4382d2SGreg Kroah-Hartman 	 */
192433d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
19254d9628a1SLeilei Zhao 	atmel_set_ops(port);
192633d64c4fSElen Song 
1927a930e528SElen Song 	if (atmel_port->prepare_rx) {
1928a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1929a930e528SElen Song 		if (retval < 0)
1930a930e528SElen Song 			atmel_set_ops(port);
1931ab4382d2SGreg Kroah-Hartman 	}
1932ab4382d2SGreg Kroah-Hartman 
1933a930e528SElen Song 	if (atmel_port->prepare_tx) {
1934a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1935a930e528SElen Song 		if (retval < 0)
1936a930e528SElen Song 			atmel_set_ops(port);
1937ab4382d2SGreg Kroah-Hartman 	}
1938ab4382d2SGreg Kroah-Hartman 
1939b5199d46SCyrille Pitchen 	/*
1940b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1941b5199d46SCyrille Pitchen 	 */
1942b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1943b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1944b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1945b5199d46SCyrille Pitchen 		unsigned int fmr;
1946b5199d46SCyrille Pitchen 
1947b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1948b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1949b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1950b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1951b5199d46SCyrille Pitchen 
19525f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
19535f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
19545f258b3eSCyrille Pitchen 
1955b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1956b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1957b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1958b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1959b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1960b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1961b5199d46SCyrille Pitchen 
1962b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1963b5199d46SCyrille Pitchen 	}
1964b5199d46SCyrille Pitchen 
1965ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1966d2d8d4c0SRichard Genoud 	atmel_port->irq_status_prev = atmel_uart_readl(port, ATMEL_US_CSR);
1967ab4382d2SGreg Kroah-Hartman 
1968ab4382d2SGreg Kroah-Hartman 	/*
1969ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1970ab4382d2SGreg Kroah-Hartman 	 */
19714e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1972ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
19734e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1974ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
1975ab4382d2SGreg Kroah-Hartman 
1976026cb432SKees Cook 	timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0);
19778bc661bfSMarek Roszko 
19788bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
19798bc661bfSMarek Roszko 		/* set UART timeout */
19804b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19812e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19822e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19832e68c22fSElen Song 		/* set USART timeout */
19842e68c22fSElen Song 		} else {
19852958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
19862958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19874e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1988ab4382d2SGreg Kroah-Hartman 
19894e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19904e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
19912e68c22fSElen Song 		}
1992ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
19934e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
199434df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
19952e68c22fSElen Song 		/* set UART timeout */
19964b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19972e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19982e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19992e68c22fSElen Song 		/* set USART timeout */
20002e68c22fSElen Song 		} else {
20012958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
20022958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
20034e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
200434df42f5SElen Song 
20054e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
20064e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
20072e68c22fSElen Song 		}
2008ab4382d2SGreg Kroah-Hartman 	} else {
2009ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
20104e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
2011ab4382d2SGreg Kroah-Hartman 	}
2012ab4382d2SGreg Kroah-Hartman 
2013ab4382d2SGreg Kroah-Hartman 	return 0;
2014ab4382d2SGreg Kroah-Hartman }
2015ab4382d2SGreg Kroah-Hartman 
2016ab4382d2SGreg Kroah-Hartman /*
2017479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
2018479e9b94SPeter Hurley  * buffer is reset.
2019479e9b94SPeter Hurley  */
2020479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
2021479e9b94SPeter Hurley {
2022479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2023479e9b94SPeter Hurley 
2024479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
20254e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
2026479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
2027479e9b94SPeter Hurley 	}
202831ca2c63SRichard Genoud 	/*
202931ca2c63SRichard Genoud 	 * in uart_flush_buffer(), the xmit circular buffer has just
203031ca2c63SRichard Genoud 	 * been cleared, so we have to reset tx_len accordingly.
203131ca2c63SRichard Genoud 	 */
203231ca2c63SRichard Genoud 	atmel_port->tx_len = 0;
2033479e9b94SPeter Hurley }
2034479e9b94SPeter Hurley 
2035479e9b94SPeter Hurley /*
2036ab4382d2SGreg Kroah-Hartman  * Disable the port
2037ab4382d2SGreg Kroah-Hartman  */
2038ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
2039ab4382d2SGreg Kroah-Hartman {
2040ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
20410cc7c6c7SMarek Roszko 
20420ae9fdefSRichard Genoud 	/* Disable modem control lines interrupts */
20430ae9fdefSRichard Genoud 	atmel_disable_ms(port);
20440ae9fdefSRichard Genoud 
204598f2082cSNicolas Ferre 	/* Disable interrupts at device level */
204698f2082cSNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
204798f2082cSNicolas Ferre 
204898f2082cSNicolas Ferre 	/* Prevent spurious interrupts from scheduling the tasklet */
204998f2082cSNicolas Ferre 	atomic_inc(&atmel_port->tasklet_shutdown);
205098f2082cSNicolas Ferre 
2051ab4382d2SGreg Kroah-Hartman 	/*
20528bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
20538bc661bfSMarek Roszko 	 * cleanup
20548bc661bfSMarek Roszko 	 */
20558bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
20568bc661bfSMarek Roszko 
205798f2082cSNicolas Ferre 	/* Make sure that no interrupt is on the fly */
205898f2082cSNicolas Ferre 	synchronize_irq(port->irq);
205998f2082cSNicolas Ferre 
20608bc661bfSMarek Roszko 	/*
20610cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
20620cc7c6c7SMarek Roszko 	 * we destroy the buffers
20630cc7c6c7SMarek Roszko 	 */
206400e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
206500e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
20660cc7c6c7SMarek Roszko 
20670cc7c6c7SMarek Roszko 	/*
20680cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
206998f2082cSNicolas Ferre 	 * disable port and break condition.
2070ab4382d2SGreg Kroah-Hartman 	 */
2071ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
2072ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
2073ab4382d2SGreg Kroah-Hartman 
20744e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
20750cc7c6c7SMarek Roszko 
2076ab4382d2SGreg Kroah-Hartman 	/*
2077ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
2078ab4382d2SGreg Kroah-Hartman 	 */
2079a930e528SElen Song 	if (atmel_port->release_rx)
2080a930e528SElen Song 		atmel_port->release_rx(port);
2081a930e528SElen Song 	if (atmel_port->release_tx)
2082a930e528SElen Song 		atmel_port->release_tx(port);
2083ab4382d2SGreg Kroah-Hartman 
2084ab4382d2SGreg Kroah-Hartman 	/*
2085bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
2086bb7e73c5SMark Deneen 	 */
2087bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
2088bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
2089bb7e73c5SMark Deneen 
2090bb7e73c5SMark Deneen 	/*
2091ab5e4e41SRichard Genoud 	 * Free the interrupts
2092ab4382d2SGreg Kroah-Hartman 	 */
2093ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
2094ab5e4e41SRichard Genoud 
2095479e9b94SPeter Hurley 	atmel_flush_buffer(port);
2096ab4382d2SGreg Kroah-Hartman }
2097ab4382d2SGreg Kroah-Hartman 
2098ab4382d2SGreg Kroah-Hartman /*
2099ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
2100ab4382d2SGreg Kroah-Hartman  */
2101ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2102ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
2103ab4382d2SGreg Kroah-Hartman {
2104ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2105ab4382d2SGreg Kroah-Hartman 
2106ab4382d2SGreg Kroah-Hartman 	switch (state) {
2107aec079f8SClaudiu Beznea 	case UART_PM_STATE_ON:
2108ab4382d2SGreg Kroah-Hartman 		/*
2109ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
2110ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
2111ab4382d2SGreg Kroah-Hartman 		 */
211291f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
2113ab4382d2SGreg Kroah-Hartman 
2114ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
21154e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
2116ab4382d2SGreg Kroah-Hartman 		break;
2117aec079f8SClaudiu Beznea 	case UART_PM_STATE_OFF:
2118ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
21194e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
21204e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
2121ab4382d2SGreg Kroah-Hartman 
2122ab4382d2SGreg Kroah-Hartman 		/*
2123ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
2124ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
2125ab4382d2SGreg Kroah-Hartman 		 */
212691f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2127*5e3ce1f2SSergiu Moga 		if (__clk_is_enabled(atmel_port->gclk))
2128*5e3ce1f2SSergiu Moga 			clk_disable_unprepare(atmel_port->gclk);
2129ab4382d2SGreg Kroah-Hartman 		break;
2130ab4382d2SGreg Kroah-Hartman 	default:
2131ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2132ab4382d2SGreg Kroah-Hartman 	}
2133ab4382d2SGreg Kroah-Hartman }
2134ab4382d2SGreg Kroah-Hartman 
2135ab4382d2SGreg Kroah-Hartman /*
2136ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2137ab4382d2SGreg Kroah-Hartman  */
2138bec5b814SIlpo Järvinen static void atmel_set_termios(struct uart_port *port,
2139bec5b814SIlpo Järvinen 			      struct ktermios *termios,
2140bec5b814SIlpo Järvinen 			      const struct ktermios *old)
2141ab4382d2SGreg Kroah-Hartman {
21425bf5635aSLudovic Desroches 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2143ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
2144*5e3ce1f2SSergiu Moga 	unsigned int old_mode, mode, imr, quot, div, cd, fp = 0;
2145*5e3ce1f2SSergiu Moga 	unsigned int baud, actual_baud, gclk_rate;
2146*5e3ce1f2SSergiu Moga 	int ret;
2147ab4382d2SGreg Kroah-Hartman 
21481cf6e8fcSCyrille Pitchen 	/* save the current mode register */
21494e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
21501cf6e8fcSCyrille Pitchen 
21511cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
21521a5a01a1SSergiu Moga 	if (atmel_port->is_usart)
21531a5a01a1SSergiu Moga 		mode &= ~(ATMEL_US_NBSTOP | ATMEL_US_PAR | ATMEL_US_CHRL |
21541a5a01a1SSergiu Moga 			  ATMEL_US_USCLKS | ATMEL_US_USMODE);
21551a5a01a1SSergiu Moga 	else
21561a5a01a1SSergiu Moga 		mode &= ~(ATMEL_UA_BRSRCCK | ATMEL_US_PAR | ATMEL_UA_FILTER);
2157ab4382d2SGreg Kroah-Hartman 
2158ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2159ab4382d2SGreg Kroah-Hartman 
2160ab4382d2SGreg Kroah-Hartman 	/* byte size */
2161ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2162ab4382d2SGreg Kroah-Hartman 	case CS5:
2163ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2164ab4382d2SGreg Kroah-Hartman 		break;
2165ab4382d2SGreg Kroah-Hartman 	case CS6:
2166ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2167ab4382d2SGreg Kroah-Hartman 		break;
2168ab4382d2SGreg Kroah-Hartman 	case CS7:
2169ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2170ab4382d2SGreg Kroah-Hartman 		break;
2171ab4382d2SGreg Kroah-Hartman 	default:
2172ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2173ab4382d2SGreg Kroah-Hartman 		break;
2174ab4382d2SGreg Kroah-Hartman 	}
2175ab4382d2SGreg Kroah-Hartman 
2176ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2177ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2178ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2179ab4382d2SGreg Kroah-Hartman 
2180ab4382d2SGreg Kroah-Hartman 	/* parity */
2181ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2182ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2183ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2184ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2185ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2186ab4382d2SGreg Kroah-Hartman 			else
2187ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2188ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2189ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2190ab4382d2SGreg Kroah-Hartman 		else
2191ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2192ab4382d2SGreg Kroah-Hartman 	} else
2193ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2194ab4382d2SGreg Kroah-Hartman 
2195ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2196ab4382d2SGreg Kroah-Hartman 
2197ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2198ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2199ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2200ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2201ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2202ab4382d2SGreg Kroah-Hartman 
220364e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2204ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
22054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2206ab4382d2SGreg Kroah-Hartman 
2207ab4382d2SGreg Kroah-Hartman 	/*
2208ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2209ab4382d2SGreg Kroah-Hartman 	 */
2210ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2211ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2212ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2213ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2214ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2215ab4382d2SGreg Kroah-Hartman 		/*
2216ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2217ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2218ab4382d2SGreg Kroah-Hartman 		 */
2219ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2220ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2221ab4382d2SGreg Kroah-Hartman 	}
2222ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2223ab4382d2SGreg Kroah-Hartman 
2224ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2225ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2226ab4382d2SGreg Kroah-Hartman 
2227ab4382d2SGreg Kroah-Hartman 	/*
2228ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2229ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2230ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2231ab4382d2SGreg Kroah-Hartman 	 */
22324e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
22334e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2234ab4382d2SGreg Kroah-Hartman 
2235ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
22364e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2237ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
2238ab4382d2SGreg Kroah-Hartman 
22391cf6e8fcSCyrille Pitchen 	/* mode */
224013bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
22414e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
22424e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2243ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
2244377fedd1SNicolas Ferre 	} else if (port->iso7816.flags & SER_ISO7816_ENABLED) {
2245377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg);
2246377fedd1SNicolas Ferre 		/* select mck clock, and output  */
2247377fedd1SNicolas Ferre 		mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
2248377fedd1SNicolas Ferre 		/* set max iterations */
2249377fedd1SNicolas Ferre 		mode |= ATMEL_US_MAX_ITER(3);
2250377fedd1SNicolas Ferre 		if ((port->iso7816.flags & SER_ISO7816_T_PARAM)
2251377fedd1SNicolas Ferre 				== SER_ISO7816_T(0))
2252377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T0;
2253377fedd1SNicolas Ferre 		else
2254377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T1;
22551cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
22561cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
22579bcffe75SRichard Genoud 		if (atmel_use_fifo(port) &&
22589bcffe75SRichard Genoud 		    !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
22599bcffe75SRichard Genoud 			/*
22609bcffe75SRichard Genoud 			 * with ATMEL_US_USMODE_HWHS set, the controller will
22619bcffe75SRichard Genoud 			 * be able to drive the RTS pin high/low when the RX
22629bcffe75SRichard Genoud 			 * FIFO is above RXFTHRES/below RXFTHRES2.
22639bcffe75SRichard Genoud 			 * It will also disable the transmitter when the CTS
22649bcffe75SRichard Genoud 			 * pin is high.
22659bcffe75SRichard Genoud 			 * This mode is not activated if CTS pin is a GPIO
22669bcffe75SRichard Genoud 			 * because in this case, the transmitter is always
22679bcffe75SRichard Genoud 			 * disabled (there must be an internal pull-up
22689bcffe75SRichard Genoud 			 * responsible for this behaviour).
22699bcffe75SRichard Genoud 			 * If the RTS pin is a GPIO, the controller won't be
22709bcffe75SRichard Genoud 			 * able to drive it according to the FIFO thresholds,
22719bcffe75SRichard Genoud 			 * but it will be handled by the driver.
22729bcffe75SRichard Genoud 			 */
22731cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
22749bcffe75SRichard Genoud 		} else {
22759bcffe75SRichard Genoud 			/*
22769bcffe75SRichard Genoud 			 * For platforms without FIFO, the flow control is
22779bcffe75SRichard Genoud 			 * handled by the driver.
22789bcffe75SRichard Genoud 			 */
22799bcffe75SRichard Genoud 			mode |= ATMEL_US_USMODE_NORMAL;
22805be605acSAlexandre Belloni 		}
22811cf6e8fcSCyrille Pitchen 	} else {
22821cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
22831cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2284ab4382d2SGreg Kroah-Hartman 	}
2285ab4382d2SGreg Kroah-Hartman 
22865bf5635aSLudovic Desroches 	/*
22875bf5635aSLudovic Desroches 	 * Set the baud rate:
22885bf5635aSLudovic Desroches 	 * Fractional baudrate allows to setup output frequency more
22895bf5635aSLudovic Desroches 	 * accurately. This feature is enabled only when using normal mode.
22905bf5635aSLudovic Desroches 	 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
22915bf5635aSLudovic Desroches 	 * Currently, OVER is always set to 0 so we get
229236131cdfSAlexey Starikovskiy 	 * baudrate = selected clock / (16 * (CD + FP / 8))
229336131cdfSAlexey Starikovskiy 	 * then
229436131cdfSAlexey Starikovskiy 	 * 8 CD + FP = selected clock / (2 * baudrate)
22955bf5635aSLudovic Desroches 	 */
22962867af2dSRomain Izard 	if (atmel_port->has_frac_baudrate) {
229736131cdfSAlexey Starikovskiy 		div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
229836131cdfSAlexey Starikovskiy 		cd = div >> 3;
229936131cdfSAlexey Starikovskiy 		fp = div & ATMEL_US_FP_MASK;
23005bf5635aSLudovic Desroches 	} else {
23015bf5635aSLudovic Desroches 		cd = uart_get_divisor(port, baud);
23025bf5635aSLudovic Desroches 	}
23035bf5635aSLudovic Desroches 
23045644bf18SSergiu Moga 	/*
23055644bf18SSergiu Moga 	 * If the current value of the Clock Divisor surpasses the 16 bit
23065644bf18SSergiu Moga 	 * ATMEL_US_CD mask and the IP is USART, switch to the Peripheral
23075644bf18SSergiu Moga 	 * Clock implicitly divided by 8.
23085644bf18SSergiu Moga 	 * If the IP is UART however, keep the highest possible value for
23095644bf18SSergiu Moga 	 * the CD and avoid needless division of CD, since UART IP's do not
23105644bf18SSergiu Moga 	 * support implicit division of the Peripheral Clock.
23115644bf18SSergiu Moga 	 */
23125644bf18SSergiu Moga 	if (atmel_port->is_usart && cd > ATMEL_US_CD) {
23135bf5635aSLudovic Desroches 		cd /= 8;
23145bf5635aSLudovic Desroches 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
23155644bf18SSergiu Moga 	} else {
23165644bf18SSergiu Moga 		cd = min_t(unsigned int, cd, ATMEL_US_CD);
23175bf5635aSLudovic Desroches 	}
23185644bf18SSergiu Moga 
2319*5e3ce1f2SSergiu Moga 	/*
2320*5e3ce1f2SSergiu Moga 	 * If there is no Fractional Part, there is a high chance that
2321*5e3ce1f2SSergiu Moga 	 * we may be able to generate a baudrate closer to the desired one
2322*5e3ce1f2SSergiu Moga 	 * if we use the GCLK as the clock source driving the baudrate
2323*5e3ce1f2SSergiu Moga 	 * generator.
2324*5e3ce1f2SSergiu Moga 	 */
2325*5e3ce1f2SSergiu Moga 	if (!atmel_port->has_frac_baudrate) {
2326*5e3ce1f2SSergiu Moga 		if (__clk_is_enabled(atmel_port->gclk))
2327*5e3ce1f2SSergiu Moga 			clk_disable_unprepare(atmel_port->gclk);
2328*5e3ce1f2SSergiu Moga 		gclk_rate = clk_round_rate(atmel_port->gclk, 16 * baud);
2329*5e3ce1f2SSergiu Moga 		actual_baud = clk_get_rate(atmel_port->clk) / (16 * cd);
2330*5e3ce1f2SSergiu Moga 		if (gclk_rate && abs(atmel_error_rate(baud, actual_baud)) >
2331*5e3ce1f2SSergiu Moga 		    abs(atmel_error_rate(baud, gclk_rate / 16))) {
2332*5e3ce1f2SSergiu Moga 			clk_set_rate(atmel_port->gclk, 16 * baud);
2333*5e3ce1f2SSergiu Moga 			ret = clk_prepare_enable(atmel_port->gclk);
2334*5e3ce1f2SSergiu Moga 			if (ret)
2335*5e3ce1f2SSergiu Moga 				goto gclk_fail;
2336*5e3ce1f2SSergiu Moga 
2337*5e3ce1f2SSergiu Moga 			if (atmel_port->is_usart) {
2338*5e3ce1f2SSergiu Moga 				mode &= ~ATMEL_US_USCLKS;
2339*5e3ce1f2SSergiu Moga 				mode |= ATMEL_US_USCLKS_GCLK;
2340*5e3ce1f2SSergiu Moga 			} else {
2341*5e3ce1f2SSergiu Moga 				mode |= ATMEL_UA_BRSRCCK;
2342*5e3ce1f2SSergiu Moga 			}
2343*5e3ce1f2SSergiu Moga 
2344*5e3ce1f2SSergiu Moga 			/*
2345*5e3ce1f2SSergiu Moga 			 * Set the Clock Divisor for GCLK to 1.
2346*5e3ce1f2SSergiu Moga 			 * Since we were able to generate the smallest
2347*5e3ce1f2SSergiu Moga 			 * multiple of the desired baudrate times 16,
2348*5e3ce1f2SSergiu Moga 			 * then we surely can generate a bigger multiple
2349*5e3ce1f2SSergiu Moga 			 * with the exact error rate for an equally increased
2350*5e3ce1f2SSergiu Moga 			 * CD. Thus no need to take into account
2351*5e3ce1f2SSergiu Moga 			 * a higher value for CD.
2352*5e3ce1f2SSergiu Moga 			 */
2353*5e3ce1f2SSergiu Moga 			cd = 1;
2354*5e3ce1f2SSergiu Moga 		}
2355*5e3ce1f2SSergiu Moga 	}
2356*5e3ce1f2SSergiu Moga 
2357*5e3ce1f2SSergiu Moga gclk_fail:
23585bf5635aSLudovic Desroches 	quot = cd | fp << ATMEL_US_FP_OFFSET;
23595bf5635aSLudovic Desroches 
2360377fedd1SNicolas Ferre 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
23614e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2362cb47b9f8SDavid Engraf 
2363cb47b9f8SDavid Engraf 	/* set the mode, clock divisor, parity, stop bits and data size */
2364cb47b9f8SDavid Engraf 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2365cb47b9f8SDavid Engraf 
2366cb47b9f8SDavid Engraf 	/*
2367cb47b9f8SDavid Engraf 	 * when switching the mode, set the RTS line state according to the
2368cb47b9f8SDavid Engraf 	 * new mode, otherwise keep the former state
2369cb47b9f8SDavid Engraf 	 */
2370cb47b9f8SDavid Engraf 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2371cb47b9f8SDavid Engraf 		unsigned int rts_state;
2372cb47b9f8SDavid Engraf 
2373cb47b9f8SDavid Engraf 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2374cb47b9f8SDavid Engraf 			/* let the hardware control the RTS line */
2375cb47b9f8SDavid Engraf 			rts_state = ATMEL_US_RTSDIS;
2376cb47b9f8SDavid Engraf 		} else {
2377cb47b9f8SDavid Engraf 			/* force RTS line to low level */
2378cb47b9f8SDavid Engraf 			rts_state = ATMEL_US_RTSEN;
2379cb47b9f8SDavid Engraf 		}
2380cb47b9f8SDavid Engraf 
2381cb47b9f8SDavid Engraf 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
2382cb47b9f8SDavid Engraf 	}
2383cb47b9f8SDavid Engraf 
23844e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
23854e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2386ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2387ab4382d2SGreg Kroah-Hartman 
2388ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
23894e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2390ab4382d2SGreg Kroah-Hartman 
2391ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2392ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
239335b675b9SRichard Genoud 		atmel_enable_ms(port);
239435b675b9SRichard Genoud 	else
239535b675b9SRichard Genoud 		atmel_disable_ms(port);
2396ab4382d2SGreg Kroah-Hartman 
2397ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2398ab4382d2SGreg Kroah-Hartman }
2399ab4382d2SGreg Kroah-Hartman 
2400732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
240142bd7a4fSViktar Palstsiuk {
2402732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
240342bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2404d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
240542bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2406d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
240742bd7a4fSViktar Palstsiuk 	} else {
240842bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2409cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2410cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2411cab68f89SPeter Hurley 			atmel_disable_ms(port);
2412cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2413cab68f89SPeter Hurley 		}
241442bd7a4fSViktar Palstsiuk 	}
241542bd7a4fSViktar Palstsiuk }
241642bd7a4fSViktar Palstsiuk 
2417ab4382d2SGreg Kroah-Hartman /*
2418ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2419ab4382d2SGreg Kroah-Hartman  */
2420ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2421ab4382d2SGreg Kroah-Hartman {
2422ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2423ab4382d2SGreg Kroah-Hartman }
2424ab4382d2SGreg Kroah-Hartman 
2425ab4382d2SGreg Kroah-Hartman /*
2426ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2427ab4382d2SGreg Kroah-Hartman  */
2428ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2429ab4382d2SGreg Kroah-Hartman {
2430c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2431c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2432ab4382d2SGreg Kroah-Hartman 
2433ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2434ab4382d2SGreg Kroah-Hartman 
2435ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2436ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2437ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2438ab4382d2SGreg Kroah-Hartman 	}
2439ab4382d2SGreg Kroah-Hartman }
2440ab4382d2SGreg Kroah-Hartman 
2441ab4382d2SGreg Kroah-Hartman /*
2442ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2443ab4382d2SGreg Kroah-Hartman  */
2444ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2445ab4382d2SGreg Kroah-Hartman {
2446c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2447c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2448ab4382d2SGreg Kroah-Hartman 
2449ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2450ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2451ab4382d2SGreg Kroah-Hartman 
2452ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2453ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2454ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2455ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2456ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2457ab4382d2SGreg Kroah-Hartman 		}
2458ab4382d2SGreg Kroah-Hartman 	}
2459ab4382d2SGreg Kroah-Hartman 
2460ab4382d2SGreg Kroah-Hartman 	return 0;
2461ab4382d2SGreg Kroah-Hartman }
2462ab4382d2SGreg Kroah-Hartman 
2463ab4382d2SGreg Kroah-Hartman /*
2464ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2465ab4382d2SGreg Kroah-Hartman  */
2466ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2467ab4382d2SGreg Kroah-Hartman {
2468ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2469ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2470ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2471ab4382d2SGreg Kroah-Hartman 	}
2472ab4382d2SGreg Kroah-Hartman }
2473ab4382d2SGreg Kroah-Hartman 
2474ab4382d2SGreg Kroah-Hartman /*
2475ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2476ab4382d2SGreg Kroah-Hartman  */
2477ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2478ab4382d2SGreg Kroah-Hartman {
2479ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2480ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2481ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2482ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2483ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2484ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2485ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2486ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2487ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2488270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2489ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2490ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2491ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2492ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2493ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2494ab4382d2SGreg Kroah-Hartman 	return ret;
2495ab4382d2SGreg Kroah-Hartman }
2496ab4382d2SGreg Kroah-Hartman 
2497ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2498ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2499ab4382d2SGreg Kroah-Hartman {
25004e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2501ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2502ab4382d2SGreg Kroah-Hartman 
2503a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2504ab4382d2SGreg Kroah-Hartman }
2505ab4382d2SGreg Kroah-Hartman 
2506ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2507ab4382d2SGreg Kroah-Hartman {
25084e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2509ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2510ab4382d2SGreg Kroah-Hartman 
2511a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2512ab4382d2SGreg Kroah-Hartman }
2513ab4382d2SGreg Kroah-Hartman #endif
2514ab4382d2SGreg Kroah-Hartman 
25155c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = {
2516ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2517ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2518ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2519ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2520ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2521ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2522ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2523ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2524ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2525ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2526ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2527ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
252842bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2529ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2530ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2531ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2532ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2533ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2534ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2535ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2536ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2537ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2538ab4382d2SGreg Kroah-Hartman #endif
2539ab4382d2SGreg Kroah-Hartman };
2540ab4382d2SGreg Kroah-Hartman 
2541af47c491SIlpo Järvinen static const struct serial_rs485 atmel_rs485_supported = {
2542af47c491SIlpo Järvinen 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX,
2543af47c491SIlpo Järvinen 	.delay_rts_before_send = 1,
2544af47c491SIlpo Järvinen 	.delay_rts_after_send = 1,
2545af47c491SIlpo Järvinen };
2546af47c491SIlpo Järvinen 
2547ab4382d2SGreg Kroah-Hartman /*
2548ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2549ab4382d2SGreg Kroah-Hartman  */
255091f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2551ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2552ab4382d2SGreg Kroah-Hartman {
255391f8c2d8SBoris BREZILLON 	int ret;
2554ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2555c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(pdev->dev.parent);
2556ab4382d2SGreg Kroah-Hartman 
25574a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2558a930e528SElen Song 	atmel_set_ops(port);
2559a930e528SElen Song 
2560ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
256192c8f7c0SAlexandre Belloni 	port->flags		= UPF_BOOT_AUTOCONF | UPF_IOREMAP;
2562ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2563ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2564ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2565c24d2531SRadu Pirea 	port->mapbase		= mpdev->resource[0].start;
25665bb221b0SRob Herring 	port->irq		= platform_get_irq(mpdev, 0);
256713bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
25680139da50SIlpo Järvinen 	port->rs485_supported	= atmel_rs485_supported;
2569377fedd1SNicolas Ferre 	port->iso7816_config	= atmel_config_iso7816;
257092c8f7c0SAlexandre Belloni 	port->membase		= NULL;
2571ab4382d2SGreg Kroah-Hartman 
2572ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2573ab4382d2SGreg Kroah-Hartman 
2574c150c0f3SLukas Wunner 	ret = uart_get_rs485_mode(port);
2575c150c0f3SLukas Wunner 	if (ret)
2576c150c0f3SLukas Wunner 		return ret;
2577c150c0f3SLukas Wunner 
2578ab4382d2SGreg Kroah-Hartman 	port->uartclk = clk_get_rate(atmel_port->clk);
2579ab4382d2SGreg Kroah-Hartman 
2580377fedd1SNicolas Ferre 	/*
2581377fedd1SNicolas Ferre 	 * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
2582377fedd1SNicolas Ferre 	 * ENDTX|TXBUFE
2583377fedd1SNicolas Ferre 	 */
2584477b8383SCodrin.Ciubotariu@microchip.com 	if (atmel_uart_is_half_duplex(port))
2585ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
258664e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2587ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2588ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2589ab4382d2SGreg Kroah-Hartman 	} else {
2590ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2591ab4382d2SGreg Kroah-Hartman 	}
259291f8c2d8SBoris BREZILLON 
259391f8c2d8SBoris BREZILLON 	return 0;
2594ab4382d2SGreg Kroah-Hartman }
2595ab4382d2SGreg Kroah-Hartman 
2596ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
25973f8bab17SJiri Slaby static void atmel_console_putchar(struct uart_port *port, unsigned char ch)
2598ab4382d2SGreg Kroah-Hartman {
25994e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2600ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2601a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2602ab4382d2SGreg Kroah-Hartman }
2603ab4382d2SGreg Kroah-Hartman 
2604ab4382d2SGreg Kroah-Hartman /*
2605ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2606ab4382d2SGreg Kroah-Hartman  */
2607ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2608ab4382d2SGreg Kroah-Hartman {
2609ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2610ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2611ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2612ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2613ab4382d2SGreg Kroah-Hartman 
2614ab4382d2SGreg Kroah-Hartman 	/*
2615ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2616ab4382d2SGreg Kroah-Hartman 	 */
26174e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
26184e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
26194e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2620ab4382d2SGreg Kroah-Hartman 
2621ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
26224e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
26234e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2624ab4382d2SGreg Kroah-Hartman 
2625497e1e16SNicolas Ferre 	/* Make sure that tx path is actually able to send characters */
2626497e1e16SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2627ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2628497e1e16SNicolas Ferre 
2629ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2630ab4382d2SGreg Kroah-Hartman 
2631ab4382d2SGreg Kroah-Hartman 	/*
2632ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2633ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2634ab4382d2SGreg Kroah-Hartman 	 */
2635ab4382d2SGreg Kroah-Hartman 	do {
26364e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2637ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2638ab4382d2SGreg Kroah-Hartman 
2639ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2640ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
26414e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2642ab4382d2SGreg Kroah-Hartman 
2643ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
26444e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2645ab4382d2SGreg Kroah-Hartman }
2646ab4382d2SGreg Kroah-Hartman 
2647ab4382d2SGreg Kroah-Hartman /*
2648ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2649ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2650ab4382d2SGreg Kroah-Hartman  */
2651ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2652ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2653ab4382d2SGreg Kroah-Hartman {
2654ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2655ab4382d2SGreg Kroah-Hartman 
2656ab4382d2SGreg Kroah-Hartman 	/*
2657ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2658ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2659ab4382d2SGreg Kroah-Hartman 	 */
26604e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2661ab4382d2SGreg Kroah-Hartman 	if (!quot)
2662ab4382d2SGreg Kroah-Hartman 		return;
2663ab4382d2SGreg Kroah-Hartman 
26644e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2665ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2666ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2667ab4382d2SGreg Kroah-Hartman 	else
2668ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2669ab4382d2SGreg Kroah-Hartman 
26704e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2671ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2672ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2673ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2674ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2675ab4382d2SGreg Kroah-Hartman 
2676ab4382d2SGreg Kroah-Hartman 	/*
2677ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2678ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2679ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2680ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2681ab4382d2SGreg Kroah-Hartman 	 */
2682ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2683ab4382d2SGreg Kroah-Hartman }
2684ab4382d2SGreg Kroah-Hartman 
2685ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2686ab4382d2SGreg Kroah-Hartman {
2687ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2688ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2689ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2690ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2691ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2692ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2693ab4382d2SGreg Kroah-Hartman 
2694ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2695ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2696ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2697ab4382d2SGreg Kroah-Hartman 	}
2698ab4382d2SGreg Kroah-Hartman 
26994e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
27004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
27014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2702ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2703ab4382d2SGreg Kroah-Hartman 
2704ab4382d2SGreg Kroah-Hartman 	if (options)
2705ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2706ab4382d2SGreg Kroah-Hartman 	else
2707ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2708ab4382d2SGreg Kroah-Hartman 
2709ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2710ab4382d2SGreg Kroah-Hartman }
2711ab4382d2SGreg Kroah-Hartman 
2712ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2713ab4382d2SGreg Kroah-Hartman 
2714ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2715ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2716ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2717ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2718ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2719ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2720ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2721ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2722ab4382d2SGreg Kroah-Hartman };
2723ab4382d2SGreg Kroah-Hartman 
2724aab68e95SMichael Walle static void atmel_serial_early_write(struct console *con, const char *s,
2725aab68e95SMichael Walle 				     unsigned int n)
2726aab68e95SMichael Walle {
2727aab68e95SMichael Walle 	struct earlycon_device *dev = con->data;
2728aab68e95SMichael Walle 
2729aab68e95SMichael Walle 	uart_console_write(&dev->port, s, n, atmel_console_putchar);
2730aab68e95SMichael Walle }
2731aab68e95SMichael Walle 
2732aab68e95SMichael Walle static int __init atmel_early_console_setup(struct earlycon_device *device,
2733aab68e95SMichael Walle 					    const char *options)
2734aab68e95SMichael Walle {
2735aab68e95SMichael Walle 	if (!device->port.membase)
2736aab68e95SMichael Walle 		return -ENODEV;
2737aab68e95SMichael Walle 
2738aab68e95SMichael Walle 	device->con->write = atmel_serial_early_write;
2739aab68e95SMichael Walle 
2740aab68e95SMichael Walle 	return 0;
2741aab68e95SMichael Walle }
2742aab68e95SMichael Walle 
2743aab68e95SMichael Walle OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91rm9200-usart",
2744aab68e95SMichael Walle 		    atmel_early_console_setup);
2745aab68e95SMichael Walle OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91sam9260-usart",
2746aab68e95SMichael Walle 		    atmel_early_console_setup);
2747aab68e95SMichael Walle 
2748ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2749ab4382d2SGreg Kroah-Hartman 
2750ab4382d2SGreg Kroah-Hartman #else
2751ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2752ab4382d2SGreg Kroah-Hartman #endif
2753ab4382d2SGreg Kroah-Hartman 
2754ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2755ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2756ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2757ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2758ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2759ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2760ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2761ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2762ab4382d2SGreg Kroah-Hartman };
2763ab4382d2SGreg Kroah-Hartman 
2764ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2765ab4382d2SGreg Kroah-Hartman {
2766ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2767ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2768ab4382d2SGreg Kroah-Hartman #else
2769ab4382d2SGreg Kroah-Hartman 	return false;
2770ab4382d2SGreg Kroah-Hartman #endif
2771ab4382d2SGreg Kroah-Hartman }
2772ab4382d2SGreg Kroah-Hartman 
2773b50058b8SClaudiu Beznea static int __maybe_unused atmel_serial_suspend(struct device *dev)
2774ab4382d2SGreg Kroah-Hartman {
2775b50058b8SClaudiu Beznea 	struct uart_port *port = dev_get_drvdata(dev);
2776ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2777ab4382d2SGreg Kroah-Hartman 
2778207f6f34SAndy Shevchenko 	if (uart_console(port) && console_suspend_enabled) {
2779ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
27804e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
27814e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2782ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2783ab4382d2SGreg Kroah-Hartman 	}
2784ab4382d2SGreg Kroah-Hartman 
2785207f6f34SAndy Shevchenko 	if (uart_console(port) && !console_suspend_enabled) {
27866a5f0e2fSAlexandre Belloni 		/* Cache register values as we won't get a full shutdown/startup
27876a5f0e2fSAlexandre Belloni 		 * cycle
27886a5f0e2fSAlexandre Belloni 		 */
27896a5f0e2fSAlexandre Belloni 		atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR);
27906a5f0e2fSAlexandre Belloni 		atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR);
27916a5f0e2fSAlexandre Belloni 		atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
27926a5f0e2fSAlexandre Belloni 		atmel_port->cache.rtor = atmel_uart_readl(port,
27936a5f0e2fSAlexandre Belloni 							  atmel_port->rtor);
27946a5f0e2fSAlexandre Belloni 		atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR);
27956a5f0e2fSAlexandre Belloni 		atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR);
27966a5f0e2fSAlexandre Belloni 		atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR);
27976a5f0e2fSAlexandre Belloni 	}
27986a5f0e2fSAlexandre Belloni 
2799ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2800b50058b8SClaudiu Beznea 	atmel_port->may_wakeup = device_may_wakeup(dev);
28012c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
28022c7af5baSBoris BREZILLON 		unsigned long flags;
28032c7af5baSBoris BREZILLON 
28042c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
28052c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
28062c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2807b50058b8SClaudiu Beznea 		device_set_wakeup_enable(dev, 0);
28082c7af5baSBoris BREZILLON 	}
2809ab4382d2SGreg Kroah-Hartman 
2810ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2811ab4382d2SGreg Kroah-Hartman 
2812ab4382d2SGreg Kroah-Hartman 	return 0;
2813ab4382d2SGreg Kroah-Hartman }
2814ab4382d2SGreg Kroah-Hartman 
2815b50058b8SClaudiu Beznea static int __maybe_unused atmel_serial_resume(struct device *dev)
2816ab4382d2SGreg Kroah-Hartman {
2817b50058b8SClaudiu Beznea 	struct uart_port *port = dev_get_drvdata(dev);
2818ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
28192c7af5baSBoris BREZILLON 	unsigned long flags;
28202c7af5baSBoris BREZILLON 
2821207f6f34SAndy Shevchenko 	if (uart_console(port) && !console_suspend_enabled) {
28226a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
28236a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
28246a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
28256a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, atmel_port->rtor,
28266a5f0e2fSAlexandre Belloni 				  atmel_port->cache.rtor);
28276a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr);
28286a5f0e2fSAlexandre Belloni 
28296a5f0e2fSAlexandre Belloni 		if (atmel_port->fifo_size) {
28306a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN |
28316a5f0e2fSAlexandre Belloni 					  ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR);
28326a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FMR,
28336a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fmr);
28346a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FIER,
28356a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fimr);
28366a5f0e2fSAlexandre Belloni 		}
28376a5f0e2fSAlexandre Belloni 		atmel_start_rx(port);
28386a5f0e2fSAlexandre Belloni 	}
28396a5f0e2fSAlexandre Belloni 
28402c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
28412c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
28422c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
28432c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
28442c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
28452c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
28462c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
28472c7af5baSBoris BREZILLON 	}
28482c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
28492c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2850ab4382d2SGreg Kroah-Hartman 
2851ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2852b50058b8SClaudiu Beznea 	device_set_wakeup_enable(dev, atmel_port->may_wakeup);
2853ab4382d2SGreg Kroah-Hartman 
2854ab4382d2SGreg Kroah-Hartman 	return 0;
2855ab4382d2SGreg Kroah-Hartman }
2856ab4382d2SGreg Kroah-Hartman 
2857b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2858b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2859b5199d46SCyrille Pitchen {
2860b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2861b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2862b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2863b5199d46SCyrille Pitchen 
2864b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2865b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2866b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2867b5199d46SCyrille Pitchen 		return;
2868b5199d46SCyrille Pitchen 
2869b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2870b5199d46SCyrille Pitchen 		return;
2871b5199d46SCyrille Pitchen 
2872b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2873b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2874b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2875b5199d46SCyrille Pitchen 		return;
2876b5199d46SCyrille Pitchen 	}
2877b5199d46SCyrille Pitchen 
2878b5199d46SCyrille Pitchen 	/*
2879b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2880b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2881b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2882b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2883b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2884b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2885b5199d46SCyrille Pitchen 	 */
2886b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2887b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2888b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2889b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2890b5199d46SCyrille Pitchen 
2891b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2892b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2893b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2894b78cd169SJaeden Amero 		atmel_port->rts_high);
2895b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2896b78cd169SJaeden Amero 		atmel_port->rts_low);
2897b5199d46SCyrille Pitchen }
2898b5199d46SCyrille Pitchen 
28999671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2900ab4382d2SGreg Kroah-Hartman {
2901b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
2902c24d2531SRadu Pirea 	struct device_node *np = pdev->dev.parent->of_node;
2903ab4382d2SGreg Kroah-Hartman 	void *data;
29048d41ab87SColin Ian King 	int ret;
2905bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2906ab4382d2SGreg Kroah-Hartman 
2907ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2908ab4382d2SGreg Kroah-Hartman 
2909c24d2531SRadu Pirea 	/*
2910c24d2531SRadu Pirea 	 * In device tree there is no node with "atmel,at91rm9200-usart-serial"
2911c24d2531SRadu Pirea 	 * as compatible string. This driver is probed by at91-usart mfd driver
2912c24d2531SRadu Pirea 	 * which is just a wrapper over the atmel_serial driver and
2913c24d2531SRadu Pirea 	 * spi-at91-usart driver. All attributes needed by this driver are
2914c24d2531SRadu Pirea 	 * found in of_node of parent.
2915c24d2531SRadu Pirea 	 */
2916c24d2531SRadu Pirea 	pdev->dev.of_node = np;
2917c24d2531SRadu Pirea 
29185fbe46b6SNicolas Ferre 	ret = of_alias_get_id(np, "serial");
29194cbf9f48SNicolas Ferre 	if (ret < 0)
29205fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
29214cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2922503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
29234cbf9f48SNicolas Ferre 
2924503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
29254cbf9f48SNicolas Ferre 		ret = -ENODEV;
29264cbf9f48SNicolas Ferre 		goto err;
29274cbf9f48SNicolas Ferre 	}
29284cbf9f48SNicolas Ferre 
2929503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
29304cbf9f48SNicolas Ferre 		/* port already in use */
29314cbf9f48SNicolas Ferre 		ret = -EBUSY;
29324cbf9f48SNicolas Ferre 		goto err;
29334cbf9f48SNicolas Ferre 	}
29344cbf9f48SNicolas Ferre 
2935b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2936b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2937b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2938078abd98SDmitry Safonov 	atmel_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_ATMEL_CONSOLE);
2939b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2940354e57f3SLinus Walleij 
294198f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
2942b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
29432c7af5baSBoris BREZILLON 
294484b476b1SClaudiu Beznea 	atmel_port->clk = devm_clk_get(&pdev->dev, "usart");
294584b476b1SClaudiu Beznea 	if (IS_ERR(atmel_port->clk)) {
294684b476b1SClaudiu Beznea 		ret = PTR_ERR(atmel_port->clk);
294784b476b1SClaudiu Beznea 		goto err;
294884b476b1SClaudiu Beznea 	}
294984b476b1SClaudiu Beznea 	ret = clk_prepare_enable(atmel_port->clk);
295084b476b1SClaudiu Beznea 	if (ret)
295184b476b1SClaudiu Beznea 		goto err;
295284b476b1SClaudiu Beznea 
2953*5e3ce1f2SSergiu Moga 	atmel_port->gclk = devm_clk_get_optional(&pdev->dev, "gclk");
2954*5e3ce1f2SSergiu Moga 	if (IS_ERR(atmel_port->gclk)) {
2955*5e3ce1f2SSergiu Moga 		ret = PTR_ERR(atmel_port->gclk);
2956*5e3ce1f2SSergiu Moga 		goto err_clk_disable_unprepare;
2957*5e3ce1f2SSergiu Moga 	}
2958*5e3ce1f2SSergiu Moga 
2959b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
296091f8c2d8SBoris BREZILLON 	if (ret)
296184b476b1SClaudiu Beznea 		goto err_clk_disable_unprepare;
2962ab4382d2SGreg Kroah-Hartman 
2963b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2964b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2965b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
296684b476b1SClaudiu Beznea 		goto err_clk_disable_unprepare;
296718dfef9cSUwe Kleine-König 	}
296818dfef9cSUwe Kleine-König 
2969b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2970ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
29716da2ec56SKees Cook 		data = kmalloc_array(ATMEL_SERIAL_RINGSIZE,
29726da2ec56SKees Cook 				     sizeof(struct atmel_uart_char),
29736da2ec56SKees Cook 				     GFP_KERNEL);
2974ab4382d2SGreg Kroah-Hartman 		if (!data)
297584b476b1SClaudiu Beznea 			goto err_clk_disable_unprepare;
2976b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2977ab4382d2SGreg Kroah-Hartman 	}
2978ab4382d2SGreg Kroah-Hartman 
2979b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2980bd737f87SRicardo Ribalda Delgado 
2981b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2982ab4382d2SGreg Kroah-Hartman 	if (ret)
2983ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2984ab4382d2SGreg Kroah-Hartman 
2985ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2986b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2987ab4382d2SGreg Kroah-Hartman 
2988bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2989b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
29904e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2991b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2992b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2993fc887b15SLinus Torvalds 	}
2994fc887b15SLinus Torvalds 
2995055560b0SElen Song 	/*
2996055560b0SElen Song 	 * Get port name of usart or uart
2997055560b0SElen Song 	 */
2998b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2999055560b0SElen Song 
3000d4f64187SCyrille Pitchen 	/*
3001d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
3002d4f64187SCyrille Pitchen 	 * is used
3003d4f64187SCyrille Pitchen 	 */
3004b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
3005d4f64187SCyrille Pitchen 
3006ab4382d2SGreg Kroah-Hartman 	return 0;
3007ab4382d2SGreg Kroah-Hartman 
3008ab4382d2SGreg Kroah-Hartman err_add_port:
3009b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
3010b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
301184b476b1SClaudiu Beznea err_clk_disable_unprepare:
301284b476b1SClaudiu Beznea 	clk_disable_unprepare(atmel_port->clk);
3013b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
30144cbf9f48SNicolas Ferre err:
3015ab4382d2SGreg Kroah-Hartman 	return ret;
3016ab4382d2SGreg Kroah-Hartman }
3017ab4382d2SGreg Kroah-Hartman 
3018f4a8ab04SRomain Izard /*
3019f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
3020f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
3021f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
3022f4a8ab04SRomain Izard  *
3023f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
3024f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
3025f4a8ab04SRomain Izard  * port in the normal case.
3026f4a8ab04SRomain Izard  */
3027f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
3028f4a8ab04SRomain Izard {
3029f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
3030f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
3031f4a8ab04SRomain Izard 	int ret = 0;
3032f4a8ab04SRomain Izard 
303300e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
303400e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
3035f4a8ab04SRomain Izard 
3036f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
3037f4a8ab04SRomain Izard 
3038f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
3039f4a8ab04SRomain Izard 
3040f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
3041f4a8ab04SRomain Izard 
3042f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
3043f4a8ab04SRomain Izard 
3044f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
3045f4a8ab04SRomain Izard 
3046c24d2531SRadu Pirea 	pdev->dev.of_node = NULL;
3047f4a8ab04SRomain Izard 
3048f4a8ab04SRomain Izard 	return ret;
3049f4a8ab04SRomain Izard }
3050f4a8ab04SRomain Izard 
3051b50058b8SClaudiu Beznea static SIMPLE_DEV_PM_OPS(atmel_serial_pm_ops, atmel_serial_suspend,
3052b50058b8SClaudiu Beznea 			 atmel_serial_resume);
3053b50058b8SClaudiu Beznea 
3054ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
3055ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
3056f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
3057ab4382d2SGreg Kroah-Hartman 	.driver		= {
3058c24d2531SRadu Pirea 		.name			= "atmel_usart_serial",
30595fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
3060b50058b8SClaudiu Beznea 		.pm			= pm_ptr(&atmel_serial_pm_ops),
3061ab4382d2SGreg Kroah-Hartman 	},
3062ab4382d2SGreg Kroah-Hartman };
3063ab4382d2SGreg Kroah-Hartman 
3064ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
3065ab4382d2SGreg Kroah-Hartman {
3066ab4382d2SGreg Kroah-Hartman 	int ret;
3067ab4382d2SGreg Kroah-Hartman 
3068ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
3069ab4382d2SGreg Kroah-Hartman 	if (ret)
3070ab4382d2SGreg Kroah-Hartman 		return ret;
3071ab4382d2SGreg Kroah-Hartman 
3072ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
3073ab4382d2SGreg Kroah-Hartman 	if (ret)
3074ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
3075ab4382d2SGreg Kroah-Hartman 
3076ab4382d2SGreg Kroah-Hartman 	return ret;
3077ab4382d2SGreg Kroah-Hartman }
3078c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
3079