xref: /openbmc/linux/drivers/tty/serial/atmel_serial.c (revision 078abd98d7f81f3a2ad2a0f6884ea0ab2f556cfe)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2ab4382d2SGreg Kroah-Hartman /*
372ce5732SAndy Shevchenko  *  Driver for Atmel AT91 Serial ports
4ab4382d2SGreg Kroah-Hartman  *  Copyright (C) 2003 Rick Bronson
5ab4382d2SGreg Kroah-Hartman  *
6ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
7ab4382d2SGreg Kroah-Hartman  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8ab4382d2SGreg Kroah-Hartman  *
9ab4382d2SGreg Kroah-Hartman  *  DMA support added by Chip Coldwell.
10ab4382d2SGreg Kroah-Hartman  */
11ab4382d2SGreg Kroah-Hartman #include <linux/tty.h>
12ab4382d2SGreg Kroah-Hartman #include <linux/ioport.h>
13ab4382d2SGreg Kroah-Hartman #include <linux/slab.h>
14ab4382d2SGreg Kroah-Hartman #include <linux/init.h>
15ab4382d2SGreg Kroah-Hartman #include <linux/serial.h>
16ab4382d2SGreg Kroah-Hartman #include <linux/clk.h>
17ab4382d2SGreg Kroah-Hartman #include <linux/console.h>
18ab4382d2SGreg Kroah-Hartman #include <linux/sysrq.h>
19ab4382d2SGreg Kroah-Hartman #include <linux/tty_flip.h>
20ab4382d2SGreg Kroah-Hartman #include <linux/platform_device.h>
215fbe46b6SNicolas Ferre #include <linux/of.h>
225fbe46b6SNicolas Ferre #include <linux/of_device.h>
23354e57f3SLinus Walleij #include <linux/of_gpio.h>
24ab4382d2SGreg Kroah-Hartman #include <linux/dma-mapping.h>
256b997babSVinod Koul #include <linux/dmaengine.h>
26ab4382d2SGreg Kroah-Hartman #include <linux/atmel_pdc.h>
27ab4382d2SGreg Kroah-Hartman #include <linux/uaccess.h>
28bcd2360cSJean-Christophe PLAGNIOL-VILLARD #include <linux/platform_data/atmel.h>
292e68c22fSElen Song #include <linux/timer.h>
30354e57f3SLinus Walleij #include <linux/gpio.h>
31e0b0baadSRichard Genoud #include <linux/gpio/consumer.h>
32e0b0baadSRichard Genoud #include <linux/err.h>
33ab5e4e41SRichard Genoud #include <linux/irq.h>
342c7af5baSBoris BREZILLON #include <linux/suspend.h>
352b5cf14bSGeliang Tang #include <linux/mm.h>
36ab4382d2SGreg Kroah-Hartman 
37377fedd1SNicolas Ferre #include <asm/div64.h>
38ab4382d2SGreg Kroah-Hartman #include <asm/io.h>
39ab4382d2SGreg Kroah-Hartman #include <asm/ioctls.h>
40ab4382d2SGreg Kroah-Hartman 
41ab4382d2SGreg Kroah-Hartman #define PDC_BUFFER_SIZE		512
42ab4382d2SGreg Kroah-Hartman /* Revisit: We should calculate this based on the actual port settings */
43ab4382d2SGreg Kroah-Hartman #define PDC_RX_TIMEOUT		(3 * 10)		/* 3 bytes */
44ab4382d2SGreg Kroah-Hartman 
45b5199d46SCyrille Pitchen /* The minium number of data FIFOs should be able to contain */
46b5199d46SCyrille Pitchen #define ATMEL_MIN_FIFO_SIZE	8
47b5199d46SCyrille Pitchen /*
48b5199d46SCyrille Pitchen  * These two offsets are substracted from the RX FIFO size to define the RTS
49b5199d46SCyrille Pitchen  * high and low thresholds
50b5199d46SCyrille Pitchen  */
51b5199d46SCyrille Pitchen #define ATMEL_RTS_HIGH_OFFSET	16
52b5199d46SCyrille Pitchen #define ATMEL_RTS_LOW_OFFSET	20
53b5199d46SCyrille Pitchen 
54ab4382d2SGreg Kroah-Hartman #include <linux/serial_core.h>
55ab4382d2SGreg Kroah-Hartman 
56e0b0baadSRichard Genoud #include "serial_mctrl_gpio.h"
578961df89SRichard Genoud #include "atmel_serial.h"
58e0b0baadSRichard Genoud 
59ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port);
60ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port);
61ab4382d2SGreg Kroah-Hartman 
62ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_TTYAT
63ab4382d2SGreg Kroah-Hartman 
64ab4382d2SGreg Kroah-Hartman /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
65ab4382d2SGreg Kroah-Hartman  * should coexist with the 8250 driver, such as if we have an external 16C550
66ab4382d2SGreg Kroah-Hartman  * UART. */
67ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	204
68ab4382d2SGreg Kroah-Hartman #define MINOR_START		154
69ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyAT"
70ab4382d2SGreg Kroah-Hartman 
71ab4382d2SGreg Kroah-Hartman #else
72ab4382d2SGreg Kroah-Hartman 
73ab4382d2SGreg Kroah-Hartman /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
74ab4382d2SGreg Kroah-Hartman  * name, but it is legally reserved for the 8250 driver. */
75ab4382d2SGreg Kroah-Hartman #define SERIAL_ATMEL_MAJOR	TTY_MAJOR
76ab4382d2SGreg Kroah-Hartman #define MINOR_START		64
77ab4382d2SGreg Kroah-Hartman #define ATMEL_DEVICENAME	"ttyS"
78ab4382d2SGreg Kroah-Hartman 
79ab4382d2SGreg Kroah-Hartman #endif
80ab4382d2SGreg Kroah-Hartman 
81ab4382d2SGreg Kroah-Hartman #define ATMEL_ISR_PASS_LIMIT	256
82ab4382d2SGreg Kroah-Hartman 
83ab4382d2SGreg Kroah-Hartman struct atmel_dma_buffer {
84ab4382d2SGreg Kroah-Hartman 	unsigned char	*buf;
85ab4382d2SGreg Kroah-Hartman 	dma_addr_t	dma_addr;
86ab4382d2SGreg Kroah-Hartman 	unsigned int	dma_size;
87ab4382d2SGreg Kroah-Hartman 	unsigned int	ofs;
88ab4382d2SGreg Kroah-Hartman };
89ab4382d2SGreg Kroah-Hartman 
90ab4382d2SGreg Kroah-Hartman struct atmel_uart_char {
91ab4382d2SGreg Kroah-Hartman 	u16		status;
92ab4382d2SGreg Kroah-Hartman 	u16		ch;
93ab4382d2SGreg Kroah-Hartman };
94ab4382d2SGreg Kroah-Hartman 
95637ba54fSLudovic Desroches /*
96637ba54fSLudovic Desroches  * Be careful, the real size of the ring buffer is
97637ba54fSLudovic Desroches  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
98637ba54fSLudovic Desroches  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
99637ba54fSLudovic Desroches  * DMA mode.
100637ba54fSLudovic Desroches  */
101ab4382d2SGreg Kroah-Hartman #define ATMEL_SERIAL_RINGSIZE 1024
102ab4382d2SGreg Kroah-Hartman 
103ab4382d2SGreg Kroah-Hartman /*
1049af92fbfSAlexandre Belloni  * at91: 6 USARTs and one DBGU port (SAM9260)
105432f9748SAlexandre Belloni  * samx7: 3 USARTs and 5 UARTs
1069af92fbfSAlexandre Belloni  */
107432f9748SAlexandre Belloni #define ATMEL_MAX_UART		8
1089af92fbfSAlexandre Belloni 
1099af92fbfSAlexandre Belloni /*
110ab4382d2SGreg Kroah-Hartman  * We wrap our port structure around the generic uart_port.
111ab4382d2SGreg Kroah-Hartman  */
112ab4382d2SGreg Kroah-Hartman struct atmel_uart_port {
113ab4382d2SGreg Kroah-Hartman 	struct uart_port	uart;		/* uart */
114ab4382d2SGreg Kroah-Hartman 	struct clk		*clk;		/* uart clock */
115ab4382d2SGreg Kroah-Hartman 	int			may_wakeup;	/* cached value of device_may_wakeup for times we need to disable it */
116ab4382d2SGreg Kroah-Hartman 	u32			backup_imr;	/* IMR saved during suspend */
117ab4382d2SGreg Kroah-Hartman 	int			break_active;	/* break being received */
118ab4382d2SGreg Kroah-Hartman 
11934df42f5SElen Song 	bool			use_dma_rx;	/* enable DMA receiver */
12064e22ebeSElen Song 	bool			use_pdc_rx;	/* enable PDC receiver */
121ab4382d2SGreg Kroah-Hartman 	short			pdc_rx_idx;	/* current PDC RX buffer */
122ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_rx[2];	/* PDC receier */
123ab4382d2SGreg Kroah-Hartman 
12408f738beSElen Song 	bool			use_dma_tx;     /* enable DMA transmitter */
12564e22ebeSElen Song 	bool			use_pdc_tx;	/* enable PDC transmitter */
126ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer	pdc_tx;		/* PDC transmitter */
127ab4382d2SGreg Kroah-Hartman 
12808f738beSElen Song 	spinlock_t			lock_tx;	/* port lock */
12934df42f5SElen Song 	spinlock_t			lock_rx;	/* port lock */
13008f738beSElen Song 	struct dma_chan			*chan_tx;
13134df42f5SElen Song 	struct dma_chan			*chan_rx;
13208f738beSElen Song 	struct dma_async_tx_descriptor	*desc_tx;
13334df42f5SElen Song 	struct dma_async_tx_descriptor	*desc_rx;
13408f738beSElen Song 	dma_cookie_t			cookie_tx;
13534df42f5SElen Song 	dma_cookie_t			cookie_rx;
13608f738beSElen Song 	struct scatterlist		sg_tx;
13734df42f5SElen Song 	struct scatterlist		sg_rx;
13800e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_rx;
13900e8e658SNicolas Ferre 	struct tasklet_struct	tasklet_tx;
14098f2082cSNicolas Ferre 	atomic_t		tasklet_shutdown;
141ab4382d2SGreg Kroah-Hartman 	unsigned int		irq_status_prev;
1425f258b3eSCyrille Pitchen 	unsigned int		tx_len;
143ab4382d2SGreg Kroah-Hartman 
144ab4382d2SGreg Kroah-Hartman 	struct circ_buf		rx_ring;
145ab4382d2SGreg Kroah-Hartman 
146e0b0baadSRichard Genoud 	struct mctrl_gpios	*gpios;
147377fedd1SNicolas Ferre 	u32			backup_mode;	/* MR saved during iso7816 operations */
148377fedd1SNicolas Ferre 	u32			backup_brgr;	/* BRGR saved during iso7816 operations */
149ab4382d2SGreg Kroah-Hartman 	unsigned int		tx_done_mask;
150b5199d46SCyrille Pitchen 	u32			fifo_size;
151b5199d46SCyrille Pitchen 	u32			rts_high;
152b5199d46SCyrille Pitchen 	u32			rts_low;
153ab5e4e41SRichard Genoud 	bool			ms_irq_enabled;
1542958cceeSLudovic Desroches 	u32			rtor;	/* address of receiver timeout register if it exists */
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 
171488ae82dSAlexandre Belloni #ifdef CONFIG_PM
1726a5f0e2fSAlexandre Belloni 	struct {
1736a5f0e2fSAlexandre Belloni 		u32		cr;
1746a5f0e2fSAlexandre Belloni 		u32		mr;
1756a5f0e2fSAlexandre Belloni 		u32		imr;
1766a5f0e2fSAlexandre Belloni 		u32		brgr;
1776a5f0e2fSAlexandre Belloni 		u32		rtor;
1786a5f0e2fSAlexandre Belloni 		u32		ttgr;
1796a5f0e2fSAlexandre Belloni 		u32		fmr;
1806a5f0e2fSAlexandre Belloni 		u32		fimr;
1816a5f0e2fSAlexandre Belloni 	} cache;
182488ae82dSAlexandre Belloni #endif
1836a5f0e2fSAlexandre Belloni 
184a930e528SElen Song 	int (*prepare_rx)(struct uart_port *port);
185a930e528SElen Song 	int (*prepare_tx)(struct uart_port *port);
186a930e528SElen Song 	void (*schedule_rx)(struct uart_port *port);
187a930e528SElen Song 	void (*schedule_tx)(struct uart_port *port);
188a930e528SElen Song 	void (*release_rx)(struct uart_port *port);
189a930e528SElen Song 	void (*release_tx)(struct uart_port *port);
190ab4382d2SGreg Kroah-Hartman };
191ab4382d2SGreg Kroah-Hartman 
192ab4382d2SGreg Kroah-Hartman static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
193503bded9SPawel Wieczorkiewicz static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
194ab4382d2SGreg Kroah-Hartman 
1955fbe46b6SNicolas Ferre #if defined(CONFIG_OF)
1965fbe46b6SNicolas Ferre static const struct of_device_id atmel_serial_dt_ids[] = {
197c24d2531SRadu Pirea 	{ .compatible = "atmel,at91rm9200-usart-serial" },
1985fbe46b6SNicolas Ferre 	{ /* sentinel */ }
1995fbe46b6SNicolas Ferre };
2005fbe46b6SNicolas Ferre #endif
2015fbe46b6SNicolas Ferre 
202ab4382d2SGreg Kroah-Hartman static inline struct atmel_uart_port *
203ab4382d2SGreg Kroah-Hartman to_atmel_uart_port(struct uart_port *uart)
204ab4382d2SGreg Kroah-Hartman {
205ab4382d2SGreg Kroah-Hartman 	return container_of(uart, struct atmel_uart_port, uart);
206ab4382d2SGreg Kroah-Hartman }
207ab4382d2SGreg Kroah-Hartman 
2084e7decdaSCyrille Pitchen static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
2094e7decdaSCyrille Pitchen {
2104e7decdaSCyrille Pitchen 	return __raw_readl(port->membase + reg);
2114e7decdaSCyrille Pitchen }
2124e7decdaSCyrille Pitchen 
2134e7decdaSCyrille Pitchen static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
2144e7decdaSCyrille Pitchen {
2154e7decdaSCyrille Pitchen 	__raw_writel(value, port->membase + reg);
2164e7decdaSCyrille Pitchen }
2174e7decdaSCyrille Pitchen 
218a6499435SCyrille Pitchen static inline u8 atmel_uart_read_char(struct uart_port *port)
219a6499435SCyrille Pitchen {
220a6499435SCyrille Pitchen 	return __raw_readb(port->membase + ATMEL_US_RHR);
221a6499435SCyrille Pitchen }
222a6499435SCyrille Pitchen 
223a6499435SCyrille Pitchen static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
224a6499435SCyrille Pitchen {
225a6499435SCyrille Pitchen 	__raw_writeb(value, port->membase + ATMEL_US_THR);
226a6499435SCyrille Pitchen }
227a6499435SCyrille Pitchen 
228f3040983SRazvan Stefanescu static inline int atmel_uart_is_half_duplex(struct uart_port *port)
229f3040983SRazvan Stefanescu {
230f3040983SRazvan Stefanescu 	return ((port->rs485.flags & SER_RS485_ENABLED) &&
231f3040983SRazvan Stefanescu 		!(port->rs485.flags & SER_RS485_RX_DURING_TX)) ||
232f3040983SRazvan Stefanescu 		(port->iso7816.flags & SER_ISO7816_ENABLED);
233f3040983SRazvan Stefanescu }
234f3040983SRazvan Stefanescu 
235ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_PDC
23664e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
237ab4382d2SGreg Kroah-Hartman {
238ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
239ab4382d2SGreg Kroah-Hartman 
24064e22ebeSElen Song 	return atmel_port->use_pdc_rx;
241ab4382d2SGreg Kroah-Hartman }
242ab4382d2SGreg Kroah-Hartman 
24364e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
244ab4382d2SGreg Kroah-Hartman {
245ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
246ab4382d2SGreg Kroah-Hartman 
24764e22ebeSElen Song 	return atmel_port->use_pdc_tx;
248ab4382d2SGreg Kroah-Hartman }
249ab4382d2SGreg Kroah-Hartman #else
25064e22ebeSElen Song static bool atmel_use_pdc_rx(struct uart_port *port)
251ab4382d2SGreg Kroah-Hartman {
252ab4382d2SGreg Kroah-Hartman 	return false;
253ab4382d2SGreg Kroah-Hartman }
254ab4382d2SGreg Kroah-Hartman 
25564e22ebeSElen Song static bool atmel_use_pdc_tx(struct uart_port *port)
256ab4382d2SGreg Kroah-Hartman {
257ab4382d2SGreg Kroah-Hartman 	return false;
258ab4382d2SGreg Kroah-Hartman }
259ab4382d2SGreg Kroah-Hartman #endif
260ab4382d2SGreg Kroah-Hartman 
26108f738beSElen Song static bool atmel_use_dma_tx(struct uart_port *port)
26208f738beSElen Song {
26308f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26408f738beSElen Song 
26508f738beSElen Song 	return atmel_port->use_dma_tx;
26608f738beSElen Song }
26708f738beSElen Song 
26834df42f5SElen Song static bool atmel_use_dma_rx(struct uart_port *port)
26934df42f5SElen Song {
27034df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27134df42f5SElen Song 
27234df42f5SElen Song 	return atmel_port->use_dma_rx;
27334df42f5SElen Song }
27434df42f5SElen Song 
2755be605acSAlexandre Belloni static bool atmel_use_fifo(struct uart_port *port)
2765be605acSAlexandre Belloni {
2775be605acSAlexandre Belloni 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2785be605acSAlexandre Belloni 
2795be605acSAlexandre Belloni 	return atmel_port->fifo_size;
2805be605acSAlexandre Belloni }
2815be605acSAlexandre Belloni 
28298f2082cSNicolas Ferre static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
28398f2082cSNicolas Ferre 				   struct tasklet_struct *t)
28498f2082cSNicolas Ferre {
28598f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown))
28698f2082cSNicolas Ferre 		tasklet_schedule(t);
28798f2082cSNicolas Ferre }
28898f2082cSNicolas Ferre 
289ab4382d2SGreg Kroah-Hartman /* Enable or disable the rs485 support */
29013bd3e6fSRicardo Ribalda Delgado static int atmel_config_rs485(struct uart_port *port,
29113bd3e6fSRicardo Ribalda Delgado 			      struct serial_rs485 *rs485conf)
292ab4382d2SGreg Kroah-Hartman {
293ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
294ab4382d2SGreg Kroah-Hartman 	unsigned int mode;
295ab4382d2SGreg Kroah-Hartman 
296ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
2974e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
298ab4382d2SGreg Kroah-Hartman 
2994e7decdaSCyrille Pitchen 	mode = atmel_uart_readl(port, ATMEL_US_MR);
300ab4382d2SGreg Kroah-Hartman 
301ab4382d2SGreg Kroah-Hartman 	/* Resetting serial mode to RS232 (0x0) */
302ab4382d2SGreg Kroah-Hartman 	mode &= ~ATMEL_US_USMODE;
303ab4382d2SGreg Kroah-Hartman 
30413bd3e6fSRicardo Ribalda Delgado 	port->rs485 = *rs485conf;
305ab4382d2SGreg Kroah-Hartman 
306ab4382d2SGreg Kroah-Hartman 	if (rs485conf->flags & SER_RS485_ENABLED) {
307ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS485\n");
308ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
3094e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
3104e7decdaSCyrille Pitchen 				  rs485conf->delay_rts_after_send);
311ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
312ab4382d2SGreg Kroah-Hartman 	} else {
313ab4382d2SGreg Kroah-Hartman 		dev_dbg(port->dev, "Setting UART to RS232\n");
31464e22ebeSElen Song 		if (atmel_use_pdc_tx(port))
315ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
316ab4382d2SGreg Kroah-Hartman 				ATMEL_US_TXBUFE;
317ab4382d2SGreg Kroah-Hartman 		else
318ab4382d2SGreg Kroah-Hartman 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
319ab4382d2SGreg Kroah-Hartman 	}
3204e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
321ab4382d2SGreg Kroah-Hartman 
322ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
3234e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
324ab4382d2SGreg Kroah-Hartman 
32513bd3e6fSRicardo Ribalda Delgado 	return 0;
326ab4382d2SGreg Kroah-Hartman }
327ab4382d2SGreg Kroah-Hartman 
328377fedd1SNicolas Ferre static unsigned int atmel_calc_cd(struct uart_port *port,
329377fedd1SNicolas Ferre 				  struct serial_iso7816 *iso7816conf)
330377fedd1SNicolas Ferre {
331377fedd1SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
332377fedd1SNicolas Ferre 	unsigned int cd;
333377fedd1SNicolas Ferre 	u64 mck_rate;
334377fedd1SNicolas Ferre 
335377fedd1SNicolas Ferre 	mck_rate = (u64)clk_get_rate(atmel_port->clk);
336377fedd1SNicolas Ferre 	do_div(mck_rate, iso7816conf->clk);
337377fedd1SNicolas Ferre 	cd = mck_rate;
338377fedd1SNicolas Ferre 	return cd;
339377fedd1SNicolas Ferre }
340377fedd1SNicolas Ferre 
341377fedd1SNicolas Ferre static unsigned int atmel_calc_fidi(struct uart_port *port,
342377fedd1SNicolas Ferre 				    struct serial_iso7816 *iso7816conf)
343377fedd1SNicolas Ferre {
344377fedd1SNicolas Ferre 	u64 fidi = 0;
345377fedd1SNicolas Ferre 
346377fedd1SNicolas Ferre 	if (iso7816conf->sc_fi && iso7816conf->sc_di) {
347377fedd1SNicolas Ferre 		fidi = (u64)iso7816conf->sc_fi;
348377fedd1SNicolas Ferre 		do_div(fidi, iso7816conf->sc_di);
349377fedd1SNicolas Ferre 	}
350377fedd1SNicolas Ferre 	return (u32)fidi;
351377fedd1SNicolas Ferre }
352377fedd1SNicolas Ferre 
353377fedd1SNicolas Ferre /* Enable or disable the iso7816 support */
354377fedd1SNicolas Ferre /* Called with interrupts disabled */
355377fedd1SNicolas Ferre static int atmel_config_iso7816(struct uart_port *port,
356377fedd1SNicolas Ferre 				struct serial_iso7816 *iso7816conf)
357377fedd1SNicolas Ferre {
358377fedd1SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
359377fedd1SNicolas Ferre 	unsigned int mode;
360377fedd1SNicolas Ferre 	unsigned int cd, fidi;
361377fedd1SNicolas Ferre 	int ret = 0;
362377fedd1SNicolas Ferre 
363377fedd1SNicolas Ferre 	/* Disable interrupts */
364377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
365377fedd1SNicolas Ferre 
366377fedd1SNicolas Ferre 	mode = atmel_uart_readl(port, ATMEL_US_MR);
367377fedd1SNicolas Ferre 
368377fedd1SNicolas Ferre 	if (iso7816conf->flags & SER_ISO7816_ENABLED) {
369377fedd1SNicolas Ferre 		mode &= ~ATMEL_US_USMODE;
370377fedd1SNicolas Ferre 
371377fedd1SNicolas Ferre 		if (iso7816conf->tg > 255) {
372377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n");
373377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
374377fedd1SNicolas Ferre 			ret = -EINVAL;
375377fedd1SNicolas Ferre 			goto err_out;
376377fedd1SNicolas Ferre 		}
377377fedd1SNicolas Ferre 
378377fedd1SNicolas Ferre 		if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
379377fedd1SNicolas Ferre 		    == SER_ISO7816_T(0)) {
380377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T0 | ATMEL_US_DSNACK;
381377fedd1SNicolas Ferre 		} else if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
382377fedd1SNicolas Ferre 			   == SER_ISO7816_T(1)) {
383377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T1 | ATMEL_US_INACK;
384377fedd1SNicolas Ferre 		} else {
385377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816: Type not supported\n");
386377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
387377fedd1SNicolas Ferre 			ret = -EINVAL;
388377fedd1SNicolas Ferre 			goto err_out;
389377fedd1SNicolas Ferre 		}
390377fedd1SNicolas Ferre 
391377fedd1SNicolas Ferre 		mode &= ~(ATMEL_US_USCLKS | ATMEL_US_NBSTOP | ATMEL_US_PAR);
392377fedd1SNicolas Ferre 
393377fedd1SNicolas Ferre 		/* select mck clock, and output  */
394377fedd1SNicolas Ferre 		mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
395377fedd1SNicolas Ferre 		/* set parity for normal/inverse mode + max iterations */
396377fedd1SNicolas Ferre 		mode |= ATMEL_US_PAR_EVEN | ATMEL_US_NBSTOP_1 | ATMEL_US_MAX_ITER(3);
397377fedd1SNicolas Ferre 
398377fedd1SNicolas Ferre 		cd = atmel_calc_cd(port, iso7816conf);
399377fedd1SNicolas Ferre 		fidi = atmel_calc_fidi(port, iso7816conf);
400377fedd1SNicolas Ferre 		if (fidi == 0) {
401377fedd1SNicolas Ferre 			dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n");
402377fedd1SNicolas Ferre 		} else if (fidi < atmel_port->fidi_min
403377fedd1SNicolas Ferre 			   || fidi > atmel_port->fidi_max) {
404377fedd1SNicolas Ferre 			dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi);
405377fedd1SNicolas Ferre 			memset(iso7816conf, 0, sizeof(struct serial_iso7816));
406377fedd1SNicolas Ferre 			ret = -EINVAL;
407377fedd1SNicolas Ferre 			goto err_out;
408377fedd1SNicolas Ferre 		}
409377fedd1SNicolas Ferre 
410377fedd1SNicolas Ferre 		if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) {
411377fedd1SNicolas Ferre 			/* port not yet in iso7816 mode: store configuration */
412377fedd1SNicolas Ferre 			atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR);
413377fedd1SNicolas Ferre 			atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
414377fedd1SNicolas Ferre 		}
415377fedd1SNicolas Ferre 
416377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg);
417377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_BRGR, cd);
418377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_FIDI, fidi);
419377fedd1SNicolas Ferre 
420377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXEN);
421377fedd1SNicolas Ferre 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION;
422377fedd1SNicolas Ferre 	} else {
423377fedd1SNicolas Ferre 		dev_dbg(port->dev, "Setting UART back to RS232\n");
424377fedd1SNicolas Ferre 		/* back to last RS232 settings */
425377fedd1SNicolas Ferre 		mode = atmel_port->backup_mode;
426377fedd1SNicolas Ferre 		memset(iso7816conf, 0, sizeof(struct serial_iso7816));
427377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, 0);
428377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr);
429377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_FIDI, 0x174);
430377fedd1SNicolas Ferre 
431377fedd1SNicolas Ferre 		if (atmel_use_pdc_tx(port))
432377fedd1SNicolas Ferre 			atmel_port->tx_done_mask = ATMEL_US_ENDTX |
433377fedd1SNicolas Ferre 						   ATMEL_US_TXBUFE;
434377fedd1SNicolas Ferre 		else
435377fedd1SNicolas Ferre 			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
436377fedd1SNicolas Ferre 	}
437377fedd1SNicolas Ferre 
438377fedd1SNicolas Ferre 	port->iso7816 = *iso7816conf;
439377fedd1SNicolas Ferre 
440377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_MR, mode);
441377fedd1SNicolas Ferre 
442377fedd1SNicolas Ferre err_out:
443377fedd1SNicolas Ferre 	/* Enable interrupts */
444377fedd1SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
445377fedd1SNicolas Ferre 
446377fedd1SNicolas Ferre 	return ret;
447377fedd1SNicolas Ferre }
448377fedd1SNicolas Ferre 
449ab4382d2SGreg Kroah-Hartman /*
450ab4382d2SGreg Kroah-Hartman  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
451ab4382d2SGreg Kroah-Hartman  */
452ab4382d2SGreg Kroah-Hartman static u_int atmel_tx_empty(struct uart_port *port)
453ab4382d2SGreg Kroah-Hartman {
454ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
455ea04f82aSRomain Izard 
456ea04f82aSRomain Izard 	if (atmel_port->tx_stopped)
457ea04f82aSRomain Izard 		return TIOCSER_TEMT;
4584e7decdaSCyrille Pitchen 	return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
4594e7decdaSCyrille Pitchen 		TIOCSER_TEMT :
4604e7decdaSCyrille Pitchen 		0;
461ab4382d2SGreg Kroah-Hartman }
462ab4382d2SGreg Kroah-Hartman 
463ab4382d2SGreg Kroah-Hartman /*
464ab4382d2SGreg Kroah-Hartman  * Set state of the modem control output lines
465ab4382d2SGreg Kroah-Hartman  */
466ab4382d2SGreg Kroah-Hartman static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
467ab4382d2SGreg Kroah-Hartman {
468ab4382d2SGreg Kroah-Hartman 	unsigned int control = 0;
4694e7decdaSCyrille Pitchen 	unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
4701cf6e8fcSCyrille Pitchen 	unsigned int rts_paused, rts_ready;
471ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
472ab4382d2SGreg Kroah-Hartman 
4731cf6e8fcSCyrille Pitchen 	/* override mode to RS485 if needed, otherwise keep the current mode */
4741cf6e8fcSCyrille Pitchen 	if (port->rs485.flags & SER_RS485_ENABLED) {
4754e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
4764e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
4771cf6e8fcSCyrille Pitchen 		mode &= ~ATMEL_US_USMODE;
4781cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_RS485;
4791cf6e8fcSCyrille Pitchen 	}
4801cf6e8fcSCyrille Pitchen 
4811cf6e8fcSCyrille Pitchen 	/* set the RTS line state according to the mode */
4821cf6e8fcSCyrille Pitchen 	if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
4831cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4841cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSEN;
4851cf6e8fcSCyrille Pitchen 
4861cf6e8fcSCyrille Pitchen 		/* give the control of the RTS line back to the hardware */
4871cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSDIS;
4881cf6e8fcSCyrille Pitchen 	} else {
4891cf6e8fcSCyrille Pitchen 		/* force RTS line to high level */
4901cf6e8fcSCyrille Pitchen 		rts_paused = ATMEL_US_RTSDIS;
4911cf6e8fcSCyrille Pitchen 
4921cf6e8fcSCyrille Pitchen 		/* force RTS line to low level */
4931cf6e8fcSCyrille Pitchen 		rts_ready = ATMEL_US_RTSEN;
4941cf6e8fcSCyrille Pitchen 	}
4951cf6e8fcSCyrille Pitchen 
496ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_RTS)
4971cf6e8fcSCyrille Pitchen 		control |= rts_ready;
498ab4382d2SGreg Kroah-Hartman 	else
4991cf6e8fcSCyrille Pitchen 		control |= rts_paused;
500ab4382d2SGreg Kroah-Hartman 
501ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_DTR)
502ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTREN;
503ab4382d2SGreg Kroah-Hartman 	else
504ab4382d2SGreg Kroah-Hartman 		control |= ATMEL_US_DTRDIS;
505ab4382d2SGreg Kroah-Hartman 
5064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, control);
507ab4382d2SGreg Kroah-Hartman 
508e0b0baadSRichard Genoud 	mctrl_gpio_set(atmel_port->gpios, mctrl);
509e0b0baadSRichard Genoud 
510ab4382d2SGreg Kroah-Hartman 	/* Local loopback mode? */
5111cf6e8fcSCyrille Pitchen 	mode &= ~ATMEL_US_CHMODE;
512ab4382d2SGreg Kroah-Hartman 	if (mctrl & TIOCM_LOOP)
513ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_LOC_LOOP;
514ab4382d2SGreg Kroah-Hartman 	else
515ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHMODE_NORMAL;
516ab4382d2SGreg Kroah-Hartman 
5174e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_MR, mode);
518ab4382d2SGreg Kroah-Hartman }
519ab4382d2SGreg Kroah-Hartman 
520ab4382d2SGreg Kroah-Hartman /*
521ab4382d2SGreg Kroah-Hartman  * Get state of the modem control input lines
522ab4382d2SGreg Kroah-Hartman  */
523ab4382d2SGreg Kroah-Hartman static u_int atmel_get_mctrl(struct uart_port *port)
524ab4382d2SGreg Kroah-Hartman {
525e0b0baadSRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
526e0b0baadSRichard Genoud 	unsigned int ret = 0, status;
527ab4382d2SGreg Kroah-Hartman 
5284e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
529ab4382d2SGreg Kroah-Hartman 
530ab4382d2SGreg Kroah-Hartman 	/*
531ab4382d2SGreg Kroah-Hartman 	 * The control signals are active low.
532ab4382d2SGreg Kroah-Hartman 	 */
533ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DCD))
534ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CD;
535ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_CTS))
536ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_CTS;
537ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_DSR))
538ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_DSR;
539ab4382d2SGreg Kroah-Hartman 	if (!(status & ATMEL_US_RI))
540ab4382d2SGreg Kroah-Hartman 		ret |= TIOCM_RI;
541ab4382d2SGreg Kroah-Hartman 
542e0b0baadSRichard Genoud 	return mctrl_gpio_get(atmel_port->gpios, &ret);
543ab4382d2SGreg Kroah-Hartman }
544ab4382d2SGreg Kroah-Hartman 
545ab4382d2SGreg Kroah-Hartman /*
546ab4382d2SGreg Kroah-Hartman  * Stop transmitting.
547ab4382d2SGreg Kroah-Hartman  */
548ab4382d2SGreg Kroah-Hartman static void atmel_stop_tx(struct uart_port *port)
549ab4382d2SGreg Kroah-Hartman {
550ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
551ab4382d2SGreg Kroah-Hartman 
55264e22ebeSElen Song 	if (atmel_use_pdc_tx(port)) {
553ab4382d2SGreg Kroah-Hartman 		/* disable PDC transmit */
5544e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
555ab4382d2SGreg Kroah-Hartman 	}
55689d82324SRichard Genoud 
55789d82324SRichard Genoud 	/*
55889d82324SRichard Genoud 	 * Disable the transmitter.
55989d82324SRichard Genoud 	 * This is mandatory when DMA is used, otherwise the DMA buffer
56089d82324SRichard Genoud 	 * is fully transmitted.
56189d82324SRichard Genoud 	 */
56289d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
563ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
56489d82324SRichard Genoud 
565ab4382d2SGreg Kroah-Hartman 	/* Disable interrupts */
5664e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
567ab4382d2SGreg Kroah-Hartman 
568f3040983SRazvan Stefanescu 	if (atmel_uart_is_half_duplex(port))
569ab4382d2SGreg Kroah-Hartman 		atmel_start_rx(port);
570f3040983SRazvan Stefanescu 
571ab4382d2SGreg Kroah-Hartman }
572ab4382d2SGreg Kroah-Hartman 
573ab4382d2SGreg Kroah-Hartman /*
574ab4382d2SGreg Kroah-Hartman  * Start transmitting.
575ab4382d2SGreg Kroah-Hartman  */
576ab4382d2SGreg Kroah-Hartman static void atmel_start_tx(struct uart_port *port)
577ab4382d2SGreg Kroah-Hartman {
578ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
579ab4382d2SGreg Kroah-Hartman 
5800058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
5810058f087SAlexandre Belloni 				       & ATMEL_PDC_TXTEN))
582ab4382d2SGreg Kroah-Hartman 		/* The transmitter is already running.  Yes, we
583ab4382d2SGreg Kroah-Hartman 		   really need this.*/
584ab4382d2SGreg Kroah-Hartman 		return;
585ab4382d2SGreg Kroah-Hartman 
5860058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
587f3040983SRazvan Stefanescu 		if (atmel_uart_is_half_duplex(port))
588ab4382d2SGreg Kroah-Hartman 			atmel_stop_rx(port);
589ab4382d2SGreg Kroah-Hartman 
5900058f087SAlexandre Belloni 	if (atmel_use_pdc_tx(port))
591ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
5924e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
5930058f087SAlexandre Belloni 
594ab4382d2SGreg Kroah-Hartman 	/* Enable interrupts */
5954e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
59689d82324SRichard Genoud 
59789d82324SRichard Genoud 	/* re-enable the transmitter */
59889d82324SRichard Genoud 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
599ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
600ab4382d2SGreg Kroah-Hartman }
601ab4382d2SGreg Kroah-Hartman 
602ab4382d2SGreg Kroah-Hartman /*
603ab4382d2SGreg Kroah-Hartman  * start receiving - port is in process of being opened.
604ab4382d2SGreg Kroah-Hartman  */
605ab4382d2SGreg Kroah-Hartman static void atmel_start_rx(struct uart_port *port)
606ab4382d2SGreg Kroah-Hartman {
6074e7decdaSCyrille Pitchen 	/* reset status and receiver */
6084e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
609ab4382d2SGreg Kroah-Hartman 
6104e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
61157c36868SSiftar, Gabe 
61264e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
613ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
6144e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
6154e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
616ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
6174e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
618ab4382d2SGreg Kroah-Hartman 	} else {
6194e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
620ab4382d2SGreg Kroah-Hartman 	}
621ab4382d2SGreg Kroah-Hartman }
622ab4382d2SGreg Kroah-Hartman 
623ab4382d2SGreg Kroah-Hartman /*
624ab4382d2SGreg Kroah-Hartman  * Stop receiving - port is in process of being closed.
625ab4382d2SGreg Kroah-Hartman  */
626ab4382d2SGreg Kroah-Hartman static void atmel_stop_rx(struct uart_port *port)
627ab4382d2SGreg Kroah-Hartman {
6284e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
62957c36868SSiftar, Gabe 
63064e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
631ab4382d2SGreg Kroah-Hartman 		/* disable PDC receive */
6324e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
6334e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
6344e7decdaSCyrille Pitchen 				  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
635ab4382d2SGreg Kroah-Hartman 				  port->read_status_mask);
636ab4382d2SGreg Kroah-Hartman 	} else {
6374e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
638ab4382d2SGreg Kroah-Hartman 	}
639ab4382d2SGreg Kroah-Hartman }
640ab4382d2SGreg Kroah-Hartman 
641ab4382d2SGreg Kroah-Hartman /*
642ab4382d2SGreg Kroah-Hartman  * Enable modem status interrupts
643ab4382d2SGreg Kroah-Hartman  */
644ab4382d2SGreg Kroah-Hartman static void atmel_enable_ms(struct uart_port *port)
645ab4382d2SGreg Kroah-Hartman {
646ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
647ab5e4e41SRichard Genoud 	uint32_t ier = 0;
648ab5e4e41SRichard Genoud 
649ab5e4e41SRichard Genoud 	/*
650ab5e4e41SRichard Genoud 	 * Interrupt should not be enabled twice
651ab5e4e41SRichard Genoud 	 */
652ab5e4e41SRichard Genoud 	if (atmel_port->ms_irq_enabled)
653ab5e4e41SRichard Genoud 		return;
654ab5e4e41SRichard Genoud 
655ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = true;
656ab5e4e41SRichard Genoud 
65718dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
658ab5e4e41SRichard Genoud 		ier |= ATMEL_US_CTSIC;
659ab5e4e41SRichard Genoud 
66018dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
661ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DSRIC;
662ab5e4e41SRichard Genoud 
66318dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
664ab5e4e41SRichard Genoud 		ier |= ATMEL_US_RIIC;
665ab5e4e41SRichard Genoud 
66618dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
667ab5e4e41SRichard Genoud 		ier |= ATMEL_US_DCDIC;
668ab5e4e41SRichard Genoud 
6694e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ier);
67018dfef9cSUwe Kleine-König 
67118dfef9cSUwe Kleine-König 	mctrl_gpio_enable_ms(atmel_port->gpios);
672ab4382d2SGreg Kroah-Hartman }
673ab4382d2SGreg Kroah-Hartman 
674ab4382d2SGreg Kroah-Hartman /*
67535b675b9SRichard Genoud  * Disable modem status interrupts
67635b675b9SRichard Genoud  */
67735b675b9SRichard Genoud static void atmel_disable_ms(struct uart_port *port)
67835b675b9SRichard Genoud {
67935b675b9SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
68035b675b9SRichard Genoud 	uint32_t idr = 0;
68135b675b9SRichard Genoud 
68235b675b9SRichard Genoud 	/*
68335b675b9SRichard Genoud 	 * Interrupt should not be disabled twice
68435b675b9SRichard Genoud 	 */
68535b675b9SRichard Genoud 	if (!atmel_port->ms_irq_enabled)
68635b675b9SRichard Genoud 		return;
68735b675b9SRichard Genoud 
68835b675b9SRichard Genoud 	atmel_port->ms_irq_enabled = false;
68935b675b9SRichard Genoud 
69018dfef9cSUwe Kleine-König 	mctrl_gpio_disable_ms(atmel_port->gpios);
69118dfef9cSUwe Kleine-König 
69218dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
69335b675b9SRichard Genoud 		idr |= ATMEL_US_CTSIC;
69435b675b9SRichard Genoud 
69518dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
69635b675b9SRichard Genoud 		idr |= ATMEL_US_DSRIC;
69735b675b9SRichard Genoud 
69818dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
69935b675b9SRichard Genoud 		idr |= ATMEL_US_RIIC;
70035b675b9SRichard Genoud 
70118dfef9cSUwe Kleine-König 	if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
70235b675b9SRichard Genoud 		idr |= ATMEL_US_DCDIC;
70335b675b9SRichard Genoud 
7044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, idr);
70535b675b9SRichard Genoud }
70635b675b9SRichard Genoud 
70735b675b9SRichard Genoud /*
708ab4382d2SGreg Kroah-Hartman  * Control the transmission of a break signal
709ab4382d2SGreg Kroah-Hartman  */
710ab4382d2SGreg Kroah-Hartman static void atmel_break_ctl(struct uart_port *port, int break_state)
711ab4382d2SGreg Kroah-Hartman {
712ab4382d2SGreg Kroah-Hartman 	if (break_state != 0)
7134e7decdaSCyrille Pitchen 		/* start break */
7144e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
715ab4382d2SGreg Kroah-Hartman 	else
7164e7decdaSCyrille Pitchen 		/* stop break */
7174e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
718ab4382d2SGreg Kroah-Hartman }
719ab4382d2SGreg Kroah-Hartman 
720ab4382d2SGreg Kroah-Hartman /*
721ab4382d2SGreg Kroah-Hartman  * Stores the incoming character in the ring buffer
722ab4382d2SGreg Kroah-Hartman  */
723ab4382d2SGreg Kroah-Hartman static void
724ab4382d2SGreg Kroah-Hartman atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
725ab4382d2SGreg Kroah-Hartman 		     unsigned int ch)
726ab4382d2SGreg Kroah-Hartman {
727ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
728ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
729ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_char *c;
730ab4382d2SGreg Kroah-Hartman 
731ab4382d2SGreg Kroah-Hartman 	if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
732ab4382d2SGreg Kroah-Hartman 		/* Buffer overflow, ignore char */
733ab4382d2SGreg Kroah-Hartman 		return;
734ab4382d2SGreg Kroah-Hartman 
735ab4382d2SGreg Kroah-Hartman 	c = &((struct atmel_uart_char *)ring->buf)[ring->head];
736ab4382d2SGreg Kroah-Hartman 	c->status	= status;
737ab4382d2SGreg Kroah-Hartman 	c->ch		= ch;
738ab4382d2SGreg Kroah-Hartman 
739ab4382d2SGreg Kroah-Hartman 	/* Make sure the character is stored before we update head. */
740ab4382d2SGreg Kroah-Hartman 	smp_wmb();
741ab4382d2SGreg Kroah-Hartman 
742ab4382d2SGreg Kroah-Hartman 	ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
743ab4382d2SGreg Kroah-Hartman }
744ab4382d2SGreg Kroah-Hartman 
745ab4382d2SGreg Kroah-Hartman /*
746ab4382d2SGreg Kroah-Hartman  * Deal with parity, framing and overrun errors.
747ab4382d2SGreg Kroah-Hartman  */
748ab4382d2SGreg Kroah-Hartman static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
749ab4382d2SGreg Kroah-Hartman {
750ab4382d2SGreg Kroah-Hartman 	/* clear error */
7514e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
752ab4382d2SGreg Kroah-Hartman 
753ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_RXBRK) {
754ab4382d2SGreg Kroah-Hartman 		/* ignore side-effect */
755ab4382d2SGreg Kroah-Hartman 		status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
756ab4382d2SGreg Kroah-Hartman 		port->icount.brk++;
757ab4382d2SGreg Kroah-Hartman 	}
758ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_PARE)
759ab4382d2SGreg Kroah-Hartman 		port->icount.parity++;
760ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_FRAME)
761ab4382d2SGreg Kroah-Hartman 		port->icount.frame++;
762ab4382d2SGreg Kroah-Hartman 	if (status & ATMEL_US_OVRE)
763ab4382d2SGreg Kroah-Hartman 		port->icount.overrun++;
764ab4382d2SGreg Kroah-Hartman }
765ab4382d2SGreg Kroah-Hartman 
766ab4382d2SGreg Kroah-Hartman /*
767ab4382d2SGreg Kroah-Hartman  * Characters received (called from interrupt handler)
768ab4382d2SGreg Kroah-Hartman  */
769ab4382d2SGreg Kroah-Hartman static void atmel_rx_chars(struct uart_port *port)
770ab4382d2SGreg Kroah-Hartman {
771ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
772ab4382d2SGreg Kroah-Hartman 	unsigned int status, ch;
773ab4382d2SGreg Kroah-Hartman 
7744e7decdaSCyrille Pitchen 	status = atmel_uart_readl(port, ATMEL_US_CSR);
775ab4382d2SGreg Kroah-Hartman 	while (status & ATMEL_US_RXRDY) {
776a6499435SCyrille Pitchen 		ch = atmel_uart_read_char(port);
777ab4382d2SGreg Kroah-Hartman 
778ab4382d2SGreg Kroah-Hartman 		/*
779ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
780ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
781ab4382d2SGreg Kroah-Hartman 		 */
782ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
783ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK)
784ab4382d2SGreg Kroah-Hartman 			     || atmel_port->break_active)) {
785ab4382d2SGreg Kroah-Hartman 
786ab4382d2SGreg Kroah-Hartman 			/* clear error */
7874e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
788ab4382d2SGreg Kroah-Hartman 
789ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK
790ab4382d2SGreg Kroah-Hartman 			    && !atmel_port->break_active) {
791ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 1;
7924e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IER,
7934e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
794ab4382d2SGreg Kroah-Hartman 			} else {
795ab4382d2SGreg Kroah-Hartman 				/*
796ab4382d2SGreg Kroah-Hartman 				 * This is either the end-of-break
797ab4382d2SGreg Kroah-Hartman 				 * condition or we've received at
798ab4382d2SGreg Kroah-Hartman 				 * least one character without RXBRK
799ab4382d2SGreg Kroah-Hartman 				 * being set. In both cases, the next
800ab4382d2SGreg Kroah-Hartman 				 * RXBRK will indicate start-of-break.
801ab4382d2SGreg Kroah-Hartman 				 */
8024e7decdaSCyrille Pitchen 				atmel_uart_writel(port, ATMEL_US_IDR,
8034e7decdaSCyrille Pitchen 						  ATMEL_US_RXBRK);
804ab4382d2SGreg Kroah-Hartman 				status &= ~ATMEL_US_RXBRK;
805ab4382d2SGreg Kroah-Hartman 				atmel_port->break_active = 0;
806ab4382d2SGreg Kroah-Hartman 			}
807ab4382d2SGreg Kroah-Hartman 		}
808ab4382d2SGreg Kroah-Hartman 
809ab4382d2SGreg Kroah-Hartman 		atmel_buffer_rx_char(port, status, ch);
8104e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
811ab4382d2SGreg Kroah-Hartman 	}
812ab4382d2SGreg Kroah-Hartman 
81398f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
814ab4382d2SGreg Kroah-Hartman }
815ab4382d2SGreg Kroah-Hartman 
816ab4382d2SGreg Kroah-Hartman /*
817ab4382d2SGreg Kroah-Hartman  * Transmit characters (called from tasklet with TXRDY interrupt
818ab4382d2SGreg Kroah-Hartman  * disabled)
819ab4382d2SGreg Kroah-Hartman  */
820ab4382d2SGreg Kroah-Hartman static void atmel_tx_chars(struct uart_port *port)
821ab4382d2SGreg Kroah-Hartman {
822ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
823ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
824ab4382d2SGreg Kroah-Hartman 
8254e7decdaSCyrille Pitchen 	if (port->x_char &&
8264e7decdaSCyrille Pitchen 	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
827a6499435SCyrille Pitchen 		atmel_uart_write_char(port, port->x_char);
828ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
829ab4382d2SGreg Kroah-Hartman 		port->x_char = 0;
830ab4382d2SGreg Kroah-Hartman 	}
831ab4382d2SGreg Kroah-Hartman 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
832ab4382d2SGreg Kroah-Hartman 		return;
833ab4382d2SGreg Kroah-Hartman 
8344e7decdaSCyrille Pitchen 	while (atmel_uart_readl(port, ATMEL_US_CSR) &
8354e7decdaSCyrille Pitchen 	       atmel_port->tx_done_mask) {
836a6499435SCyrille Pitchen 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
837ab4382d2SGreg Kroah-Hartman 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
838ab4382d2SGreg Kroah-Hartman 		port->icount.tx++;
839ab4382d2SGreg Kroah-Hartman 		if (uart_circ_empty(xmit))
840ab4382d2SGreg Kroah-Hartman 			break;
841ab4382d2SGreg Kroah-Hartman 	}
842ab4382d2SGreg Kroah-Hartman 
843ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
844ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
845ab4382d2SGreg Kroah-Hartman 
846ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit))
847ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
8484e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
8494e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
850ab4382d2SGreg Kroah-Hartman }
851ab4382d2SGreg Kroah-Hartman 
85208f738beSElen Song static void atmel_complete_tx_dma(void *arg)
85308f738beSElen Song {
85408f738beSElen Song 	struct atmel_uart_port *atmel_port = arg;
85508f738beSElen Song 	struct uart_port *port = &atmel_port->uart;
85608f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
85708f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
85808f738beSElen Song 	unsigned long flags;
85908f738beSElen Song 
86008f738beSElen Song 	spin_lock_irqsave(&port->lock, flags);
86108f738beSElen Song 
86208f738beSElen Song 	if (chan)
86308f738beSElen Song 		dmaengine_terminate_all(chan);
8645f258b3eSCyrille Pitchen 	xmit->tail += atmel_port->tx_len;
86508f738beSElen Song 	xmit->tail &= UART_XMIT_SIZE - 1;
86608f738beSElen Song 
8675f258b3eSCyrille Pitchen 	port->icount.tx += atmel_port->tx_len;
86808f738beSElen Song 
86908f738beSElen Song 	spin_lock_irq(&atmel_port->lock_tx);
87008f738beSElen Song 	async_tx_ack(atmel_port->desc_tx);
87108f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
87208f738beSElen Song 	atmel_port->desc_tx = NULL;
87308f738beSElen Song 	spin_unlock_irq(&atmel_port->lock_tx);
87408f738beSElen Song 
87508f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
87608f738beSElen Song 		uart_write_wakeup(port);
87708f738beSElen Song 
8781842dc2eSCyrille Pitchen 	/*
8791842dc2eSCyrille Pitchen 	 * xmit is a circular buffer so, if we have just send data from
8801842dc2eSCyrille Pitchen 	 * xmit->tail to the end of xmit->buf, now we have to transmit the
8811842dc2eSCyrille Pitchen 	 * remaining data from the beginning of xmit->buf to xmit->head.
8821842dc2eSCyrille Pitchen 	 */
88308f738beSElen Song 	if (!uart_circ_empty(xmit))
88498f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
885f3040983SRazvan Stefanescu 	else if (atmel_uart_is_half_duplex(port)) {
88669646d7aSRazvan Stefanescu 		/*
88769646d7aSRazvan Stefanescu 		 * DMA done, re-enable TXEMPTY and signal that we can stop
88869646d7aSRazvan Stefanescu 		 * TX and start RX for RS485
88969646d7aSRazvan Stefanescu 		 */
89069646d7aSRazvan Stefanescu 		atmel_port->hd_start_rx = true;
89169646d7aSRazvan Stefanescu 		atmel_uart_writel(port, ATMEL_US_IER,
89269646d7aSRazvan Stefanescu 				  atmel_port->tx_done_mask);
893b389f173SRichard Genoud 	}
89408f738beSElen Song 
89508f738beSElen Song 	spin_unlock_irqrestore(&port->lock, flags);
89608f738beSElen Song }
89708f738beSElen Song 
89808f738beSElen Song static void atmel_release_tx_dma(struct uart_port *port)
89908f738beSElen Song {
90008f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
90108f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
90208f738beSElen Song 
90308f738beSElen Song 	if (chan) {
90408f738beSElen Song 		dmaengine_terminate_all(chan);
90508f738beSElen Song 		dma_release_channel(chan);
90608f738beSElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
90748479148SWolfram Sang 				DMA_TO_DEVICE);
90808f738beSElen Song 	}
90908f738beSElen Song 
91008f738beSElen Song 	atmel_port->desc_tx = NULL;
91108f738beSElen Song 	atmel_port->chan_tx = NULL;
91208f738beSElen Song 	atmel_port->cookie_tx = -EINVAL;
91308f738beSElen Song }
91408f738beSElen Song 
91508f738beSElen Song /*
91608f738beSElen Song  * Called from tasklet with TXRDY interrupt is disabled.
91708f738beSElen Song  */
91808f738beSElen Song static void atmel_tx_dma(struct uart_port *port)
91908f738beSElen Song {
92008f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
92108f738beSElen Song 	struct circ_buf *xmit = &port->state->xmit;
92208f738beSElen Song 	struct dma_chan *chan = atmel_port->chan_tx;
92308f738beSElen Song 	struct dma_async_tx_descriptor *desc;
9245f258b3eSCyrille Pitchen 	struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
9255f258b3eSCyrille Pitchen 	unsigned int tx_len, part1_len, part2_len, sg_len;
9265f258b3eSCyrille Pitchen 	dma_addr_t phys_addr;
92708f738beSElen Song 
92808f738beSElen Song 	/* Make sure we have an idle channel */
92908f738beSElen Song 	if (atmel_port->desc_tx != NULL)
93008f738beSElen Song 		return;
93108f738beSElen Song 
93208f738beSElen Song 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
93308f738beSElen Song 		/*
93408f738beSElen Song 		 * DMA is idle now.
93508f738beSElen Song 		 * Port xmit buffer is already mapped,
93608f738beSElen Song 		 * and it is one page... Just adjust
93708f738beSElen Song 		 * offsets and lengths. Since it is a circular buffer,
93808f738beSElen Song 		 * we have to transmit till the end, and then the rest.
93908f738beSElen Song 		 * Take the port lock to get a
94008f738beSElen Song 		 * consistent xmit buffer state.
94108f738beSElen Song 		 */
9425f258b3eSCyrille Pitchen 		tx_len = CIRC_CNT_TO_END(xmit->head,
94308f738beSElen Song 					 xmit->tail,
94408f738beSElen Song 					 UART_XMIT_SIZE);
9455f258b3eSCyrille Pitchen 
9465f258b3eSCyrille Pitchen 		if (atmel_port->fifo_size) {
9475f258b3eSCyrille Pitchen 			/* multi data mode */
9485f258b3eSCyrille Pitchen 			part1_len = (tx_len & ~0x3); /* DWORD access */
9495f258b3eSCyrille Pitchen 			part2_len = (tx_len & 0x3); /* BYTE access */
9505f258b3eSCyrille Pitchen 		} else {
9515f258b3eSCyrille Pitchen 			/* single data (legacy) mode */
9525f258b3eSCyrille Pitchen 			part1_len = 0;
9535f258b3eSCyrille Pitchen 			part2_len = tx_len; /* BYTE access only */
9545f258b3eSCyrille Pitchen 		}
9555f258b3eSCyrille Pitchen 
9565f258b3eSCyrille Pitchen 		sg_init_table(sgl, 2);
9575f258b3eSCyrille Pitchen 		sg_len = 0;
9585f258b3eSCyrille Pitchen 		phys_addr = sg_dma_address(sg_tx) + xmit->tail;
9595f258b3eSCyrille Pitchen 		if (part1_len) {
9605f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
9615f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
9625f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part1_len;
9635f258b3eSCyrille Pitchen 
9645f258b3eSCyrille Pitchen 			phys_addr += part1_len;
9655f258b3eSCyrille Pitchen 		}
9665f258b3eSCyrille Pitchen 
9675f258b3eSCyrille Pitchen 		if (part2_len) {
9685f258b3eSCyrille Pitchen 			sg = &sgl[sg_len++];
9695f258b3eSCyrille Pitchen 			sg_dma_address(sg) = phys_addr;
9705f258b3eSCyrille Pitchen 			sg_dma_len(sg) = part2_len;
9715f258b3eSCyrille Pitchen 		}
9725f258b3eSCyrille Pitchen 
9735f258b3eSCyrille Pitchen 		/*
9745f258b3eSCyrille Pitchen 		 * save tx_len so atmel_complete_tx_dma() will increase
9755f258b3eSCyrille Pitchen 		 * xmit->tail correctly
9765f258b3eSCyrille Pitchen 		 */
9775f258b3eSCyrille Pitchen 		atmel_port->tx_len = tx_len;
97808f738beSElen Song 
97908f738beSElen Song 		desc = dmaengine_prep_slave_sg(chan,
9805f258b3eSCyrille Pitchen 					       sgl,
9815f258b3eSCyrille Pitchen 					       sg_len,
98208f738beSElen Song 					       DMA_MEM_TO_DEV,
98308f738beSElen Song 					       DMA_PREP_INTERRUPT |
98408f738beSElen Song 					       DMA_CTRL_ACK);
98508f738beSElen Song 		if (!desc) {
98608f738beSElen Song 			dev_err(port->dev, "Failed to send via dma!\n");
98708f738beSElen Song 			return;
98808f738beSElen Song 		}
98908f738beSElen Song 
9905f258b3eSCyrille Pitchen 		dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
99108f738beSElen Song 
99208f738beSElen Song 		atmel_port->desc_tx = desc;
99308f738beSElen Song 		desc->callback = atmel_complete_tx_dma;
99408f738beSElen Song 		desc->callback_param = atmel_port;
99508f738beSElen Song 		atmel_port->cookie_tx = dmaengine_submit(desc);
99608f738beSElen Song 	}
99708f738beSElen Song 
99808f738beSElen Song 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
99908f738beSElen Song 		uart_write_wakeup(port);
100008f738beSElen Song }
100108f738beSElen Song 
100208f738beSElen Song static int atmel_prepare_tx_dma(struct uart_port *port)
100308f738beSElen Song {
100408f738beSElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1005c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
100608f738beSElen Song 	dma_cap_mask_t		mask;
100708f738beSElen Song 	struct dma_slave_config config;
100808f738beSElen Song 	int ret, nent;
100908f738beSElen Song 
101008f738beSElen Song 	dma_cap_zero(mask);
101108f738beSElen Song 	dma_cap_set(DMA_SLAVE, mask);
101208f738beSElen Song 
1013c24d2531SRadu Pirea 	atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx");
101408f738beSElen Song 	if (atmel_port->chan_tx == NULL)
101508f738beSElen Song 		goto chan_err;
101608f738beSElen Song 	dev_info(port->dev, "using %s for tx DMA transfers\n",
101708f738beSElen Song 		dma_chan_name(atmel_port->chan_tx));
101808f738beSElen Song 
101908f738beSElen Song 	spin_lock_init(&atmel_port->lock_tx);
102008f738beSElen Song 	sg_init_table(&atmel_port->sg_tx, 1);
102108f738beSElen Song 	/* UART circular tx buffer is an aligned page. */
10222c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
102308f738beSElen Song 	sg_set_page(&atmel_port->sg_tx,
102408f738beSElen Song 			virt_to_page(port->state->xmit.buf),
102508f738beSElen Song 			UART_XMIT_SIZE,
10262b5cf14bSGeliang Tang 			offset_in_page(port->state->xmit.buf));
102708f738beSElen Song 	nent = dma_map_sg(port->dev,
102808f738beSElen Song 				&atmel_port->sg_tx,
102908f738beSElen Song 				1,
103048479148SWolfram Sang 				DMA_TO_DEVICE);
103108f738beSElen Song 
103208f738beSElen Song 	if (!nent) {
103308f738beSElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
103408f738beSElen Song 		goto chan_err;
103508f738beSElen Song 	} else {
1036c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
103708f738beSElen Song 			sg_dma_len(&atmel_port->sg_tx),
103808f738beSElen Song 			port->state->xmit.buf,
1039c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_tx));
104008f738beSElen Song 	}
104108f738beSElen Song 
104208f738beSElen Song 	/* Configure the slave DMA */
104308f738beSElen Song 	memset(&config, 0, sizeof(config));
104408f738beSElen Song 	config.direction = DMA_MEM_TO_DEV;
10455f258b3eSCyrille Pitchen 	config.dst_addr_width = (atmel_port->fifo_size) ?
10465f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_4_BYTES :
10475f258b3eSCyrille Pitchen 				DMA_SLAVE_BUSWIDTH_1_BYTE;
104808f738beSElen Song 	config.dst_addr = port->mapbase + ATMEL_US_THR;
1049a8d4e016SLudovic Desroches 	config.dst_maxburst = 1;
105008f738beSElen Song 
10515483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_tx,
10525483c10eSMaxime Ripard 				     &config);
105308f738beSElen Song 	if (ret) {
105408f738beSElen Song 		dev_err(port->dev, "DMA tx slave configuration failed\n");
105508f738beSElen Song 		goto chan_err;
105608f738beSElen Song 	}
105708f738beSElen Song 
105808f738beSElen Song 	return 0;
105908f738beSElen Song 
106008f738beSElen Song chan_err:
106108f738beSElen Song 	dev_err(port->dev, "TX channel not available, switch to pio\n");
106208f738beSElen Song 	atmel_port->use_dma_tx = 0;
106308f738beSElen Song 	if (atmel_port->chan_tx)
106408f738beSElen Song 		atmel_release_tx_dma(port);
106508f738beSElen Song 	return -EINVAL;
106608f738beSElen Song }
106708f738beSElen Song 
106834df42f5SElen Song static void atmel_complete_rx_dma(void *arg)
106934df42f5SElen Song {
107034df42f5SElen Song 	struct uart_port *port = arg;
107134df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
107234df42f5SElen Song 
107398f2082cSNicolas Ferre 	atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
107434df42f5SElen Song }
107534df42f5SElen Song 
107634df42f5SElen Song static void atmel_release_rx_dma(struct uart_port *port)
107734df42f5SElen Song {
107834df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
107934df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
108034df42f5SElen Song 
108134df42f5SElen Song 	if (chan) {
108234df42f5SElen Song 		dmaengine_terminate_all(chan);
108334df42f5SElen Song 		dma_release_channel(chan);
108434df42f5SElen Song 		dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
108548479148SWolfram Sang 				DMA_FROM_DEVICE);
108634df42f5SElen Song 	}
108734df42f5SElen Song 
108834df42f5SElen Song 	atmel_port->desc_rx = NULL;
108934df42f5SElen Song 	atmel_port->chan_rx = NULL;
109034df42f5SElen Song 	atmel_port->cookie_rx = -EINVAL;
109134df42f5SElen Song }
109234df42f5SElen Song 
109334df42f5SElen Song static void atmel_rx_from_dma(struct uart_port *port)
109434df42f5SElen Song {
109534df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
109666f37aafSCyrille Pitchen 	struct tty_port *tport = &port->state->port;
109734df42f5SElen Song 	struct circ_buf *ring = &atmel_port->rx_ring;
109834df42f5SElen Song 	struct dma_chan *chan = atmel_port->chan_rx;
109934df42f5SElen Song 	struct dma_tx_state state;
110034df42f5SElen Song 	enum dma_status dmastat;
110166f37aafSCyrille Pitchen 	size_t count;
110234df42f5SElen Song 
110334df42f5SElen Song 
110434df42f5SElen Song 	/* Reset the UART timeout early so that we don't miss one */
11054e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
110634df42f5SElen Song 	dmastat = dmaengine_tx_status(chan,
110734df42f5SElen Song 				atmel_port->cookie_rx,
110834df42f5SElen Song 				&state);
110934df42f5SElen Song 	/* Restart a new tasklet if DMA status is error */
111034df42f5SElen Song 	if (dmastat == DMA_ERROR) {
111134df42f5SElen Song 		dev_dbg(port->dev, "Get residue error, restart tasklet\n");
11124e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
111398f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
111434df42f5SElen Song 		return;
111534df42f5SElen Song 	}
111666f37aafSCyrille Pitchen 
111766f37aafSCyrille Pitchen 	/* CPU claims ownership of RX DMA buffer */
111866f37aafSCyrille Pitchen 	dma_sync_sg_for_cpu(port->dev,
111966f37aafSCyrille Pitchen 			    &atmel_port->sg_rx,
112066f37aafSCyrille Pitchen 			    1,
1121485819b5SCyrille Pitchen 			    DMA_FROM_DEVICE);
112234df42f5SElen Song 
112334df42f5SElen Song 	/*
112466f37aafSCyrille Pitchen 	 * ring->head points to the end of data already written by the DMA.
112566f37aafSCyrille Pitchen 	 * ring->tail points to the beginning of data to be read by the
112666f37aafSCyrille Pitchen 	 * framework.
112766f37aafSCyrille Pitchen 	 * The current transfer size should not be larger than the dma buffer
112866f37aafSCyrille Pitchen 	 * length.
112934df42f5SElen Song 	 */
113066f37aafSCyrille Pitchen 	ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
113166f37aafSCyrille Pitchen 	BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
113266f37aafSCyrille Pitchen 	/*
113366f37aafSCyrille Pitchen 	 * At this point ring->head may point to the first byte right after the
113466f37aafSCyrille Pitchen 	 * last byte of the dma buffer:
113566f37aafSCyrille Pitchen 	 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
113666f37aafSCyrille Pitchen 	 *
113766f37aafSCyrille Pitchen 	 * However ring->tail must always points inside the dma buffer:
113866f37aafSCyrille Pitchen 	 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
113966f37aafSCyrille Pitchen 	 *
114066f37aafSCyrille Pitchen 	 * Since we use a ring buffer, we have to handle the case
114166f37aafSCyrille Pitchen 	 * where head is lower than tail. In such a case, we first read from
114266f37aafSCyrille Pitchen 	 * tail to the end of the buffer then reset tail.
114366f37aafSCyrille Pitchen 	 */
114466f37aafSCyrille Pitchen 	if (ring->head < ring->tail) {
114566f37aafSCyrille Pitchen 		count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
114634df42f5SElen Song 
114766f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
114866f37aafSCyrille Pitchen 		ring->tail = 0;
114934df42f5SElen Song 		port->icount.rx += count;
115034df42f5SElen Song 	}
115134df42f5SElen Song 
115266f37aafSCyrille Pitchen 	/* Finally we read data from tail to head */
115366f37aafSCyrille Pitchen 	if (ring->tail < ring->head) {
115466f37aafSCyrille Pitchen 		count = ring->head - ring->tail;
115566f37aafSCyrille Pitchen 
115666f37aafSCyrille Pitchen 		tty_insert_flip_string(tport, ring->buf + ring->tail, count);
115766f37aafSCyrille Pitchen 		/* Wrap ring->head if needed */
115866f37aafSCyrille Pitchen 		if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
115966f37aafSCyrille Pitchen 			ring->head = 0;
116066f37aafSCyrille Pitchen 		ring->tail = ring->head;
116166f37aafSCyrille Pitchen 		port->icount.rx += count;
116266f37aafSCyrille Pitchen 	}
116366f37aafSCyrille Pitchen 
116466f37aafSCyrille Pitchen 	/* USART retreives ownership of RX DMA buffer */
116566f37aafSCyrille Pitchen 	dma_sync_sg_for_device(port->dev,
116666f37aafSCyrille Pitchen 			       &atmel_port->sg_rx,
116766f37aafSCyrille Pitchen 			       1,
1168485819b5SCyrille Pitchen 			       DMA_FROM_DEVICE);
116966f37aafSCyrille Pitchen 
117066f37aafSCyrille Pitchen 	/*
117166f37aafSCyrille Pitchen 	 * Drop the lock here since it might end up calling
117266f37aafSCyrille Pitchen 	 * uart_start(), which takes the lock.
117366f37aafSCyrille Pitchen 	 */
117466f37aafSCyrille Pitchen 	spin_unlock(&port->lock);
117566f37aafSCyrille Pitchen 	tty_flip_buffer_push(tport);
117666f37aafSCyrille Pitchen 	spin_lock(&port->lock);
117766f37aafSCyrille Pitchen 
11784e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
117934df42f5SElen Song }
118034df42f5SElen Song 
118134df42f5SElen Song static int atmel_prepare_rx_dma(struct uart_port *port)
118234df42f5SElen Song {
118334df42f5SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1184c24d2531SRadu Pirea 	struct device *mfd_dev = port->dev->parent;
118534df42f5SElen Song 	struct dma_async_tx_descriptor *desc;
118634df42f5SElen Song 	dma_cap_mask_t		mask;
118734df42f5SElen Song 	struct dma_slave_config config;
118834df42f5SElen Song 	struct circ_buf		*ring;
118934df42f5SElen Song 	int ret, nent;
119034df42f5SElen Song 
119134df42f5SElen Song 	ring = &atmel_port->rx_ring;
119234df42f5SElen Song 
119334df42f5SElen Song 	dma_cap_zero(mask);
119434df42f5SElen Song 	dma_cap_set(DMA_CYCLIC, mask);
119534df42f5SElen Song 
1196c24d2531SRadu Pirea 	atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx");
119734df42f5SElen Song 	if (atmel_port->chan_rx == NULL)
119834df42f5SElen Song 		goto chan_err;
119934df42f5SElen Song 	dev_info(port->dev, "using %s for rx DMA transfers\n",
120034df42f5SElen Song 		dma_chan_name(atmel_port->chan_rx));
120134df42f5SElen Song 
120234df42f5SElen Song 	spin_lock_init(&atmel_port->lock_rx);
120334df42f5SElen Song 	sg_init_table(&atmel_port->sg_rx, 1);
120434df42f5SElen Song 	/* UART circular rx buffer is an aligned page. */
12052c277054SLeilei Zhao 	BUG_ON(!PAGE_ALIGNED(ring->buf));
120634df42f5SElen Song 	sg_set_page(&atmel_port->sg_rx,
120734df42f5SElen Song 		    virt_to_page(ring->buf),
1208a510880fSLeilei Zhao 		    sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
12092b5cf14bSGeliang Tang 		    offset_in_page(ring->buf));
121034df42f5SElen Song 	nent = dma_map_sg(port->dev,
121134df42f5SElen Song 			  &atmel_port->sg_rx,
121234df42f5SElen Song 			  1,
121348479148SWolfram Sang 			  DMA_FROM_DEVICE);
121434df42f5SElen Song 
121534df42f5SElen Song 	if (!nent) {
121634df42f5SElen Song 		dev_dbg(port->dev, "need to release resource of dma\n");
121734df42f5SElen Song 		goto chan_err;
121834df42f5SElen Song 	} else {
1219c8d1f022SUwe Kleine-König 		dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
122034df42f5SElen Song 			sg_dma_len(&atmel_port->sg_rx),
122134df42f5SElen Song 			ring->buf,
1222c8d1f022SUwe Kleine-König 			&sg_dma_address(&atmel_port->sg_rx));
122334df42f5SElen Song 	}
122434df42f5SElen Song 
122534df42f5SElen Song 	/* Configure the slave DMA */
122634df42f5SElen Song 	memset(&config, 0, sizeof(config));
122734df42f5SElen Song 	config.direction = DMA_DEV_TO_MEM;
122834df42f5SElen Song 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
122934df42f5SElen Song 	config.src_addr = port->mapbase + ATMEL_US_RHR;
1230a8d4e016SLudovic Desroches 	config.src_maxburst = 1;
123134df42f5SElen Song 
12325483c10eSMaxime Ripard 	ret = dmaengine_slave_config(atmel_port->chan_rx,
12335483c10eSMaxime Ripard 				     &config);
123434df42f5SElen Song 	if (ret) {
123534df42f5SElen Song 		dev_err(port->dev, "DMA rx slave configuration failed\n");
123634df42f5SElen Song 		goto chan_err;
123734df42f5SElen Song 	}
123834df42f5SElen Song 	/*
123934df42f5SElen Song 	 * Prepare a cyclic dma transfer, assign 2 descriptors,
124034df42f5SElen Song 	 * each one is half ring buffer size
124134df42f5SElen Song 	 */
124234df42f5SElen Song 	desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
124334df42f5SElen Song 					 sg_dma_address(&atmel_port->sg_rx),
124434df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx),
124534df42f5SElen Song 					 sg_dma_len(&atmel_port->sg_rx)/2,
124634df42f5SElen Song 					 DMA_DEV_TO_MEM,
124734df42f5SElen Song 					 DMA_PREP_INTERRUPT);
1248c85be041SKangjie Lu 	if (!desc) {
1249c85be041SKangjie Lu 		dev_err(port->dev, "Preparing DMA cyclic failed\n");
1250c85be041SKangjie Lu 		goto chan_err;
1251c85be041SKangjie Lu 	}
125234df42f5SElen Song 	desc->callback = atmel_complete_rx_dma;
125334df42f5SElen Song 	desc->callback_param = port;
125434df42f5SElen Song 	atmel_port->desc_rx = desc;
125534df42f5SElen Song 	atmel_port->cookie_rx = dmaengine_submit(desc);
125634df42f5SElen Song 
125734df42f5SElen Song 	return 0;
125834df42f5SElen Song 
125934df42f5SElen Song chan_err:
126034df42f5SElen Song 	dev_err(port->dev, "RX channel not available, switch to pio\n");
126134df42f5SElen Song 	atmel_port->use_dma_rx = 0;
126234df42f5SElen Song 	if (atmel_port->chan_rx)
126334df42f5SElen Song 		atmel_release_rx_dma(port);
126434df42f5SElen Song 	return -EINVAL;
126534df42f5SElen Song }
126634df42f5SElen Song 
1267026cb432SKees Cook static void atmel_uart_timer_callback(struct timer_list *t)
12682e68c22fSElen Song {
1269026cb432SKees Cook 	struct atmel_uart_port *atmel_port = from_timer(atmel_port, t,
1270026cb432SKees Cook 							uart_timer);
1271026cb432SKees Cook 	struct uart_port *port = &atmel_port->uart;
12722e68c22fSElen Song 
127398f2082cSNicolas Ferre 	if (!atomic_read(&atmel_port->tasklet_shutdown)) {
127400e8e658SNicolas Ferre 		tasklet_schedule(&atmel_port->tasklet_rx);
127598f2082cSNicolas Ferre 		mod_timer(&atmel_port->uart_timer,
127698f2082cSNicolas Ferre 			  jiffies + uart_poll_timeout(port));
127798f2082cSNicolas Ferre 	}
12782e68c22fSElen Song }
12792e68c22fSElen Song 
1280ab4382d2SGreg Kroah-Hartman /*
1281ab4382d2SGreg Kroah-Hartman  * receive interrupt handler.
1282ab4382d2SGreg Kroah-Hartman  */
1283ab4382d2SGreg Kroah-Hartman static void
1284ab4382d2SGreg Kroah-Hartman atmel_handle_receive(struct uart_port *port, unsigned int pending)
1285ab4382d2SGreg Kroah-Hartman {
1286ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1287ab4382d2SGreg Kroah-Hartman 
128864e22ebeSElen Song 	if (atmel_use_pdc_rx(port)) {
1289ab4382d2SGreg Kroah-Hartman 		/*
1290ab4382d2SGreg Kroah-Hartman 		 * PDC receive. Just schedule the tasklet and let it
1291ab4382d2SGreg Kroah-Hartman 		 * figure out the details.
1292ab4382d2SGreg Kroah-Hartman 		 *
1293ab4382d2SGreg Kroah-Hartman 		 * TODO: We're not handling error flags correctly at
1294ab4382d2SGreg Kroah-Hartman 		 * the moment.
1295ab4382d2SGreg Kroah-Hartman 		 */
1296ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
12974e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
12984e7decdaSCyrille Pitchen 					  (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
129998f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
130098f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
1301ab4382d2SGreg Kroah-Hartman 		}
1302ab4382d2SGreg Kroah-Hartman 
1303ab4382d2SGreg Kroah-Hartman 		if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1304ab4382d2SGreg Kroah-Hartman 				ATMEL_US_FRAME | ATMEL_US_PARE))
1305ab4382d2SGreg Kroah-Hartman 			atmel_pdc_rxerr(port, pending);
1306ab4382d2SGreg Kroah-Hartman 	}
1307ab4382d2SGreg Kroah-Hartman 
130834df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
130934df42f5SElen Song 		if (pending & ATMEL_US_TIMEOUT) {
13104e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR,
13114e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
131298f2082cSNicolas Ferre 			atmel_tasklet_schedule(atmel_port,
131398f2082cSNicolas Ferre 					       &atmel_port->tasklet_rx);
131434df42f5SElen Song 		}
131534df42f5SElen Song 	}
131634df42f5SElen Song 
1317ab4382d2SGreg Kroah-Hartman 	/* Interrupt receive */
1318ab4382d2SGreg Kroah-Hartman 	if (pending & ATMEL_US_RXRDY)
1319ab4382d2SGreg Kroah-Hartman 		atmel_rx_chars(port);
1320ab4382d2SGreg Kroah-Hartman 	else if (pending & ATMEL_US_RXBRK) {
1321ab4382d2SGreg Kroah-Hartman 		/*
1322ab4382d2SGreg Kroah-Hartman 		 * End of break detected. If it came along with a
1323ab4382d2SGreg Kroah-Hartman 		 * character, atmel_rx_chars will handle it.
1324ab4382d2SGreg Kroah-Hartman 		 */
13254e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
13264e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1327ab4382d2SGreg Kroah-Hartman 		atmel_port->break_active = 0;
1328ab4382d2SGreg Kroah-Hartman 	}
1329ab4382d2SGreg Kroah-Hartman }
1330ab4382d2SGreg Kroah-Hartman 
1331ab4382d2SGreg Kroah-Hartman /*
1332ab4382d2SGreg Kroah-Hartman  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1333ab4382d2SGreg Kroah-Hartman  */
1334ab4382d2SGreg Kroah-Hartman static void
1335ab4382d2SGreg Kroah-Hartman atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1336ab4382d2SGreg Kroah-Hartman {
1337ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1338ab4382d2SGreg Kroah-Hartman 
1339ab4382d2SGreg Kroah-Hartman 	if (pending & atmel_port->tx_done_mask) {
13404e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR,
13414e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
134269646d7aSRazvan Stefanescu 
134369646d7aSRazvan Stefanescu 		/* Start RX if flag was set and FIFO is empty */
134469646d7aSRazvan Stefanescu 		if (atmel_port->hd_start_rx) {
134569646d7aSRazvan Stefanescu 			if (!(atmel_uart_readl(port, ATMEL_US_CSR)
134669646d7aSRazvan Stefanescu 					& ATMEL_US_TXEMPTY))
134769646d7aSRazvan Stefanescu 				dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n");
134869646d7aSRazvan Stefanescu 
134969646d7aSRazvan Stefanescu 			atmel_port->hd_start_rx = false;
135069646d7aSRazvan Stefanescu 			atmel_start_rx(port);
135169646d7aSRazvan Stefanescu 		}
135269646d7aSRazvan Stefanescu 
135398f2082cSNicolas Ferre 		atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1354ab4382d2SGreg Kroah-Hartman 	}
1355ab4382d2SGreg Kroah-Hartman }
1356ab4382d2SGreg Kroah-Hartman 
1357ab4382d2SGreg Kroah-Hartman /*
1358ab4382d2SGreg Kroah-Hartman  * status flags interrupt handler.
1359ab4382d2SGreg Kroah-Hartman  */
1360ab4382d2SGreg Kroah-Hartman static void
1361ab4382d2SGreg Kroah-Hartman atmel_handle_status(struct uart_port *port, unsigned int pending,
1362ab4382d2SGreg Kroah-Hartman 		    unsigned int status)
1363ab4382d2SGreg Kroah-Hartman {
1364ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
13659205218eSNicolas Ferre 	unsigned int status_change;
1366ab4382d2SGreg Kroah-Hartman 
1367ab4382d2SGreg Kroah-Hartman 	if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1368ab4382d2SGreg Kroah-Hartman 				| ATMEL_US_CTSIC)) {
13699205218eSNicolas Ferre 		status_change = status ^ atmel_port->irq_status_prev;
1370d033e82dSLeilei Zhao 		atmel_port->irq_status_prev = status;
13719205218eSNicolas Ferre 
13729205218eSNicolas Ferre 		if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
13739205218eSNicolas Ferre 					| ATMEL_US_DCD | ATMEL_US_CTS)) {
13749205218eSNicolas Ferre 			/* TODO: All reads to CSR will clear these interrupts! */
13759205218eSNicolas Ferre 			if (status_change & ATMEL_US_RI)
13769205218eSNicolas Ferre 				port->icount.rng++;
13779205218eSNicolas Ferre 			if (status_change & ATMEL_US_DSR)
13789205218eSNicolas Ferre 				port->icount.dsr++;
13799205218eSNicolas Ferre 			if (status_change & ATMEL_US_DCD)
13809205218eSNicolas Ferre 				uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
13819205218eSNicolas Ferre 			if (status_change & ATMEL_US_CTS)
13829205218eSNicolas Ferre 				uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
13839205218eSNicolas Ferre 
13849205218eSNicolas Ferre 			wake_up_interruptible(&port->state->port.delta_msr_wait);
13859205218eSNicolas Ferre 		}
1386ab4382d2SGreg Kroah-Hartman 	}
1387377fedd1SNicolas Ferre 
1388377fedd1SNicolas Ferre 	if (pending & (ATMEL_US_NACK | ATMEL_US_ITERATION))
1389377fedd1SNicolas Ferre 		dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending);
1390ab4382d2SGreg Kroah-Hartman }
1391ab4382d2SGreg Kroah-Hartman 
1392ab4382d2SGreg Kroah-Hartman /*
1393ab4382d2SGreg Kroah-Hartman  * Interrupt handler
1394ab4382d2SGreg Kroah-Hartman  */
1395ab4382d2SGreg Kroah-Hartman static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1396ab4382d2SGreg Kroah-Hartman {
1397ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = dev_id;
1398ab5e4e41SRichard Genoud 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
13992c7af5baSBoris BREZILLON 	unsigned int status, pending, mask, pass_counter = 0;
1400ab4382d2SGreg Kroah-Hartman 
14012c7af5baSBoris BREZILLON 	spin_lock(&atmel_port->lock_suspended);
14022c7af5baSBoris BREZILLON 
1403ab4382d2SGreg Kroah-Hartman 	do {
1404d2d8d4c0SRichard Genoud 		status = atmel_uart_readl(port, ATMEL_US_CSR);
14054e7decdaSCyrille Pitchen 		mask = atmel_uart_readl(port, ATMEL_US_IMR);
14062c7af5baSBoris BREZILLON 		pending = status & mask;
1407ab4382d2SGreg Kroah-Hartman 		if (!pending)
1408ab4382d2SGreg Kroah-Hartman 			break;
1409ab4382d2SGreg Kroah-Hartman 
14102c7af5baSBoris BREZILLON 		if (atmel_port->suspended) {
14112c7af5baSBoris BREZILLON 			atmel_port->pending |= pending;
14122c7af5baSBoris BREZILLON 			atmel_port->pending_status = status;
14134e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IDR, mask);
14142c7af5baSBoris BREZILLON 			pm_system_wakeup();
14152c7af5baSBoris BREZILLON 			break;
14162c7af5baSBoris BREZILLON 		}
14172c7af5baSBoris BREZILLON 
1418ab4382d2SGreg Kroah-Hartman 		atmel_handle_receive(port, pending);
1419ab4382d2SGreg Kroah-Hartman 		atmel_handle_status(port, pending, status);
1420ab4382d2SGreg Kroah-Hartman 		atmel_handle_transmit(port, pending);
1421ab4382d2SGreg Kroah-Hartman 	} while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1422ab4382d2SGreg Kroah-Hartman 
14232c7af5baSBoris BREZILLON 	spin_unlock(&atmel_port->lock_suspended);
14242c7af5baSBoris BREZILLON 
1425ab4382d2SGreg Kroah-Hartman 	return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1426ab4382d2SGreg Kroah-Hartman }
1427ab4382d2SGreg Kroah-Hartman 
1428a930e528SElen Song static void atmel_release_tx_pdc(struct uart_port *port)
1429a930e528SElen Song {
1430a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1431a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1432a930e528SElen Song 
1433a930e528SElen Song 	dma_unmap_single(port->dev,
1434a930e528SElen Song 			 pdc->dma_addr,
1435a930e528SElen Song 			 pdc->dma_size,
1436a930e528SElen Song 			 DMA_TO_DEVICE);
1437a930e528SElen Song }
1438a930e528SElen Song 
1439ab4382d2SGreg Kroah-Hartman /*
1440ab4382d2SGreg Kroah-Hartman  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1441ab4382d2SGreg Kroah-Hartman  */
144264e22ebeSElen Song static void atmel_tx_pdc(struct uart_port *port)
1443ab4382d2SGreg Kroah-Hartman {
1444ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1445ab4382d2SGreg Kroah-Hartman 	struct circ_buf *xmit = &port->state->xmit;
1446ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1447ab4382d2SGreg Kroah-Hartman 	int count;
1448ab4382d2SGreg Kroah-Hartman 
1449ab4382d2SGreg Kroah-Hartman 	/* nothing left to transmit? */
14504e7decdaSCyrille Pitchen 	if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1451ab4382d2SGreg Kroah-Hartman 		return;
1452ab4382d2SGreg Kroah-Hartman 
1453ab4382d2SGreg Kroah-Hartman 	xmit->tail += pdc->ofs;
1454ab4382d2SGreg Kroah-Hartman 	xmit->tail &= UART_XMIT_SIZE - 1;
1455ab4382d2SGreg Kroah-Hartman 
1456ab4382d2SGreg Kroah-Hartman 	port->icount.tx += pdc->ofs;
1457ab4382d2SGreg Kroah-Hartman 	pdc->ofs = 0;
1458ab4382d2SGreg Kroah-Hartman 
1459ab4382d2SGreg Kroah-Hartman 	/* more to transmit - setup next transfer */
1460ab4382d2SGreg Kroah-Hartman 
1461ab4382d2SGreg Kroah-Hartman 	/* disable PDC transmit */
14624e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1463ab4382d2SGreg Kroah-Hartman 
1464ab4382d2SGreg Kroah-Hartman 	if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1465ab4382d2SGreg Kroah-Hartman 		dma_sync_single_for_device(port->dev,
1466ab4382d2SGreg Kroah-Hartman 					   pdc->dma_addr,
1467ab4382d2SGreg Kroah-Hartman 					   pdc->dma_size,
1468ab4382d2SGreg Kroah-Hartman 					   DMA_TO_DEVICE);
1469ab4382d2SGreg Kroah-Hartman 
1470ab4382d2SGreg Kroah-Hartman 		count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1471ab4382d2SGreg Kroah-Hartman 		pdc->ofs = count;
1472ab4382d2SGreg Kroah-Hartman 
14734e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TPR,
14744e7decdaSCyrille Pitchen 				  pdc->dma_addr + xmit->tail);
14754e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1476ab4382d2SGreg Kroah-Hartman 		/* re-enable PDC transmit */
14774e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1478ab4382d2SGreg Kroah-Hartman 		/* Enable interrupts */
14794e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER,
14804e7decdaSCyrille Pitchen 				  atmel_port->tx_done_mask);
1481ab4382d2SGreg Kroah-Hartman 	} else {
1482f3040983SRazvan Stefanescu 		if (atmel_uart_is_half_duplex(port)) {
1483ab4382d2SGreg Kroah-Hartman 			/* DMA done, stop TX, start RX for RS485 */
1484ab4382d2SGreg Kroah-Hartman 			atmel_start_rx(port);
1485ab4382d2SGreg Kroah-Hartman 		}
1486ab4382d2SGreg Kroah-Hartman 	}
1487ab4382d2SGreg Kroah-Hartman 
1488ab4382d2SGreg Kroah-Hartman 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1489ab4382d2SGreg Kroah-Hartman 		uart_write_wakeup(port);
1490ab4382d2SGreg Kroah-Hartman }
1491ab4382d2SGreg Kroah-Hartman 
1492a930e528SElen Song static int atmel_prepare_tx_pdc(struct uart_port *port)
1493a930e528SElen Song {
1494a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1495a930e528SElen Song 	struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1496a930e528SElen Song 	struct circ_buf *xmit = &port->state->xmit;
1497a930e528SElen Song 
1498a930e528SElen Song 	pdc->buf = xmit->buf;
1499a930e528SElen Song 	pdc->dma_addr = dma_map_single(port->dev,
1500a930e528SElen Song 					pdc->buf,
1501a930e528SElen Song 					UART_XMIT_SIZE,
1502a930e528SElen Song 					DMA_TO_DEVICE);
1503a930e528SElen Song 	pdc->dma_size = UART_XMIT_SIZE;
1504a930e528SElen Song 	pdc->ofs = 0;
1505a930e528SElen Song 
1506a930e528SElen Song 	return 0;
1507a930e528SElen Song }
1508a930e528SElen Song 
1509ab4382d2SGreg Kroah-Hartman static void atmel_rx_from_ring(struct uart_port *port)
1510ab4382d2SGreg Kroah-Hartman {
1511ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1512ab4382d2SGreg Kroah-Hartman 	struct circ_buf *ring = &atmel_port->rx_ring;
1513ab4382d2SGreg Kroah-Hartman 	unsigned int flg;
1514ab4382d2SGreg Kroah-Hartman 	unsigned int status;
1515ab4382d2SGreg Kroah-Hartman 
1516ab4382d2SGreg Kroah-Hartman 	while (ring->head != ring->tail) {
1517ab4382d2SGreg Kroah-Hartman 		struct atmel_uart_char c;
1518ab4382d2SGreg Kroah-Hartman 
1519ab4382d2SGreg Kroah-Hartman 		/* Make sure c is loaded after head. */
1520ab4382d2SGreg Kroah-Hartman 		smp_rmb();
1521ab4382d2SGreg Kroah-Hartman 
1522ab4382d2SGreg Kroah-Hartman 		c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1523ab4382d2SGreg Kroah-Hartman 
1524ab4382d2SGreg Kroah-Hartman 		ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1525ab4382d2SGreg Kroah-Hartman 
1526ab4382d2SGreg Kroah-Hartman 		port->icount.rx++;
1527ab4382d2SGreg Kroah-Hartman 		status = c.status;
1528ab4382d2SGreg Kroah-Hartman 		flg = TTY_NORMAL;
1529ab4382d2SGreg Kroah-Hartman 
1530ab4382d2SGreg Kroah-Hartman 		/*
1531ab4382d2SGreg Kroah-Hartman 		 * note that the error handling code is
1532ab4382d2SGreg Kroah-Hartman 		 * out of the main execution path
1533ab4382d2SGreg Kroah-Hartman 		 */
1534ab4382d2SGreg Kroah-Hartman 		if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1535ab4382d2SGreg Kroah-Hartman 				       | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1536ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK) {
1537ab4382d2SGreg Kroah-Hartman 				/* ignore side-effect */
1538ab4382d2SGreg Kroah-Hartman 				status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1539ab4382d2SGreg Kroah-Hartman 
1540ab4382d2SGreg Kroah-Hartman 				port->icount.brk++;
1541ab4382d2SGreg Kroah-Hartman 				if (uart_handle_break(port))
1542ab4382d2SGreg Kroah-Hartman 					continue;
1543ab4382d2SGreg Kroah-Hartman 			}
1544ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_PARE)
1545ab4382d2SGreg Kroah-Hartman 				port->icount.parity++;
1546ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_FRAME)
1547ab4382d2SGreg Kroah-Hartman 				port->icount.frame++;
1548ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_OVRE)
1549ab4382d2SGreg Kroah-Hartman 				port->icount.overrun++;
1550ab4382d2SGreg Kroah-Hartman 
1551ab4382d2SGreg Kroah-Hartman 			status &= port->read_status_mask;
1552ab4382d2SGreg Kroah-Hartman 
1553ab4382d2SGreg Kroah-Hartman 			if (status & ATMEL_US_RXBRK)
1554ab4382d2SGreg Kroah-Hartman 				flg = TTY_BREAK;
1555ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_PARE)
1556ab4382d2SGreg Kroah-Hartman 				flg = TTY_PARITY;
1557ab4382d2SGreg Kroah-Hartman 			else if (status & ATMEL_US_FRAME)
1558ab4382d2SGreg Kroah-Hartman 				flg = TTY_FRAME;
1559ab4382d2SGreg Kroah-Hartman 		}
1560ab4382d2SGreg Kroah-Hartman 
1561ab4382d2SGreg Kroah-Hartman 
1562ab4382d2SGreg Kroah-Hartman 		if (uart_handle_sysrq_char(port, c.ch))
1563ab4382d2SGreg Kroah-Hartman 			continue;
1564ab4382d2SGreg Kroah-Hartman 
1565ab4382d2SGreg Kroah-Hartman 		uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1566ab4382d2SGreg Kroah-Hartman 	}
1567ab4382d2SGreg Kroah-Hartman 
1568ab4382d2SGreg Kroah-Hartman 	/*
1569ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1570ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1571ab4382d2SGreg Kroah-Hartman 	 */
1572ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
15732e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->state->port);
1574ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1575ab4382d2SGreg Kroah-Hartman }
1576ab4382d2SGreg Kroah-Hartman 
1577a930e528SElen Song static void atmel_release_rx_pdc(struct uart_port *port)
1578a930e528SElen Song {
1579a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1580a930e528SElen Song 	int i;
1581a930e528SElen Song 
1582a930e528SElen Song 	for (i = 0; i < 2; i++) {
1583a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1584a930e528SElen Song 
1585a930e528SElen Song 		dma_unmap_single(port->dev,
1586a930e528SElen Song 				 pdc->dma_addr,
1587a930e528SElen Song 				 pdc->dma_size,
1588a930e528SElen Song 				 DMA_FROM_DEVICE);
1589a930e528SElen Song 		kfree(pdc->buf);
1590a930e528SElen Song 	}
1591a930e528SElen Song }
1592a930e528SElen Song 
159364e22ebeSElen Song static void atmel_rx_from_pdc(struct uart_port *port)
1594ab4382d2SGreg Kroah-Hartman {
1595ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
159605c7cd39SJiri Slaby 	struct tty_port *tport = &port->state->port;
1597ab4382d2SGreg Kroah-Hartman 	struct atmel_dma_buffer *pdc;
1598ab4382d2SGreg Kroah-Hartman 	int rx_idx = atmel_port->pdc_rx_idx;
1599ab4382d2SGreg Kroah-Hartman 	unsigned int head;
1600ab4382d2SGreg Kroah-Hartman 	unsigned int tail;
1601ab4382d2SGreg Kroah-Hartman 	unsigned int count;
1602ab4382d2SGreg Kroah-Hartman 
1603ab4382d2SGreg Kroah-Hartman 	do {
1604ab4382d2SGreg Kroah-Hartman 		/* Reset the UART timeout early so that we don't miss one */
16054e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1606ab4382d2SGreg Kroah-Hartman 
1607ab4382d2SGreg Kroah-Hartman 		pdc = &atmel_port->pdc_rx[rx_idx];
16084e7decdaSCyrille Pitchen 		head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1609ab4382d2SGreg Kroah-Hartman 		tail = pdc->ofs;
1610ab4382d2SGreg Kroah-Hartman 
1611ab4382d2SGreg Kroah-Hartman 		/* If the PDC has switched buffers, RPR won't contain
1612ab4382d2SGreg Kroah-Hartman 		 * any address within the current buffer. Since head
1613ab4382d2SGreg Kroah-Hartman 		 * is unsigned, we just need a one-way comparison to
1614ab4382d2SGreg Kroah-Hartman 		 * find out.
1615ab4382d2SGreg Kroah-Hartman 		 *
1616ab4382d2SGreg Kroah-Hartman 		 * In this case, we just need to consume the entire
1617ab4382d2SGreg Kroah-Hartman 		 * buffer and resubmit it for DMA. This will clear the
1618ab4382d2SGreg Kroah-Hartman 		 * ENDRX bit as well, so that we can safely re-enable
1619ab4382d2SGreg Kroah-Hartman 		 * all interrupts below.
1620ab4382d2SGreg Kroah-Hartman 		 */
1621ab4382d2SGreg Kroah-Hartman 		head = min(head, pdc->dma_size);
1622ab4382d2SGreg Kroah-Hartman 
1623ab4382d2SGreg Kroah-Hartman 		if (likely(head != tail)) {
1624ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1625ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1626ab4382d2SGreg Kroah-Hartman 
1627ab4382d2SGreg Kroah-Hartman 			/*
1628ab4382d2SGreg Kroah-Hartman 			 * head will only wrap around when we recycle
1629ab4382d2SGreg Kroah-Hartman 			 * the DMA buffer, and when that happens, we
1630ab4382d2SGreg Kroah-Hartman 			 * explicitly set tail to 0. So head will
1631ab4382d2SGreg Kroah-Hartman 			 * always be greater than tail.
1632ab4382d2SGreg Kroah-Hartman 			 */
1633ab4382d2SGreg Kroah-Hartman 			count = head - tail;
1634ab4382d2SGreg Kroah-Hartman 
163505c7cd39SJiri Slaby 			tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
163605c7cd39SJiri Slaby 						count);
1637ab4382d2SGreg Kroah-Hartman 
1638ab4382d2SGreg Kroah-Hartman 			dma_sync_single_for_device(port->dev, pdc->dma_addr,
1639ab4382d2SGreg Kroah-Hartman 					pdc->dma_size, DMA_FROM_DEVICE);
1640ab4382d2SGreg Kroah-Hartman 
1641ab4382d2SGreg Kroah-Hartman 			port->icount.rx += count;
1642ab4382d2SGreg Kroah-Hartman 			pdc->ofs = head;
1643ab4382d2SGreg Kroah-Hartman 		}
1644ab4382d2SGreg Kroah-Hartman 
1645ab4382d2SGreg Kroah-Hartman 		/*
1646ab4382d2SGreg Kroah-Hartman 		 * If the current buffer is full, we need to check if
1647ab4382d2SGreg Kroah-Hartman 		 * the next one contains any additional data.
1648ab4382d2SGreg Kroah-Hartman 		 */
1649ab4382d2SGreg Kroah-Hartman 		if (head >= pdc->dma_size) {
1650ab4382d2SGreg Kroah-Hartman 			pdc->ofs = 0;
16514e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
16524e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1653ab4382d2SGreg Kroah-Hartman 
1654ab4382d2SGreg Kroah-Hartman 			rx_idx = !rx_idx;
1655ab4382d2SGreg Kroah-Hartman 			atmel_port->pdc_rx_idx = rx_idx;
1656ab4382d2SGreg Kroah-Hartman 		}
1657ab4382d2SGreg Kroah-Hartman 	} while (head >= pdc->dma_size);
1658ab4382d2SGreg Kroah-Hartman 
1659ab4382d2SGreg Kroah-Hartman 	/*
1660ab4382d2SGreg Kroah-Hartman 	 * Drop the lock here since it might end up calling
1661ab4382d2SGreg Kroah-Hartman 	 * uart_start(), which takes the lock.
1662ab4382d2SGreg Kroah-Hartman 	 */
1663ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
16642e124b4aSJiri Slaby 	tty_flip_buffer_push(tport);
1665ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1666ab4382d2SGreg Kroah-Hartman 
16674e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER,
16684e7decdaSCyrille Pitchen 			  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1669ab4382d2SGreg Kroah-Hartman }
1670ab4382d2SGreg Kroah-Hartman 
1671a930e528SElen Song static int atmel_prepare_rx_pdc(struct uart_port *port)
1672a930e528SElen Song {
1673a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1674a930e528SElen Song 	int i;
1675a930e528SElen Song 
1676a930e528SElen Song 	for (i = 0; i < 2; i++) {
1677a930e528SElen Song 		struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1678a930e528SElen Song 
1679a930e528SElen Song 		pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1680a930e528SElen Song 		if (pdc->buf == NULL) {
1681a930e528SElen Song 			if (i != 0) {
1682a930e528SElen Song 				dma_unmap_single(port->dev,
1683a930e528SElen Song 					atmel_port->pdc_rx[0].dma_addr,
1684a930e528SElen Song 					PDC_BUFFER_SIZE,
1685a930e528SElen Song 					DMA_FROM_DEVICE);
1686a930e528SElen Song 				kfree(atmel_port->pdc_rx[0].buf);
1687a930e528SElen Song 			}
1688a930e528SElen Song 			atmel_port->use_pdc_rx = 0;
1689a930e528SElen Song 			return -ENOMEM;
1690a930e528SElen Song 		}
1691a930e528SElen Song 		pdc->dma_addr = dma_map_single(port->dev,
1692a930e528SElen Song 						pdc->buf,
1693a930e528SElen Song 						PDC_BUFFER_SIZE,
1694a930e528SElen Song 						DMA_FROM_DEVICE);
1695a930e528SElen Song 		pdc->dma_size = PDC_BUFFER_SIZE;
1696a930e528SElen Song 		pdc->ofs = 0;
1697a930e528SElen Song 	}
1698a930e528SElen Song 
1699a930e528SElen Song 	atmel_port->pdc_rx_idx = 0;
1700a930e528SElen Song 
17014e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
17024e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1703a930e528SElen Song 
17044e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNPR,
17054e7decdaSCyrille Pitchen 			  atmel_port->pdc_rx[1].dma_addr);
17064e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1707a930e528SElen Song 
1708a930e528SElen Song 	return 0;
1709a930e528SElen Song }
1710a930e528SElen Song 
1711ab4382d2SGreg Kroah-Hartman /*
1712ab4382d2SGreg Kroah-Hartman  * tasklet handling tty stuff outside the interrupt handler.
1713ab4382d2SGreg Kroah-Hartman  */
171400e8e658SNicolas Ferre static void atmel_tasklet_rx_func(unsigned long data)
1715ab4382d2SGreg Kroah-Hartman {
1716ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = (struct uart_port *)data;
1717ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1718ab4382d2SGreg Kroah-Hartman 
1719ab4382d2SGreg Kroah-Hartman 	/* The interrupt handler does not take the lock */
1720ab4382d2SGreg Kroah-Hartman 	spin_lock(&port->lock);
1721a930e528SElen Song 	atmel_port->schedule_rx(port);
172200e8e658SNicolas Ferre 	spin_unlock(&port->lock);
172300e8e658SNicolas Ferre }
1724ab4382d2SGreg Kroah-Hartman 
172500e8e658SNicolas Ferre static void atmel_tasklet_tx_func(unsigned long data)
172600e8e658SNicolas Ferre {
172700e8e658SNicolas Ferre 	struct uart_port *port = (struct uart_port *)data;
172800e8e658SNicolas Ferre 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
172900e8e658SNicolas Ferre 
173000e8e658SNicolas Ferre 	/* The interrupt handler does not take the lock */
173100e8e658SNicolas Ferre 	spin_lock(&port->lock);
173200e8e658SNicolas Ferre 	atmel_port->schedule_tx(port);
1733ab4382d2SGreg Kroah-Hartman 	spin_unlock(&port->lock);
1734ab4382d2SGreg Kroah-Hartman }
1735ab4382d2SGreg Kroah-Hartman 
17364a1e8888SLeilei Zhao static void atmel_init_property(struct atmel_uart_port *atmel_port,
173733d64c4fSElen Song 				struct platform_device *pdev)
173833d64c4fSElen Song {
173933d64c4fSElen Song 	struct device_node *np = pdev->dev.of_node;
174033d64c4fSElen Song 
174133d64c4fSElen Song 	/* DMA/PDC usage specification */
1742490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1743490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
174433d64c4fSElen Song 			atmel_port->use_dma_rx  = true;
174533d64c4fSElen Song 			atmel_port->use_pdc_rx  = false;
174633d64c4fSElen Song 		} else {
174733d64c4fSElen Song 			atmel_port->use_dma_rx  = false;
174833d64c4fSElen Song 			atmel_port->use_pdc_rx  = true;
174933d64c4fSElen Song 		}
175033d64c4fSElen Song 	} else {
175133d64c4fSElen Song 		atmel_port->use_dma_rx  = false;
175233d64c4fSElen Song 		atmel_port->use_pdc_rx  = false;
175333d64c4fSElen Song 	}
175433d64c4fSElen Song 
1755490d5ce2SJulia Lawall 	if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1756490d5ce2SJulia Lawall 		if (of_property_read_bool(np, "dmas")) {
175733d64c4fSElen Song 			atmel_port->use_dma_tx  = true;
175833d64c4fSElen Song 			atmel_port->use_pdc_tx  = false;
175933d64c4fSElen Song 		} else {
176033d64c4fSElen Song 			atmel_port->use_dma_tx  = false;
176133d64c4fSElen Song 			atmel_port->use_pdc_tx  = true;
176233d64c4fSElen Song 		}
176333d64c4fSElen Song 	} else {
176433d64c4fSElen Song 		atmel_port->use_dma_tx  = false;
176533d64c4fSElen Song 		atmel_port->use_pdc_tx  = false;
176633d64c4fSElen Song 	}
176733d64c4fSElen Song }
176833d64c4fSElen Song 
1769a930e528SElen Song static void atmel_set_ops(struct uart_port *port)
1770a930e528SElen Song {
1771a930e528SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1772a930e528SElen Song 
177334df42f5SElen Song 	if (atmel_use_dma_rx(port)) {
177434df42f5SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_dma;
177534df42f5SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_dma;
177634df42f5SElen Song 		atmel_port->release_rx = &atmel_release_rx_dma;
177734df42f5SElen Song 	} else if (atmel_use_pdc_rx(port)) {
1778a930e528SElen Song 		atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1779a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_pdc;
1780a930e528SElen Song 		atmel_port->release_rx = &atmel_release_rx_pdc;
1781a930e528SElen Song 	} else {
1782a930e528SElen Song 		atmel_port->prepare_rx = NULL;
1783a930e528SElen Song 		atmel_port->schedule_rx = &atmel_rx_from_ring;
1784a930e528SElen Song 		atmel_port->release_rx = NULL;
1785a930e528SElen Song 	}
1786a930e528SElen Song 
178708f738beSElen Song 	if (atmel_use_dma_tx(port)) {
178808f738beSElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_dma;
178908f738beSElen Song 		atmel_port->schedule_tx = &atmel_tx_dma;
179008f738beSElen Song 		atmel_port->release_tx = &atmel_release_tx_dma;
179108f738beSElen Song 	} else if (atmel_use_pdc_tx(port)) {
1792a930e528SElen Song 		atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1793a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_pdc;
1794a930e528SElen Song 		atmel_port->release_tx = &atmel_release_tx_pdc;
1795a930e528SElen Song 	} else {
1796a930e528SElen Song 		atmel_port->prepare_tx = NULL;
1797a930e528SElen Song 		atmel_port->schedule_tx = &atmel_tx_chars;
1798a930e528SElen Song 		atmel_port->release_tx = NULL;
1799a930e528SElen Song 	}
1800a930e528SElen Song }
1801a930e528SElen Song 
1802ab4382d2SGreg Kroah-Hartman /*
1803055560b0SElen Song  * Get ip name usart or uart
1804055560b0SElen Song  */
1805892db58bSNicolas Ferre static void atmel_get_ip_name(struct uart_port *port)
1806055560b0SElen Song {
1807055560b0SElen Song 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
18084e7decdaSCyrille Pitchen 	int name = atmel_uart_readl(port, ATMEL_US_NAME);
1809731d9caeSNicolas Ferre 	u32 version;
18101d673fb9SNicolas Ferre 	u32 usart, dbgu_uart, new_uart;
18114b769371SNicolas Ferre 	/* ASCII decoding for IP version */
18124b769371SNicolas Ferre 	usart = 0x55534152;	/* USAR(T) */
18134b769371SNicolas Ferre 	dbgu_uart = 0x44424755;	/* DBGU */
18141d673fb9SNicolas Ferre 	new_uart = 0x55415254;	/* UART */
1815055560b0SElen Song 
18165bf5635aSLudovic Desroches 	/*
18175bf5635aSLudovic Desroches 	 * Only USART devices from at91sam9260 SOC implement fractional
18182867af2dSRomain Izard 	 * baudrate. It is available for all asynchronous modes, with the
18192867af2dSRomain Izard 	 * following restriction: the sampling clock's duty cycle is not
18202867af2dSRomain Izard 	 * constant.
18215bf5635aSLudovic Desroches 	 */
18225bf5635aSLudovic Desroches 	atmel_port->has_frac_baudrate = false;
18234b769371SNicolas Ferre 	atmel_port->has_hw_timer = false;
1824055560b0SElen Song 
18252958cceeSLudovic Desroches 	if (name == new_uart) {
18262958cceeSLudovic Desroches 		dev_dbg(port->dev, "Uart with hw timer");
18274b769371SNicolas Ferre 		atmel_port->has_hw_timer = true;
18282958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_UA_RTOR;
18292958cceeSLudovic Desroches 	} else if (name == usart) {
18302958cceeSLudovic Desroches 		dev_dbg(port->dev, "Usart\n");
18315bf5635aSLudovic Desroches 		atmel_port->has_frac_baudrate = true;
18322958cceeSLudovic Desroches 		atmel_port->has_hw_timer = true;
18332958cceeSLudovic Desroches 		atmel_port->rtor = ATMEL_US_RTOR;
1834377fedd1SNicolas Ferre 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1835377fedd1SNicolas Ferre 		switch (version) {
1836377fedd1SNicolas Ferre 		case 0x814:	/* sama5d2 */
1837377fedd1SNicolas Ferre 			/* fall through */
1838377fedd1SNicolas Ferre 		case 0x701:	/* sama5d4 */
1839377fedd1SNicolas Ferre 			atmel_port->fidi_min = 3;
1840377fedd1SNicolas Ferre 			atmel_port->fidi_max = 65535;
1841377fedd1SNicolas Ferre 			break;
1842377fedd1SNicolas Ferre 		case 0x502:	/* sam9x5, sama5d3 */
1843377fedd1SNicolas Ferre 			atmel_port->fidi_min = 3;
1844377fedd1SNicolas Ferre 			atmel_port->fidi_max = 2047;
1845377fedd1SNicolas Ferre 			break;
1846377fedd1SNicolas Ferre 		default:
1847377fedd1SNicolas Ferre 			atmel_port->fidi_min = 1;
1848377fedd1SNicolas Ferre 			atmel_port->fidi_max = 2047;
1849377fedd1SNicolas Ferre 		}
18504b769371SNicolas Ferre 	} else if (name == dbgu_uart) {
18514b769371SNicolas Ferre 		dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1852055560b0SElen Song 	} else {
1853731d9caeSNicolas Ferre 		/* fallback for older SoCs: use version field */
18544e7decdaSCyrille Pitchen 		version = atmel_uart_readl(port, ATMEL_US_VERSION);
1855731d9caeSNicolas Ferre 		switch (version) {
1856731d9caeSNicolas Ferre 		case 0x302:
1857731d9caeSNicolas Ferre 		case 0x10213:
1858fd63a890SJonas Danielsson 		case 0x10302:
1859731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is usart\n");
18605bf5635aSLudovic Desroches 			atmel_port->has_frac_baudrate = true;
18614b769371SNicolas Ferre 			atmel_port->has_hw_timer = true;
18622958cceeSLudovic Desroches 			atmel_port->rtor = ATMEL_US_RTOR;
1863731d9caeSNicolas Ferre 			break;
1864731d9caeSNicolas Ferre 		case 0x203:
1865731d9caeSNicolas Ferre 		case 0x10202:
1866731d9caeSNicolas Ferre 			dev_dbg(port->dev, "This version is uart\n");
1867731d9caeSNicolas Ferre 			break;
1868731d9caeSNicolas Ferre 		default:
1869731d9caeSNicolas Ferre 			dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1870731d9caeSNicolas Ferre 		}
1871055560b0SElen Song 	}
1872055560b0SElen Song }
1873055560b0SElen Song 
1874055560b0SElen Song /*
1875ab4382d2SGreg Kroah-Hartman  * Perform initialization and enable port for reception
1876ab4382d2SGreg Kroah-Hartman  */
1877ab4382d2SGreg Kroah-Hartman static int atmel_startup(struct uart_port *port)
1878ab4382d2SGreg Kroah-Hartman {
187933d64c4fSElen Song 	struct platform_device *pdev = to_platform_device(port->dev);
1880ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1881ab4382d2SGreg Kroah-Hartman 	int retval;
1882ab4382d2SGreg Kroah-Hartman 
1883ab4382d2SGreg Kroah-Hartman 	/*
1884ab4382d2SGreg Kroah-Hartman 	 * Ensure that no interrupts are enabled otherwise when
1885ab4382d2SGreg Kroah-Hartman 	 * request_irq() is called we could get stuck trying to
1886ab4382d2SGreg Kroah-Hartman 	 * handle an unexpected interrupt
1887ab4382d2SGreg Kroah-Hartman 	 */
18884e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
1889ab5e4e41SRichard Genoud 	atmel_port->ms_irq_enabled = false;
1890ab4382d2SGreg Kroah-Hartman 
1891ab4382d2SGreg Kroah-Hartman 	/*
1892ab4382d2SGreg Kroah-Hartman 	 * Allocate the IRQ
1893ab4382d2SGreg Kroah-Hartman 	 */
18942c7af5baSBoris BREZILLON 	retval = request_irq(port->irq, atmel_interrupt,
18952c7af5baSBoris BREZILLON 			     IRQF_SHARED | IRQF_COND_SUSPEND,
18969594b5beSSebastian Andrzej Siewior 			     dev_name(&pdev->dev), port);
1897ab4382d2SGreg Kroah-Hartman 	if (retval) {
1898ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_startup - Can't get irq\n");
1899ab4382d2SGreg Kroah-Hartman 		return retval;
1900ab4382d2SGreg Kroah-Hartman 	}
1901ab4382d2SGreg Kroah-Hartman 
190298f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
190398f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
190498f2082cSNicolas Ferre 			(unsigned long)port);
190598f2082cSNicolas Ferre 	tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
190698f2082cSNicolas Ferre 			(unsigned long)port);
19071e125786SLeilei Zhao 
1908ab5e4e41SRichard Genoud 	/*
1909ab4382d2SGreg Kroah-Hartman 	 * Initialize DMA (if necessary)
1910ab4382d2SGreg Kroah-Hartman 	 */
191133d64c4fSElen Song 	atmel_init_property(atmel_port, pdev);
19124d9628a1SLeilei Zhao 	atmel_set_ops(port);
191333d64c4fSElen Song 
1914a930e528SElen Song 	if (atmel_port->prepare_rx) {
1915a930e528SElen Song 		retval = atmel_port->prepare_rx(port);
1916a930e528SElen Song 		if (retval < 0)
1917a930e528SElen Song 			atmel_set_ops(port);
1918ab4382d2SGreg Kroah-Hartman 	}
1919ab4382d2SGreg Kroah-Hartman 
1920a930e528SElen Song 	if (atmel_port->prepare_tx) {
1921a930e528SElen Song 		retval = atmel_port->prepare_tx(port);
1922a930e528SElen Song 		if (retval < 0)
1923a930e528SElen Song 			atmel_set_ops(port);
1924ab4382d2SGreg Kroah-Hartman 	}
1925ab4382d2SGreg Kroah-Hartman 
1926b5199d46SCyrille Pitchen 	/*
1927b5199d46SCyrille Pitchen 	 * Enable FIFO when available
1928b5199d46SCyrille Pitchen 	 */
1929b5199d46SCyrille Pitchen 	if (atmel_port->fifo_size) {
1930b5199d46SCyrille Pitchen 		unsigned int txrdym = ATMEL_US_ONE_DATA;
1931b5199d46SCyrille Pitchen 		unsigned int rxrdym = ATMEL_US_ONE_DATA;
1932b5199d46SCyrille Pitchen 		unsigned int fmr;
1933b5199d46SCyrille Pitchen 
1934b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR,
1935b5199d46SCyrille Pitchen 				  ATMEL_US_FIFOEN |
1936b5199d46SCyrille Pitchen 				  ATMEL_US_RXFCLR |
1937b5199d46SCyrille Pitchen 				  ATMEL_US_TXFLCLR);
1938b5199d46SCyrille Pitchen 
19395f258b3eSCyrille Pitchen 		if (atmel_use_dma_tx(port))
19405f258b3eSCyrille Pitchen 			txrdym = ATMEL_US_FOUR_DATA;
19415f258b3eSCyrille Pitchen 
1942b5199d46SCyrille Pitchen 		fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1943b5199d46SCyrille Pitchen 		if (atmel_port->rts_high &&
1944b5199d46SCyrille Pitchen 		    atmel_port->rts_low)
1945b5199d46SCyrille Pitchen 			fmr |=	ATMEL_US_FRTSC |
1946b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1947b5199d46SCyrille Pitchen 				ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1948b5199d46SCyrille Pitchen 
1949b5199d46SCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1950b5199d46SCyrille Pitchen 	}
1951b5199d46SCyrille Pitchen 
1952ab4382d2SGreg Kroah-Hartman 	/* Save current CSR for comparison in atmel_tasklet_func() */
1953d2d8d4c0SRichard Genoud 	atmel_port->irq_status_prev = atmel_uart_readl(port, ATMEL_US_CSR);
1954ab4382d2SGreg Kroah-Hartman 
1955ab4382d2SGreg Kroah-Hartman 	/*
1956ab4382d2SGreg Kroah-Hartman 	 * Finally, enable the serial port
1957ab4382d2SGreg Kroah-Hartman 	 */
19584e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1959ab4382d2SGreg Kroah-Hartman 	/* enable xmit & rcvr */
19604e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1961ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
1962ab4382d2SGreg Kroah-Hartman 
1963026cb432SKees Cook 	timer_setup(&atmel_port->uart_timer, atmel_uart_timer_callback, 0);
19648bc661bfSMarek Roszko 
19658bc661bfSMarek Roszko 	if (atmel_use_pdc_rx(port)) {
19668bc661bfSMarek Roszko 		/* set UART timeout */
19674b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19682e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19692e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19702e68c22fSElen Song 		/* set USART timeout */
19712e68c22fSElen Song 		} else {
19722958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
19732958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19744e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1975ab4382d2SGreg Kroah-Hartman 
19764e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19774e7decdaSCyrille Pitchen 					  ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
19782e68c22fSElen Song 		}
1979ab4382d2SGreg Kroah-Hartman 		/* enable PDC controller */
19804e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
198134df42f5SElen Song 	} else if (atmel_use_dma_rx(port)) {
19822e68c22fSElen Song 		/* set UART timeout */
19834b769371SNicolas Ferre 		if (!atmel_port->has_hw_timer) {
19842e68c22fSElen Song 			mod_timer(&atmel_port->uart_timer,
19852e68c22fSElen Song 					jiffies + uart_poll_timeout(port));
19862e68c22fSElen Song 		/* set USART timeout */
19872e68c22fSElen Song 		} else {
19882958cceeSLudovic Desroches 			atmel_uart_writel(port, atmel_port->rtor,
19892958cceeSLudovic Desroches 					  PDC_RX_TIMEOUT);
19904e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
199134df42f5SElen Song 
19924e7decdaSCyrille Pitchen 			atmel_uart_writel(port, ATMEL_US_IER,
19934e7decdaSCyrille Pitchen 					  ATMEL_US_TIMEOUT);
19942e68c22fSElen Song 		}
1995ab4382d2SGreg Kroah-Hartman 	} else {
1996ab4382d2SGreg Kroah-Hartman 		/* enable receive only */
19974e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1998ab4382d2SGreg Kroah-Hartman 	}
1999ab4382d2SGreg Kroah-Hartman 
2000ab4382d2SGreg Kroah-Hartman 	return 0;
2001ab4382d2SGreg Kroah-Hartman }
2002ab4382d2SGreg Kroah-Hartman 
2003ab4382d2SGreg Kroah-Hartman /*
2004479e9b94SPeter Hurley  * Flush any TX data submitted for DMA. Called when the TX circular
2005479e9b94SPeter Hurley  * buffer is reset.
2006479e9b94SPeter Hurley  */
2007479e9b94SPeter Hurley static void atmel_flush_buffer(struct uart_port *port)
2008479e9b94SPeter Hurley {
2009479e9b94SPeter Hurley 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2010479e9b94SPeter Hurley 
2011479e9b94SPeter Hurley 	if (atmel_use_pdc_tx(port)) {
20124e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
2013479e9b94SPeter Hurley 		atmel_port->pdc_tx.ofs = 0;
2014479e9b94SPeter Hurley 	}
201531ca2c63SRichard Genoud 	/*
201631ca2c63SRichard Genoud 	 * in uart_flush_buffer(), the xmit circular buffer has just
201731ca2c63SRichard Genoud 	 * been cleared, so we have to reset tx_len accordingly.
201831ca2c63SRichard Genoud 	 */
201931ca2c63SRichard Genoud 	atmel_port->tx_len = 0;
2020479e9b94SPeter Hurley }
2021479e9b94SPeter Hurley 
2022479e9b94SPeter Hurley /*
2023ab4382d2SGreg Kroah-Hartman  * Disable the port
2024ab4382d2SGreg Kroah-Hartman  */
2025ab4382d2SGreg Kroah-Hartman static void atmel_shutdown(struct uart_port *port)
2026ab4382d2SGreg Kroah-Hartman {
2027ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
20280cc7c6c7SMarek Roszko 
20290ae9fdefSRichard Genoud 	/* Disable modem control lines interrupts */
20300ae9fdefSRichard Genoud 	atmel_disable_ms(port);
20310ae9fdefSRichard Genoud 
203298f2082cSNicolas Ferre 	/* Disable interrupts at device level */
203398f2082cSNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
203498f2082cSNicolas Ferre 
203598f2082cSNicolas Ferre 	/* Prevent spurious interrupts from scheduling the tasklet */
203698f2082cSNicolas Ferre 	atomic_inc(&atmel_port->tasklet_shutdown);
203798f2082cSNicolas Ferre 
2038ab4382d2SGreg Kroah-Hartman 	/*
20398bc661bfSMarek Roszko 	 * Prevent any tasklets being scheduled during
20408bc661bfSMarek Roszko 	 * cleanup
20418bc661bfSMarek Roszko 	 */
20428bc661bfSMarek Roszko 	del_timer_sync(&atmel_port->uart_timer);
20438bc661bfSMarek Roszko 
204498f2082cSNicolas Ferre 	/* Make sure that no interrupt is on the fly */
204598f2082cSNicolas Ferre 	synchronize_irq(port->irq);
204698f2082cSNicolas Ferre 
20478bc661bfSMarek Roszko 	/*
20480cc7c6c7SMarek Roszko 	 * Clear out any scheduled tasklets before
20490cc7c6c7SMarek Roszko 	 * we destroy the buffers
20500cc7c6c7SMarek Roszko 	 */
205100e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
205200e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
20530cc7c6c7SMarek Roszko 
20540cc7c6c7SMarek Roszko 	/*
20550cc7c6c7SMarek Roszko 	 * Ensure everything is stopped and
205698f2082cSNicolas Ferre 	 * disable port and break condition.
2057ab4382d2SGreg Kroah-Hartman 	 */
2058ab4382d2SGreg Kroah-Hartman 	atmel_stop_rx(port);
2059ab4382d2SGreg Kroah-Hartman 	atmel_stop_tx(port);
2060ab4382d2SGreg Kroah-Hartman 
20614e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
20620cc7c6c7SMarek Roszko 
2063ab4382d2SGreg Kroah-Hartman 	/*
2064ab4382d2SGreg Kroah-Hartman 	 * Shut-down the DMA.
2065ab4382d2SGreg Kroah-Hartman 	 */
2066a930e528SElen Song 	if (atmel_port->release_rx)
2067a930e528SElen Song 		atmel_port->release_rx(port);
2068a930e528SElen Song 	if (atmel_port->release_tx)
2069a930e528SElen Song 		atmel_port->release_tx(port);
2070ab4382d2SGreg Kroah-Hartman 
2071ab4382d2SGreg Kroah-Hartman 	/*
2072bb7e73c5SMark Deneen 	 * Reset ring buffer pointers
2073bb7e73c5SMark Deneen 	 */
2074bb7e73c5SMark Deneen 	atmel_port->rx_ring.head = 0;
2075bb7e73c5SMark Deneen 	atmel_port->rx_ring.tail = 0;
2076bb7e73c5SMark Deneen 
2077bb7e73c5SMark Deneen 	/*
2078ab5e4e41SRichard Genoud 	 * Free the interrupts
2079ab4382d2SGreg Kroah-Hartman 	 */
2080ab4382d2SGreg Kroah-Hartman 	free_irq(port->irq, port);
2081ab5e4e41SRichard Genoud 
2082479e9b94SPeter Hurley 	atmel_flush_buffer(port);
2083ab4382d2SGreg Kroah-Hartman }
2084ab4382d2SGreg Kroah-Hartman 
2085ab4382d2SGreg Kroah-Hartman /*
2086ab4382d2SGreg Kroah-Hartman  * Power / Clock management.
2087ab4382d2SGreg Kroah-Hartman  */
2088ab4382d2SGreg Kroah-Hartman static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2089ab4382d2SGreg Kroah-Hartman 			    unsigned int oldstate)
2090ab4382d2SGreg Kroah-Hartman {
2091ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2092ab4382d2SGreg Kroah-Hartman 
2093ab4382d2SGreg Kroah-Hartman 	switch (state) {
2094ab4382d2SGreg Kroah-Hartman 	case 0:
2095ab4382d2SGreg Kroah-Hartman 		/*
2096ab4382d2SGreg Kroah-Hartman 		 * Enable the peripheral clock for this serial port.
2097ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_open() or a resume event.
2098ab4382d2SGreg Kroah-Hartman 		 */
209991f8c2d8SBoris BREZILLON 		clk_prepare_enable(atmel_port->clk);
2100ab4382d2SGreg Kroah-Hartman 
2101ab4382d2SGreg Kroah-Hartman 		/* re-enable interrupts if we disabled some on suspend */
21024e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
2103ab4382d2SGreg Kroah-Hartman 		break;
2104ab4382d2SGreg Kroah-Hartman 	case 3:
2105ab4382d2SGreg Kroah-Hartman 		/* Back up the interrupt mask and disable all interrupts */
21064e7decdaSCyrille Pitchen 		atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
21074e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IDR, -1);
2108ab4382d2SGreg Kroah-Hartman 
2109ab4382d2SGreg Kroah-Hartman 		/*
2110ab4382d2SGreg Kroah-Hartman 		 * Disable the peripheral clock for this serial port.
2111ab4382d2SGreg Kroah-Hartman 		 * This is called on uart_close() or a suspend event.
2112ab4382d2SGreg Kroah-Hartman 		 */
211391f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2114ab4382d2SGreg Kroah-Hartman 		break;
2115ab4382d2SGreg Kroah-Hartman 	default:
2116ddaa6037SRichard Genoud 		dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2117ab4382d2SGreg Kroah-Hartman 	}
2118ab4382d2SGreg Kroah-Hartman }
2119ab4382d2SGreg Kroah-Hartman 
2120ab4382d2SGreg Kroah-Hartman /*
2121ab4382d2SGreg Kroah-Hartman  * Change the port parameters
2122ab4382d2SGreg Kroah-Hartman  */
2123ab4382d2SGreg Kroah-Hartman static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2124ab4382d2SGreg Kroah-Hartman 			      struct ktermios *old)
2125ab4382d2SGreg Kroah-Hartman {
21265bf5635aSLudovic Desroches 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2127ab4382d2SGreg Kroah-Hartman 	unsigned long flags;
21285bf5635aSLudovic Desroches 	unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
2129ab4382d2SGreg Kroah-Hartman 
21301cf6e8fcSCyrille Pitchen 	/* save the current mode register */
21314e7decdaSCyrille Pitchen 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
21321cf6e8fcSCyrille Pitchen 
21331cf6e8fcSCyrille Pitchen 	/* reset the mode, clock divisor, parity, stop bits and data size */
21341cf6e8fcSCyrille Pitchen 	mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
21351cf6e8fcSCyrille Pitchen 		  ATMEL_US_PAR | ATMEL_US_USMODE);
2136ab4382d2SGreg Kroah-Hartman 
2137ab4382d2SGreg Kroah-Hartman 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2138ab4382d2SGreg Kroah-Hartman 
2139ab4382d2SGreg Kroah-Hartman 	/* byte size */
2140ab4382d2SGreg Kroah-Hartman 	switch (termios->c_cflag & CSIZE) {
2141ab4382d2SGreg Kroah-Hartman 	case CS5:
2142ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_5;
2143ab4382d2SGreg Kroah-Hartman 		break;
2144ab4382d2SGreg Kroah-Hartman 	case CS6:
2145ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_6;
2146ab4382d2SGreg Kroah-Hartman 		break;
2147ab4382d2SGreg Kroah-Hartman 	case CS7:
2148ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_7;
2149ab4382d2SGreg Kroah-Hartman 		break;
2150ab4382d2SGreg Kroah-Hartman 	default:
2151ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_CHRL_8;
2152ab4382d2SGreg Kroah-Hartman 		break;
2153ab4382d2SGreg Kroah-Hartman 	}
2154ab4382d2SGreg Kroah-Hartman 
2155ab4382d2SGreg Kroah-Hartman 	/* stop bits */
2156ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & CSTOPB)
2157ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_NBSTOP_2;
2158ab4382d2SGreg Kroah-Hartman 
2159ab4382d2SGreg Kroah-Hartman 	/* parity */
2160ab4382d2SGreg Kroah-Hartman 	if (termios->c_cflag & PARENB) {
2161ab4382d2SGreg Kroah-Hartman 		/* Mark or Space parity */
2162ab4382d2SGreg Kroah-Hartman 		if (termios->c_cflag & CMSPAR) {
2163ab4382d2SGreg Kroah-Hartman 			if (termios->c_cflag & PARODD)
2164ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_MARK;
2165ab4382d2SGreg Kroah-Hartman 			else
2166ab4382d2SGreg Kroah-Hartman 				mode |= ATMEL_US_PAR_SPACE;
2167ab4382d2SGreg Kroah-Hartman 		} else if (termios->c_cflag & PARODD)
2168ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_ODD;
2169ab4382d2SGreg Kroah-Hartman 		else
2170ab4382d2SGreg Kroah-Hartman 			mode |= ATMEL_US_PAR_EVEN;
2171ab4382d2SGreg Kroah-Hartman 	} else
2172ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_PAR_NONE;
2173ab4382d2SGreg Kroah-Hartman 
2174ab4382d2SGreg Kroah-Hartman 	spin_lock_irqsave(&port->lock, flags);
2175ab4382d2SGreg Kroah-Hartman 
2176ab4382d2SGreg Kroah-Hartman 	port->read_status_mask = ATMEL_US_OVRE;
2177ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & INPCK)
2178ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2179ef8b9ddcSPeter Hurley 	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2180ab4382d2SGreg Kroah-Hartman 		port->read_status_mask |= ATMEL_US_RXBRK;
2181ab4382d2SGreg Kroah-Hartman 
218264e22ebeSElen Song 	if (atmel_use_pdc_rx(port))
2183ab4382d2SGreg Kroah-Hartman 		/* need to enable error interrupts */
21844e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2185ab4382d2SGreg Kroah-Hartman 
2186ab4382d2SGreg Kroah-Hartman 	/*
2187ab4382d2SGreg Kroah-Hartman 	 * Characters to ignore
2188ab4382d2SGreg Kroah-Hartman 	 */
2189ab4382d2SGreg Kroah-Hartman 	port->ignore_status_mask = 0;
2190ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNPAR)
2191ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2192ab4382d2SGreg Kroah-Hartman 	if (termios->c_iflag & IGNBRK) {
2193ab4382d2SGreg Kroah-Hartman 		port->ignore_status_mask |= ATMEL_US_RXBRK;
2194ab4382d2SGreg Kroah-Hartman 		/*
2195ab4382d2SGreg Kroah-Hartman 		 * If we're ignoring parity and break indicators,
2196ab4382d2SGreg Kroah-Hartman 		 * ignore overruns too (for real raw support).
2197ab4382d2SGreg Kroah-Hartman 		 */
2198ab4382d2SGreg Kroah-Hartman 		if (termios->c_iflag & IGNPAR)
2199ab4382d2SGreg Kroah-Hartman 			port->ignore_status_mask |= ATMEL_US_OVRE;
2200ab4382d2SGreg Kroah-Hartman 	}
2201ab4382d2SGreg Kroah-Hartman 	/* TODO: Ignore all characters if CREAD is set.*/
2202ab4382d2SGreg Kroah-Hartman 
2203ab4382d2SGreg Kroah-Hartman 	/* update the per-port timeout */
2204ab4382d2SGreg Kroah-Hartman 	uart_update_timeout(port, termios->c_cflag, baud);
2205ab4382d2SGreg Kroah-Hartman 
2206ab4382d2SGreg Kroah-Hartman 	/*
2207ab4382d2SGreg Kroah-Hartman 	 * save/disable interrupts. The tty layer will ensure that the
2208ab4382d2SGreg Kroah-Hartman 	 * transmitter is empty if requested by the caller, so there's
2209ab4382d2SGreg Kroah-Hartman 	 * no need to wait for it here.
2210ab4382d2SGreg Kroah-Hartman 	 */
22114e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
22124e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
2213ab4382d2SGreg Kroah-Hartman 
2214ab4382d2SGreg Kroah-Hartman 	/* disable receiver and transmitter */
22154e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2216ea04f82aSRomain Izard 	atmel_port->tx_stopped = true;
2217ab4382d2SGreg Kroah-Hartman 
22181cf6e8fcSCyrille Pitchen 	/* mode */
221913bd3e6fSRicardo Ribalda Delgado 	if (port->rs485.flags & SER_RS485_ENABLED) {
22204e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_TTGR,
22214e7decdaSCyrille Pitchen 				  port->rs485.delay_rts_after_send);
2222ab4382d2SGreg Kroah-Hartman 		mode |= ATMEL_US_USMODE_RS485;
2223377fedd1SNicolas Ferre 	} else if (port->iso7816.flags & SER_ISO7816_ENABLED) {
2224377fedd1SNicolas Ferre 		atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg);
2225377fedd1SNicolas Ferre 		/* select mck clock, and output  */
2226377fedd1SNicolas Ferre 		mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
2227377fedd1SNicolas Ferre 		/* set max iterations */
2228377fedd1SNicolas Ferre 		mode |= ATMEL_US_MAX_ITER(3);
2229377fedd1SNicolas Ferre 		if ((port->iso7816.flags & SER_ISO7816_T_PARAM)
2230377fedd1SNicolas Ferre 				== SER_ISO7816_T(0))
2231377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T0;
2232377fedd1SNicolas Ferre 		else
2233377fedd1SNicolas Ferre 			mode |= ATMEL_US_USMODE_ISO7816_T1;
22341cf6e8fcSCyrille Pitchen 	} else if (termios->c_cflag & CRTSCTS) {
22351cf6e8fcSCyrille Pitchen 		/* RS232 with hardware handshake (RTS/CTS) */
22369bcffe75SRichard Genoud 		if (atmel_use_fifo(port) &&
22379bcffe75SRichard Genoud 		    !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
22389bcffe75SRichard Genoud 			/*
22399bcffe75SRichard Genoud 			 * with ATMEL_US_USMODE_HWHS set, the controller will
22409bcffe75SRichard Genoud 			 * be able to drive the RTS pin high/low when the RX
22419bcffe75SRichard Genoud 			 * FIFO is above RXFTHRES/below RXFTHRES2.
22429bcffe75SRichard Genoud 			 * It will also disable the transmitter when the CTS
22439bcffe75SRichard Genoud 			 * pin is high.
22449bcffe75SRichard Genoud 			 * This mode is not activated if CTS pin is a GPIO
22459bcffe75SRichard Genoud 			 * because in this case, the transmitter is always
22469bcffe75SRichard Genoud 			 * disabled (there must be an internal pull-up
22479bcffe75SRichard Genoud 			 * responsible for this behaviour).
22489bcffe75SRichard Genoud 			 * If the RTS pin is a GPIO, the controller won't be
22499bcffe75SRichard Genoud 			 * able to drive it according to the FIFO thresholds,
22509bcffe75SRichard Genoud 			 * but it will be handled by the driver.
22519bcffe75SRichard Genoud 			 */
22521cf6e8fcSCyrille Pitchen 			mode |= ATMEL_US_USMODE_HWHS;
22539bcffe75SRichard Genoud 		} else {
22549bcffe75SRichard Genoud 			/*
22559bcffe75SRichard Genoud 			 * For platforms without FIFO, the flow control is
22569bcffe75SRichard Genoud 			 * handled by the driver.
22579bcffe75SRichard Genoud 			 */
22589bcffe75SRichard Genoud 			mode |= ATMEL_US_USMODE_NORMAL;
22595be605acSAlexandre Belloni 		}
22601cf6e8fcSCyrille Pitchen 	} else {
22611cf6e8fcSCyrille Pitchen 		/* RS232 without hadware handshake */
22621cf6e8fcSCyrille Pitchen 		mode |= ATMEL_US_USMODE_NORMAL;
2263ab4382d2SGreg Kroah-Hartman 	}
2264ab4382d2SGreg Kroah-Hartman 
22656dbd54e4SGreg Kroah-Hartman 	/* set the mode, clock divisor, parity, stop bits and data size */
22666dbd54e4SGreg Kroah-Hartman 	atmel_uart_writel(port, ATMEL_US_MR, mode);
22676dbd54e4SGreg Kroah-Hartman 
22681cf6e8fcSCyrille Pitchen 	/*
22691cf6e8fcSCyrille Pitchen 	 * when switching the mode, set the RTS line state according to the
22701cf6e8fcSCyrille Pitchen 	 * new mode, otherwise keep the former state
22711cf6e8fcSCyrille Pitchen 	 */
22721cf6e8fcSCyrille Pitchen 	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
22731cf6e8fcSCyrille Pitchen 		unsigned int rts_state;
22741cf6e8fcSCyrille Pitchen 
22751cf6e8fcSCyrille Pitchen 		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
22761cf6e8fcSCyrille Pitchen 			/* let the hardware control the RTS line */
22771cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSDIS;
22781cf6e8fcSCyrille Pitchen 		} else {
22791cf6e8fcSCyrille Pitchen 			/* force RTS line to low level */
22801cf6e8fcSCyrille Pitchen 			rts_state = ATMEL_US_RTSEN;
22811cf6e8fcSCyrille Pitchen 		}
22821cf6e8fcSCyrille Pitchen 
22834e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
22841cf6e8fcSCyrille Pitchen 	}
22851cf6e8fcSCyrille Pitchen 
22865bf5635aSLudovic Desroches 	/*
22875bf5635aSLudovic Desroches 	 * Set the baud rate:
22885bf5635aSLudovic Desroches 	 * Fractional baudrate allows to setup output frequency more
22895bf5635aSLudovic Desroches 	 * accurately. This feature is enabled only when using normal mode.
22905bf5635aSLudovic Desroches 	 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
22915bf5635aSLudovic Desroches 	 * Currently, OVER is always set to 0 so we get
229236131cdfSAlexey Starikovskiy 	 * baudrate = selected clock / (16 * (CD + FP / 8))
229336131cdfSAlexey Starikovskiy 	 * then
229436131cdfSAlexey Starikovskiy 	 * 8 CD + FP = selected clock / (2 * baudrate)
22955bf5635aSLudovic Desroches 	 */
22962867af2dSRomain Izard 	if (atmel_port->has_frac_baudrate) {
229736131cdfSAlexey Starikovskiy 		div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
229836131cdfSAlexey Starikovskiy 		cd = div >> 3;
229936131cdfSAlexey Starikovskiy 		fp = div & ATMEL_US_FP_MASK;
23005bf5635aSLudovic Desroches 	} else {
23015bf5635aSLudovic Desroches 		cd = uart_get_divisor(port, baud);
23025bf5635aSLudovic Desroches 	}
23035bf5635aSLudovic Desroches 
23045bf5635aSLudovic Desroches 	if (cd > 65535) {	/* BRGR is 16-bit, so switch to slower clock */
23055bf5635aSLudovic Desroches 		cd /= 8;
23065bf5635aSLudovic Desroches 		mode |= ATMEL_US_USCLKS_MCK_DIV8;
23075bf5635aSLudovic Desroches 	}
23085bf5635aSLudovic Desroches 	quot = cd | fp << ATMEL_US_FP_OFFSET;
23095bf5635aSLudovic Desroches 
2310377fedd1SNicolas Ferre 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
23114e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
23124e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
23134e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2314ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2315ab4382d2SGreg Kroah-Hartman 
2316ab4382d2SGreg Kroah-Hartman 	/* restore interrupts */
23174e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2318ab4382d2SGreg Kroah-Hartman 
2319ab4382d2SGreg Kroah-Hartman 	/* CTS flow-control and modem-status interrupts */
2320ab4382d2SGreg Kroah-Hartman 	if (UART_ENABLE_MS(port, termios->c_cflag))
232135b675b9SRichard Genoud 		atmel_enable_ms(port);
232235b675b9SRichard Genoud 	else
232335b675b9SRichard Genoud 		atmel_disable_ms(port);
2324ab4382d2SGreg Kroah-Hartman 
2325ab4382d2SGreg Kroah-Hartman 	spin_unlock_irqrestore(&port->lock, flags);
2326ab4382d2SGreg Kroah-Hartman }
2327ab4382d2SGreg Kroah-Hartman 
2328732a84a0SPeter Hurley static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
232942bd7a4fSViktar Palstsiuk {
2330732a84a0SPeter Hurley 	if (termios->c_line == N_PPS) {
233142bd7a4fSViktar Palstsiuk 		port->flags |= UPF_HARDPPS_CD;
2332d41510ceSPeter Hurley 		spin_lock_irq(&port->lock);
233342bd7a4fSViktar Palstsiuk 		atmel_enable_ms(port);
2334d41510ceSPeter Hurley 		spin_unlock_irq(&port->lock);
233542bd7a4fSViktar Palstsiuk 	} else {
233642bd7a4fSViktar Palstsiuk 		port->flags &= ~UPF_HARDPPS_CD;
2337cab68f89SPeter Hurley 		if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2338cab68f89SPeter Hurley 			spin_lock_irq(&port->lock);
2339cab68f89SPeter Hurley 			atmel_disable_ms(port);
2340cab68f89SPeter Hurley 			spin_unlock_irq(&port->lock);
2341cab68f89SPeter Hurley 		}
234242bd7a4fSViktar Palstsiuk 	}
234342bd7a4fSViktar Palstsiuk }
234442bd7a4fSViktar Palstsiuk 
2345ab4382d2SGreg Kroah-Hartman /*
2346ab4382d2SGreg Kroah-Hartman  * Return string describing the specified port
2347ab4382d2SGreg Kroah-Hartman  */
2348ab4382d2SGreg Kroah-Hartman static const char *atmel_type(struct uart_port *port)
2349ab4382d2SGreg Kroah-Hartman {
2350ab4382d2SGreg Kroah-Hartman 	return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2351ab4382d2SGreg Kroah-Hartman }
2352ab4382d2SGreg Kroah-Hartman 
2353ab4382d2SGreg Kroah-Hartman /*
2354ab4382d2SGreg Kroah-Hartman  * Release the memory region(s) being used by 'port'.
2355ab4382d2SGreg Kroah-Hartman  */
2356ab4382d2SGreg Kroah-Hartman static void atmel_release_port(struct uart_port *port)
2357ab4382d2SGreg Kroah-Hartman {
2358c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2359c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2360ab4382d2SGreg Kroah-Hartman 
2361ab4382d2SGreg Kroah-Hartman 	release_mem_region(port->mapbase, size);
2362ab4382d2SGreg Kroah-Hartman 
2363ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2364ab4382d2SGreg Kroah-Hartman 		iounmap(port->membase);
2365ab4382d2SGreg Kroah-Hartman 		port->membase = NULL;
2366ab4382d2SGreg Kroah-Hartman 	}
2367ab4382d2SGreg Kroah-Hartman }
2368ab4382d2SGreg Kroah-Hartman 
2369ab4382d2SGreg Kroah-Hartman /*
2370ab4382d2SGreg Kroah-Hartman  * Request the memory region(s) being used by 'port'.
2371ab4382d2SGreg Kroah-Hartman  */
2372ab4382d2SGreg Kroah-Hartman static int atmel_request_port(struct uart_port *port)
2373ab4382d2SGreg Kroah-Hartman {
2374c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(port->dev->parent);
2375c24d2531SRadu Pirea 	int size = resource_size(mpdev->resource);
2376ab4382d2SGreg Kroah-Hartman 
2377ab4382d2SGreg Kroah-Hartman 	if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2378ab4382d2SGreg Kroah-Hartman 		return -EBUSY;
2379ab4382d2SGreg Kroah-Hartman 
2380ab4382d2SGreg Kroah-Hartman 	if (port->flags & UPF_IOREMAP) {
2381ab4382d2SGreg Kroah-Hartman 		port->membase = ioremap(port->mapbase, size);
2382ab4382d2SGreg Kroah-Hartman 		if (port->membase == NULL) {
2383ab4382d2SGreg Kroah-Hartman 			release_mem_region(port->mapbase, size);
2384ab4382d2SGreg Kroah-Hartman 			return -ENOMEM;
2385ab4382d2SGreg Kroah-Hartman 		}
2386ab4382d2SGreg Kroah-Hartman 	}
2387ab4382d2SGreg Kroah-Hartman 
2388ab4382d2SGreg Kroah-Hartman 	return 0;
2389ab4382d2SGreg Kroah-Hartman }
2390ab4382d2SGreg Kroah-Hartman 
2391ab4382d2SGreg Kroah-Hartman /*
2392ab4382d2SGreg Kroah-Hartman  * Configure/autoconfigure the port.
2393ab4382d2SGreg Kroah-Hartman  */
2394ab4382d2SGreg Kroah-Hartman static void atmel_config_port(struct uart_port *port, int flags)
2395ab4382d2SGreg Kroah-Hartman {
2396ab4382d2SGreg Kroah-Hartman 	if (flags & UART_CONFIG_TYPE) {
2397ab4382d2SGreg Kroah-Hartman 		port->type = PORT_ATMEL;
2398ab4382d2SGreg Kroah-Hartman 		atmel_request_port(port);
2399ab4382d2SGreg Kroah-Hartman 	}
2400ab4382d2SGreg Kroah-Hartman }
2401ab4382d2SGreg Kroah-Hartman 
2402ab4382d2SGreg Kroah-Hartman /*
2403ab4382d2SGreg Kroah-Hartman  * Verify the new serial_struct (for TIOCSSERIAL).
2404ab4382d2SGreg Kroah-Hartman  */
2405ab4382d2SGreg Kroah-Hartman static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2406ab4382d2SGreg Kroah-Hartman {
2407ab4382d2SGreg Kroah-Hartman 	int ret = 0;
2408ab4382d2SGreg Kroah-Hartman 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2409ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2410ab4382d2SGreg Kroah-Hartman 	if (port->irq != ser->irq)
2411ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2412ab4382d2SGreg Kroah-Hartman 	if (ser->io_type != SERIAL_IO_MEM)
2413ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2414ab4382d2SGreg Kroah-Hartman 	if (port->uartclk / 16 != ser->baud_base)
2415ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2416270c2adeSAndre Przywara 	if (port->mapbase != (unsigned long)ser->iomem_base)
2417ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2418ab4382d2SGreg Kroah-Hartman 	if (port->iobase != ser->port)
2419ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2420ab4382d2SGreg Kroah-Hartman 	if (ser->hub6 != 0)
2421ab4382d2SGreg Kroah-Hartman 		ret = -EINVAL;
2422ab4382d2SGreg Kroah-Hartman 	return ret;
2423ab4382d2SGreg Kroah-Hartman }
2424ab4382d2SGreg Kroah-Hartman 
2425ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2426ab4382d2SGreg Kroah-Hartman static int atmel_poll_get_char(struct uart_port *port)
2427ab4382d2SGreg Kroah-Hartman {
24284e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2429ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2430ab4382d2SGreg Kroah-Hartman 
2431a6499435SCyrille Pitchen 	return atmel_uart_read_char(port);
2432ab4382d2SGreg Kroah-Hartman }
2433ab4382d2SGreg Kroah-Hartman 
2434ab4382d2SGreg Kroah-Hartman static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2435ab4382d2SGreg Kroah-Hartman {
24364e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2437ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2438ab4382d2SGreg Kroah-Hartman 
2439a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2440ab4382d2SGreg Kroah-Hartman }
2441ab4382d2SGreg Kroah-Hartman #endif
2442ab4382d2SGreg Kroah-Hartman 
24435c7dcdb6SJulia Lawall static const struct uart_ops atmel_pops = {
2444ab4382d2SGreg Kroah-Hartman 	.tx_empty	= atmel_tx_empty,
2445ab4382d2SGreg Kroah-Hartman 	.set_mctrl	= atmel_set_mctrl,
2446ab4382d2SGreg Kroah-Hartman 	.get_mctrl	= atmel_get_mctrl,
2447ab4382d2SGreg Kroah-Hartman 	.stop_tx	= atmel_stop_tx,
2448ab4382d2SGreg Kroah-Hartman 	.start_tx	= atmel_start_tx,
2449ab4382d2SGreg Kroah-Hartman 	.stop_rx	= atmel_stop_rx,
2450ab4382d2SGreg Kroah-Hartman 	.enable_ms	= atmel_enable_ms,
2451ab4382d2SGreg Kroah-Hartman 	.break_ctl	= atmel_break_ctl,
2452ab4382d2SGreg Kroah-Hartman 	.startup	= atmel_startup,
2453ab4382d2SGreg Kroah-Hartman 	.shutdown	= atmel_shutdown,
2454ab4382d2SGreg Kroah-Hartman 	.flush_buffer	= atmel_flush_buffer,
2455ab4382d2SGreg Kroah-Hartman 	.set_termios	= atmel_set_termios,
245642bd7a4fSViktar Palstsiuk 	.set_ldisc	= atmel_set_ldisc,
2457ab4382d2SGreg Kroah-Hartman 	.type		= atmel_type,
2458ab4382d2SGreg Kroah-Hartman 	.release_port	= atmel_release_port,
2459ab4382d2SGreg Kroah-Hartman 	.request_port	= atmel_request_port,
2460ab4382d2SGreg Kroah-Hartman 	.config_port	= atmel_config_port,
2461ab4382d2SGreg Kroah-Hartman 	.verify_port	= atmel_verify_port,
2462ab4382d2SGreg Kroah-Hartman 	.pm		= atmel_serial_pm,
2463ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
2464ab4382d2SGreg Kroah-Hartman 	.poll_get_char	= atmel_poll_get_char,
2465ab4382d2SGreg Kroah-Hartman 	.poll_put_char	= atmel_poll_put_char,
2466ab4382d2SGreg Kroah-Hartman #endif
2467ab4382d2SGreg Kroah-Hartman };
2468ab4382d2SGreg Kroah-Hartman 
2469ab4382d2SGreg Kroah-Hartman /*
2470ab4382d2SGreg Kroah-Hartman  * Configure the port from the platform device resource info.
2471ab4382d2SGreg Kroah-Hartman  */
247291f8c2d8SBoris BREZILLON static int atmel_init_port(struct atmel_uart_port *atmel_port,
2473ab4382d2SGreg Kroah-Hartman 				      struct platform_device *pdev)
2474ab4382d2SGreg Kroah-Hartman {
247591f8c2d8SBoris BREZILLON 	int ret;
2476ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_port->uart;
2477c24d2531SRadu Pirea 	struct platform_device *mpdev = to_platform_device(pdev->dev.parent);
2478ab4382d2SGreg Kroah-Hartman 
24794a1e8888SLeilei Zhao 	atmel_init_property(atmel_port, pdev);
2480a930e528SElen Song 	atmel_set_ops(port);
2481a930e528SElen Song 
2482c24d2531SRadu Pirea 	uart_get_rs485_mode(&mpdev->dev, &port->rs485);
248333d64c4fSElen Song 
2484ab4382d2SGreg Kroah-Hartman 	port->iotype		= UPIO_MEM;
248592c8f7c0SAlexandre Belloni 	port->flags		= UPF_BOOT_AUTOCONF | UPF_IOREMAP;
2486ab4382d2SGreg Kroah-Hartman 	port->ops		= &atmel_pops;
2487ab4382d2SGreg Kroah-Hartman 	port->fifosize		= 1;
2488ab4382d2SGreg Kroah-Hartman 	port->dev		= &pdev->dev;
2489c24d2531SRadu Pirea 	port->mapbase		= mpdev->resource[0].start;
2490c24d2531SRadu Pirea 	port->irq		= mpdev->resource[1].start;
249113bd3e6fSRicardo Ribalda Delgado 	port->rs485_config	= atmel_config_rs485;
2492377fedd1SNicolas Ferre 	port->iso7816_config	= atmel_config_iso7816;
249392c8f7c0SAlexandre Belloni 	port->membase		= NULL;
2494ab4382d2SGreg Kroah-Hartman 
2495ab4382d2SGreg Kroah-Hartman 	memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2496ab4382d2SGreg Kroah-Hartman 
2497ab4382d2SGreg Kroah-Hartman 	/* for console, the clock could already be configured */
2498ab4382d2SGreg Kroah-Hartman 	if (!atmel_port->clk) {
2499c24d2531SRadu Pirea 		atmel_port->clk = clk_get(&mpdev->dev, "usart");
250091f8c2d8SBoris BREZILLON 		if (IS_ERR(atmel_port->clk)) {
250191f8c2d8SBoris BREZILLON 			ret = PTR_ERR(atmel_port->clk);
250291f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
250391f8c2d8SBoris BREZILLON 			return ret;
250491f8c2d8SBoris BREZILLON 		}
250591f8c2d8SBoris BREZILLON 		ret = clk_prepare_enable(atmel_port->clk);
250691f8c2d8SBoris BREZILLON 		if (ret) {
250791f8c2d8SBoris BREZILLON 			clk_put(atmel_port->clk);
250891f8c2d8SBoris BREZILLON 			atmel_port->clk = NULL;
250991f8c2d8SBoris BREZILLON 			return ret;
251091f8c2d8SBoris BREZILLON 		}
2511ab4382d2SGreg Kroah-Hartman 		port->uartclk = clk_get_rate(atmel_port->clk);
251291f8c2d8SBoris BREZILLON 		clk_disable_unprepare(atmel_port->clk);
2513ab4382d2SGreg Kroah-Hartman 		/* only enable clock when USART is in use */
2514ab4382d2SGreg Kroah-Hartman 	}
2515ab4382d2SGreg Kroah-Hartman 
2516377fedd1SNicolas Ferre 	/*
2517377fedd1SNicolas Ferre 	 * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
2518377fedd1SNicolas Ferre 	 * ENDTX|TXBUFE
2519377fedd1SNicolas Ferre 	 */
2520377fedd1SNicolas Ferre 	if (port->rs485.flags & SER_RS485_ENABLED ||
2521377fedd1SNicolas Ferre 	    port->iso7816.flags & SER_ISO7816_ENABLED)
2522ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
252364e22ebeSElen Song 	else if (atmel_use_pdc_tx(port)) {
2524ab4382d2SGreg Kroah-Hartman 		port->fifosize = PDC_BUFFER_SIZE;
2525ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2526ab4382d2SGreg Kroah-Hartman 	} else {
2527ab4382d2SGreg Kroah-Hartman 		atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2528ab4382d2SGreg Kroah-Hartman 	}
252991f8c2d8SBoris BREZILLON 
253091f8c2d8SBoris BREZILLON 	return 0;
2531ab4382d2SGreg Kroah-Hartman }
2532ab4382d2SGreg Kroah-Hartman 
2533ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2534ab4382d2SGreg Kroah-Hartman static void atmel_console_putchar(struct uart_port *port, int ch)
2535ab4382d2SGreg Kroah-Hartman {
25364e7decdaSCyrille Pitchen 	while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2537ab4382d2SGreg Kroah-Hartman 		cpu_relax();
2538a6499435SCyrille Pitchen 	atmel_uart_write_char(port, ch);
2539ab4382d2SGreg Kroah-Hartman }
2540ab4382d2SGreg Kroah-Hartman 
2541ab4382d2SGreg Kroah-Hartman /*
2542ab4382d2SGreg Kroah-Hartman  * Interrupts are disabled on entering
2543ab4382d2SGreg Kroah-Hartman  */
2544ab4382d2SGreg Kroah-Hartman static void atmel_console_write(struct console *co, const char *s, u_int count)
2545ab4382d2SGreg Kroah-Hartman {
2546ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2547ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2548ab4382d2SGreg Kroah-Hartman 	unsigned int status, imr;
2549ab4382d2SGreg Kroah-Hartman 	unsigned int pdc_tx;
2550ab4382d2SGreg Kroah-Hartman 
2551ab4382d2SGreg Kroah-Hartman 	/*
2552ab4382d2SGreg Kroah-Hartman 	 * First, save IMR and then disable interrupts
2553ab4382d2SGreg Kroah-Hartman 	 */
25544e7decdaSCyrille Pitchen 	imr = atmel_uart_readl(port, ATMEL_US_IMR);
25554e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR,
25564e7decdaSCyrille Pitchen 			  ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2557ab4382d2SGreg Kroah-Hartman 
2558ab4382d2SGreg Kroah-Hartman 	/* Store PDC transmit status and disable it */
25594e7decdaSCyrille Pitchen 	pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
25604e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2561ab4382d2SGreg Kroah-Hartman 
2562497e1e16SNicolas Ferre 	/* Make sure that tx path is actually able to send characters */
2563497e1e16SNicolas Ferre 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2564ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2565497e1e16SNicolas Ferre 
2566ab4382d2SGreg Kroah-Hartman 	uart_console_write(port, s, count, atmel_console_putchar);
2567ab4382d2SGreg Kroah-Hartman 
2568ab4382d2SGreg Kroah-Hartman 	/*
2569ab4382d2SGreg Kroah-Hartman 	 * Finally, wait for transmitter to become empty
2570ab4382d2SGreg Kroah-Hartman 	 * and restore IMR
2571ab4382d2SGreg Kroah-Hartman 	 */
2572ab4382d2SGreg Kroah-Hartman 	do {
25734e7decdaSCyrille Pitchen 		status = atmel_uart_readl(port, ATMEL_US_CSR);
2574ab4382d2SGreg Kroah-Hartman 	} while (!(status & ATMEL_US_TXRDY));
2575ab4382d2SGreg Kroah-Hartman 
2576ab4382d2SGreg Kroah-Hartman 	/* Restore PDC transmit status */
2577ab4382d2SGreg Kroah-Hartman 	if (pdc_tx)
25784e7decdaSCyrille Pitchen 		atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2579ab4382d2SGreg Kroah-Hartman 
2580ab4382d2SGreg Kroah-Hartman 	/* set interrupts back the way they were */
25814e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IER, imr);
2582ab4382d2SGreg Kroah-Hartman }
2583ab4382d2SGreg Kroah-Hartman 
2584ab4382d2SGreg Kroah-Hartman /*
2585ab4382d2SGreg Kroah-Hartman  * If the port was already initialised (eg, by a boot loader),
2586ab4382d2SGreg Kroah-Hartman  * try to determine the current setup.
2587ab4382d2SGreg Kroah-Hartman  */
2588ab4382d2SGreg Kroah-Hartman static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2589ab4382d2SGreg Kroah-Hartman 					     int *parity, int *bits)
2590ab4382d2SGreg Kroah-Hartman {
2591ab4382d2SGreg Kroah-Hartman 	unsigned int mr, quot;
2592ab4382d2SGreg Kroah-Hartman 
2593ab4382d2SGreg Kroah-Hartman 	/*
2594ab4382d2SGreg Kroah-Hartman 	 * If the baud rate generator isn't running, the port wasn't
2595ab4382d2SGreg Kroah-Hartman 	 * initialized by the boot loader.
2596ab4382d2SGreg Kroah-Hartman 	 */
25974e7decdaSCyrille Pitchen 	quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2598ab4382d2SGreg Kroah-Hartman 	if (!quot)
2599ab4382d2SGreg Kroah-Hartman 		return;
2600ab4382d2SGreg Kroah-Hartman 
26014e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2602ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_CHRL_8)
2603ab4382d2SGreg Kroah-Hartman 		*bits = 8;
2604ab4382d2SGreg Kroah-Hartman 	else
2605ab4382d2SGreg Kroah-Hartman 		*bits = 7;
2606ab4382d2SGreg Kroah-Hartman 
26074e7decdaSCyrille Pitchen 	mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2608ab4382d2SGreg Kroah-Hartman 	if (mr == ATMEL_US_PAR_EVEN)
2609ab4382d2SGreg Kroah-Hartman 		*parity = 'e';
2610ab4382d2SGreg Kroah-Hartman 	else if (mr == ATMEL_US_PAR_ODD)
2611ab4382d2SGreg Kroah-Hartman 		*parity = 'o';
2612ab4382d2SGreg Kroah-Hartman 
2613ab4382d2SGreg Kroah-Hartman 	/*
2614ab4382d2SGreg Kroah-Hartman 	 * The serial core only rounds down when matching this to a
2615ab4382d2SGreg Kroah-Hartman 	 * supported baud rate. Make sure we don't end up slightly
2616ab4382d2SGreg Kroah-Hartman 	 * lower than one of those, as it would make us fall through
2617ab4382d2SGreg Kroah-Hartman 	 * to a much lower baud rate than we really want.
2618ab4382d2SGreg Kroah-Hartman 	 */
2619ab4382d2SGreg Kroah-Hartman 	*baud = port->uartclk / (16 * (quot - 1));
2620ab4382d2SGreg Kroah-Hartman }
2621ab4382d2SGreg Kroah-Hartman 
2622ab4382d2SGreg Kroah-Hartman static int __init atmel_console_setup(struct console *co, char *options)
2623ab4382d2SGreg Kroah-Hartman {
262491f8c2d8SBoris BREZILLON 	int ret;
2625ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = &atmel_ports[co->index].uart;
2626ea04f82aSRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2627ab4382d2SGreg Kroah-Hartman 	int baud = 115200;
2628ab4382d2SGreg Kroah-Hartman 	int bits = 8;
2629ab4382d2SGreg Kroah-Hartman 	int parity = 'n';
2630ab4382d2SGreg Kroah-Hartman 	int flow = 'n';
2631ab4382d2SGreg Kroah-Hartman 
2632ab4382d2SGreg Kroah-Hartman 	if (port->membase == NULL) {
2633ab4382d2SGreg Kroah-Hartman 		/* Port not initialized yet - delay setup */
2634ab4382d2SGreg Kroah-Hartman 		return -ENODEV;
2635ab4382d2SGreg Kroah-Hartman 	}
2636ab4382d2SGreg Kroah-Hartman 
263791f8c2d8SBoris BREZILLON 	ret = clk_prepare_enable(atmel_ports[co->index].clk);
263891f8c2d8SBoris BREZILLON 	if (ret)
263991f8c2d8SBoris BREZILLON 		return ret;
2640ab4382d2SGreg Kroah-Hartman 
26414e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_IDR, -1);
26424e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
26434e7decdaSCyrille Pitchen 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2644ea04f82aSRomain Izard 	atmel_port->tx_stopped = false;
2645ab4382d2SGreg Kroah-Hartman 
2646ab4382d2SGreg Kroah-Hartman 	if (options)
2647ab4382d2SGreg Kroah-Hartman 		uart_parse_options(options, &baud, &parity, &bits, &flow);
2648ab4382d2SGreg Kroah-Hartman 	else
2649ab4382d2SGreg Kroah-Hartman 		atmel_console_get_options(port, &baud, &parity, &bits);
2650ab4382d2SGreg Kroah-Hartman 
2651ab4382d2SGreg Kroah-Hartman 	return uart_set_options(port, co, baud, parity, bits, flow);
2652ab4382d2SGreg Kroah-Hartman }
2653ab4382d2SGreg Kroah-Hartman 
2654ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart;
2655ab4382d2SGreg Kroah-Hartman 
2656ab4382d2SGreg Kroah-Hartman static struct console atmel_console = {
2657ab4382d2SGreg Kroah-Hartman 	.name		= ATMEL_DEVICENAME,
2658ab4382d2SGreg Kroah-Hartman 	.write		= atmel_console_write,
2659ab4382d2SGreg Kroah-Hartman 	.device		= uart_console_device,
2660ab4382d2SGreg Kroah-Hartman 	.setup		= atmel_console_setup,
2661ab4382d2SGreg Kroah-Hartman 	.flags		= CON_PRINTBUFFER,
2662ab4382d2SGreg Kroah-Hartman 	.index		= -1,
2663ab4382d2SGreg Kroah-Hartman 	.data		= &atmel_uart,
2664ab4382d2SGreg Kroah-Hartman };
2665ab4382d2SGreg Kroah-Hartman 
2666ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	(&atmel_console)
2667ab4382d2SGreg Kroah-Hartman 
2668ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2669ab4382d2SGreg Kroah-Hartman {
2670ab4382d2SGreg Kroah-Hartman 	return port->cons && port->cons->index == port->line;
2671ab4382d2SGreg Kroah-Hartman }
2672ab4382d2SGreg Kroah-Hartman 
2673ab4382d2SGreg Kroah-Hartman #else
2674ab4382d2SGreg Kroah-Hartman #define ATMEL_CONSOLE_DEVICE	NULL
2675ab4382d2SGreg Kroah-Hartman 
2676ab4382d2SGreg Kroah-Hartman static inline bool atmel_is_console_port(struct uart_port *port)
2677ab4382d2SGreg Kroah-Hartman {
2678ab4382d2SGreg Kroah-Hartman 	return false;
2679ab4382d2SGreg Kroah-Hartman }
2680ab4382d2SGreg Kroah-Hartman #endif
2681ab4382d2SGreg Kroah-Hartman 
2682ab4382d2SGreg Kroah-Hartman static struct uart_driver atmel_uart = {
2683ab4382d2SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
2684ab4382d2SGreg Kroah-Hartman 	.driver_name	= "atmel_serial",
2685ab4382d2SGreg Kroah-Hartman 	.dev_name	= ATMEL_DEVICENAME,
2686ab4382d2SGreg Kroah-Hartman 	.major		= SERIAL_ATMEL_MAJOR,
2687ab4382d2SGreg Kroah-Hartman 	.minor		= MINOR_START,
2688ab4382d2SGreg Kroah-Hartman 	.nr		= ATMEL_MAX_UART,
2689ab4382d2SGreg Kroah-Hartman 	.cons		= ATMEL_CONSOLE_DEVICE,
2690ab4382d2SGreg Kroah-Hartman };
2691ab4382d2SGreg Kroah-Hartman 
2692ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_PM
2693ab4382d2SGreg Kroah-Hartman static bool atmel_serial_clk_will_stop(void)
2694ab4382d2SGreg Kroah-Hartman {
2695ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_ARCH_AT91
2696ab4382d2SGreg Kroah-Hartman 	return at91_suspend_entering_slow_clock();
2697ab4382d2SGreg Kroah-Hartman #else
2698ab4382d2SGreg Kroah-Hartman 	return false;
2699ab4382d2SGreg Kroah-Hartman #endif
2700ab4382d2SGreg Kroah-Hartman }
2701ab4382d2SGreg Kroah-Hartman 
2702ab4382d2SGreg Kroah-Hartman static int atmel_serial_suspend(struct platform_device *pdev,
2703ab4382d2SGreg Kroah-Hartman 				pm_message_t state)
2704ab4382d2SGreg Kroah-Hartman {
2705ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2706ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2707ab4382d2SGreg Kroah-Hartman 
2708ab4382d2SGreg Kroah-Hartman 	if (atmel_is_console_port(port) && console_suspend_enabled) {
2709ab4382d2SGreg Kroah-Hartman 		/* Drain the TX shifter */
27104e7decdaSCyrille Pitchen 		while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
27114e7decdaSCyrille Pitchen 			 ATMEL_US_TXEMPTY))
2712ab4382d2SGreg Kroah-Hartman 			cpu_relax();
2713ab4382d2SGreg Kroah-Hartman 	}
2714ab4382d2SGreg Kroah-Hartman 
27156a5f0e2fSAlexandre Belloni 	if (atmel_is_console_port(port) && !console_suspend_enabled) {
27166a5f0e2fSAlexandre Belloni 		/* Cache register values as we won't get a full shutdown/startup
27176a5f0e2fSAlexandre Belloni 		 * cycle
27186a5f0e2fSAlexandre Belloni 		 */
27196a5f0e2fSAlexandre Belloni 		atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR);
27206a5f0e2fSAlexandre Belloni 		atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR);
27216a5f0e2fSAlexandre Belloni 		atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
27226a5f0e2fSAlexandre Belloni 		atmel_port->cache.rtor = atmel_uart_readl(port,
27236a5f0e2fSAlexandre Belloni 							  atmel_port->rtor);
27246a5f0e2fSAlexandre Belloni 		atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR);
27256a5f0e2fSAlexandre Belloni 		atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR);
27266a5f0e2fSAlexandre Belloni 		atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR);
27276a5f0e2fSAlexandre Belloni 	}
27286a5f0e2fSAlexandre Belloni 
2729ab4382d2SGreg Kroah-Hartman 	/* we can not wake up if we're running on slow clock */
2730ab4382d2SGreg Kroah-Hartman 	atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
27312c7af5baSBoris BREZILLON 	if (atmel_serial_clk_will_stop()) {
27322c7af5baSBoris BREZILLON 		unsigned long flags;
27332c7af5baSBoris BREZILLON 
27342c7af5baSBoris BREZILLON 		spin_lock_irqsave(&atmel_port->lock_suspended, flags);
27352c7af5baSBoris BREZILLON 		atmel_port->suspended = true;
27362c7af5baSBoris BREZILLON 		spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2737ab4382d2SGreg Kroah-Hartman 		device_set_wakeup_enable(&pdev->dev, 0);
27382c7af5baSBoris BREZILLON 	}
2739ab4382d2SGreg Kroah-Hartman 
2740ab4382d2SGreg Kroah-Hartman 	uart_suspend_port(&atmel_uart, port);
2741ab4382d2SGreg Kroah-Hartman 
2742ab4382d2SGreg Kroah-Hartman 	return 0;
2743ab4382d2SGreg Kroah-Hartman }
2744ab4382d2SGreg Kroah-Hartman 
2745ab4382d2SGreg Kroah-Hartman static int atmel_serial_resume(struct platform_device *pdev)
2746ab4382d2SGreg Kroah-Hartman {
2747ab4382d2SGreg Kroah-Hartman 	struct uart_port *port = platform_get_drvdata(pdev);
2748ab4382d2SGreg Kroah-Hartman 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
27492c7af5baSBoris BREZILLON 	unsigned long flags;
27502c7af5baSBoris BREZILLON 
27516a5f0e2fSAlexandre Belloni 	if (atmel_is_console_port(port) && !console_suspend_enabled) {
27526a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
27536a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
27546a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
27556a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, atmel_port->rtor,
27566a5f0e2fSAlexandre Belloni 				  atmel_port->cache.rtor);
27576a5f0e2fSAlexandre Belloni 		atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr);
27586a5f0e2fSAlexandre Belloni 
27596a5f0e2fSAlexandre Belloni 		if (atmel_port->fifo_size) {
27606a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN |
27616a5f0e2fSAlexandre Belloni 					  ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR);
27626a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FMR,
27636a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fmr);
27646a5f0e2fSAlexandre Belloni 			atmel_uart_writel(port, ATMEL_US_FIER,
27656a5f0e2fSAlexandre Belloni 					  atmel_port->cache.fimr);
27666a5f0e2fSAlexandre Belloni 		}
27676a5f0e2fSAlexandre Belloni 		atmel_start_rx(port);
27686a5f0e2fSAlexandre Belloni 	}
27696a5f0e2fSAlexandre Belloni 
27702c7af5baSBoris BREZILLON 	spin_lock_irqsave(&atmel_port->lock_suspended, flags);
27712c7af5baSBoris BREZILLON 	if (atmel_port->pending) {
27722c7af5baSBoris BREZILLON 		atmel_handle_receive(port, atmel_port->pending);
27732c7af5baSBoris BREZILLON 		atmel_handle_status(port, atmel_port->pending,
27742c7af5baSBoris BREZILLON 				    atmel_port->pending_status);
27752c7af5baSBoris BREZILLON 		atmel_handle_transmit(port, atmel_port->pending);
27762c7af5baSBoris BREZILLON 		atmel_port->pending = 0;
27772c7af5baSBoris BREZILLON 	}
27782c7af5baSBoris BREZILLON 	atmel_port->suspended = false;
27792c7af5baSBoris BREZILLON 	spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2780ab4382d2SGreg Kroah-Hartman 
2781ab4382d2SGreg Kroah-Hartman 	uart_resume_port(&atmel_uart, port);
2782ab4382d2SGreg Kroah-Hartman 	device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2783ab4382d2SGreg Kroah-Hartman 
2784ab4382d2SGreg Kroah-Hartman 	return 0;
2785ab4382d2SGreg Kroah-Hartman }
2786ab4382d2SGreg Kroah-Hartman #else
2787ab4382d2SGreg Kroah-Hartman #define atmel_serial_suspend NULL
2788ab4382d2SGreg Kroah-Hartman #define atmel_serial_resume NULL
2789ab4382d2SGreg Kroah-Hartman #endif
2790ab4382d2SGreg Kroah-Hartman 
2791b78cd169SJaeden Amero static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2792b5199d46SCyrille Pitchen 				     struct platform_device *pdev)
2793b5199d46SCyrille Pitchen {
2794b78cd169SJaeden Amero 	atmel_port->fifo_size = 0;
2795b78cd169SJaeden Amero 	atmel_port->rts_low = 0;
2796b78cd169SJaeden Amero 	atmel_port->rts_high = 0;
2797b5199d46SCyrille Pitchen 
2798b5199d46SCyrille Pitchen 	if (of_property_read_u32(pdev->dev.of_node,
2799b5199d46SCyrille Pitchen 				 "atmel,fifo-size",
2800b78cd169SJaeden Amero 				 &atmel_port->fifo_size))
2801b5199d46SCyrille Pitchen 		return;
2802b5199d46SCyrille Pitchen 
2803b78cd169SJaeden Amero 	if (!atmel_port->fifo_size)
2804b5199d46SCyrille Pitchen 		return;
2805b5199d46SCyrille Pitchen 
2806b78cd169SJaeden Amero 	if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2807b78cd169SJaeden Amero 		atmel_port->fifo_size = 0;
2808b5199d46SCyrille Pitchen 		dev_err(&pdev->dev, "Invalid FIFO size\n");
2809b5199d46SCyrille Pitchen 		return;
2810b5199d46SCyrille Pitchen 	}
2811b5199d46SCyrille Pitchen 
2812b5199d46SCyrille Pitchen 	/*
2813b5199d46SCyrille Pitchen 	 * 0 <= rts_low <= rts_high <= fifo_size
2814b5199d46SCyrille Pitchen 	 * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2815b5199d46SCyrille Pitchen 	 * to flush their internal TX FIFO, commonly up to 16 data, before
2816b5199d46SCyrille Pitchen 	 * actually stopping to send new data. So we try to set the RTS High
2817b5199d46SCyrille Pitchen 	 * Threshold to a reasonably high value respecting this 16 data
2818b5199d46SCyrille Pitchen 	 * empirical rule when possible.
2819b5199d46SCyrille Pitchen 	 */
2820b78cd169SJaeden Amero 	atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2821b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2822b78cd169SJaeden Amero 	atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2823b78cd169SJaeden Amero 			       atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2824b5199d46SCyrille Pitchen 
2825b5199d46SCyrille Pitchen 	dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2826b78cd169SJaeden Amero 		 atmel_port->fifo_size);
2827b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2828b78cd169SJaeden Amero 		atmel_port->rts_high);
2829b5199d46SCyrille Pitchen 	dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2830b78cd169SJaeden Amero 		atmel_port->rts_low);
2831b5199d46SCyrille Pitchen }
2832b5199d46SCyrille Pitchen 
28339671f099SBill Pemberton static int atmel_serial_probe(struct platform_device *pdev)
2834ab4382d2SGreg Kroah-Hartman {
2835b78cd169SJaeden Amero 	struct atmel_uart_port *atmel_port;
2836c24d2531SRadu Pirea 	struct device_node *np = pdev->dev.parent->of_node;
2837ab4382d2SGreg Kroah-Hartman 	void *data;
28388d41ab87SColin Ian King 	int ret;
2839bd737f87SRicardo Ribalda Delgado 	bool rs485_enabled;
2840ab4382d2SGreg Kroah-Hartman 
2841ab4382d2SGreg Kroah-Hartman 	BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2842ab4382d2SGreg Kroah-Hartman 
2843c24d2531SRadu Pirea 	/*
2844c24d2531SRadu Pirea 	 * In device tree there is no node with "atmel,at91rm9200-usart-serial"
2845c24d2531SRadu Pirea 	 * as compatible string. This driver is probed by at91-usart mfd driver
2846c24d2531SRadu Pirea 	 * which is just a wrapper over the atmel_serial driver and
2847c24d2531SRadu Pirea 	 * spi-at91-usart driver. All attributes needed by this driver are
2848c24d2531SRadu Pirea 	 * found in of_node of parent.
2849c24d2531SRadu Pirea 	 */
2850c24d2531SRadu Pirea 	pdev->dev.of_node = np;
2851c24d2531SRadu Pirea 
28525fbe46b6SNicolas Ferre 	ret = of_alias_get_id(np, "serial");
28534cbf9f48SNicolas Ferre 	if (ret < 0)
28545fbe46b6SNicolas Ferre 		/* port id not found in platform data nor device-tree aliases:
28554cbf9f48SNicolas Ferre 		 * auto-enumerate it */
2856503bded9SPawel Wieczorkiewicz 		ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
28574cbf9f48SNicolas Ferre 
2858503bded9SPawel Wieczorkiewicz 	if (ret >= ATMEL_MAX_UART) {
28594cbf9f48SNicolas Ferre 		ret = -ENODEV;
28604cbf9f48SNicolas Ferre 		goto err;
28614cbf9f48SNicolas Ferre 	}
28624cbf9f48SNicolas Ferre 
2863503bded9SPawel Wieczorkiewicz 	if (test_and_set_bit(ret, atmel_ports_in_use)) {
28644cbf9f48SNicolas Ferre 		/* port already in use */
28654cbf9f48SNicolas Ferre 		ret = -EBUSY;
28664cbf9f48SNicolas Ferre 		goto err;
28674cbf9f48SNicolas Ferre 	}
28684cbf9f48SNicolas Ferre 
2869b78cd169SJaeden Amero 	atmel_port = &atmel_ports[ret];
2870b78cd169SJaeden Amero 	atmel_port->backup_imr = 0;
2871b78cd169SJaeden Amero 	atmel_port->uart.line = ret;
2872*078abd98SDmitry Safonov 	atmel_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_ATMEL_CONSOLE);
2873b78cd169SJaeden Amero 	atmel_serial_probe_fifos(atmel_port, pdev);
2874354e57f3SLinus Walleij 
287598f2082cSNicolas Ferre 	atomic_set(&atmel_port->tasklet_shutdown, 0);
2876b78cd169SJaeden Amero 	spin_lock_init(&atmel_port->lock_suspended);
28772c7af5baSBoris BREZILLON 
2878b78cd169SJaeden Amero 	ret = atmel_init_port(atmel_port, pdev);
287991f8c2d8SBoris BREZILLON 	if (ret)
28806fbb9bdfSCyrille Pitchen 		goto err_clear_bit;
2881ab4382d2SGreg Kroah-Hartman 
2882b78cd169SJaeden Amero 	atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2883b78cd169SJaeden Amero 	if (IS_ERR(atmel_port->gpios)) {
2884b78cd169SJaeden Amero 		ret = PTR_ERR(atmel_port->gpios);
288518dfef9cSUwe Kleine-König 		goto err_clear_bit;
288618dfef9cSUwe Kleine-König 	}
288718dfef9cSUwe Kleine-König 
2888b78cd169SJaeden Amero 	if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2889ab4382d2SGreg Kroah-Hartman 		ret = -ENOMEM;
28906da2ec56SKees Cook 		data = kmalloc_array(ATMEL_SERIAL_RINGSIZE,
28916da2ec56SKees Cook 				     sizeof(struct atmel_uart_char),
28926da2ec56SKees Cook 				     GFP_KERNEL);
2893ab4382d2SGreg Kroah-Hartman 		if (!data)
2894ab4382d2SGreg Kroah-Hartman 			goto err_alloc_ring;
2895b78cd169SJaeden Amero 		atmel_port->rx_ring.buf = data;
2896ab4382d2SGreg Kroah-Hartman 	}
2897ab4382d2SGreg Kroah-Hartman 
2898b78cd169SJaeden Amero 	rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2899bd737f87SRicardo Ribalda Delgado 
2900b78cd169SJaeden Amero 	ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2901ab4382d2SGreg Kroah-Hartman 	if (ret)
2902ab4382d2SGreg Kroah-Hartman 		goto err_add_port;
2903ab4382d2SGreg Kroah-Hartman 
2904ab4382d2SGreg Kroah-Hartman #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2905b78cd169SJaeden Amero 	if (atmel_is_console_port(&atmel_port->uart)
2906ab4382d2SGreg Kroah-Hartman 			&& ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2907ab4382d2SGreg Kroah-Hartman 		/*
2908ab4382d2SGreg Kroah-Hartman 		 * The serial core enabled the clock for us, so undo
290991f8c2d8SBoris BREZILLON 		 * the clk_prepare_enable() in atmel_console_setup()
2910ab4382d2SGreg Kroah-Hartman 		 */
2911b78cd169SJaeden Amero 		clk_disable_unprepare(atmel_port->clk);
2912ab4382d2SGreg Kroah-Hartman 	}
2913ab4382d2SGreg Kroah-Hartman #endif
2914ab4382d2SGreg Kroah-Hartman 
2915ab4382d2SGreg Kroah-Hartman 	device_init_wakeup(&pdev->dev, 1);
2916b78cd169SJaeden Amero 	platform_set_drvdata(pdev, atmel_port);
2917ab4382d2SGreg Kroah-Hartman 
2918d4f64187SCyrille Pitchen 	/*
2919d4f64187SCyrille Pitchen 	 * The peripheral clock has been disabled by atmel_init_port():
2920d4f64187SCyrille Pitchen 	 * enable it before accessing I/O registers
2921d4f64187SCyrille Pitchen 	 */
2922b78cd169SJaeden Amero 	clk_prepare_enable(atmel_port->clk);
2923d4f64187SCyrille Pitchen 
2924bd737f87SRicardo Ribalda Delgado 	if (rs485_enabled) {
2925b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
29264e7decdaSCyrille Pitchen 				  ATMEL_US_USMODE_NORMAL);
2927b78cd169SJaeden Amero 		atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2928b78cd169SJaeden Amero 				  ATMEL_US_RTSEN);
2929fc887b15SLinus Torvalds 	}
2930fc887b15SLinus Torvalds 
2931055560b0SElen Song 	/*
2932055560b0SElen Song 	 * Get port name of usart or uart
2933055560b0SElen Song 	 */
2934b78cd169SJaeden Amero 	atmel_get_ip_name(&atmel_port->uart);
2935055560b0SElen Song 
2936d4f64187SCyrille Pitchen 	/*
2937d4f64187SCyrille Pitchen 	 * The peripheral clock can now safely be disabled till the port
2938d4f64187SCyrille Pitchen 	 * is used
2939d4f64187SCyrille Pitchen 	 */
2940b78cd169SJaeden Amero 	clk_disable_unprepare(atmel_port->clk);
2941d4f64187SCyrille Pitchen 
2942ab4382d2SGreg Kroah-Hartman 	return 0;
2943ab4382d2SGreg Kroah-Hartman 
2944ab4382d2SGreg Kroah-Hartman err_add_port:
2945b78cd169SJaeden Amero 	kfree(atmel_port->rx_ring.buf);
2946b78cd169SJaeden Amero 	atmel_port->rx_ring.buf = NULL;
2947ab4382d2SGreg Kroah-Hartman err_alloc_ring:
2948b78cd169SJaeden Amero 	if (!atmel_is_console_port(&atmel_port->uart)) {
2949b78cd169SJaeden Amero 		clk_put(atmel_port->clk);
2950b78cd169SJaeden Amero 		atmel_port->clk = NULL;
2951ab4382d2SGreg Kroah-Hartman 	}
29526fbb9bdfSCyrille Pitchen err_clear_bit:
2953b78cd169SJaeden Amero 	clear_bit(atmel_port->uart.line, atmel_ports_in_use);
29544cbf9f48SNicolas Ferre err:
2955ab4382d2SGreg Kroah-Hartman 	return ret;
2956ab4382d2SGreg Kroah-Hartman }
2957ab4382d2SGreg Kroah-Hartman 
2958f4a8ab04SRomain Izard /*
2959f4a8ab04SRomain Izard  * Even if the driver is not modular, it makes sense to be able to
2960f4a8ab04SRomain Izard  * unbind a device: there can be many bound devices, and there are
2961f4a8ab04SRomain Izard  * situations where dynamic binding and unbinding can be useful.
2962f4a8ab04SRomain Izard  *
2963f4a8ab04SRomain Izard  * For example, a connected device can require a specific firmware update
2964f4a8ab04SRomain Izard  * protocol that needs bitbanging on IO lines, but use the regular serial
2965f4a8ab04SRomain Izard  * port in the normal case.
2966f4a8ab04SRomain Izard  */
2967f4a8ab04SRomain Izard static int atmel_serial_remove(struct platform_device *pdev)
2968f4a8ab04SRomain Izard {
2969f4a8ab04SRomain Izard 	struct uart_port *port = platform_get_drvdata(pdev);
2970f4a8ab04SRomain Izard 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2971f4a8ab04SRomain Izard 	int ret = 0;
2972f4a8ab04SRomain Izard 
297300e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_rx);
297400e8e658SNicolas Ferre 	tasklet_kill(&atmel_port->tasklet_tx);
2975f4a8ab04SRomain Izard 
2976f4a8ab04SRomain Izard 	device_init_wakeup(&pdev->dev, 0);
2977f4a8ab04SRomain Izard 
2978f4a8ab04SRomain Izard 	ret = uart_remove_one_port(&atmel_uart, port);
2979f4a8ab04SRomain Izard 
2980f4a8ab04SRomain Izard 	kfree(atmel_port->rx_ring.buf);
2981f4a8ab04SRomain Izard 
2982f4a8ab04SRomain Izard 	/* "port" is allocated statically, so we shouldn't free it */
2983f4a8ab04SRomain Izard 
2984f4a8ab04SRomain Izard 	clear_bit(port->line, atmel_ports_in_use);
2985f4a8ab04SRomain Izard 
2986f4a8ab04SRomain Izard 	clk_put(atmel_port->clk);
2987f4a8ab04SRomain Izard 	atmel_port->clk = NULL;
2988c24d2531SRadu Pirea 	pdev->dev.of_node = NULL;
2989f4a8ab04SRomain Izard 
2990f4a8ab04SRomain Izard 	return ret;
2991f4a8ab04SRomain Izard }
2992f4a8ab04SRomain Izard 
2993ab4382d2SGreg Kroah-Hartman static struct platform_driver atmel_serial_driver = {
2994ab4382d2SGreg Kroah-Hartman 	.probe		= atmel_serial_probe,
2995f4a8ab04SRomain Izard 	.remove		= atmel_serial_remove,
2996ab4382d2SGreg Kroah-Hartman 	.suspend	= atmel_serial_suspend,
2997ab4382d2SGreg Kroah-Hartman 	.resume		= atmel_serial_resume,
2998ab4382d2SGreg Kroah-Hartman 	.driver		= {
2999c24d2531SRadu Pirea 		.name			= "atmel_usart_serial",
30005fbe46b6SNicolas Ferre 		.of_match_table		= of_match_ptr(atmel_serial_dt_ids),
3001ab4382d2SGreg Kroah-Hartman 	},
3002ab4382d2SGreg Kroah-Hartman };
3003ab4382d2SGreg Kroah-Hartman 
3004ab4382d2SGreg Kroah-Hartman static int __init atmel_serial_init(void)
3005ab4382d2SGreg Kroah-Hartman {
3006ab4382d2SGreg Kroah-Hartman 	int ret;
3007ab4382d2SGreg Kroah-Hartman 
3008ab4382d2SGreg Kroah-Hartman 	ret = uart_register_driver(&atmel_uart);
3009ab4382d2SGreg Kroah-Hartman 	if (ret)
3010ab4382d2SGreg Kroah-Hartman 		return ret;
3011ab4382d2SGreg Kroah-Hartman 
3012ab4382d2SGreg Kroah-Hartman 	ret = platform_driver_register(&atmel_serial_driver);
3013ab4382d2SGreg Kroah-Hartman 	if (ret)
3014ab4382d2SGreg Kroah-Hartman 		uart_unregister_driver(&atmel_uart);
3015ab4382d2SGreg Kroah-Hartman 
3016ab4382d2SGreg Kroah-Hartman 	return ret;
3017ab4382d2SGreg Kroah-Hartman }
3018c39dfebcSPaul Gortmaker device_initcall(atmel_serial_init);
3019