xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
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>
185e3ce1f2SSergiu 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>
24ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
256b997babSVinod Koul #include <linux/dmaengine.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
28bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
292e68c22fSElen Song #include <linux/timer.h>
30e0b0baadSRichard Genoud #include <linux/err.h>
31ab5e4e41SRichard Genoud #include <linux/irq.h>
322c7af5baSBoris BREZILLON #include <linux/suspend.h>
332b5cf14bSGeliang Tang #include <linux/mm.h>
34635b2589SZihao Tang #include <linux/io.h>
35ab4382d2SGreg Kroah-Hartman 
36377fedd1SNicolas Ferre #include <asm/div64.h>
37ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
38ab4382d2SGreg Kroah-Hartman 
39ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
40ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
41ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
42ab4382d2SGreg Kroah-Hartman 
43b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
44b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
45b5199d46SCyrille Pitchen /*
46b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
47b5199d46SCyrille Pitchen  * high and low thresholds
48b5199d46SCyrille Pitchen  */
49b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
50b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
51b5199d46SCyrille Pitchen 
52ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
53ab4382d2SGreg Kroah-Hartman 
54e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
558961df89SRichard Genoud #include "atmel_serial.h"
56e0b0baadSRichard Genoud 
57ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
58ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
59ab4382d2SGreg Kroah-Hartman 
60ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
61ab4382d2SGreg Kroah-Hartman 
62ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
63ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
64ab4382d2SGreg Kroah-Hartman  * UART. */
65ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
66ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
67ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
68ab4382d2SGreg Kroah-Hartman 
69ab4382d2SGreg Kroah-Hartman #else
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
72ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
73ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
74ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
75ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
76ab4382d2SGreg Kroah-Hartman 
77ab4382d2SGreg Kroah-Hartman #endif
78ab4382d2SGreg Kroah-Hartman 
79ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
80ab4382d2SGreg Kroah-Hartman 
81ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
82ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
83ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
84ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
85ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
86ab4382d2SGreg Kroah-Hartman };
87ab4382d2SGreg Kroah-Hartman 
88ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
89ab4382d2SGreg Kroah-Hartman 	u16		status;
90ab4382d2SGreg Kroah-Hartman 	u16		ch;
91ab4382d2SGreg Kroah-Hartman };
92ab4382d2SGreg Kroah-Hartman 
93637ba54fSLudovic Desroches /*
94637ba54fSLudovic Desroches  * Be careful, the real size of the ring buffer is
95637ba54fSLudovic Desroches  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
96637ba54fSLudovic Desroches  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
97637ba54fSLudovic Desroches  * DMA mode.
98637ba54fSLudovic Desroches  */
99ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
100ab4382d2SGreg Kroah-Hartman 
101ab4382d2SGreg Kroah-Hartman /*
1029af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
103432f9748SAlexandre Belloni  * samx7: 3 USARTs and 5 UARTs
1049af92fbfSAlexandre Belloni  */
105432f9748SAlexandre Belloni #define ATMEL_MAX_UART		8
1069af92fbfSAlexandre Belloni 
1079af92fbfSAlexandre Belloni /*
108ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
109ab4382d2SGreg Kroah-Hartman  */
110ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
111ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
112ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
1135e3ce1f2SSergiu Moga 	struct clk		*gclk;		/* uart generic clock */
114ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
115ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
116ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
117ab4382d2SGreg Kroah-Hartman 
11834df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
11964e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
120ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
121ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
122ab4382d2SGreg Kroah-Hartman 
12308f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
12464e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
125ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
126ab4382d2SGreg Kroah-Hartman 
12708f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
12834df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
12908f738beSElen Song 	struct dma_chan			*chan_tx;
13034df42f5SElen Song 	struct dma_chan			*chan_rx;
13108f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
13234df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
13308f738beSElen Song 	dma_cookie_t			cookie_tx;
13434df42f5SElen Song 	dma_cookie_t			cookie_rx;
13508f738beSElen Song 	struct scatterlist		sg_tx;
13634df42f5SElen Song 	struct scatterlist		sg_rx;
13700e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_rx;
13800e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_tx;
13998f2082cSNicolas Ferre 	atomic_t		tasklet_shutdown;
140ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
1415f258b3eSCyrille Pitchen 	unsigned int		tx_len;
142ab4382d2SGreg Kroah-Hartman 
143ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
144ab4382d2SGreg Kroah-Hartman 
145e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
146377fedd1SNicolas Ferre 	u32			backup_mode;	/* MR saved during iso7816 operations */
147377fedd1SNicolas Ferre 	u32			backup_brgr;	/* BRGR saved during iso7816 operations */
148ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
149b5199d46SCyrille Pitchen 	u32			fifo_size;
150b5199d46SCyrille Pitchen 	u32			rts_high;
151b5199d46SCyrille Pitchen 	u32			rts_low;
152ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1532958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
1545644bf18SSergiu Moga 	bool			is_usart;
1555bf5635aSLudovic Desroches 	bool			has_frac_baudrate;
1564b769371SNicolas Ferre 	bool			has_hw_timer;
1574b769371SNicolas Ferre 	struct timer_list	uart_timer;
1582c7af5baSBoris BREZILLON 
159ea04f82aSRomain Izard 	bool			tx_stopped;
1602c7af5baSBoris BREZILLON 	bool			suspended;
1612c7af5baSBoris BREZILLON 	unsigned int		pending;
1622c7af5baSBoris BREZILLON 	unsigned int		pending_status;
1632c7af5baSBoris BREZILLON 	spinlock_t		lock_suspended;
1642c7af5baSBoris BREZILLON 
16569646d7aSRazvan Stefanescu 	bool			hd_start_rx;	/* can start RX during half-duplex operation */
16669646d7aSRazvan Stefanescu 
167377fedd1SNicolas Ferre 	/* ISO7816 */
168377fedd1SNicolas Ferre 	unsigned int		fidi_min;
169377fedd1SNicolas Ferre 	unsigned int		fidi_max;
170377fedd1SNicolas Ferre 
1716a5f0e2fSAlexandre Belloni 	struct {
1726a5f0e2fSAlexandre Belloni 		u32		cr;
1736a5f0e2fSAlexandre Belloni 		u32		mr;
1746a5f0e2fSAlexandre Belloni 		u32		imr;
1756a5f0e2fSAlexandre Belloni 		u32		brgr;
1766a5f0e2fSAlexandre Belloni 		u32		rtor;
1776a5f0e2fSAlexandre Belloni 		u32		ttgr;
1786a5f0e2fSAlexandre Belloni 		u32		fmr;
1796a5f0e2fSAlexandre Belloni 		u32		fimr;
1806a5f0e2fSAlexandre Belloni 	} cache;
1816a5f0e2fSAlexandre Belloni 
182a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
183a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
184a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
185a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
186a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
187a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
188ab4382d2SGreg Kroah-Hartman };
189ab4382d2SGreg Kroah-Hartman 
190ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
191503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
192ab4382d2SGreg Kroah-Hartman 
1935fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1945fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
195c24d2531SRadu Pirea 	{ .compatible = "atmel,at91rm9200-usart-serial" },
1965fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1975fbe46b6SNicolas Ferre };
1985fbe46b6SNicolas Ferre #endif
1995fbe46b6SNicolas Ferre 
200ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
to_atmel_uart_port(struct uart_port * uart)201ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
202ab4382d2SGreg Kroah-Hartman {
203ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
204ab4382d2SGreg Kroah-Hartman }
205ab4382d2SGreg Kroah-Hartman 
atmel_uart_readl(struct uart_port * port,u32 reg)2064e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2074e7decdaSCyrille Pitchen {
2084e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2094e7decdaSCyrille Pitchen }
2104e7decdaSCyrille Pitchen 
atmel_uart_writel(struct uart_port * port,u32 reg,u32 value)2114e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2124e7decdaSCyrille Pitchen {
2134e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2144e7decdaSCyrille Pitchen }
2154e7decdaSCyrille Pitchen 
atmel_uart_read_char(struct uart_port * port)216a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
217a6499435SCyrille Pitchen {
218a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
219a6499435SCyrille Pitchen }
220a6499435SCyrille Pitchen 
atmel_uart_write_char(struct uart_port * port,u8 value)221a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
222a6499435SCyrille Pitchen {
223a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
224a6499435SCyrille Pitchen }
225a6499435SCyrille Pitchen 
atmel_uart_is_half_duplex(struct uart_port * port)226f3040983SRazvan Stefanescu static inline int atmel_uart_is_half_duplex(struct uart_port *port)
227f3040983SRazvan Stefanescu {
228f3040983SRazvan Stefanescu 	return ((port->rs485.flags & SER_RS485_ENABLED) &&
229f3040983SRazvan Stefanescu 		!(port->rs485.flags & SER_RS485_RX_DURING_TX)) ||
230f3040983SRazvan Stefanescu 		(port->iso7816.flags & SER_ISO7816_ENABLED);
231f3040983SRazvan Stefanescu }
232f3040983SRazvan Stefanescu 
atmel_error_rate(int desired_value,int actual_value)2335e3ce1f2SSergiu Moga static inline int atmel_error_rate(int desired_value, int actual_value)
2345e3ce1f2SSergiu Moga {
2355e3ce1f2SSergiu Moga 	return 100 - (desired_value * 100) / actual_value;
2365e3ce1f2SSergiu Moga }
2375e3ce1f2SSergiu Moga 
238ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
atmel_use_pdc_rx(struct uart_port * port)23964e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
240ab4382d2SGreg Kroah-Hartman {
241ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
242ab4382d2SGreg Kroah-Hartman 
24364e22ebeSElen Song 	return atmel_port->use_pdc_rx;
244ab4382d2SGreg Kroah-Hartman }
245ab4382d2SGreg Kroah-Hartman 
atmel_use_pdc_tx(struct uart_port * port)24664e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
247ab4382d2SGreg Kroah-Hartman {
248ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
249ab4382d2SGreg Kroah-Hartman 
25064e22ebeSElen Song 	return atmel_port->use_pdc_tx;
251ab4382d2SGreg Kroah-Hartman }
252ab4382d2SGreg Kroah-Hartman #else
atmel_use_pdc_rx(struct uart_port * port)25364e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
254ab4382d2SGreg Kroah-Hartman {
255ab4382d2SGreg Kroah-Hartman 	return false;
256ab4382d2SGreg Kroah-Hartman }
257ab4382d2SGreg Kroah-Hartman 
atmel_use_pdc_tx(struct uart_port * port)25864e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
259ab4382d2SGreg Kroah-Hartman {
260ab4382d2SGreg Kroah-Hartman 	return false;
261ab4382d2SGreg Kroah-Hartman }
262ab4382d2SGreg Kroah-Hartman #endif
263ab4382d2SGreg Kroah-Hartman 
atmel_use_dma_tx(struct uart_port * port)26408f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26508f738beSElen Song {
26608f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26708f738beSElen Song 
26808f738beSElen Song 	return atmel_port->use_dma_tx;
26908f738beSElen Song }
27008f738beSElen Song 
atmel_use_dma_rx(struct uart_port * port)27134df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
27234df42f5SElen Song {
27334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27434df42f5SElen Song 
27534df42f5SElen Song 	return atmel_port->use_dma_rx;
27634df42f5SElen Song }
27734df42f5SElen Song 
atmel_use_fifo(struct uart_port * port)2785be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2795be605acSAlexandre Belloni {
2805be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2815be605acSAlexandre Belloni 
2825be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2835be605acSAlexandre Belloni }
2845be605acSAlexandre Belloni 
atmel_tasklet_schedule(struct atmel_uart_port * atmel_port,struct tasklet_struct * t)28598f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
28698f2082cSNicolas Ferre 				   struct tasklet_struct *t)
28798f2082cSNicolas Ferre {
28898f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown))
28998f2082cSNicolas Ferre 		tasklet_schedule(t);
29098f2082cSNicolas Ferre }
29198f2082cSNicolas Ferre 
292ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
atmel_config_rs485(struct uart_port * port,struct ktermios * termios,struct serial_rs485 * rs485conf)293ae50bb27SIlpo Järvinen static int atmel_config_rs485(struct uart_port *port, struct ktermios *termios,
29413bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
295ab4382d2SGreg Kroah-Hartman {
296ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
297ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
298ab4382d2SGreg Kroah-Hartman 
299ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
3004e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
301ab4382d2SGreg Kroah-Hartman 
3024e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
303ab4382d2SGreg Kroah-Hartman 
304ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
305ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
30660efd051SLino Sanfilippo 		if (rs485conf->flags & SER_RS485_RX_DURING_TX)
307477b8383SCodrin.Ciubotariu@microchip.com 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
308477b8383SCodrin.Ciubotariu@microchip.com 		else
309ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
310477b8383SCodrin.Ciubotariu@microchip.com 
3114e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3124e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
313692a8ebcSSergiu Moga 		mode &= ~ATMEL_US_USMODE;
314ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
315ab4382d2SGreg Kroah-Hartman 	} else {
316ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
31764e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
318ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
319ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
320ab4382d2SGreg Kroah-Hartman 		else
321ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
322ab4382d2SGreg Kroah-Hartman 	}
3234e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
324ab4382d2SGreg Kroah-Hartman 
325ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3264e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
327ab4382d2SGreg Kroah-Hartman 
32813bd3e6fSRicardo Ribalda Delgado 	return 0;
329ab4382d2SGreg Kroah-Hartman }
330ab4382d2SGreg Kroah-Hartman 
atmel_calc_cd(struct uart_port * port,struct serial_iso7816 * iso7816conf)331377fedd1SNicolas Ferre static unsigned int atmel_calc_cd(struct uart_port *port,
332377fedd1SNicolas Ferre 				  struct serial_iso7816 *iso7816conf)
333377fedd1SNicolas Ferre {
334377fedd1SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
335377fedd1SNicolas Ferre 	unsigned int cd;
336377fedd1SNicolas Ferre 	u64 mck_rate;
337377fedd1SNicolas Ferre 
338377fedd1SNicolas Ferre 	mck_rate = (u64)clk_get_rate(atmel_port->clk);
339377fedd1SNicolas Ferre 	do_div(mck_rate, iso7816conf->clk);
340377fedd1SNicolas Ferre 	cd = mck_rate;
341377fedd1SNicolas Ferre 	return cd;
342377fedd1SNicolas Ferre }
343377fedd1SNicolas Ferre 
atmel_calc_fidi(struct uart_port * port,struct serial_iso7816 * iso7816conf)344377fedd1SNicolas Ferre static unsigned int atmel_calc_fidi(struct uart_port *port,
345377fedd1SNicolas Ferre 				    struct serial_iso7816 *iso7816conf)
346377fedd1SNicolas Ferre {
347377fedd1SNicolas Ferre 	u64 fidi = 0;
348377fedd1SNicolas Ferre 
349377fedd1SNicolas Ferre 	if (iso7816conf->sc_fi && iso7816conf->sc_di) {
350377fedd1SNicolas Ferre 		fidi = (u64)iso7816conf->sc_fi;
351377fedd1SNicolas Ferre 		do_div(fidi, iso7816conf->sc_di);
352377fedd1SNicolas Ferre 	}
353377fedd1SNicolas Ferre 	return (u32)fidi;
354377fedd1SNicolas Ferre }
355377fedd1SNicolas Ferre 
356377fedd1SNicolas Ferre /* Enable or disable the iso7816 support */
357377fedd1SNicolas Ferre /* Called with interrupts disabled */
atmel_config_iso7816(struct uart_port * port,struct serial_iso7816 * iso7816conf)358377fedd1SNicolas Ferre static int atmel_config_iso7816(struct uart_port *port,
359377fedd1SNicolas Ferre 				struct serial_iso7816 *iso7816conf)
360377fedd1SNicolas Ferre {
361377fedd1SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
362377fedd1SNicolas Ferre 	unsigned int mode;
363377fedd1SNicolas Ferre 	unsigned int cd, fidi;
364377fedd1SNicolas Ferre 	int ret = 0;
365377fedd1SNicolas Ferre 
366377fedd1SNicolas Ferre 	/* Disable interrupts */
367377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
368377fedd1SNicolas Ferre 
369377fedd1SNicolas Ferre 	mode = atmel_uart_readl(port, ATMEL_US_MR);
370377fedd1SNicolas Ferre 
371377fedd1SNicolas Ferre 	if (iso7816conf->flags & SER_ISO7816_ENABLED) {
372377fedd1SNicolas Ferre 		mode &= ~ATMEL_US_USMODE;
373377fedd1SNicolas Ferre 
374377fedd1SNicolas Ferre 		if (iso7816conf->tg > 255) {
375377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n");
376377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
377377fedd1SNicolas Ferre 			ret = -EINVAL;
378377fedd1SNicolas Ferre 			goto err_out;
379377fedd1SNicolas Ferre 		}
380377fedd1SNicolas Ferre 
381377fedd1SNicolas Ferre 		if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
382377fedd1SNicolas Ferre 		    == SER_ISO7816_T(0)) {
383377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T0 | ATMEL_US_DSNACK;
384377fedd1SNicolas Ferre 		} else if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
385377fedd1SNicolas Ferre 			   == SER_ISO7816_T(1)) {
386377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T1 | ATMEL_US_INACK;
387377fedd1SNicolas Ferre 		} else {
388377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816: Type not supported\n");
389377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
390377fedd1SNicolas Ferre 			ret = -EINVAL;
391377fedd1SNicolas Ferre 			goto err_out;
392377fedd1SNicolas Ferre 		}
393377fedd1SNicolas Ferre 
394377fedd1SNicolas Ferre 		mode &= ~(ATMEL_US_USCLKS | ATMEL_US_NBSTOP | ATMEL_US_PAR);
395377fedd1SNicolas Ferre 
396377fedd1SNicolas Ferre 		/* select mck clock, and output  */
397377fedd1SNicolas Ferre 		mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
398377fedd1SNicolas Ferre 		/* set parity for normal/inverse mode + max iterations */
399377fedd1SNicolas Ferre 		mode |= ATMEL_US_PAR_EVEN | ATMEL_US_NBSTOP_1 | ATMEL_US_MAX_ITER(3);
400377fedd1SNicolas Ferre 
401377fedd1SNicolas Ferre 		cd = atmel_calc_cd(port, iso7816conf);
402377fedd1SNicolas Ferre 		fidi = atmel_calc_fidi(port, iso7816conf);
403377fedd1SNicolas Ferre 		if (fidi == 0) {
404377fedd1SNicolas Ferre 			dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n");
405377fedd1SNicolas Ferre 		} else if (fidi < atmel_port->fidi_min
406377fedd1SNicolas Ferre 			   || fidi > atmel_port->fidi_max) {
407377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi);
408377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
409377fedd1SNicolas Ferre 			ret = -EINVAL;
410377fedd1SNicolas Ferre 			goto err_out;
411377fedd1SNicolas Ferre 		}
412377fedd1SNicolas Ferre 
413377fedd1SNicolas Ferre 		if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) {
414377fedd1SNicolas Ferre 			/* port not yet in iso7816 mode: store configuration */
415377fedd1SNicolas Ferre 			atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR);
416377fedd1SNicolas Ferre 			atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
417377fedd1SNicolas Ferre 		}
418377fedd1SNicolas Ferre 
419377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg);
420377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_BRGR, cd);
421377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_FIDI, fidi);
422377fedd1SNicolas Ferre 
423377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXEN);
424377fedd1SNicolas Ferre 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION;
425377fedd1SNicolas Ferre 	} else {
426377fedd1SNicolas Ferre 		dev_dbg(port->dev, "Setting UART back to RS232\n");
427377fedd1SNicolas Ferre 		/* back to last RS232 settings */
428377fedd1SNicolas Ferre 		mode = atmel_port->backup_mode;
429377fedd1SNicolas Ferre 		memset(iso7816conf, 0, sizeof(struct serial_iso7816));
430377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, 0);
431377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr);
432377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_FIDI, 0x174);
433377fedd1SNicolas Ferre 
434377fedd1SNicolas Ferre 		if (atmel_use_pdc_tx(port))
435377fedd1SNicolas Ferre 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
436377fedd1SNicolas Ferre 						   ATMEL_US_TXBUFE;
437377fedd1SNicolas Ferre 		else
438377fedd1SNicolas Ferre 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
439377fedd1SNicolas Ferre 	}
440377fedd1SNicolas Ferre 
441377fedd1SNicolas Ferre 	port->iso7816 = *iso7816conf;
442377fedd1SNicolas Ferre 
443377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_MR, mode);
444377fedd1SNicolas Ferre 
445377fedd1SNicolas Ferre err_out:
446377fedd1SNicolas Ferre 	/* Enable interrupts */
447377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
448377fedd1SNicolas Ferre 
449377fedd1SNicolas Ferre 	return ret;
450377fedd1SNicolas Ferre }
451377fedd1SNicolas Ferre 
452ab4382d2SGreg Kroah-Hartman /*
453ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
454ab4382d2SGreg Kroah-Hartman  */
atmel_tx_empty(struct uart_port * port)455ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
456ab4382d2SGreg Kroah-Hartman {
457ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
458ea04f82aSRomain Izard 
459ea04f82aSRomain Izard 	if (atmel_port->tx_stopped)
460ea04f82aSRomain Izard 		return TIOCSER_TEMT;
4614e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
4624e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
4634e7decdaSCyrille Pitchen 		0;
464ab4382d2SGreg Kroah-Hartman }
465ab4382d2SGreg Kroah-Hartman 
466ab4382d2SGreg Kroah-Hartman /*
467ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
468ab4382d2SGreg Kroah-Hartman  */
atmel_set_mctrl(struct uart_port * port,u_int mctrl)469ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
470ab4382d2SGreg Kroah-Hartman {
471ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
4724e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
4731cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
474ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
475ab4382d2SGreg Kroah-Hartman 
4761cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
4771cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
4784e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
4794e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
4801cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
4811cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
4821cf6e8fcSCyrille Pitchen 	}
4831cf6e8fcSCyrille Pitchen 
4841cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
4851cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
4861cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4871cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4881cf6e8fcSCyrille Pitchen 
4891cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4901cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4911cf6e8fcSCyrille Pitchen 	} else {
4921cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4931cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4941cf6e8fcSCyrille Pitchen 
4951cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4961cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4971cf6e8fcSCyrille Pitchen 	}
4981cf6e8fcSCyrille Pitchen 
499ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
5001cf6e8fcSCyrille Pitchen 		control |= rts_ready;
501ab4382d2SGreg Kroah-Hartman 	else
5021cf6e8fcSCyrille Pitchen 		control |= rts_paused;
503ab4382d2SGreg Kroah-Hartman 
504ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
505ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
506ab4382d2SGreg Kroah-Hartman 	else
507ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
508ab4382d2SGreg Kroah-Hartman 
5094e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
510ab4382d2SGreg Kroah-Hartman 
511e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
512e0b0baadSRichard Genoud 
513ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
5141cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
515ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
516ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
517ab4382d2SGreg Kroah-Hartman 	else
518ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
519ab4382d2SGreg Kroah-Hartman 
5204e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
521ab4382d2SGreg Kroah-Hartman }
522ab4382d2SGreg Kroah-Hartman 
523ab4382d2SGreg Kroah-Hartman /*
524ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
525ab4382d2SGreg Kroah-Hartman  */
atmel_get_mctrl(struct uart_port * port)526ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
527ab4382d2SGreg Kroah-Hartman {
528e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
529e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
530ab4382d2SGreg Kroah-Hartman 
5314e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
532ab4382d2SGreg Kroah-Hartman 
533ab4382d2SGreg Kroah-Hartman 	/*
534ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
535ab4382d2SGreg Kroah-Hartman 	 */
536ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
537ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
538ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
539ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
540ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
541ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
542ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
543ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
544ab4382d2SGreg Kroah-Hartman 
545e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
546ab4382d2SGreg Kroah-Hartman }
547ab4382d2SGreg Kroah-Hartman 
548ab4382d2SGreg Kroah-Hartman /*
549ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
550ab4382d2SGreg Kroah-Hartman  */
atmel_stop_tx(struct uart_port * port)551ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
552ab4382d2SGreg Kroah-Hartman {
553ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
55494ec165cSJiri Slaby (SUSE) 	bool is_pdc = atmel_use_pdc_tx(port);
5556373ab4dSJiri Slaby (SUSE) 	bool is_dma = is_pdc || atmel_use_dma_tx(port);
556ab4382d2SGreg Kroah-Hartman 
55794ec165cSJiri Slaby (SUSE) 	if (is_pdc) {
558ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
5594e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
560ab4382d2SGreg Kroah-Hartman 	}
56189d82324SRichard Genoud 
5626373ab4dSJiri Slaby (SUSE) 	if (is_dma) {
56389d82324SRichard Genoud 		/*
56489d82324SRichard Genoud 		 * Disable the transmitter.
56589d82324SRichard Genoud 		 * This is mandatory when DMA is used, otherwise the DMA buffer
56689d82324SRichard Genoud 		 * is fully transmitted.
56789d82324SRichard Genoud 		 */
56889d82324SRichard Genoud 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
569ea04f82aSRomain Izard 		atmel_port->tx_stopped = true;
5706373ab4dSJiri Slaby (SUSE) 	}
57189d82324SRichard Genoud 
572ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
5734e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
574ab4382d2SGreg Kroah-Hartman 
575f3040983SRazvan Stefanescu 	if (atmel_uart_is_half_duplex(port))
57604b5bfe3SNicolas Ferre 		if (!atomic_read(&atmel_port->tasklet_shutdown))
577ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
578ab4382d2SGreg Kroah-Hartman }
579ab4382d2SGreg Kroah-Hartman 
580ab4382d2SGreg Kroah-Hartman /*
581ab4382d2SGreg Kroah-Hartman  * Start transmitting.
582ab4382d2SGreg Kroah-Hartman  */
atmel_start_tx(struct uart_port * port)583ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
584ab4382d2SGreg Kroah-Hartman {
585ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
58694ec165cSJiri Slaby (SUSE) 	bool is_pdc = atmel_use_pdc_tx(port);
58794ec165cSJiri Slaby (SUSE) 	bool is_dma = is_pdc || atmel_use_dma_tx(port);
588ab4382d2SGreg Kroah-Hartman 
58994ec165cSJiri Slaby (SUSE) 	if (is_pdc && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
5900058f087SAlexandre Belloni 				       & ATMEL_PDC_TXTEN))
591ab4382d2SGreg Kroah-Hartman 		/* The transmitter is already running.  Yes, we
592ab4382d2SGreg Kroah-Hartman 		   really need this.*/
593ab4382d2SGreg Kroah-Hartman 		return;
594ab4382d2SGreg Kroah-Hartman 
59594ec165cSJiri Slaby (SUSE) 	if (is_dma && atmel_uart_is_half_duplex(port))
596ab4382d2SGreg Kroah-Hartman 		atmel_stop_rx(port);
597ab4382d2SGreg Kroah-Hartman 
59894ec165cSJiri Slaby (SUSE) 	if (is_pdc) {
599ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
6004e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
60194ec165cSJiri Slaby (SUSE) 	}
6020058f087SAlexandre Belloni 
603ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
6044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
60589d82324SRichard Genoud 
6066373ab4dSJiri Slaby (SUSE) 	if (is_dma) {
60789d82324SRichard Genoud 		/* re-enable the transmitter */
60889d82324SRichard Genoud 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
609ea04f82aSRomain Izard 		atmel_port->tx_stopped = false;
610ab4382d2SGreg Kroah-Hartman 	}
6116373ab4dSJiri Slaby (SUSE) }
612ab4382d2SGreg Kroah-Hartman 
613ab4382d2SGreg Kroah-Hartman /*
614ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
615ab4382d2SGreg Kroah-Hartman  */
atmel_start_rx(struct uart_port * port)616ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
617ab4382d2SGreg Kroah-Hartman {
6184e7decdaSCyrille Pitchen 	/* reset status and receiver */
6194e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
620ab4382d2SGreg Kroah-Hartman 
6214e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
62257c36868SSiftar, Gabe 
62364e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
624ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
6254e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
6264e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
627ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
6284e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
629ab4382d2SGreg Kroah-Hartman 	} else {
6304e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
631ab4382d2SGreg Kroah-Hartman 	}
632ab4382d2SGreg Kroah-Hartman }
633ab4382d2SGreg Kroah-Hartman 
634ab4382d2SGreg Kroah-Hartman /*
635ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
636ab4382d2SGreg Kroah-Hartman  */
atmel_stop_rx(struct uart_port * port)637ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
638ab4382d2SGreg Kroah-Hartman {
6394e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
64057c36868SSiftar, Gabe 
64164e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
642ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
6434e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
6444e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
6454e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
646ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
647ab4382d2SGreg Kroah-Hartman 	} else {
6484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
649ab4382d2SGreg Kroah-Hartman 	}
650ab4382d2SGreg Kroah-Hartman }
651ab4382d2SGreg Kroah-Hartman 
652ab4382d2SGreg Kroah-Hartman /*
653ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
654ab4382d2SGreg Kroah-Hartman  */
atmel_enable_ms(struct uart_port * port)655ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
656ab4382d2SGreg Kroah-Hartman {
657ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
658ab5e4e41SRichard Genoud 	uint32_t ier = 0;
659ab5e4e41SRichard Genoud 
660ab5e4e41SRichard Genoud 	/*
661ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
662ab5e4e41SRichard Genoud 	 */
663ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
664ab5e4e41SRichard Genoud 		return;
665ab5e4e41SRichard Genoud 
666ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
667ab5e4e41SRichard Genoud 
66818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
669ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
670ab5e4e41SRichard Genoud 
67118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
672ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
673ab5e4e41SRichard Genoud 
67418dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
675ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
676ab5e4e41SRichard Genoud 
67718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
678ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
679ab5e4e41SRichard Genoud 
6804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
68118dfef9cSUwe Kleine-König 
68218dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
683ab4382d2SGreg Kroah-Hartman }
684ab4382d2SGreg Kroah-Hartman 
685ab4382d2SGreg Kroah-Hartman /*
68635b675b9SRichard Genoud  * Disable modem status interrupts
68735b675b9SRichard Genoud  */
atmel_disable_ms(struct uart_port * port)68835b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
68935b675b9SRichard Genoud {
69035b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
69135b675b9SRichard Genoud 	uint32_t idr = 0;
69235b675b9SRichard Genoud 
69335b675b9SRichard Genoud 	/*
69435b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
69535b675b9SRichard Genoud 	 */
69635b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
69735b675b9SRichard Genoud 		return;
69835b675b9SRichard Genoud 
69935b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
70035b675b9SRichard Genoud 
70118dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
70218dfef9cSUwe Kleine-König 
70318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
70435b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
70535b675b9SRichard Genoud 
70618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
70735b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
70835b675b9SRichard Genoud 
70918dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
71035b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
71135b675b9SRichard Genoud 
71218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
71335b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
71435b675b9SRichard Genoud 
7154e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
71635b675b9SRichard Genoud }
71735b675b9SRichard Genoud 
71835b675b9SRichard Genoud /*
719ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
720ab4382d2SGreg Kroah-Hartman  */
atmel_break_ctl(struct uart_port * port,int break_state)721ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
722ab4382d2SGreg Kroah-Hartman {
723ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
7244e7decdaSCyrille Pitchen 		/* start break */
7254e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
726ab4382d2SGreg Kroah-Hartman 	else
7274e7decdaSCyrille Pitchen 		/* stop break */
7284e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
729ab4382d2SGreg Kroah-Hartman }
730ab4382d2SGreg Kroah-Hartman 
731ab4382d2SGreg Kroah-Hartman /*
732ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
733ab4382d2SGreg Kroah-Hartman  */
734ab4382d2SGreg Kroah-Hartman static void
atmel_buffer_rx_char(struct uart_port * port,unsigned int status,unsigned int ch)735ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
736ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
737ab4382d2SGreg Kroah-Hartman {
738ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
739ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
740ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
741ab4382d2SGreg Kroah-Hartman 
742ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
743ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
744ab4382d2SGreg Kroah-Hartman 		return;
745ab4382d2SGreg Kroah-Hartman 
746ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
747ab4382d2SGreg Kroah-Hartman 	c->status	= status;
748ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
749ab4382d2SGreg Kroah-Hartman 
750ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
751ab4382d2SGreg Kroah-Hartman 	smp_wmb();
752ab4382d2SGreg Kroah-Hartman 
753ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
754ab4382d2SGreg Kroah-Hartman }
755ab4382d2SGreg Kroah-Hartman 
756ab4382d2SGreg Kroah-Hartman /*
757ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
758ab4382d2SGreg Kroah-Hartman  */
atmel_pdc_rxerr(struct uart_port * port,unsigned int status)759ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
760ab4382d2SGreg Kroah-Hartman {
761ab4382d2SGreg Kroah-Hartman 	/* clear error */
7624e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
763ab4382d2SGreg Kroah-Hartman 
764ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
765ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
766ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
767ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
768ab4382d2SGreg Kroah-Hartman 	}
769ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
770ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
771ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
772ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
773ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
774ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
775ab4382d2SGreg Kroah-Hartman }
776ab4382d2SGreg Kroah-Hartman 
777ab4382d2SGreg Kroah-Hartman /*
778ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
779ab4382d2SGreg Kroah-Hartman  */
atmel_rx_chars(struct uart_port * port)780ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
781ab4382d2SGreg Kroah-Hartman {
782ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
783ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
784ab4382d2SGreg Kroah-Hartman 
7854e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
786ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
787a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
788ab4382d2SGreg Kroah-Hartman 
789ab4382d2SGreg Kroah-Hartman 		/*
790ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
791ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
792ab4382d2SGreg Kroah-Hartman 		 */
793ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
794ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
795ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
796ab4382d2SGreg Kroah-Hartman 
797ab4382d2SGreg Kroah-Hartman 			/* clear error */
7984e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
799ab4382d2SGreg Kroah-Hartman 
800ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
801ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
802ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
8034e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
8044e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
805ab4382d2SGreg Kroah-Hartman 			} else {
806ab4382d2SGreg Kroah-Hartman 				/*
807ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
808ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
809ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
810ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
811ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
812ab4382d2SGreg Kroah-Hartman 				 */
8134e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
8144e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
815ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
816ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
817ab4382d2SGreg Kroah-Hartman 			}
818ab4382d2SGreg Kroah-Hartman 		}
819ab4382d2SGreg Kroah-Hartman 
820ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
8214e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
822ab4382d2SGreg Kroah-Hartman 	}
823ab4382d2SGreg Kroah-Hartman 
82498f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
825ab4382d2SGreg Kroah-Hartman }
826ab4382d2SGreg Kroah-Hartman 
827ab4382d2SGreg Kroah-Hartman /*
828ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
829ab4382d2SGreg Kroah-Hartman  * disabled)
830ab4382d2SGreg Kroah-Hartman  */
atmel_tx_chars(struct uart_port * port)831ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
832ab4382d2SGreg Kroah-Hartman {
833ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
8342d141e68SJiri Slaby (SUSE) 	bool pending;
8352d141e68SJiri Slaby (SUSE) 	u8 ch;
836ab4382d2SGreg Kroah-Hartman 
8372d141e68SJiri Slaby (SUSE) 	pending = uart_port_tx(port, ch,
8382d141e68SJiri Slaby (SUSE) 		atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY,
8392d141e68SJiri Slaby (SUSE) 		atmel_uart_write_char(port, ch));
8402d141e68SJiri Slaby (SUSE) 	if (pending) {
841477b8383SCodrin.Ciubotariu@microchip.com 		/* we still have characters to transmit, so we should continue
842477b8383SCodrin.Ciubotariu@microchip.com 		 * transmitting them when TX is ready, regardless of
843477b8383SCodrin.Ciubotariu@microchip.com 		 * mode or duplexity
844477b8383SCodrin.Ciubotariu@microchip.com 		 */
845477b8383SCodrin.Ciubotariu@microchip.com 		atmel_port->tx_done_mask |= ATMEL_US_TXRDY;
846477b8383SCodrin.Ciubotariu@microchip.com 
847ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
8484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
8494e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
850477b8383SCodrin.Ciubotariu@microchip.com 	} else {
851477b8383SCodrin.Ciubotariu@microchip.com 		if (atmel_uart_is_half_duplex(port))
852477b8383SCodrin.Ciubotariu@microchip.com 			atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY;
853477b8383SCodrin.Ciubotariu@microchip.com 	}
854ab4382d2SGreg Kroah-Hartman }
855ab4382d2SGreg Kroah-Hartman 
atmel_complete_tx_dma(void * arg)85608f738beSElen Song static void atmel_complete_tx_dma(void *arg)
85708f738beSElen Song {
85808f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
85908f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
86008f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
86108f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
86208f738beSElen Song 	unsigned long flags;
86308f738beSElen Song 
86408f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
86508f738beSElen Song 
86608f738beSElen Song 	if (chan)
86708f738beSElen Song 		dmaengine_terminate_all(chan);
868add147a4SIlpo Järvinen 	uart_xmit_advance(port, atmel_port->tx_len);
86908f738beSElen Song 
87027a82683SDan Carpenter 	spin_lock(&atmel_port->lock_tx);
87108f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
87208f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
87308f738beSElen Song 	atmel_port->desc_tx = NULL;
87427a82683SDan Carpenter 	spin_unlock(&atmel_port->lock_tx);
87508f738beSElen Song 
87608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
87708f738beSElen Song 		uart_write_wakeup(port);
87808f738beSElen Song 
8791842dc2eSCyrille Pitchen 	/*
8801842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
8811842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
8821842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
8831842dc2eSCyrille Pitchen 	 */
88408f738beSElen Song 	if (!uart_circ_empty(xmit))
88598f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
886f3040983SRazvan Stefanescu 	else if (atmel_uart_is_half_duplex(port)) {
88769646d7aSRazvan Stefanescu 		/*
88869646d7aSRazvan Stefanescu 		 * DMA done, re-enable TXEMPTY and signal that we can stop
88969646d7aSRazvan Stefanescu 		 * TX and start RX for RS485
89069646d7aSRazvan Stefanescu 		 */
89169646d7aSRazvan Stefanescu 		atmel_port->hd_start_rx = true;
89269646d7aSRazvan Stefanescu 		atmel_uart_writel(port, ATMEL_US_IER,
89369646d7aSRazvan Stefanescu 				  atmel_port->tx_done_mask);
894b389f173SRichard Genoud 	}
89508f738beSElen Song 
89608f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
89708f738beSElen Song }
89808f738beSElen Song 
atmel_release_tx_dma(struct uart_port * port)89908f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
90008f738beSElen Song {
90108f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
90208f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
90308f738beSElen Song 
90408f738beSElen Song 	if (chan) {
90508f738beSElen Song 		dmaengine_terminate_all(chan);
90608f738beSElen Song 		dma_release_channel(chan);
90708f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
90848479148SWolfram Sang 				DMA_TO_DEVICE);
90908f738beSElen Song 	}
91008f738beSElen Song 
91108f738beSElen Song 	atmel_port->desc_tx = NULL;
91208f738beSElen Song 	atmel_port->chan_tx = NULL;
91308f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
91408f738beSElen Song }
91508f738beSElen Song 
91608f738beSElen Song /*
91708f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
91808f738beSElen Song  */
atmel_tx_dma(struct uart_port * port)91908f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
92008f738beSElen Song {
92108f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
92208f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
92308f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
92408f738beSElen Song 	struct dma_async_tx_descriptor *desc;
9255f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
9265f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
9275f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
92808f738beSElen Song 
92908f738beSElen Song 	/* Make sure we have an idle channel */
93008f738beSElen Song 	if (atmel_port->desc_tx != NULL)
93108f738beSElen Song 		return;
93208f738beSElen Song 
93308f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
93408f738beSElen Song 		/*
93508f738beSElen Song 		 * DMA is idle now.
93608f738beSElen Song 		 * Port xmit buffer is already mapped,
93708f738beSElen Song 		 * and it is one page... Just adjust
93808f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
93908f738beSElen Song 		 * we have to transmit till the end, and then the rest.
94008f738beSElen Song 		 * Take the port lock to get a
94108f738beSElen Song 		 * consistent xmit buffer state.
94208f738beSElen Song 		 */
9435f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
94408f738beSElen Song 					 xmit->tail,
94508f738beSElen Song 					 UART_XMIT_SIZE);
9465f258b3eSCyrille Pitchen 
9475f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
9485f258b3eSCyrille Pitchen 			/* multi data mode */
9495f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
9505f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
9515f258b3eSCyrille Pitchen 		} else {
9525f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
9535f258b3eSCyrille Pitchen 			part1_len = 0;
9545f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
9555f258b3eSCyrille Pitchen 		}
9565f258b3eSCyrille Pitchen 
9575f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
9585f258b3eSCyrille Pitchen 		sg_len = 0;
9595f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
9605f258b3eSCyrille Pitchen 		if (part1_len) {
9615f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
9625f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
9635f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
9645f258b3eSCyrille Pitchen 
9655f258b3eSCyrille Pitchen 			phys_addr += part1_len;
9665f258b3eSCyrille Pitchen 		}
9675f258b3eSCyrille Pitchen 
9685f258b3eSCyrille Pitchen 		if (part2_len) {
9695f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
9705f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
9715f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
9725f258b3eSCyrille Pitchen 		}
9735f258b3eSCyrille Pitchen 
9745f258b3eSCyrille Pitchen 		/*
9755f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
9765f258b3eSCyrille Pitchen 		 * xmit->tail correctly
9775f258b3eSCyrille Pitchen 		 */
9785f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
97908f738beSElen Song 
98008f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
9815f258b3eSCyrille Pitchen 					       sgl,
9825f258b3eSCyrille Pitchen 					       sg_len,
98308f738beSElen Song 					       DMA_MEM_TO_DEV,
98408f738beSElen Song 					       DMA_PREP_INTERRUPT |
98508f738beSElen Song 					       DMA_CTRL_ACK);
98608f738beSElen Song 		if (!desc) {
98708f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
98808f738beSElen Song 			return;
98908f738beSElen Song 		}
99008f738beSElen Song 
9915f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
99208f738beSElen Song 
99308f738beSElen Song 		atmel_port->desc_tx = desc;
99408f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
99508f738beSElen Song 		desc->callback_param = atmel_port;
99608f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
9971e67bd2bSTudor Ambarus 		if (dma_submit_error(atmel_port->cookie_tx)) {
9981e67bd2bSTudor Ambarus 			dev_err(port->dev, "dma_submit_error %d\n",
9991e67bd2bSTudor Ambarus 				atmel_port->cookie_tx);
10001e67bd2bSTudor Ambarus 			return;
10011e67bd2bSTudor Ambarus 		}
10024f4b9b58STudor Ambarus 
10034f4b9b58STudor Ambarus 		dma_async_issue_pending(chan);
100408f738beSElen Song 	}
100508f738beSElen Song 
100608f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
100708f738beSElen Song 		uart_write_wakeup(port);
100808f738beSElen Song }
100908f738beSElen Song 
atmel_prepare_tx_dma(struct uart_port * port)101008f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
101108f738beSElen Song {
101208f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1013c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
101408f738beSElen Song 	dma_cap_mask_t		mask;
101508f738beSElen Song 	struct dma_slave_config config;
101608f738beSElen Song 	int ret, nent;
101708f738beSElen Song 
101808f738beSElen Song 	dma_cap_zero(mask);
101908f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
102008f738beSElen Song 
1021c24d2531SRadu Pirea 	atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx");
102208f738beSElen Song 	if (atmel_port->chan_tx == NULL)
102308f738beSElen Song 		goto chan_err;
102408f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
102508f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
102608f738beSElen Song 
102708f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
102808f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
102908f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
10302c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
103108f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
103208f738beSElen Song 			virt_to_page(port->state->xmit.buf),
103308f738beSElen Song 			UART_XMIT_SIZE,
10342b5cf14bSGeliang Tang 			offset_in_page(port->state->xmit.buf));
103508f738beSElen Song 	nent = dma_map_sg(port->dev,
103608f738beSElen Song 				&atmel_port->sg_tx,
103708f738beSElen Song 				1,
103848479148SWolfram Sang 				DMA_TO_DEVICE);
103908f738beSElen Song 
104008f738beSElen Song 	if (!nent) {
104108f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
104208f738beSElen Song 		goto chan_err;
104308f738beSElen Song 	} else {
1044c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
104508f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
104608f738beSElen Song 			port->state->xmit.buf,
1047c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
104808f738beSElen Song 	}
104908f738beSElen Song 
105008f738beSElen Song 	/* Configure the slave DMA */
105108f738beSElen Song 	memset(&config, 0, sizeof(config));
105208f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
10535f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
10545f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
10555f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
105608f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
1057a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
105808f738beSElen Song 
10595483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
10605483c10eSMaxime Ripard 				     &config);
106108f738beSElen Song 	if (ret) {
106208f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
106308f738beSElen Song 		goto chan_err;
106408f738beSElen Song 	}
106508f738beSElen Song 
106608f738beSElen Song 	return 0;
106708f738beSElen Song 
106808f738beSElen Song chan_err:
106908f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
107036ce7cffSZheng Bin 	atmel_port->use_dma_tx = false;
107108f738beSElen Song 	if (atmel_port->chan_tx)
107208f738beSElen Song 		atmel_release_tx_dma(port);
107308f738beSElen Song 	return -EINVAL;
107408f738beSElen Song }
107508f738beSElen Song 
atmel_complete_rx_dma(void * arg)107634df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
107734df42f5SElen Song {
107834df42f5SElen Song 	struct uart_port *port = arg;
107934df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
108034df42f5SElen Song 
108198f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
108234df42f5SElen Song }
108334df42f5SElen Song 
atmel_release_rx_dma(struct uart_port * port)108434df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
108534df42f5SElen Song {
108634df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
108734df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
108834df42f5SElen Song 
108934df42f5SElen Song 	if (chan) {
109034df42f5SElen Song 		dmaengine_terminate_all(chan);
109134df42f5SElen Song 		dma_release_channel(chan);
109234df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
109348479148SWolfram Sang 				DMA_FROM_DEVICE);
109434df42f5SElen Song 	}
109534df42f5SElen Song 
109634df42f5SElen Song 	atmel_port->desc_rx = NULL;
109734df42f5SElen Song 	atmel_port->chan_rx = NULL;
109834df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
109934df42f5SElen Song }
110034df42f5SElen Song 
atmel_rx_from_dma(struct uart_port * port)110134df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
110234df42f5SElen Song {
110334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
110466f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
110534df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
110634df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
110734df42f5SElen Song 	struct dma_tx_state state;
110834df42f5SElen Song 	enum dma_status dmastat;
110966f37aafSCyrille Pitchen 	size_t count;
111034df42f5SElen Song 
111134df42f5SElen Song 
111234df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
11134e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
111434df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
111534df42f5SElen Song 				atmel_port->cookie_rx,
111634df42f5SElen Song 				&state);
111734df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
111834df42f5SElen Song 	if (dmastat == DMA_ERROR) {
111934df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
11204e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
112198f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
112234df42f5SElen Song 		return;
112334df42f5SElen Song 	}
112466f37aafSCyrille Pitchen 
112566f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
112666f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
112766f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
112866f37aafSCyrille Pitchen 			    1,
1129485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
113034df42f5SElen Song 
113134df42f5SElen Song 	/*
113266f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
113366f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
113466f37aafSCyrille Pitchen 	 * framework.
113566f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
113666f37aafSCyrille Pitchen 	 * length.
113734df42f5SElen Song 	 */
113866f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
113966f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
114066f37aafSCyrille Pitchen 	/*
114166f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
114266f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
114366f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
114466f37aafSCyrille Pitchen 	 *
114566f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
114666f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
114766f37aafSCyrille Pitchen 	 *
114866f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
114966f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
115066f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
115166f37aafSCyrille Pitchen 	 */
115266f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
115366f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
115434df42f5SElen Song 
115566f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
115666f37aafSCyrille Pitchen 		ring->tail = 0;
115734df42f5SElen Song 		port->icount.rx += count;
115834df42f5SElen Song 	}
115934df42f5SElen Song 
116066f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
116166f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
116266f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
116366f37aafSCyrille Pitchen 
116466f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
116566f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
116666f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
116766f37aafSCyrille Pitchen 			ring->head = 0;
116866f37aafSCyrille Pitchen 		ring->tail = ring->head;
116966f37aafSCyrille Pitchen 		port->icount.rx += count;
117066f37aafSCyrille Pitchen 	}
117166f37aafSCyrille Pitchen 
117266f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
117366f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
117466f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
117566f37aafSCyrille Pitchen 			       1,
1176485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
117766f37aafSCyrille Pitchen 
117866f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
117966f37aafSCyrille Pitchen 
11804e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
118134df42f5SElen Song }
118234df42f5SElen Song 
atmel_prepare_rx_dma(struct uart_port * port)118334df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
118434df42f5SElen Song {
118534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1186c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
118734df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
118834df42f5SElen Song 	dma_cap_mask_t		mask;
118934df42f5SElen Song 	struct dma_slave_config config;
119034df42f5SElen Song 	struct circ_buf		*ring;
119134df42f5SElen Song 	int ret, nent;
119234df42f5SElen Song 
119334df42f5SElen Song 	ring = &atmel_port->rx_ring;
119434df42f5SElen Song 
119534df42f5SElen Song 	dma_cap_zero(mask);
119634df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
119734df42f5SElen Song 
1198c24d2531SRadu Pirea 	atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx");
119934df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
120034df42f5SElen Song 		goto chan_err;
120134df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
120234df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
120334df42f5SElen Song 
120434df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
120534df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
120634df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
12072c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
120834df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
120934df42f5SElen Song 		    virt_to_page(ring->buf),
1210a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
12112b5cf14bSGeliang Tang 		    offset_in_page(ring->buf));
121234df42f5SElen Song 	nent = dma_map_sg(port->dev,
121334df42f5SElen Song 			  &atmel_port->sg_rx,
121434df42f5SElen Song 			  1,
121548479148SWolfram Sang 			  DMA_FROM_DEVICE);
121634df42f5SElen Song 
121734df42f5SElen Song 	if (!nent) {
121834df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
121934df42f5SElen Song 		goto chan_err;
122034df42f5SElen Song 	} else {
1221c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
122234df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
122334df42f5SElen Song 			ring->buf,
1224c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
122534df42f5SElen Song 	}
122634df42f5SElen Song 
122734df42f5SElen Song 	/* Configure the slave DMA */
122834df42f5SElen Song 	memset(&config, 0, sizeof(config));
122934df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
123034df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
123134df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1232a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
123334df42f5SElen Song 
12345483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
12355483c10eSMaxime Ripard 				     &config);
123634df42f5SElen Song 	if (ret) {
123734df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
123834df42f5SElen Song 		goto chan_err;
123934df42f5SElen Song 	}
124034df42f5SElen Song 	/*
124134df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
124234df42f5SElen Song 	 * each one is half ring buffer size
124334df42f5SElen Song 	 */
124434df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
124534df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
124634df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
124734df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
124834df42f5SElen Song 					 DMA_DEV_TO_MEM,
124934df42f5SElen Song 					 DMA_PREP_INTERRUPT);
1250c85be041SKangjie Lu 	if (!desc) {
1251c85be041SKangjie Lu 		dev_err(port->dev, "Preparing DMA cyclic failed\n");
1252c85be041SKangjie Lu 		goto chan_err;
1253c85be041SKangjie Lu 	}
125434df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
125534df42f5SElen Song 	desc->callback_param = port;
125634df42f5SElen Song 	atmel_port->desc_rx = desc;
125734df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
12581e67bd2bSTudor Ambarus 	if (dma_submit_error(atmel_port->cookie_rx)) {
12591e67bd2bSTudor Ambarus 		dev_err(port->dev, "dma_submit_error %d\n",
12601e67bd2bSTudor Ambarus 			atmel_port->cookie_rx);
12611e67bd2bSTudor Ambarus 		goto chan_err;
12621e67bd2bSTudor Ambarus 	}
126334df42f5SElen Song 
12644f4b9b58STudor Ambarus 	dma_async_issue_pending(atmel_port->chan_rx);
12654f4b9b58STudor Ambarus 
126634df42f5SElen Song 	return 0;
126734df42f5SElen Song 
126834df42f5SElen Song chan_err:
126934df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
127036ce7cffSZheng Bin 	atmel_port->use_dma_rx = false;
127134df42f5SElen Song 	if (atmel_port->chan_rx)
127234df42f5SElen Song 		atmel_release_rx_dma(port);
127334df42f5SElen Song 	return -EINVAL;
127434df42f5SElen Song }
127534df42f5SElen Song 
atmel_uart_timer_callback(struct timer_list * t)1276026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t)
12772e68c22fSElen Song {
1278026cb432SKees Cook 	struct atmel_uart_port *atmel_port = from_timer(atmel_port, t,
1279026cb432SKees Cook 							uart_timer);
1280026cb432SKees Cook 	struct uart_port *port = &atmel_port->uart;
12812e68c22fSElen Song 
128298f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown)) {
128300e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
128498f2082cSNicolas Ferre 		mod_timer(&atmel_port->uart_timer,
128598f2082cSNicolas Ferre 			  jiffies + uart_poll_timeout(port));
128698f2082cSNicolas Ferre 	}
12872e68c22fSElen Song }
12882e68c22fSElen Song 
1289ab4382d2SGreg Kroah-Hartman /*
1290ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1291ab4382d2SGreg Kroah-Hartman  */
1292ab4382d2SGreg Kroah-Hartman static void
atmel_handle_receive(struct uart_port * port,unsigned int pending)1293ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1294ab4382d2SGreg Kroah-Hartman {
1295ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1296ab4382d2SGreg Kroah-Hartman 
129764e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1298ab4382d2SGreg Kroah-Hartman 		/*
1299ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1300ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1301ab4382d2SGreg Kroah-Hartman 		 *
1302ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1303ab4382d2SGreg Kroah-Hartman 		 * the moment.
1304ab4382d2SGreg Kroah-Hartman 		 */
1305ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
13064e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
13074e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
130898f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
130998f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
1310ab4382d2SGreg Kroah-Hartman 		}
1311ab4382d2SGreg Kroah-Hartman 
1312ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1313ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1314ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1315ab4382d2SGreg Kroah-Hartman 	}
1316ab4382d2SGreg Kroah-Hartman 
131734df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
131834df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
13194e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
13204e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
132198f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
132298f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
132334df42f5SElen Song 		}
132434df42f5SElen Song 	}
132534df42f5SElen Song 
1326ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1327ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1328ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1329ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1330ab4382d2SGreg Kroah-Hartman 		/*
1331ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1332ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1333ab4382d2SGreg Kroah-Hartman 		 */
13344e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
13354e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1336ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1337ab4382d2SGreg Kroah-Hartman 	}
1338ab4382d2SGreg Kroah-Hartman }
1339ab4382d2SGreg Kroah-Hartman 
1340ab4382d2SGreg Kroah-Hartman /*
1341ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1342ab4382d2SGreg Kroah-Hartman  */
1343ab4382d2SGreg Kroah-Hartman static void
atmel_handle_transmit(struct uart_port * port,unsigned int pending)1344ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1345ab4382d2SGreg Kroah-Hartman {
1346ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1347ab4382d2SGreg Kroah-Hartman 
1348ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
13494e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
13504e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
135169646d7aSRazvan Stefanescu 
135269646d7aSRazvan Stefanescu 		/* Start RX if flag was set and FIFO is empty */
135369646d7aSRazvan Stefanescu 		if (atmel_port->hd_start_rx) {
135469646d7aSRazvan Stefanescu 			if (!(atmel_uart_readl(port, ATMEL_US_CSR)
135569646d7aSRazvan Stefanescu 					& ATMEL_US_TXEMPTY))
135669646d7aSRazvan Stefanescu 				dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n");
135769646d7aSRazvan Stefanescu 
135869646d7aSRazvan Stefanescu 			atmel_port->hd_start_rx = false;
135969646d7aSRazvan Stefanescu 			atmel_start_rx(port);
136069646d7aSRazvan Stefanescu 		}
136169646d7aSRazvan Stefanescu 
136298f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1363ab4382d2SGreg Kroah-Hartman 	}
1364ab4382d2SGreg Kroah-Hartman }
1365ab4382d2SGreg Kroah-Hartman 
1366ab4382d2SGreg Kroah-Hartman /*
1367ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1368ab4382d2SGreg Kroah-Hartman  */
1369ab4382d2SGreg Kroah-Hartman static void
atmel_handle_status(struct uart_port * port,unsigned int pending,unsigned int status)1370ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1371ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1372ab4382d2SGreg Kroah-Hartman {
1373ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
13749205218eSNicolas Ferre 	unsigned int status_change;
1375ab4382d2SGreg Kroah-Hartman 
1376ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1377ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
13789205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1379d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
13809205218eSNicolas Ferre 
13819205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
13829205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
13839205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
13849205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
13859205218eSNicolas Ferre 				port->icount.rng++;
13869205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
13879205218eSNicolas Ferre 				port->icount.dsr++;
13889205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
13899205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
13909205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
13919205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
13929205218eSNicolas Ferre 
13939205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
13949205218eSNicolas Ferre 		}
1395ab4382d2SGreg Kroah-Hartman 	}
1396377fedd1SNicolas Ferre 
1397377fedd1SNicolas Ferre 	if (pending & (ATMEL_US_NACK | ATMEL_US_ITERATION))
1398377fedd1SNicolas Ferre 		dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending);
1399ab4382d2SGreg Kroah-Hartman }
1400ab4382d2SGreg Kroah-Hartman 
1401ab4382d2SGreg Kroah-Hartman /*
1402ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1403ab4382d2SGreg Kroah-Hartman  */
atmel_interrupt(int irq,void * dev_id)1404ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1405ab4382d2SGreg Kroah-Hartman {
1406ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1407ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
14082c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1409ab4382d2SGreg Kroah-Hartman 
14102c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
14112c7af5baSBoris BREZILLON 
1412ab4382d2SGreg Kroah-Hartman 	do {
1413d2d8d4c0SRichard Genoud 		status = atmel_uart_readl(port, ATMEL_US_CSR);
14144e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
14152c7af5baSBoris BREZILLON 		pending = status & mask;
1416ab4382d2SGreg Kroah-Hartman 		if (!pending)
1417ab4382d2SGreg Kroah-Hartman 			break;
1418ab4382d2SGreg Kroah-Hartman 
14192c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
14202c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
14212c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
14224e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
14232c7af5baSBoris BREZILLON 			pm_system_wakeup();
14242c7af5baSBoris BREZILLON 			break;
14252c7af5baSBoris BREZILLON 		}
14262c7af5baSBoris BREZILLON 
1427ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1428ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1429ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1430ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1431ab4382d2SGreg Kroah-Hartman 
14322c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
14332c7af5baSBoris BREZILLON 
1434ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1435ab4382d2SGreg Kroah-Hartman }
1436ab4382d2SGreg Kroah-Hartman 
atmel_release_tx_pdc(struct uart_port * port)1437a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1438a930e528SElen Song {
1439a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1440a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1441a930e528SElen Song 
1442a930e528SElen Song 	dma_unmap_single(port->dev,
1443a930e528SElen Song 			 pdc->dma_addr,
1444a930e528SElen Song 			 pdc->dma_size,
1445a930e528SElen Song 			 DMA_TO_DEVICE);
1446a930e528SElen Song }
1447a930e528SElen Song 
1448ab4382d2SGreg Kroah-Hartman /*
1449ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1450ab4382d2SGreg Kroah-Hartman  */
atmel_tx_pdc(struct uart_port * port)145164e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1452ab4382d2SGreg Kroah-Hartman {
1453ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1454ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1455ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1456ab4382d2SGreg Kroah-Hartman 	int count;
1457ab4382d2SGreg Kroah-Hartman 
1458ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
14594e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1460ab4382d2SGreg Kroah-Hartman 		return;
1461add147a4SIlpo Järvinen 	uart_xmit_advance(port, pdc->ofs);
1462ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1463ab4382d2SGreg Kroah-Hartman 
1464ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1465ab4382d2SGreg Kroah-Hartman 
1466ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
14674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1468ab4382d2SGreg Kroah-Hartman 
1469ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1470ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1471ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1472ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1473ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1474ab4382d2SGreg Kroah-Hartman 
1475ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1476ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1477ab4382d2SGreg Kroah-Hartman 
14784e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
14794e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
14804e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1481ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
14824e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1483ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
14844e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
14854e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1486ab4382d2SGreg Kroah-Hartman 	} else {
1487f3040983SRazvan Stefanescu 		if (atmel_uart_is_half_duplex(port)) {
1488ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1489ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1490ab4382d2SGreg Kroah-Hartman 		}
1491ab4382d2SGreg Kroah-Hartman 	}
1492ab4382d2SGreg Kroah-Hartman 
1493ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1494ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1495ab4382d2SGreg Kroah-Hartman }
1496ab4382d2SGreg Kroah-Hartman 
atmel_prepare_tx_pdc(struct uart_port * port)1497a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1498a930e528SElen Song {
1499a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1500a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1501a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1502a930e528SElen Song 
1503a930e528SElen Song 	pdc->buf = xmit->buf;
1504a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1505a930e528SElen Song 					pdc->buf,
1506a930e528SElen Song 					UART_XMIT_SIZE,
1507a930e528SElen Song 					DMA_TO_DEVICE);
1508a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1509a930e528SElen Song 	pdc->ofs = 0;
1510a930e528SElen Song 
1511a930e528SElen Song 	return 0;
1512a930e528SElen Song }
1513a930e528SElen Song 
atmel_rx_from_ring(struct uart_port * port)1514ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1515ab4382d2SGreg Kroah-Hartman {
1516ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1517ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1518ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1519fd2b55f8SJiri Slaby 	u8 flg;
1520ab4382d2SGreg Kroah-Hartman 
1521ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1522ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1523ab4382d2SGreg Kroah-Hartman 
1524ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1525ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1526ab4382d2SGreg Kroah-Hartman 
1527ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1528ab4382d2SGreg Kroah-Hartman 
1529ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1530ab4382d2SGreg Kroah-Hartman 
1531ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1532ab4382d2SGreg Kroah-Hartman 		status = c.status;
1533ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1534ab4382d2SGreg Kroah-Hartman 
1535ab4382d2SGreg Kroah-Hartman 		/*
1536ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1537ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1538ab4382d2SGreg Kroah-Hartman 		 */
1539ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1540ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1541ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1542ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1543ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1544ab4382d2SGreg Kroah-Hartman 
1545ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1546ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1547ab4382d2SGreg Kroah-Hartman 					continue;
1548ab4382d2SGreg Kroah-Hartman 			}
1549ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1550ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1551ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1552ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1553ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1554ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1555ab4382d2SGreg Kroah-Hartman 
1556ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1557ab4382d2SGreg Kroah-Hartman 
1558ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1559ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1560ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1561ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1562ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1563ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1564ab4382d2SGreg Kroah-Hartman 		}
1565ab4382d2SGreg Kroah-Hartman 
1566ab4382d2SGreg Kroah-Hartman 
1567ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1568ab4382d2SGreg Kroah-Hartman 			continue;
1569ab4382d2SGreg Kroah-Hartman 
1570ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1571ab4382d2SGreg Kroah-Hartman 	}
1572ab4382d2SGreg Kroah-Hartman 
15732e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1574ab4382d2SGreg Kroah-Hartman }
1575ab4382d2SGreg Kroah-Hartman 
atmel_release_rx_pdc(struct uart_port * port)1576a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1577a930e528SElen Song {
1578a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1579a930e528SElen Song 	int i;
1580a930e528SElen Song 
1581a930e528SElen Song 	for (i = 0; i < 2; i++) {
1582a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1583a930e528SElen Song 
1584a930e528SElen Song 		dma_unmap_single(port->dev,
1585a930e528SElen Song 				 pdc->dma_addr,
1586a930e528SElen Song 				 pdc->dma_size,
1587a930e528SElen Song 				 DMA_FROM_DEVICE);
1588a930e528SElen Song 		kfree(pdc->buf);
1589a930e528SElen Song 	}
1590a930e528SElen Song }
1591a930e528SElen Song 
atmel_rx_from_pdc(struct uart_port * port)159264e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1593ab4382d2SGreg Kroah-Hartman {
1594ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
159505c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1596ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1597ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1598ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1599ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1600ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1601ab4382d2SGreg Kroah-Hartman 
1602ab4382d2SGreg Kroah-Hartman 	do {
1603ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
16044e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1605ab4382d2SGreg Kroah-Hartman 
1606ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
16074e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1608ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1609ab4382d2SGreg Kroah-Hartman 
1610ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1611ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1612ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1613ab4382d2SGreg Kroah-Hartman 		 * find out.
1614ab4382d2SGreg Kroah-Hartman 		 *
1615ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1616ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1617ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1618ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1619ab4382d2SGreg Kroah-Hartman 		 */
1620ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1621ab4382d2SGreg Kroah-Hartman 
1622ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1623ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1624ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1625ab4382d2SGreg Kroah-Hartman 
1626ab4382d2SGreg Kroah-Hartman 			/*
1627ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1628ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1629ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1630ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1631ab4382d2SGreg Kroah-Hartman 			 */
1632ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1633ab4382d2SGreg Kroah-Hartman 
163405c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
163505c7cd39SJiri Slaby 						count);
1636ab4382d2SGreg Kroah-Hartman 
1637ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1638ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1639ab4382d2SGreg Kroah-Hartman 
1640ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1641ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1642ab4382d2SGreg Kroah-Hartman 		}
1643ab4382d2SGreg Kroah-Hartman 
1644ab4382d2SGreg Kroah-Hartman 		/*
1645ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1646ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1647ab4382d2SGreg Kroah-Hartman 		 */
1648ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1649ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
16504e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
16514e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1652ab4382d2SGreg Kroah-Hartman 
1653ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1654ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1655ab4382d2SGreg Kroah-Hartman 		}
1656ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1657ab4382d2SGreg Kroah-Hartman 
16582e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1659ab4382d2SGreg Kroah-Hartman 
16604e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
16614e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1662ab4382d2SGreg Kroah-Hartman }
1663ab4382d2SGreg Kroah-Hartman 
atmel_prepare_rx_pdc(struct uart_port * port)1664a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1665a930e528SElen Song {
1666a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1667a930e528SElen Song 	int i;
1668a930e528SElen Song 
1669a930e528SElen Song 	for (i = 0; i < 2; i++) {
1670a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1671a930e528SElen Song 
1672a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1673a930e528SElen Song 		if (pdc->buf == NULL) {
1674a930e528SElen Song 			if (i != 0) {
1675a930e528SElen Song 				dma_unmap_single(port->dev,
1676a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1677a930e528SElen Song 					PDC_BUFFER_SIZE,
1678a930e528SElen Song 					DMA_FROM_DEVICE);
1679a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1680a930e528SElen Song 			}
168136ce7cffSZheng Bin 			atmel_port->use_pdc_rx = false;
1682a930e528SElen Song 			return -ENOMEM;
1683a930e528SElen Song 		}
1684a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1685a930e528SElen Song 						pdc->buf,
1686a930e528SElen Song 						PDC_BUFFER_SIZE,
1687a930e528SElen Song 						DMA_FROM_DEVICE);
1688a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1689a930e528SElen Song 		pdc->ofs = 0;
1690a930e528SElen Song 	}
1691a930e528SElen Song 
1692a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1693a930e528SElen Song 
16944e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
16954e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1696a930e528SElen Song 
16974e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
16984e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
16994e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1700a930e528SElen Song 
1701a930e528SElen Song 	return 0;
1702a930e528SElen Song }
1703a930e528SElen Song 
1704ab4382d2SGreg Kroah-Hartman /*
1705ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1706ab4382d2SGreg Kroah-Hartman  */
atmel_tasklet_rx_func(struct tasklet_struct * t)170741e85e44SAllen Pais static void atmel_tasklet_rx_func(struct tasklet_struct *t)
1708ab4382d2SGreg Kroah-Hartman {
170941e85e44SAllen Pais 	struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t,
171041e85e44SAllen Pais 							  tasklet_rx);
171141e85e44SAllen Pais 	struct uart_port *port = &atmel_port->uart;
1712ab4382d2SGreg Kroah-Hartman 
1713ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1714ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1715a930e528SElen Song 	atmel_port->schedule_rx(port);
171600e8e658SNicolas Ferre 	spin_unlock(&port->lock);
171700e8e658SNicolas Ferre }
1718ab4382d2SGreg Kroah-Hartman 
atmel_tasklet_tx_func(struct tasklet_struct * t)171941e85e44SAllen Pais static void atmel_tasklet_tx_func(struct tasklet_struct *t)
172000e8e658SNicolas Ferre {
172141e85e44SAllen Pais 	struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t,
172241e85e44SAllen Pais 							  tasklet_tx);
172341e85e44SAllen Pais 	struct uart_port *port = &atmel_port->uart;
172400e8e658SNicolas Ferre 
172500e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
172600e8e658SNicolas Ferre 	spin_lock(&port->lock);
172700e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1728ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1729ab4382d2SGreg Kroah-Hartman }
1730ab4382d2SGreg Kroah-Hartman 
atmel_init_property(struct atmel_uart_port * atmel_port,struct platform_device * pdev)17314a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
173233d64c4fSElen Song 				struct platform_device *pdev)
173333d64c4fSElen Song {
173433d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
173533d64c4fSElen Song 
173633d64c4fSElen Song 	/* DMA/PDC usage specification */
1737490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1738490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
173933d64c4fSElen Song 			atmel_port->use_dma_rx  = true;
174033d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
174133d64c4fSElen Song 		} else {
174233d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
174333d64c4fSElen Song 			atmel_port->use_pdc_rx  = true;
174433d64c4fSElen Song 		}
174533d64c4fSElen Song 	} else {
174633d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
174733d64c4fSElen Song 		atmel_port->use_pdc_rx  = false;
174833d64c4fSElen Song 	}
174933d64c4fSElen Song 
1750490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1751490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
175233d64c4fSElen Song 			atmel_port->use_dma_tx  = true;
175333d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
175433d64c4fSElen Song 		} else {
175533d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
175633d64c4fSElen Song 			atmel_port->use_pdc_tx  = true;
175733d64c4fSElen Song 		}
175833d64c4fSElen Song 	} else {
175933d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
176033d64c4fSElen Song 		atmel_port->use_pdc_tx  = false;
176133d64c4fSElen Song 	}
176233d64c4fSElen Song }
176333d64c4fSElen Song 
atmel_set_ops(struct uart_port * port)1764a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1765a930e528SElen Song {
1766a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1767a930e528SElen Song 
176834df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
176934df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
177034df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
177134df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
177234df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1773a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1774a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1775a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1776a930e528SElen Song 	} else {
1777a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1778a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1779a930e528SElen Song 		atmel_port->release_rx = NULL;
1780a930e528SElen Song 	}
1781a930e528SElen Song 
178208f738beSElen Song 	if (atmel_use_dma_tx(port)) {
178308f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
178408f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
178508f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
178608f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1787a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1788a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1789a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1790a930e528SElen Song 	} else {
1791a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1792a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1793a930e528SElen Song 		atmel_port->release_tx = NULL;
1794a930e528SElen Song 	}
1795a930e528SElen Song }
1796a930e528SElen Song 
1797ab4382d2SGreg Kroah-Hartman /*
1798055560b0SElen Song  * Get ip name usart or uart
1799055560b0SElen Song  */
atmel_get_ip_name(struct uart_port * port)1800892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1801055560b0SElen Song {
1802055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
18034e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1804731d9caeSNicolas Ferre 	u32 version;
18051d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
18064b769371SNicolas Ferre 	/* ASCII decoding for IP version */
18074b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
18084b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
18091d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1810055560b0SElen Song 
18115bf5635aSLudovic Desroches 	/*
18125bf5635aSLudovic Desroches 	 * Only USART devices from at91sam9260 SOC implement fractional
18132867af2dSRomain Izard 	 * baudrate. It is available for all asynchronous modes, with the
18142867af2dSRomain Izard 	 * following restriction: the sampling clock's duty cycle is not
18152867af2dSRomain Izard 	 * constant.
18165bf5635aSLudovic Desroches 	 */
18175bf5635aSLudovic Desroches 	atmel_port->has_frac_baudrate = false;
18184b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
18195644bf18SSergiu Moga 	atmel_port->is_usart = false;
1820055560b0SElen Song 
18212958cceeSLudovic Desroches 	if (name == new_uart) {
18222958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
18234b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
18242958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
18252958cceeSLudovic Desroches 	} else if (name == usart) {
18262958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
18275bf5635aSLudovic Desroches 		atmel_port->has_frac_baudrate = true;
18282958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
18295644bf18SSergiu Moga 		atmel_port->is_usart = true;
18302958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
1831377fedd1SNicolas Ferre 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1832377fedd1SNicolas Ferre 		switch (version) {
1833377fedd1SNicolas Ferre 		case 0x814:	/* sama5d2 */
1834df561f66SGustavo A. R. Silva 			fallthrough;
1835377fedd1SNicolas Ferre 		case 0x701:	/* sama5d4 */
1836377fedd1SNicolas Ferre 			atmel_port->fidi_min = 3;
1837377fedd1SNicolas Ferre 			atmel_port->fidi_max = 65535;
1838377fedd1SNicolas Ferre 			break;
1839377fedd1SNicolas Ferre 		case 0x502:	/* sam9x5, sama5d3 */
1840377fedd1SNicolas Ferre 			atmel_port->fidi_min = 3;
1841377fedd1SNicolas Ferre 			atmel_port->fidi_max = 2047;
1842377fedd1SNicolas Ferre 			break;
1843377fedd1SNicolas Ferre 		default:
1844377fedd1SNicolas Ferre 			atmel_port->fidi_min = 1;
1845377fedd1SNicolas Ferre 			atmel_port->fidi_max = 2047;
1846377fedd1SNicolas Ferre 		}
18474b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
18484b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1849055560b0SElen Song 	} else {
1850731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
18514e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1852731d9caeSNicolas Ferre 		switch (version) {
1853731d9caeSNicolas Ferre 		case 0x302:
1854731d9caeSNicolas Ferre 		case 0x10213:
1855fd63a890SJonas Danielsson 		case 0x10302:
1856731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
18575bf5635aSLudovic Desroches 			atmel_port->has_frac_baudrate = true;
18584b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
18595644bf18SSergiu Moga 			atmel_port->is_usart = true;
18602958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1861731d9caeSNicolas Ferre 			break;
1862731d9caeSNicolas Ferre 		case 0x203:
1863731d9caeSNicolas Ferre 		case 0x10202:
1864731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1865731d9caeSNicolas Ferre 			break;
1866731d9caeSNicolas Ferre 		default:
1867731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1868731d9caeSNicolas Ferre 		}
1869055560b0SElen Song 	}
1870055560b0SElen Song }
1871055560b0SElen Song 
1872055560b0SElen Song /*
1873ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1874ab4382d2SGreg Kroah-Hartman  */
atmel_startup(struct uart_port * port)1875ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1876ab4382d2SGreg Kroah-Hartman {
187733d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1878ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1879ab4382d2SGreg Kroah-Hartman 	int retval;
1880ab4382d2SGreg Kroah-Hartman 
1881ab4382d2SGreg Kroah-Hartman 	/*
1882ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1883ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1884ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1885ab4382d2SGreg Kroah-Hartman 	 */
18864e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1887ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1888ab4382d2SGreg Kroah-Hartman 
1889ab4382d2SGreg Kroah-Hartman 	/*
1890ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1891ab4382d2SGreg Kroah-Hartman 	 */
18922c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
18932c7af5baSBoris BREZILLON 			     IRQF_SHARED | IRQF_COND_SUSPEND,
18949594b5beSSebastian Andrzej Siewior 			     dev_name(&pdev->dev), port);
1895ab4382d2SGreg Kroah-Hartman 	if (retval) {
1896ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1897ab4382d2SGreg Kroah-Hartman 		return retval;
1898ab4382d2SGreg Kroah-Hartman 	}
1899ab4382d2SGreg Kroah-Hartman 
190098f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
190141e85e44SAllen Pais 	tasklet_setup(&atmel_port->tasklet_rx, atmel_tasklet_rx_func);
190241e85e44SAllen Pais 	tasklet_setup(&atmel_port->tasklet_tx, atmel_tasklet_tx_func);
19031e125786SLeilei Zhao 
1904ab5e4e41SRichard Genoud 	/*
1905ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1906ab4382d2SGreg Kroah-Hartman 	 */
190733d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
19084d9628a1SLeilei Zhao 	atmel_set_ops(port);
190933d64c4fSElen Song 
1910a930e528SElen Song 	if (atmel_port->prepare_rx) {
1911a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1912a930e528SElen Song 		if (retval < 0)
1913a930e528SElen Song 			atmel_set_ops(port);
1914ab4382d2SGreg Kroah-Hartman 	}
1915ab4382d2SGreg Kroah-Hartman 
1916a930e528SElen Song 	if (atmel_port->prepare_tx) {
1917a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1918a930e528SElen Song 		if (retval < 0)
1919a930e528SElen Song 			atmel_set_ops(port);
1920ab4382d2SGreg Kroah-Hartman 	}
1921ab4382d2SGreg Kroah-Hartman 
1922b5199d46SCyrille Pitchen 	/*
1923b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1924b5199d46SCyrille Pitchen 	 */
1925b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1926b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1927b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1928b5199d46SCyrille Pitchen 		unsigned int fmr;
1929b5199d46SCyrille Pitchen 
1930b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1931b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1932b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1933b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1934b5199d46SCyrille Pitchen 
19355f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
19365f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
19375f258b3eSCyrille Pitchen 
1938b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1939b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1940b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1941b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1942b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1943b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1944b5199d46SCyrille Pitchen 
1945b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1946b5199d46SCyrille Pitchen 	}
1947b5199d46SCyrille Pitchen 
1948ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1949d2d8d4c0SRichard Genoud 	atmel_port->irq_status_prev = atmel_uart_readl(port, ATMEL_US_CSR);
1950ab4382d2SGreg Kroah-Hartman 
1951ab4382d2SGreg Kroah-Hartman 	/*
1952ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1953ab4382d2SGreg Kroah-Hartman 	 */
19544e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1955ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
19564e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1957ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
1958ab4382d2SGreg Kroah-Hartman 
1959026cb432SKees Cook 	timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0);
19608bc661bfSMarek Roszko 
19618bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
19628bc661bfSMarek Roszko 		/* set UART timeout */
19634b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19642e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19652e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19662e68c22fSElen Song 		/* set USART timeout */
19672e68c22fSElen Song 		} else {
19682958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
19692958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19704e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1971ab4382d2SGreg Kroah-Hartman 
19724e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19734e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
19742e68c22fSElen Song 		}
1975ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
19764e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
197734df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
19782e68c22fSElen Song 		/* set UART timeout */
19794b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19802e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19812e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19822e68c22fSElen Song 		/* set USART timeout */
19832e68c22fSElen Song 		} else {
19842958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
19852958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19864e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
198734df42f5SElen Song 
19884e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19894e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
19902e68c22fSElen Song 		}
1991ab4382d2SGreg Kroah-Hartman 	} else {
1992ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
19934e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1994ab4382d2SGreg Kroah-Hartman 	}
1995ab4382d2SGreg Kroah-Hartman 
1996ab4382d2SGreg Kroah-Hartman 	return 0;
1997ab4382d2SGreg Kroah-Hartman }
1998ab4382d2SGreg Kroah-Hartman 
1999ab4382d2SGreg Kroah-Hartman /*
2000479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
2001479e9b94SPeter Hurley  * buffer is reset.
2002479e9b94SPeter Hurley  */
atmel_flush_buffer(struct uart_port * port)2003479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
2004479e9b94SPeter Hurley {
2005479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2006479e9b94SPeter Hurley 
2007479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
20084e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
2009479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
2010479e9b94SPeter Hurley 	}
201131ca2c63SRichard Genoud 	/*
201231ca2c63SRichard Genoud 	 * in uart_flush_buffer(), the xmit circular buffer has just
201331ca2c63SRichard Genoud 	 * been cleared, so we have to reset tx_len accordingly.
201431ca2c63SRichard Genoud 	 */
201531ca2c63SRichard Genoud 	atmel_port->tx_len = 0;
2016479e9b94SPeter Hurley }
2017479e9b94SPeter Hurley 
2018479e9b94SPeter Hurley /*
2019ab4382d2SGreg Kroah-Hartman  * Disable the port
2020ab4382d2SGreg Kroah-Hartman  */
atmel_shutdown(struct uart_port * port)2021ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
2022ab4382d2SGreg Kroah-Hartman {
2023ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
20240cc7c6c7SMarek Roszko 
20250ae9fdefSRichard Genoud 	/* Disable modem control lines interrupts */
20260ae9fdefSRichard Genoud 	atmel_disable_ms(port);
20270ae9fdefSRichard Genoud 
202898f2082cSNicolas Ferre 	/* Disable interrupts at device level */
202998f2082cSNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
203098f2082cSNicolas Ferre 
203198f2082cSNicolas Ferre 	/* Prevent spurious interrupts from scheduling the tasklet */
203298f2082cSNicolas Ferre 	atomic_inc(&atmel_port->tasklet_shutdown);
203398f2082cSNicolas Ferre 
2034ab4382d2SGreg Kroah-Hartman 	/*
20358bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
20368bc661bfSMarek Roszko 	 * cleanup
20378bc661bfSMarek Roszko 	 */
20388bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
20398bc661bfSMarek Roszko 
204098f2082cSNicolas Ferre 	/* Make sure that no interrupt is on the fly */
204198f2082cSNicolas Ferre 	synchronize_irq(port->irq);
204298f2082cSNicolas Ferre 
20438bc661bfSMarek Roszko 	/*
20440cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
20450cc7c6c7SMarek Roszko 	 * we destroy the buffers
20460cc7c6c7SMarek Roszko 	 */
204700e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
204800e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
20490cc7c6c7SMarek Roszko 
20500cc7c6c7SMarek Roszko 	/*
20510cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
205298f2082cSNicolas Ferre 	 * disable port and break condition.
2053ab4382d2SGreg Kroah-Hartman 	 */
2054ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
2055ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
2056ab4382d2SGreg Kroah-Hartman 
20574e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
20580cc7c6c7SMarek Roszko 
2059ab4382d2SGreg Kroah-Hartman 	/*
2060ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
2061ab4382d2SGreg Kroah-Hartman 	 */
2062a930e528SElen Song 	if (atmel_port->release_rx)
2063a930e528SElen Song 		atmel_port->release_rx(port);
2064a930e528SElen Song 	if (atmel_port->release_tx)
2065a930e528SElen Song 		atmel_port->release_tx(port);
2066ab4382d2SGreg Kroah-Hartman 
2067ab4382d2SGreg Kroah-Hartman 	/*
2068bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
2069bb7e73c5SMark Deneen 	 */
2070bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
2071bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
2072bb7e73c5SMark Deneen 
2073bb7e73c5SMark Deneen 	/*
2074ab5e4e41SRichard Genoud 	 * Free the interrupts
2075ab4382d2SGreg Kroah-Hartman 	 */
2076ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
2077ab5e4e41SRichard Genoud 
2078479e9b94SPeter Hurley 	atmel_flush_buffer(port);
2079ab4382d2SGreg Kroah-Hartman }
2080ab4382d2SGreg Kroah-Hartman 
2081ab4382d2SGreg Kroah-Hartman /*
2082ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
2083ab4382d2SGreg Kroah-Hartman  */
atmel_serial_pm(struct uart_port * port,unsigned int state,unsigned int oldstate)2084ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2085ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
2086ab4382d2SGreg Kroah-Hartman {
2087ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2088ab4382d2SGreg Kroah-Hartman 
2089ab4382d2SGreg Kroah-Hartman 	switch (state) {
2090aec079f8SClaudiu Beznea 	case UART_PM_STATE_ON:
2091ab4382d2SGreg Kroah-Hartman 		/*
2092ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
2093ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
2094ab4382d2SGreg Kroah-Hartman 		 */
209591f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
2096ab4382d2SGreg Kroah-Hartman 
2097ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
20984e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
2099ab4382d2SGreg Kroah-Hartman 		break;
2100aec079f8SClaudiu Beznea 	case UART_PM_STATE_OFF:
2101ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
21024e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
21034e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
2104ab4382d2SGreg Kroah-Hartman 
2105ab4382d2SGreg Kroah-Hartman 		/*
2106ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
2107ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
2108ab4382d2SGreg Kroah-Hartman 		 */
210991f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
21105e3ce1f2SSergiu Moga 		if (__clk_is_enabled(atmel_port->gclk))
21115e3ce1f2SSergiu Moga 			clk_disable_unprepare(atmel_port->gclk);
2112ab4382d2SGreg Kroah-Hartman 		break;
2113ab4382d2SGreg Kroah-Hartman 	default:
2114ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2115ab4382d2SGreg Kroah-Hartman 	}
2116ab4382d2SGreg Kroah-Hartman }
2117ab4382d2SGreg Kroah-Hartman 
2118ab4382d2SGreg Kroah-Hartman /*
2119ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2120ab4382d2SGreg Kroah-Hartman  */
atmel_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)2121bec5b814SIlpo Järvinen static void atmel_set_termios(struct uart_port *port,
2122bec5b814SIlpo Järvinen 			      struct ktermios *termios,
2123bec5b814SIlpo Järvinen 			      const struct ktermios *old)
2124ab4382d2SGreg Kroah-Hartman {
21255bf5635aSLudovic Desroches 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2126ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
21275e3ce1f2SSergiu Moga 	unsigned int old_mode, mode, imr, quot, div, cd, fp = 0;
21285e3ce1f2SSergiu Moga 	unsigned int baud, actual_baud, gclk_rate;
21295e3ce1f2SSergiu Moga 	int ret;
2130ab4382d2SGreg Kroah-Hartman 
21311cf6e8fcSCyrille Pitchen 	/* save the current mode register */
21324e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
21331cf6e8fcSCyrille Pitchen 
21341cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
21351a5a01a1SSergiu Moga 	if (atmel_port->is_usart)
21361a5a01a1SSergiu Moga 		mode &= ~(ATMEL_US_NBSTOP | ATMEL_US_PAR | ATMEL_US_CHRL |
21371a5a01a1SSergiu Moga 			  ATMEL_US_USCLKS | ATMEL_US_USMODE);
21381a5a01a1SSergiu Moga 	else
21391a5a01a1SSergiu Moga 		mode &= ~(ATMEL_UA_BRSRCCK | ATMEL_US_PAR | ATMEL_UA_FILTER);
2140ab4382d2SGreg Kroah-Hartman 
2141ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2142ab4382d2SGreg Kroah-Hartman 
2143ab4382d2SGreg Kroah-Hartman 	/* byte size */
2144ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2145ab4382d2SGreg Kroah-Hartman 	case CS5:
2146ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2147ab4382d2SGreg Kroah-Hartman 		break;
2148ab4382d2SGreg Kroah-Hartman 	case CS6:
2149ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2150ab4382d2SGreg Kroah-Hartman 		break;
2151ab4382d2SGreg Kroah-Hartman 	case CS7:
2152ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2153ab4382d2SGreg Kroah-Hartman 		break;
2154ab4382d2SGreg Kroah-Hartman 	default:
2155ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2156ab4382d2SGreg Kroah-Hartman 		break;
2157ab4382d2SGreg Kroah-Hartman 	}
2158ab4382d2SGreg Kroah-Hartman 
2159ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2160ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2161ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2162ab4382d2SGreg Kroah-Hartman 
2163ab4382d2SGreg Kroah-Hartman 	/* parity */
2164ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2165ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2166ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2167ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2168ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2169ab4382d2SGreg Kroah-Hartman 			else
2170ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2171ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2172ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2173ab4382d2SGreg Kroah-Hartman 		else
2174ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2175ab4382d2SGreg Kroah-Hartman 	} else
2176ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2177ab4382d2SGreg Kroah-Hartman 
2178ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2179ab4382d2SGreg Kroah-Hartman 
2180ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2181ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2182ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2183ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2184ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2185ab4382d2SGreg Kroah-Hartman 
218664e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2187ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
21884e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2189ab4382d2SGreg Kroah-Hartman 
2190ab4382d2SGreg Kroah-Hartman 	/*
2191ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2192ab4382d2SGreg Kroah-Hartman 	 */
2193ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2194ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2195ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2196ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2197ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2198ab4382d2SGreg Kroah-Hartman 		/*
2199ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2200ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2201ab4382d2SGreg Kroah-Hartman 		 */
2202ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2203ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2204ab4382d2SGreg Kroah-Hartman 	}
2205ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2206ab4382d2SGreg Kroah-Hartman 
2207ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2208ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2209ab4382d2SGreg Kroah-Hartman 
2210ab4382d2SGreg Kroah-Hartman 	/*
2211ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2212ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2213ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2214ab4382d2SGreg Kroah-Hartman 	 */
22154e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
22164e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2217ab4382d2SGreg Kroah-Hartman 
2218ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
22194e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2220ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
2221ab4382d2SGreg Kroah-Hartman 
22221cf6e8fcSCyrille Pitchen 	/* mode */
222313bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
22244e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
22254e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2226ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
2227377fedd1SNicolas Ferre 	} else if (port->iso7816.flags & SER_ISO7816_ENABLED) {
2228377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg);
2229377fedd1SNicolas Ferre 		/* select mck clock, and output  */
2230377fedd1SNicolas Ferre 		mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
2231377fedd1SNicolas Ferre 		/* set max iterations */
2232377fedd1SNicolas Ferre 		mode |= ATMEL_US_MAX_ITER(3);
2233377fedd1SNicolas Ferre 		if ((port->iso7816.flags & SER_ISO7816_T_PARAM)
2234377fedd1SNicolas Ferre 				== SER_ISO7816_T(0))
2235377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T0;
2236377fedd1SNicolas Ferre 		else
2237377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T1;
22381cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
22391cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
22409bcffe75SRichard Genoud 		if (atmel_use_fifo(port) &&
22419bcffe75SRichard Genoud 		    !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
22429bcffe75SRichard Genoud 			/*
22439bcffe75SRichard Genoud 			 * with ATMEL_US_USMODE_HWHS set, the controller will
22449bcffe75SRichard Genoud 			 * be able to drive the RTS pin high/low when the RX
22459bcffe75SRichard Genoud 			 * FIFO is above RXFTHRES/below RXFTHRES2.
22469bcffe75SRichard Genoud 			 * It will also disable the transmitter when the CTS
22479bcffe75SRichard Genoud 			 * pin is high.
22489bcffe75SRichard Genoud 			 * This mode is not activated if CTS pin is a GPIO
22499bcffe75SRichard Genoud 			 * because in this case, the transmitter is always
22509bcffe75SRichard Genoud 			 * disabled (there must be an internal pull-up
22519bcffe75SRichard Genoud 			 * responsible for this behaviour).
22529bcffe75SRichard Genoud 			 * If the RTS pin is a GPIO, the controller won't be
22539bcffe75SRichard Genoud 			 * able to drive it according to the FIFO thresholds,
22549bcffe75SRichard Genoud 			 * but it will be handled by the driver.
22559bcffe75SRichard Genoud 			 */
22561cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
22579bcffe75SRichard Genoud 		} else {
22589bcffe75SRichard Genoud 			/*
22599bcffe75SRichard Genoud 			 * For platforms without FIFO, the flow control is
22609bcffe75SRichard Genoud 			 * handled by the driver.
22619bcffe75SRichard Genoud 			 */
22629bcffe75SRichard Genoud 			mode |= ATMEL_US_USMODE_NORMAL;
22635be605acSAlexandre Belloni 		}
22641cf6e8fcSCyrille Pitchen 	} else {
22651cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
22661cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2267ab4382d2SGreg Kroah-Hartman 	}
2268ab4382d2SGreg Kroah-Hartman 
22695bf5635aSLudovic Desroches 	/*
22705bf5635aSLudovic Desroches 	 * Set the baud rate:
22715bf5635aSLudovic Desroches 	 * Fractional baudrate allows to setup output frequency more
22725bf5635aSLudovic Desroches 	 * accurately. This feature is enabled only when using normal mode.
22735bf5635aSLudovic Desroches 	 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
22745bf5635aSLudovic Desroches 	 * Currently, OVER is always set to 0 so we get
227536131cdfSAlexey Starikovskiy 	 * baudrate = selected clock / (16 * (CD + FP / 8))
227636131cdfSAlexey Starikovskiy 	 * then
227736131cdfSAlexey Starikovskiy 	 * 8 CD + FP = selected clock / (2 * baudrate)
22785bf5635aSLudovic Desroches 	 */
22792867af2dSRomain Izard 	if (atmel_port->has_frac_baudrate) {
228036131cdfSAlexey Starikovskiy 		div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
228136131cdfSAlexey Starikovskiy 		cd = div >> 3;
228236131cdfSAlexey Starikovskiy 		fp = div & ATMEL_US_FP_MASK;
22835bf5635aSLudovic Desroches 	} else {
22845bf5635aSLudovic Desroches 		cd = uart_get_divisor(port, baud);
22855bf5635aSLudovic Desroches 	}
22865bf5635aSLudovic Desroches 
22875644bf18SSergiu Moga 	/*
22885644bf18SSergiu Moga 	 * If the current value of the Clock Divisor surpasses the 16 bit
22895644bf18SSergiu Moga 	 * ATMEL_US_CD mask and the IP is USART, switch to the Peripheral
22905644bf18SSergiu Moga 	 * Clock implicitly divided by 8.
22915644bf18SSergiu Moga 	 * If the IP is UART however, keep the highest possible value for
22925644bf18SSergiu Moga 	 * the CD and avoid needless division of CD, since UART IP's do not
22935644bf18SSergiu Moga 	 * support implicit division of the Peripheral Clock.
22945644bf18SSergiu Moga 	 */
22955644bf18SSergiu Moga 	if (atmel_port->is_usart && cd > ATMEL_US_CD) {
22965bf5635aSLudovic Desroches 		cd /= 8;
22975bf5635aSLudovic Desroches 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
22985644bf18SSergiu Moga 	} else {
22995644bf18SSergiu Moga 		cd = min_t(unsigned int, cd, ATMEL_US_CD);
23005bf5635aSLudovic Desroches 	}
23015644bf18SSergiu Moga 
23025e3ce1f2SSergiu Moga 	/*
23035e3ce1f2SSergiu Moga 	 * If there is no Fractional Part, there is a high chance that
23045e3ce1f2SSergiu Moga 	 * we may be able to generate a baudrate closer to the desired one
23055e3ce1f2SSergiu Moga 	 * if we use the GCLK as the clock source driving the baudrate
23065e3ce1f2SSergiu Moga 	 * generator.
23075e3ce1f2SSergiu Moga 	 */
23085e3ce1f2SSergiu Moga 	if (!atmel_port->has_frac_baudrate) {
23095e3ce1f2SSergiu Moga 		if (__clk_is_enabled(atmel_port->gclk))
23105e3ce1f2SSergiu Moga 			clk_disable_unprepare(atmel_port->gclk);
23115e3ce1f2SSergiu Moga 		gclk_rate = clk_round_rate(atmel_port->gclk, 16 * baud);
23125e3ce1f2SSergiu Moga 		actual_baud = clk_get_rate(atmel_port->clk) / (16 * cd);
23135e3ce1f2SSergiu Moga 		if (gclk_rate && abs(atmel_error_rate(baud, actual_baud)) >
23145e3ce1f2SSergiu Moga 		    abs(atmel_error_rate(baud, gclk_rate / 16))) {
23155e3ce1f2SSergiu Moga 			clk_set_rate(atmel_port->gclk, 16 * baud);
23165e3ce1f2SSergiu Moga 			ret = clk_prepare_enable(atmel_port->gclk);
23175e3ce1f2SSergiu Moga 			if (ret)
23185e3ce1f2SSergiu Moga 				goto gclk_fail;
23195e3ce1f2SSergiu Moga 
23205e3ce1f2SSergiu Moga 			if (atmel_port->is_usart) {
23215e3ce1f2SSergiu Moga 				mode &= ~ATMEL_US_USCLKS;
23225e3ce1f2SSergiu Moga 				mode |= ATMEL_US_USCLKS_GCLK;
23235e3ce1f2SSergiu Moga 			} else {
23245e3ce1f2SSergiu Moga 				mode |= ATMEL_UA_BRSRCCK;
23255e3ce1f2SSergiu Moga 			}
23265e3ce1f2SSergiu Moga 
23275e3ce1f2SSergiu Moga 			/*
23285e3ce1f2SSergiu Moga 			 * Set the Clock Divisor for GCLK to 1.
23295e3ce1f2SSergiu Moga 			 * Since we were able to generate the smallest
23305e3ce1f2SSergiu Moga 			 * multiple of the desired baudrate times 16,
23315e3ce1f2SSergiu Moga 			 * then we surely can generate a bigger multiple
23325e3ce1f2SSergiu Moga 			 * with the exact error rate for an equally increased
23335e3ce1f2SSergiu Moga 			 * CD. Thus no need to take into account
23345e3ce1f2SSergiu Moga 			 * a higher value for CD.
23355e3ce1f2SSergiu Moga 			 */
23365e3ce1f2SSergiu Moga 			cd = 1;
23375e3ce1f2SSergiu Moga 		}
23385e3ce1f2SSergiu Moga 	}
23395e3ce1f2SSergiu Moga 
23405e3ce1f2SSergiu Moga gclk_fail:
23415bf5635aSLudovic Desroches 	quot = cd | fp << ATMEL_US_FP_OFFSET;
23425bf5635aSLudovic Desroches 
2343377fedd1SNicolas Ferre 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
23444e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2345cb47b9f8SDavid Engraf 
2346cb47b9f8SDavid Engraf 	/* set the mode, clock divisor, parity, stop bits and data size */
2347cb47b9f8SDavid Engraf 	atmel_uart_writel(port, ATMEL_US_MR, mode);
2348cb47b9f8SDavid Engraf 
2349cb47b9f8SDavid Engraf 	/*
2350cb47b9f8SDavid Engraf 	 * when switching the mode, set the RTS line state according to the
2351cb47b9f8SDavid Engraf 	 * new mode, otherwise keep the former state
2352cb47b9f8SDavid Engraf 	 */
2353cb47b9f8SDavid Engraf 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2354cb47b9f8SDavid Engraf 		unsigned int rts_state;
2355cb47b9f8SDavid Engraf 
2356cb47b9f8SDavid Engraf 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2357cb47b9f8SDavid Engraf 			/* let the hardware control the RTS line */
2358cb47b9f8SDavid Engraf 			rts_state = ATMEL_US_RTSDIS;
2359cb47b9f8SDavid Engraf 		} else {
2360cb47b9f8SDavid Engraf 			/* force RTS line to low level */
2361cb47b9f8SDavid Engraf 			rts_state = ATMEL_US_RTSEN;
2362cb47b9f8SDavid Engraf 		}
2363cb47b9f8SDavid Engraf 
2364cb47b9f8SDavid Engraf 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
2365cb47b9f8SDavid Engraf 	}
2366cb47b9f8SDavid Engraf 
23674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
23684e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2369ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2370ab4382d2SGreg Kroah-Hartman 
2371ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
23724e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2373ab4382d2SGreg Kroah-Hartman 
2374ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2375ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
237635b675b9SRichard Genoud 		atmel_enable_ms(port);
237735b675b9SRichard Genoud 	else
237835b675b9SRichard Genoud 		atmel_disable_ms(port);
2379ab4382d2SGreg Kroah-Hartman 
2380ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2381ab4382d2SGreg Kroah-Hartman }
2382ab4382d2SGreg Kroah-Hartman 
atmel_set_ldisc(struct uart_port * port,struct ktermios * termios)2383732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
238442bd7a4fSViktar Palstsiuk {
2385732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
238642bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2387d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
238842bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2389d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
239042bd7a4fSViktar Palstsiuk 	} else {
239142bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2392cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2393cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2394cab68f89SPeter Hurley 			atmel_disable_ms(port);
2395cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2396cab68f89SPeter Hurley 		}
239742bd7a4fSViktar Palstsiuk 	}
239842bd7a4fSViktar Palstsiuk }
239942bd7a4fSViktar Palstsiuk 
2400ab4382d2SGreg Kroah-Hartman /*
2401ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2402ab4382d2SGreg Kroah-Hartman  */
atmel_type(struct uart_port * port)2403ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2404ab4382d2SGreg Kroah-Hartman {
2405ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2406ab4382d2SGreg Kroah-Hartman }
2407ab4382d2SGreg Kroah-Hartman 
2408ab4382d2SGreg Kroah-Hartman /*
2409ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2410ab4382d2SGreg Kroah-Hartman  */
atmel_release_port(struct uart_port * port)2411ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2412ab4382d2SGreg Kroah-Hartman {
2413c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2414c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2415ab4382d2SGreg Kroah-Hartman 
2416ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2417ab4382d2SGreg Kroah-Hartman 
2418ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2419ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2420ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2421ab4382d2SGreg Kroah-Hartman 	}
2422ab4382d2SGreg Kroah-Hartman }
2423ab4382d2SGreg Kroah-Hartman 
2424ab4382d2SGreg Kroah-Hartman /*
2425ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2426ab4382d2SGreg Kroah-Hartman  */
atmel_request_port(struct uart_port * port)2427ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2428ab4382d2SGreg Kroah-Hartman {
2429c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2430c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2431ab4382d2SGreg Kroah-Hartman 
2432ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2433ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2434ab4382d2SGreg Kroah-Hartman 
2435ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2436ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2437ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2438ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2439ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2440ab4382d2SGreg Kroah-Hartman 		}
2441ab4382d2SGreg Kroah-Hartman 	}
2442ab4382d2SGreg Kroah-Hartman 
2443ab4382d2SGreg Kroah-Hartman 	return 0;
2444ab4382d2SGreg Kroah-Hartman }
2445ab4382d2SGreg Kroah-Hartman 
2446ab4382d2SGreg Kroah-Hartman /*
2447ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2448ab4382d2SGreg Kroah-Hartman  */
atmel_config_port(struct uart_port * port,int flags)2449ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2450ab4382d2SGreg Kroah-Hartman {
2451ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2452ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2453ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2454ab4382d2SGreg Kroah-Hartman 	}
2455ab4382d2SGreg Kroah-Hartman }
2456ab4382d2SGreg Kroah-Hartman 
2457ab4382d2SGreg Kroah-Hartman /*
2458ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2459ab4382d2SGreg Kroah-Hartman  */
atmel_verify_port(struct uart_port * port,struct serial_struct * ser)2460ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2461ab4382d2SGreg Kroah-Hartman {
2462ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2463ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2464ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2465ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2466ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2467ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2468ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2469ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2470ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2471270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2472ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2473ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2474ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2475ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2476ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2477ab4382d2SGreg Kroah-Hartman 	return ret;
2478ab4382d2SGreg Kroah-Hartman }
2479ab4382d2SGreg Kroah-Hartman 
2480ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
atmel_poll_get_char(struct uart_port * port)2481ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2482ab4382d2SGreg Kroah-Hartman {
24834e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2484ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2485ab4382d2SGreg Kroah-Hartman 
2486a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2487ab4382d2SGreg Kroah-Hartman }
2488ab4382d2SGreg Kroah-Hartman 
atmel_poll_put_char(struct uart_port * port,unsigned char ch)2489ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2490ab4382d2SGreg Kroah-Hartman {
24914e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2492ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2493ab4382d2SGreg Kroah-Hartman 
2494a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2495ab4382d2SGreg Kroah-Hartman }
2496ab4382d2SGreg Kroah-Hartman #endif
2497ab4382d2SGreg Kroah-Hartman 
24985c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = {
2499ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2500ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2501ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2502ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2503ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2504ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2505ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2506ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2507ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2508ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2509ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2510ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
251142bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2512ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2513ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2514ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2515ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2516ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2517ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2518ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2519ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2520ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2521ab4382d2SGreg Kroah-Hartman #endif
2522ab4382d2SGreg Kroah-Hartman };
2523ab4382d2SGreg Kroah-Hartman 
2524af47c491SIlpo Järvinen static const struct serial_rs485 atmel_rs485_supported = {
2525*63e80efaSMathieu Othacehe 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RX_DURING_TX,
2526af47c491SIlpo Järvinen 	.delay_rts_before_send = 1,
2527af47c491SIlpo Järvinen 	.delay_rts_after_send = 1,
2528af47c491SIlpo Järvinen };
2529af47c491SIlpo Järvinen 
2530ab4382d2SGreg Kroah-Hartman /*
2531ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2532ab4382d2SGreg Kroah-Hartman  */
atmel_init_port(struct atmel_uart_port * atmel_port,struct platform_device * pdev)253391f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2534ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2535ab4382d2SGreg Kroah-Hartman {
253691f8c2d8SBoris BREZILLON 	int ret;
2537ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2538c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(pdev->dev.parent);
2539ab4382d2SGreg Kroah-Hartman 
25404a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2541a930e528SElen Song 	atmel_set_ops(port);
2542a930e528SElen Song 
2543ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
254492c8f7c0SAlexandre Belloni 	port->flags		= UPF_BOOT_AUTOCONF | UPF_IOREMAP;
2545ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2546ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2547ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2548c24d2531SRadu Pirea 	port->mapbase		= mpdev->resource[0].start;
25495bb221b0SRob Herring 	port->irq		= platform_get_irq(mpdev, 0);
255013bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
25510139da50SIlpo Järvinen 	port->rs485_supported	= atmel_rs485_supported;
2552377fedd1SNicolas Ferre 	port->iso7816_config	= atmel_config_iso7816;
255392c8f7c0SAlexandre Belloni 	port->membase		= NULL;
2554ab4382d2SGreg Kroah-Hartman 
2555ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2556ab4382d2SGreg Kroah-Hartman 
2557c150c0f3SLukas Wunner 	ret = uart_get_rs485_mode(port);
2558c150c0f3SLukas Wunner 	if (ret)
2559c150c0f3SLukas Wunner 		return ret;
2560c150c0f3SLukas Wunner 
2561ab4382d2SGreg Kroah-Hartman 	port->uartclk = clk_get_rate(atmel_port->clk);
2562ab4382d2SGreg Kroah-Hartman 
2563377fedd1SNicolas Ferre 	/*
2564377fedd1SNicolas Ferre 	 * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
2565377fedd1SNicolas Ferre 	 * ENDTX|TXBUFE
2566377fedd1SNicolas Ferre 	 */
2567477b8383SCodrin.Ciubotariu@microchip.com 	if (atmel_uart_is_half_duplex(port))
2568ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
256964e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2570ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2571ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2572ab4382d2SGreg Kroah-Hartman 	} else {
2573ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2574ab4382d2SGreg Kroah-Hartman 	}
257591f8c2d8SBoris BREZILLON 
257691f8c2d8SBoris BREZILLON 	return 0;
2577ab4382d2SGreg Kroah-Hartman }
2578ab4382d2SGreg Kroah-Hartman 
2579ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
atmel_console_putchar(struct uart_port * port,unsigned char ch)25803f8bab17SJiri Slaby static void atmel_console_putchar(struct uart_port *port, unsigned char ch)
2581ab4382d2SGreg Kroah-Hartman {
25824e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2583ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2584a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2585ab4382d2SGreg Kroah-Hartman }
2586ab4382d2SGreg Kroah-Hartman 
2587ab4382d2SGreg Kroah-Hartman /*
2588ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2589ab4382d2SGreg Kroah-Hartman  */
atmel_console_write(struct console * co,const char * s,u_int count)2590ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2591ab4382d2SGreg Kroah-Hartman {
2592ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2593ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2594ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2595ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2596ab4382d2SGreg Kroah-Hartman 
2597ab4382d2SGreg Kroah-Hartman 	/*
2598ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2599ab4382d2SGreg Kroah-Hartman 	 */
26004e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
26014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
26024e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2603ab4382d2SGreg Kroah-Hartman 
2604ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
26054e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
26064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2607ab4382d2SGreg Kroah-Hartman 
2608497e1e16SNicolas Ferre 	/* Make sure that tx path is actually able to send characters */
2609497e1e16SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2610ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2611497e1e16SNicolas Ferre 
2612ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2613ab4382d2SGreg Kroah-Hartman 
2614ab4382d2SGreg Kroah-Hartman 	/*
2615ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2616ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2617ab4382d2SGreg Kroah-Hartman 	 */
2618ab4382d2SGreg Kroah-Hartman 	do {
26194e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2620ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2621ab4382d2SGreg Kroah-Hartman 
2622ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2623ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
26244e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2625ab4382d2SGreg Kroah-Hartman 
2626ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
26274e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2628ab4382d2SGreg Kroah-Hartman }
2629ab4382d2SGreg Kroah-Hartman 
2630ab4382d2SGreg Kroah-Hartman /*
2631ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2632ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2633ab4382d2SGreg Kroah-Hartman  */
atmel_console_get_options(struct uart_port * port,int * baud,int * parity,int * bits)2634ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2635ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2636ab4382d2SGreg Kroah-Hartman {
2637ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2638ab4382d2SGreg Kroah-Hartman 
2639ab4382d2SGreg Kroah-Hartman 	/*
2640ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2641ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2642ab4382d2SGreg Kroah-Hartman 	 */
26434e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2644ab4382d2SGreg Kroah-Hartman 	if (!quot)
2645ab4382d2SGreg Kroah-Hartman 		return;
2646ab4382d2SGreg Kroah-Hartman 
26474e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2648ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2649ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2650ab4382d2SGreg Kroah-Hartman 	else
2651ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2652ab4382d2SGreg Kroah-Hartman 
26534e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2654ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2655ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2656ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2657ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2658ab4382d2SGreg Kroah-Hartman 
26595bfdd3c6STobias Schramm 	*baud = port->uartclk / (16 * quot);
2660ab4382d2SGreg Kroah-Hartman }
2661ab4382d2SGreg Kroah-Hartman 
atmel_console_setup(struct console * co,char * options)2662ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2663ab4382d2SGreg Kroah-Hartman {
2664ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2665ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2666ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2667ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2668ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2669ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2670ab4382d2SGreg Kroah-Hartman 
2671ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2672ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2673ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2674ab4382d2SGreg Kroah-Hartman 	}
2675ab4382d2SGreg Kroah-Hartman 
26764e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
26774e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
26784e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2679ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2680ab4382d2SGreg Kroah-Hartman 
2681ab4382d2SGreg Kroah-Hartman 	if (options)
2682ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2683ab4382d2SGreg Kroah-Hartman 	else
2684ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2685ab4382d2SGreg Kroah-Hartman 
2686ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2687ab4382d2SGreg Kroah-Hartman }
2688ab4382d2SGreg Kroah-Hartman 
2689ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2690ab4382d2SGreg Kroah-Hartman 
2691ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2692ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2693ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2694ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2695ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2696ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2697ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2698ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2699ab4382d2SGreg Kroah-Hartman };
2700ab4382d2SGreg Kroah-Hartman 
atmel_serial_early_write(struct console * con,const char * s,unsigned int n)2701aab68e95SMichael Walle static void atmel_serial_early_write(struct console *con, const char *s,
2702aab68e95SMichael Walle 				     unsigned int n)
2703aab68e95SMichael Walle {
2704aab68e95SMichael Walle 	struct earlycon_device *dev = con->data;
2705aab68e95SMichael Walle 
2706aab68e95SMichael Walle 	uart_console_write(&dev->port, s, n, atmel_console_putchar);
2707aab68e95SMichael Walle }
2708aab68e95SMichael Walle 
atmel_early_console_setup(struct earlycon_device * device,const char * options)2709aab68e95SMichael Walle static int __init atmel_early_console_setup(struct earlycon_device *device,
2710aab68e95SMichael Walle 					    const char *options)
2711aab68e95SMichael Walle {
2712aab68e95SMichael Walle 	if (!device->port.membase)
2713aab68e95SMichael Walle 		return -ENODEV;
2714aab68e95SMichael Walle 
2715aab68e95SMichael Walle 	device->con->write = atmel_serial_early_write;
2716aab68e95SMichael Walle 
2717aab68e95SMichael Walle 	return 0;
2718aab68e95SMichael Walle }
2719aab68e95SMichael Walle 
2720aab68e95SMichael Walle OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91rm9200-usart",
2721aab68e95SMichael Walle 		    atmel_early_console_setup);
2722aab68e95SMichael Walle OF_EARLYCON_DECLARE(atmel_serial, "atmel,at91sam9260-usart",
2723aab68e95SMichael Walle 		    atmel_early_console_setup);
2724aab68e95SMichael Walle 
2725ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2726ab4382d2SGreg Kroah-Hartman 
2727ab4382d2SGreg Kroah-Hartman #else
2728ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2729ab4382d2SGreg Kroah-Hartman #endif
2730ab4382d2SGreg Kroah-Hartman 
2731ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2732ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2733ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2734ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2735ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2736ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2737ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2738ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2739ab4382d2SGreg Kroah-Hartman };
2740ab4382d2SGreg Kroah-Hartman 
atmel_serial_clk_will_stop(void)2741ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2742ab4382d2SGreg Kroah-Hartman {
2743ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2744ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2745ab4382d2SGreg Kroah-Hartman #else
2746ab4382d2SGreg Kroah-Hartman 	return false;
2747ab4382d2SGreg Kroah-Hartman #endif
2748ab4382d2SGreg Kroah-Hartman }
2749ab4382d2SGreg Kroah-Hartman 
atmel_serial_suspend(struct device * dev)2750b50058b8SClaudiu Beznea static int __maybe_unused atmel_serial_suspend(struct device *dev)
2751ab4382d2SGreg Kroah-Hartman {
2752b50058b8SClaudiu Beznea 	struct uart_port *port = dev_get_drvdata(dev);
2753ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2754ab4382d2SGreg Kroah-Hartman 
2755207f6f34SAndy Shevchenko 	if (uart_console(port) && console_suspend_enabled) {
2756ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
27574e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
27584e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2759ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2760ab4382d2SGreg Kroah-Hartman 	}
2761ab4382d2SGreg Kroah-Hartman 
2762207f6f34SAndy Shevchenko 	if (uart_console(port) && !console_suspend_enabled) {
27636a5f0e2fSAlexandre Belloni 		/* Cache register values as we won't get a full shutdown/startup
27646a5f0e2fSAlexandre Belloni 		 * cycle
27656a5f0e2fSAlexandre Belloni 		 */
27666a5f0e2fSAlexandre Belloni 		atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR);
27676a5f0e2fSAlexandre Belloni 		atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR);
27686a5f0e2fSAlexandre Belloni 		atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
27696a5f0e2fSAlexandre Belloni 		atmel_port->cache.rtor = atmel_uart_readl(port,
27706a5f0e2fSAlexandre Belloni 							  atmel_port->rtor);
27716a5f0e2fSAlexandre Belloni 		atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR);
27726a5f0e2fSAlexandre Belloni 		atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR);
27736a5f0e2fSAlexandre Belloni 		atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR);
27746a5f0e2fSAlexandre Belloni 	}
27756a5f0e2fSAlexandre Belloni 
2776ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2777b50058b8SClaudiu Beznea 	atmel_port->may_wakeup = device_may_wakeup(dev);
27782c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
27792c7af5baSBoris BREZILLON 		unsigned long flags;
27802c7af5baSBoris BREZILLON 
27812c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
27822c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
27832c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2784b50058b8SClaudiu Beznea 		device_set_wakeup_enable(dev, 0);
27852c7af5baSBoris BREZILLON 	}
2786ab4382d2SGreg Kroah-Hartman 
2787ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2788ab4382d2SGreg Kroah-Hartman 
2789ab4382d2SGreg Kroah-Hartman 	return 0;
2790ab4382d2SGreg Kroah-Hartman }
2791ab4382d2SGreg Kroah-Hartman 
atmel_serial_resume(struct device * dev)2792b50058b8SClaudiu Beznea static int __maybe_unused atmel_serial_resume(struct device *dev)
2793ab4382d2SGreg Kroah-Hartman {
2794b50058b8SClaudiu Beznea 	struct uart_port *port = dev_get_drvdata(dev);
2795ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27962c7af5baSBoris BREZILLON 	unsigned long flags;
27972c7af5baSBoris BREZILLON 
2798207f6f34SAndy Shevchenko 	if (uart_console(port) && !console_suspend_enabled) {
27996a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
28006a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
28016a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
28026a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, atmel_port->rtor,
28036a5f0e2fSAlexandre Belloni 				  atmel_port->cache.rtor);
28046a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr);
28056a5f0e2fSAlexandre Belloni 
28066a5f0e2fSAlexandre Belloni 		if (atmel_port->fifo_size) {
28076a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN |
28086a5f0e2fSAlexandre Belloni 					  ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR);
28096a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FMR,
28106a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fmr);
28116a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FIER,
28126a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fimr);
28136a5f0e2fSAlexandre Belloni 		}
28146a5f0e2fSAlexandre Belloni 		atmel_start_rx(port);
28156a5f0e2fSAlexandre Belloni 	}
28166a5f0e2fSAlexandre Belloni 
28172c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
28182c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
28192c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
28202c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
28212c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
28222c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
28232c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
28242c7af5baSBoris BREZILLON 	}
28252c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
28262c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2827ab4382d2SGreg Kroah-Hartman 
2828ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2829b50058b8SClaudiu Beznea 	device_set_wakeup_enable(dev, atmel_port->may_wakeup);
2830ab4382d2SGreg Kroah-Hartman 
2831ab4382d2SGreg Kroah-Hartman 	return 0;
2832ab4382d2SGreg Kroah-Hartman }
2833ab4382d2SGreg Kroah-Hartman 
atmel_serial_probe_fifos(struct atmel_uart_port * atmel_port,struct platform_device * pdev)2834b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2835b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2836b5199d46SCyrille Pitchen {
2837b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2838b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2839b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2840b5199d46SCyrille Pitchen 
2841b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2842b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2843b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2844b5199d46SCyrille Pitchen 		return;
2845b5199d46SCyrille Pitchen 
2846b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2847b5199d46SCyrille Pitchen 		return;
2848b5199d46SCyrille Pitchen 
2849b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2850b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2851b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2852b5199d46SCyrille Pitchen 		return;
2853b5199d46SCyrille Pitchen 	}
2854b5199d46SCyrille Pitchen 
2855b5199d46SCyrille Pitchen 	/*
2856b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2857b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2858b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2859b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2860b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2861b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2862b5199d46SCyrille Pitchen 	 */
2863b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2864b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2865b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2866b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2867b5199d46SCyrille Pitchen 
2868b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2869b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2870b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2871b78cd169SJaeden Amero 		atmel_port->rts_high);
2872b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2873b78cd169SJaeden Amero 		atmel_port->rts_low);
2874b5199d46SCyrille Pitchen }
2875b5199d46SCyrille Pitchen 
atmel_serial_probe(struct platform_device * pdev)28769671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2877ab4382d2SGreg Kroah-Hartman {
2878b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
2879c24d2531SRadu Pirea 	struct device_node *np = pdev->dev.parent->of_node;
2880ab4382d2SGreg Kroah-Hartman 	void *data;
28818d41ab87SColin Ian King 	int ret;
2882bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2883ab4382d2SGreg Kroah-Hartman 
2884ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2885ab4382d2SGreg Kroah-Hartman 
2886c24d2531SRadu Pirea 	/*
2887c24d2531SRadu Pirea 	 * In device tree there is no node with "atmel,at91rm9200-usart-serial"
2888c24d2531SRadu Pirea 	 * as compatible string. This driver is probed by at91-usart mfd driver
2889c24d2531SRadu Pirea 	 * which is just a wrapper over the atmel_serial driver and
2890c24d2531SRadu Pirea 	 * spi-at91-usart driver. All attributes needed by this driver are
2891c24d2531SRadu Pirea 	 * found in of_node of parent.
2892c24d2531SRadu Pirea 	 */
2893c24d2531SRadu Pirea 	pdev->dev.of_node = np;
2894c24d2531SRadu Pirea 
28955fbe46b6SNicolas Ferre 	ret = of_alias_get_id(np, "serial");
28964cbf9f48SNicolas Ferre 	if (ret < 0)
28975fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
28984cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2899503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
29004cbf9f48SNicolas Ferre 
2901503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
29024cbf9f48SNicolas Ferre 		ret = -ENODEV;
29034cbf9f48SNicolas Ferre 		goto err;
29044cbf9f48SNicolas Ferre 	}
29054cbf9f48SNicolas Ferre 
2906503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
29074cbf9f48SNicolas Ferre 		/* port already in use */
29084cbf9f48SNicolas Ferre 		ret = -EBUSY;
29094cbf9f48SNicolas Ferre 		goto err;
29104cbf9f48SNicolas Ferre 	}
29114cbf9f48SNicolas Ferre 
2912b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2913b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2914b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2915078abd98SDmitry Safonov 	atmel_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_ATMEL_CONSOLE);
2916b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2917354e57f3SLinus Walleij 
291898f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
2919b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
29202c7af5baSBoris BREZILLON 
292184b476b1SClaudiu Beznea 	atmel_port->clk = devm_clk_get(&pdev->dev, "usart");
292284b476b1SClaudiu Beznea 	if (IS_ERR(atmel_port->clk)) {
292384b476b1SClaudiu Beznea 		ret = PTR_ERR(atmel_port->clk);
292484b476b1SClaudiu Beznea 		goto err;
292584b476b1SClaudiu Beznea 	}
292684b476b1SClaudiu Beznea 	ret = clk_prepare_enable(atmel_port->clk);
292784b476b1SClaudiu Beznea 	if (ret)
292884b476b1SClaudiu Beznea 		goto err;
292984b476b1SClaudiu Beznea 
29305e3ce1f2SSergiu Moga 	atmel_port->gclk = devm_clk_get_optional(&pdev->dev, "gclk");
29315e3ce1f2SSergiu Moga 	if (IS_ERR(atmel_port->gclk)) {
29325e3ce1f2SSergiu Moga 		ret = PTR_ERR(atmel_port->gclk);
29335e3ce1f2SSergiu Moga 		goto err_clk_disable_unprepare;
29345e3ce1f2SSergiu Moga 	}
29355e3ce1f2SSergiu Moga 
2936b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
293791f8c2d8SBoris BREZILLON 	if (ret)
293884b476b1SClaudiu Beznea 		goto err_clk_disable_unprepare;
2939ab4382d2SGreg Kroah-Hartman 
2940b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2941b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2942b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
294384b476b1SClaudiu Beznea 		goto err_clk_disable_unprepare;
294418dfef9cSUwe Kleine-König 	}
294518dfef9cSUwe Kleine-König 
2946b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2947ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
29486da2ec56SKees Cook 		data = kmalloc_array(ATMEL_SERIAL_RINGSIZE,
29496da2ec56SKees Cook 				     sizeof(struct atmel_uart_char),
29506da2ec56SKees Cook 				     GFP_KERNEL);
2951ab4382d2SGreg Kroah-Hartman 		if (!data)
295284b476b1SClaudiu Beznea 			goto err_clk_disable_unprepare;
2953b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2954ab4382d2SGreg Kroah-Hartman 	}
2955ab4382d2SGreg Kroah-Hartman 
2956b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2957bd737f87SRicardo Ribalda Delgado 
2958b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2959ab4382d2SGreg Kroah-Hartman 	if (ret)
2960ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2961ab4382d2SGreg Kroah-Hartman 
2962ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2963b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2964ab4382d2SGreg Kroah-Hartman 
2965bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2966b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
29674e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2968b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2969b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2970fc887b15SLinus Torvalds 	}
2971fc887b15SLinus Torvalds 
2972055560b0SElen Song 	/*
2973055560b0SElen Song 	 * Get port name of usart or uart
2974055560b0SElen Song 	 */
2975b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2976055560b0SElen Song 
2977d4f64187SCyrille Pitchen 	/*
2978d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2979d4f64187SCyrille Pitchen 	 * is used
2980d4f64187SCyrille Pitchen 	 */
2981b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2982d4f64187SCyrille Pitchen 
2983ab4382d2SGreg Kroah-Hartman 	return 0;
2984ab4382d2SGreg Kroah-Hartman 
2985ab4382d2SGreg Kroah-Hartman err_add_port:
2986b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2987b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
298884b476b1SClaudiu Beznea err_clk_disable_unprepare:
298984b476b1SClaudiu Beznea 	clk_disable_unprepare(atmel_port->clk);
2990b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
29914cbf9f48SNicolas Ferre err:
2992ab4382d2SGreg Kroah-Hartman 	return ret;
2993ab4382d2SGreg Kroah-Hartman }
2994ab4382d2SGreg Kroah-Hartman 
2995f4a8ab04SRomain Izard /*
2996f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2997f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2998f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2999f4a8ab04SRomain Izard  *
3000f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
3001f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
3002f4a8ab04SRomain Izard  * port in the normal case.
3003f4a8ab04SRomain Izard  */
atmel_serial_remove(struct platform_device * pdev)3004f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
3005f4a8ab04SRomain Izard {
3006f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
3007f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
3008f4a8ab04SRomain Izard 
300900e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
301000e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
3011f4a8ab04SRomain Izard 
3012f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
3013f4a8ab04SRomain Izard 
3014d5b3d02dSUwe Kleine-König 	uart_remove_one_port(&atmel_uart, port);
3015f4a8ab04SRomain Izard 
3016f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
3017f4a8ab04SRomain Izard 
3018f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
3019f4a8ab04SRomain Izard 
3020f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
3021f4a8ab04SRomain Izard 
3022c24d2531SRadu Pirea 	pdev->dev.of_node = NULL;
3023f4a8ab04SRomain Izard 
3024d5b3d02dSUwe Kleine-König 	return 0;
3025f4a8ab04SRomain Izard }
3026f4a8ab04SRomain Izard 
3027b50058b8SClaudiu Beznea static SIMPLE_DEV_PM_OPS(atmel_serial_pm_ops, atmel_serial_suspend,
3028b50058b8SClaudiu Beznea 			 atmel_serial_resume);
3029b50058b8SClaudiu Beznea 
3030ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
3031ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
3032f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
3033ab4382d2SGreg Kroah-Hartman 	.driver		= {
3034c24d2531SRadu Pirea 		.name			= "atmel_usart_serial",
30355fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
3036b50058b8SClaudiu Beznea 		.pm			= pm_ptr(&atmel_serial_pm_ops),
3037ab4382d2SGreg Kroah-Hartman 	},
3038ab4382d2SGreg Kroah-Hartman };
3039ab4382d2SGreg Kroah-Hartman 
atmel_serial_init(void)3040ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
3041ab4382d2SGreg Kroah-Hartman {
3042ab4382d2SGreg Kroah-Hartman 	int ret;
3043ab4382d2SGreg Kroah-Hartman 
3044ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
3045ab4382d2SGreg Kroah-Hartman 	if (ret)
3046ab4382d2SGreg Kroah-Hartman 		return ret;
3047ab4382d2SGreg Kroah-Hartman 
3048ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
3049ab4382d2SGreg Kroah-Hartman 	if (ret)
3050ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
3051ab4382d2SGreg Kroah-Hartman 
3052ab4382d2SGreg Kroah-Hartman 	return ret;
3053ab4382d2SGreg Kroah-Hartman }
3054c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
3055