xref: /linux/drivers/tty/serial/pic32_uart.c (revision a23e1966932464e1c5226cb9ac4ce1d5fc10ba22)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2157b9394SAndrei Pistirica /*
3157b9394SAndrei Pistirica  * PIC32 Integrated Serial Driver.
4157b9394SAndrei Pistirica  *
5157b9394SAndrei Pistirica  * Copyright (C) 2015 Microchip Technology, Inc.
6157b9394SAndrei Pistirica  *
7157b9394SAndrei Pistirica  * Authors:
8157b9394SAndrei Pistirica  *   Sorin-Andrei Pistirica <andrei.pistirica@microchip.com>
9157b9394SAndrei Pistirica  */
10157b9394SAndrei Pistirica 
11157b9394SAndrei Pistirica #include <linux/kernel.h>
12157b9394SAndrei Pistirica #include <linux/platform_device.h>
13157b9394SAndrei Pistirica #include <linux/of.h>
14157b9394SAndrei Pistirica #include <linux/of_irq.h>
15157b9394SAndrei Pistirica #include <linux/of_gpio.h>
16157b9394SAndrei Pistirica #include <linux/init.h>
17157b9394SAndrei Pistirica #include <linux/module.h>
18157b9394SAndrei Pistirica #include <linux/slab.h>
19157b9394SAndrei Pistirica #include <linux/console.h>
20157b9394SAndrei Pistirica #include <linux/clk.h>
21157b9394SAndrei Pistirica #include <linux/tty.h>
22157b9394SAndrei Pistirica #include <linux/tty_flip.h>
23157b9394SAndrei Pistirica #include <linux/serial_core.h>
24157b9394SAndrei Pistirica #include <linux/delay.h>
25157b9394SAndrei Pistirica 
26157b9394SAndrei Pistirica #include <asm/mach-pic32/pic32.h>
27157b9394SAndrei Pistirica 
28157b9394SAndrei Pistirica /* UART name and device definitions */
29157b9394SAndrei Pistirica #define PIC32_DEV_NAME		"pic32-uart"
30157b9394SAndrei Pistirica #define PIC32_MAX_UARTS		6
31157b9394SAndrei Pistirica #define PIC32_SDEV_NAME		"ttyPIC"
32157b9394SAndrei Pistirica 
3329574d0dSJiri Slaby #define PIC32_UART_DFLT_BRATE		9600
3429574d0dSJiri Slaby #define PIC32_UART_TX_FIFO_DEPTH	8
3529574d0dSJiri Slaby #define PIC32_UART_RX_FIFO_DEPTH	8
3629574d0dSJiri Slaby 
3729574d0dSJiri Slaby #define PIC32_UART_MODE		0x00
3829574d0dSJiri Slaby #define PIC32_UART_STA		0x10
3929574d0dSJiri Slaby #define PIC32_UART_TX		0x20
4029574d0dSJiri Slaby #define PIC32_UART_RX		0x30
4129574d0dSJiri Slaby #define PIC32_UART_BRG		0x40
4229574d0dSJiri Slaby 
4329574d0dSJiri Slaby /* struct pic32_sport - pic32 serial port descriptor
4429574d0dSJiri Slaby  * @port: uart port descriptor
4529574d0dSJiri Slaby  * @idx: port index
4629574d0dSJiri Slaby  * @irq_fault: virtual fault interrupt number
4729574d0dSJiri Slaby  * @irq_fault_name: irq fault name
4829574d0dSJiri Slaby  * @irq_rx: virtual rx interrupt number
4929574d0dSJiri Slaby  * @irq_rx_name: irq rx name
5029574d0dSJiri Slaby  * @irq_tx: virtual tx interrupt number
5129574d0dSJiri Slaby  * @irq_tx_name: irq tx name
52e9c9d3bbSAndy Shevchenko  * @cts_gpiod: clear to send GPIO
5329574d0dSJiri Slaby  * @dev: device descriptor
5429574d0dSJiri Slaby  **/
5529574d0dSJiri Slaby struct pic32_sport {
5629574d0dSJiri Slaby 	struct uart_port port;
5729574d0dSJiri Slaby 	int idx;
5829574d0dSJiri Slaby 
5929574d0dSJiri Slaby 	int irq_fault;
6029574d0dSJiri Slaby 	const char *irq_fault_name;
6129574d0dSJiri Slaby 	int irq_rx;
6229574d0dSJiri Slaby 	const char *irq_rx_name;
6329574d0dSJiri Slaby 	int irq_tx;
6429574d0dSJiri Slaby 	const char *irq_tx_name;
65e8616bd0SJiri Slaby 	bool enable_tx_irq;
6629574d0dSJiri Slaby 
67e9c9d3bbSAndy Shevchenko 	struct gpio_desc *cts_gpiod;
6829574d0dSJiri Slaby 
6929574d0dSJiri Slaby 	struct clk *clk;
7029574d0dSJiri Slaby 
7129574d0dSJiri Slaby 	struct device *dev;
7229574d0dSJiri Slaby };
7341231472SJiri Slaby 
7441231472SJiri Slaby static inline struct pic32_sport *to_pic32_sport(struct uart_port *port)
7541231472SJiri Slaby {
7641231472SJiri Slaby 	return container_of(port, struct pic32_sport, port);
7741231472SJiri Slaby }
7829574d0dSJiri Slaby 
7929574d0dSJiri Slaby static inline void pic32_uart_writel(struct pic32_sport *sport,
8029574d0dSJiri Slaby 					u32 reg, u32 val)
8129574d0dSJiri Slaby {
82343f23cfSJiri Slaby 	__raw_writel(val, sport->port.membase + reg);
8329574d0dSJiri Slaby }
8429574d0dSJiri Slaby 
8529574d0dSJiri Slaby static inline u32 pic32_uart_readl(struct pic32_sport *sport, u32 reg)
8629574d0dSJiri Slaby {
87343f23cfSJiri Slaby 	return	__raw_readl(sport->port.membase + reg);
8829574d0dSJiri Slaby }
8929574d0dSJiri Slaby 
9029574d0dSJiri Slaby /* pic32 uart mode register bits */
9129574d0dSJiri Slaby #define PIC32_UART_MODE_ON        BIT(15)
9229574d0dSJiri Slaby #define PIC32_UART_MODE_FRZ       BIT(14)
9329574d0dSJiri Slaby #define PIC32_UART_MODE_SIDL      BIT(13)
9429574d0dSJiri Slaby #define PIC32_UART_MODE_IREN      BIT(12)
9529574d0dSJiri Slaby #define PIC32_UART_MODE_RTSMD     BIT(11)
9629574d0dSJiri Slaby #define PIC32_UART_MODE_RESV1     BIT(10)
9729574d0dSJiri Slaby #define PIC32_UART_MODE_UEN1      BIT(9)
9829574d0dSJiri Slaby #define PIC32_UART_MODE_UEN0      BIT(8)
9929574d0dSJiri Slaby #define PIC32_UART_MODE_WAKE      BIT(7)
10029574d0dSJiri Slaby #define PIC32_UART_MODE_LPBK      BIT(6)
10129574d0dSJiri Slaby #define PIC32_UART_MODE_ABAUD     BIT(5)
10229574d0dSJiri Slaby #define PIC32_UART_MODE_RXINV     BIT(4)
10329574d0dSJiri Slaby #define PIC32_UART_MODE_BRGH      BIT(3)
10429574d0dSJiri Slaby #define PIC32_UART_MODE_PDSEL1    BIT(2)
10529574d0dSJiri Slaby #define PIC32_UART_MODE_PDSEL0    BIT(1)
10629574d0dSJiri Slaby #define PIC32_UART_MODE_STSEL     BIT(0)
10729574d0dSJiri Slaby 
10829574d0dSJiri Slaby /* pic32 uart status register bits */
10929574d0dSJiri Slaby #define PIC32_UART_STA_UTXISEL1   BIT(15)
11029574d0dSJiri Slaby #define PIC32_UART_STA_UTXISEL0   BIT(14)
11129574d0dSJiri Slaby #define PIC32_UART_STA_UTXINV     BIT(13)
11229574d0dSJiri Slaby #define PIC32_UART_STA_URXEN      BIT(12)
11329574d0dSJiri Slaby #define PIC32_UART_STA_UTXBRK     BIT(11)
11429574d0dSJiri Slaby #define PIC32_UART_STA_UTXEN      BIT(10)
11529574d0dSJiri Slaby #define PIC32_UART_STA_UTXBF      BIT(9)
11629574d0dSJiri Slaby #define PIC32_UART_STA_TRMT       BIT(8)
11729574d0dSJiri Slaby #define PIC32_UART_STA_URXISEL1   BIT(7)
11829574d0dSJiri Slaby #define PIC32_UART_STA_URXISEL0   BIT(6)
11929574d0dSJiri Slaby #define PIC32_UART_STA_ADDEN      BIT(5)
12029574d0dSJiri Slaby #define PIC32_UART_STA_RIDLE      BIT(4)
12129574d0dSJiri Slaby #define PIC32_UART_STA_PERR       BIT(3)
12229574d0dSJiri Slaby #define PIC32_UART_STA_FERR       BIT(2)
12329574d0dSJiri Slaby #define PIC32_UART_STA_OERR       BIT(1)
12429574d0dSJiri Slaby #define PIC32_UART_STA_URXDA      BIT(0)
12529574d0dSJiri Slaby 
126157b9394SAndrei Pistirica /* pic32_sport pointer for console use */
127157b9394SAndrei Pistirica static struct pic32_sport *pic32_sports[PIC32_MAX_UARTS];
128157b9394SAndrei Pistirica 
129157b9394SAndrei Pistirica static inline void pic32_wait_deplete_txbuf(struct pic32_sport *sport)
130157b9394SAndrei Pistirica {
131157b9394SAndrei Pistirica 	/* wait for tx empty, otherwise chars will be lost or corrupted */
132157b9394SAndrei Pistirica 	while (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_TRMT))
133157b9394SAndrei Pistirica 		udelay(1);
134157b9394SAndrei Pistirica }
135157b9394SAndrei Pistirica 
136157b9394SAndrei Pistirica /* serial core request to check if uart tx buffer is empty */
137157b9394SAndrei Pistirica static unsigned int pic32_uart_tx_empty(struct uart_port *port)
138157b9394SAndrei Pistirica {
139157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
140157b9394SAndrei Pistirica 	u32 val = pic32_uart_readl(sport, PIC32_UART_STA);
141157b9394SAndrei Pistirica 
142157b9394SAndrei Pistirica 	return (val & PIC32_UART_STA_TRMT) ? 1 : 0;
143157b9394SAndrei Pistirica }
144157b9394SAndrei Pistirica 
145157b9394SAndrei Pistirica /* serial core request to set UART outputs */
146157b9394SAndrei Pistirica static void pic32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
147157b9394SAndrei Pistirica {
148157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
149157b9394SAndrei Pistirica 
150157b9394SAndrei Pistirica 	/* set loopback mode */
151157b9394SAndrei Pistirica 	if (mctrl & TIOCM_LOOP)
152157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
153157b9394SAndrei Pistirica 					PIC32_UART_MODE_LPBK);
154157b9394SAndrei Pistirica 	else
155157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
156157b9394SAndrei Pistirica 					PIC32_UART_MODE_LPBK);
157157b9394SAndrei Pistirica }
158157b9394SAndrei Pistirica 
159157b9394SAndrei Pistirica /* serial core request to return the state of misc UART input pins */
160157b9394SAndrei Pistirica static unsigned int pic32_uart_get_mctrl(struct uart_port *port)
161157b9394SAndrei Pistirica {
162157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
163157b9394SAndrei Pistirica 	unsigned int mctrl = 0;
164157b9394SAndrei Pistirica 
165e9c9d3bbSAndy Shevchenko 	/* get the state of CTS input pin for this port */
166e9c9d3bbSAndy Shevchenko 	if (!sport->cts_gpiod)
167157b9394SAndrei Pistirica 		mctrl |= TIOCM_CTS;
168e9c9d3bbSAndy Shevchenko 	else if (gpiod_get_value(sport->cts_gpiod))
169157b9394SAndrei Pistirica 		mctrl |= TIOCM_CTS;
170157b9394SAndrei Pistirica 
171157b9394SAndrei Pistirica 	/* DSR and CD are not supported in PIC32, so return 1
172157b9394SAndrei Pistirica 	 * RI is not supported in PIC32, so return 0
173157b9394SAndrei Pistirica 	 */
174157b9394SAndrei Pistirica 	mctrl |= TIOCM_CD;
175157b9394SAndrei Pistirica 	mctrl |= TIOCM_DSR;
176157b9394SAndrei Pistirica 
177157b9394SAndrei Pistirica 	return mctrl;
178157b9394SAndrei Pistirica }
179157b9394SAndrei Pistirica 
180157b9394SAndrei Pistirica /* stop tx and start tx are not called in pairs, therefore a flag indicates
181157b9394SAndrei Pistirica  * the status of irq to control the irq-depth.
182157b9394SAndrei Pistirica  */
183157b9394SAndrei Pistirica static inline void pic32_uart_irqtxen(struct pic32_sport *sport, u8 en)
184157b9394SAndrei Pistirica {
185e8616bd0SJiri Slaby 	if (en && !sport->enable_tx_irq) {
186157b9394SAndrei Pistirica 		enable_irq(sport->irq_tx);
187e8616bd0SJiri Slaby 		sport->enable_tx_irq = true;
188e8616bd0SJiri Slaby 	} else if (!en && sport->enable_tx_irq) {
189157b9394SAndrei Pistirica 		/* use disable_irq_nosync() and not disable_irq() to avoid self
190157b9394SAndrei Pistirica 		 * imposed deadlock by not waiting for irq handler to end,
191157b9394SAndrei Pistirica 		 * since this callback is called from interrupt context.
192157b9394SAndrei Pistirica 		 */
193157b9394SAndrei Pistirica 		disable_irq_nosync(sport->irq_tx);
194e8616bd0SJiri Slaby 		sport->enable_tx_irq = false;
195157b9394SAndrei Pistirica 	}
196157b9394SAndrei Pistirica }
197157b9394SAndrei Pistirica 
198157b9394SAndrei Pistirica /* serial core request to disable tx ASAP (used for flow control) */
199157b9394SAndrei Pistirica static void pic32_uart_stop_tx(struct uart_port *port)
200157b9394SAndrei Pistirica {
201157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
202157b9394SAndrei Pistirica 
203157b9394SAndrei Pistirica 	if (!(pic32_uart_readl(sport, PIC32_UART_MODE) & PIC32_UART_MODE_ON))
204157b9394SAndrei Pistirica 		return;
205157b9394SAndrei Pistirica 
206157b9394SAndrei Pistirica 	if (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_UTXEN))
207157b9394SAndrei Pistirica 		return;
208157b9394SAndrei Pistirica 
209157b9394SAndrei Pistirica 	/* wait for tx empty */
210157b9394SAndrei Pistirica 	pic32_wait_deplete_txbuf(sport);
211157b9394SAndrei Pistirica 
212157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
213157b9394SAndrei Pistirica 				PIC32_UART_STA_UTXEN);
214157b9394SAndrei Pistirica 	pic32_uart_irqtxen(sport, 0);
215157b9394SAndrei Pistirica }
216157b9394SAndrei Pistirica 
217157b9394SAndrei Pistirica /* serial core request to (re)enable tx */
218157b9394SAndrei Pistirica static void pic32_uart_start_tx(struct uart_port *port)
219157b9394SAndrei Pistirica {
220157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
221157b9394SAndrei Pistirica 
222157b9394SAndrei Pistirica 	pic32_uart_irqtxen(sport, 1);
223157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
224157b9394SAndrei Pistirica 				PIC32_UART_STA_UTXEN);
225157b9394SAndrei Pistirica }
226157b9394SAndrei Pistirica 
227157b9394SAndrei Pistirica /* serial core request to stop rx, called before port shutdown */
228157b9394SAndrei Pistirica static void pic32_uart_stop_rx(struct uart_port *port)
229157b9394SAndrei Pistirica {
230157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
231157b9394SAndrei Pistirica 
232157b9394SAndrei Pistirica 	/* disable rx interrupts */
233157b9394SAndrei Pistirica 	disable_irq(sport->irq_rx);
234157b9394SAndrei Pistirica 
235157b9394SAndrei Pistirica 	/* receiver Enable bit OFF */
236157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
237157b9394SAndrei Pistirica 				PIC32_UART_STA_URXEN);
238157b9394SAndrei Pistirica }
239157b9394SAndrei Pistirica 
240157b9394SAndrei Pistirica /* serial core request to start/stop emitting break char */
241157b9394SAndrei Pistirica static void pic32_uart_break_ctl(struct uart_port *port, int ctl)
242157b9394SAndrei Pistirica {
243157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
244157b9394SAndrei Pistirica 	unsigned long flags;
245157b9394SAndrei Pistirica 
2468975ed8cSThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
247157b9394SAndrei Pistirica 
248157b9394SAndrei Pistirica 	if (ctl)
249157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
250157b9394SAndrei Pistirica 					PIC32_UART_STA_UTXBRK);
251157b9394SAndrei Pistirica 	else
252157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
253157b9394SAndrei Pistirica 					PIC32_UART_STA_UTXBRK);
254157b9394SAndrei Pistirica 
2558975ed8cSThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
256157b9394SAndrei Pistirica }
257157b9394SAndrei Pistirica 
258157b9394SAndrei Pistirica /* get port type in string format */
259157b9394SAndrei Pistirica static const char *pic32_uart_type(struct uart_port *port)
260157b9394SAndrei Pistirica {
261157b9394SAndrei Pistirica 	return (port->type == PORT_PIC32) ? PIC32_DEV_NAME : NULL;
262157b9394SAndrei Pistirica }
263157b9394SAndrei Pistirica 
264157b9394SAndrei Pistirica /* read all chars in rx fifo and send them to core */
265157b9394SAndrei Pistirica static void pic32_uart_do_rx(struct uart_port *port)
266157b9394SAndrei Pistirica {
267157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
268157b9394SAndrei Pistirica 	struct tty_port *tty;
269157b9394SAndrei Pistirica 	unsigned int max_count;
270157b9394SAndrei Pistirica 
271157b9394SAndrei Pistirica 	/* limit number of char read in interrupt, should not be
272157b9394SAndrei Pistirica 	 * higher than fifo size anyway since we're much faster than
273157b9394SAndrei Pistirica 	 * serial port
274157b9394SAndrei Pistirica 	 */
275157b9394SAndrei Pistirica 	max_count = PIC32_UART_RX_FIFO_DEPTH;
276157b9394SAndrei Pistirica 
2778975ed8cSThomas Gleixner 	uart_port_lock(port);
278157b9394SAndrei Pistirica 
279157b9394SAndrei Pistirica 	tty = &port->state->port;
280157b9394SAndrei Pistirica 
281157b9394SAndrei Pistirica 	do {
282157b9394SAndrei Pistirica 		u32 sta_reg, c;
283157b9394SAndrei Pistirica 		char flag;
284157b9394SAndrei Pistirica 
285157b9394SAndrei Pistirica 		/* get overrun/fifo empty information from status register */
286157b9394SAndrei Pistirica 		sta_reg = pic32_uart_readl(sport, PIC32_UART_STA);
287157b9394SAndrei Pistirica 		if (unlikely(sta_reg & PIC32_UART_STA_OERR)) {
288157b9394SAndrei Pistirica 
289157b9394SAndrei Pistirica 			/* fifo reset is required to clear interrupt */
290157b9394SAndrei Pistirica 			pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
291157b9394SAndrei Pistirica 						PIC32_UART_STA_OERR);
292157b9394SAndrei Pistirica 
293157b9394SAndrei Pistirica 			port->icount.overrun++;
294157b9394SAndrei Pistirica 			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
295157b9394SAndrei Pistirica 		}
296157b9394SAndrei Pistirica 
297157b9394SAndrei Pistirica 		/* Can at least one more character can be read? */
298157b9394SAndrei Pistirica 		if (!(sta_reg & PIC32_UART_STA_URXDA))
299157b9394SAndrei Pistirica 			break;
300157b9394SAndrei Pistirica 
301157b9394SAndrei Pistirica 		/* read the character and increment the rx counter */
302157b9394SAndrei Pistirica 		c = pic32_uart_readl(sport, PIC32_UART_RX);
303157b9394SAndrei Pistirica 
304157b9394SAndrei Pistirica 		port->icount.rx++;
305157b9394SAndrei Pistirica 		flag = TTY_NORMAL;
306157b9394SAndrei Pistirica 		c &= 0xff;
307157b9394SAndrei Pistirica 
308157b9394SAndrei Pistirica 		if (unlikely((sta_reg & PIC32_UART_STA_PERR) ||
309157b9394SAndrei Pistirica 			     (sta_reg & PIC32_UART_STA_FERR))) {
310157b9394SAndrei Pistirica 
311157b9394SAndrei Pistirica 			/* do stats first */
312157b9394SAndrei Pistirica 			if (sta_reg & PIC32_UART_STA_PERR)
313157b9394SAndrei Pistirica 				port->icount.parity++;
314157b9394SAndrei Pistirica 			if (sta_reg & PIC32_UART_STA_FERR)
315157b9394SAndrei Pistirica 				port->icount.frame++;
316157b9394SAndrei Pistirica 
317157b9394SAndrei Pistirica 			/* update flag wrt read_status_mask */
318157b9394SAndrei Pistirica 			sta_reg &= port->read_status_mask;
319157b9394SAndrei Pistirica 
320157b9394SAndrei Pistirica 			if (sta_reg & PIC32_UART_STA_FERR)
321157b9394SAndrei Pistirica 				flag = TTY_FRAME;
322157b9394SAndrei Pistirica 			if (sta_reg & PIC32_UART_STA_PERR)
323157b9394SAndrei Pistirica 				flag = TTY_PARITY;
324157b9394SAndrei Pistirica 		}
325157b9394SAndrei Pistirica 
326157b9394SAndrei Pistirica 		if (uart_handle_sysrq_char(port, c))
327157b9394SAndrei Pistirica 			continue;
328157b9394SAndrei Pistirica 
329157b9394SAndrei Pistirica 		if ((sta_reg & port->ignore_status_mask) == 0)
330157b9394SAndrei Pistirica 			tty_insert_flip_char(tty, c, flag);
331157b9394SAndrei Pistirica 
332157b9394SAndrei Pistirica 	} while (--max_count);
333157b9394SAndrei Pistirica 
3348975ed8cSThomas Gleixner 	uart_port_unlock(port);
335157b9394SAndrei Pistirica 
336157b9394SAndrei Pistirica 	tty_flip_buffer_push(tty);
337157b9394SAndrei Pistirica }
338157b9394SAndrei Pistirica 
339157b9394SAndrei Pistirica /* fill tx fifo with chars to send, stop when fifo is about to be full
340157b9394SAndrei Pistirica  * or when all chars have been sent.
341157b9394SAndrei Pistirica  */
342157b9394SAndrei Pistirica static void pic32_uart_do_tx(struct uart_port *port)
343157b9394SAndrei Pistirica {
344157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
345157b9394SAndrei Pistirica 	struct circ_buf *xmit = &port->state->xmit;
346157b9394SAndrei Pistirica 	unsigned int max_count = PIC32_UART_TX_FIFO_DEPTH;
347157b9394SAndrei Pistirica 
348157b9394SAndrei Pistirica 	if (port->x_char) {
349157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_UART_TX, port->x_char);
350157b9394SAndrei Pistirica 		port->icount.tx++;
351157b9394SAndrei Pistirica 		port->x_char = 0;
352157b9394SAndrei Pistirica 		return;
353157b9394SAndrei Pistirica 	}
354157b9394SAndrei Pistirica 
355157b9394SAndrei Pistirica 	if (uart_tx_stopped(port)) {
356157b9394SAndrei Pistirica 		pic32_uart_stop_tx(port);
357157b9394SAndrei Pistirica 		return;
358157b9394SAndrei Pistirica 	}
359157b9394SAndrei Pistirica 
360157b9394SAndrei Pistirica 	if (uart_circ_empty(xmit))
361157b9394SAndrei Pistirica 		goto txq_empty;
362157b9394SAndrei Pistirica 
363157b9394SAndrei Pistirica 	/* keep stuffing chars into uart tx buffer
364157b9394SAndrei Pistirica 	 * 1) until uart fifo is full
365157b9394SAndrei Pistirica 	 * or
366157b9394SAndrei Pistirica 	 * 2) until the circ buffer is empty
367157b9394SAndrei Pistirica 	 * (all chars have been sent)
368157b9394SAndrei Pistirica 	 * or
369157b9394SAndrei Pistirica 	 * 3) until the max count is reached
370157b9394SAndrei Pistirica 	 * (prevents lingering here for too long in certain cases)
371157b9394SAndrei Pistirica 	 */
372157b9394SAndrei Pistirica 	while (!(PIC32_UART_STA_UTXBF &
373157b9394SAndrei Pistirica 		pic32_uart_readl(sport, PIC32_UART_STA))) {
374157b9394SAndrei Pistirica 		unsigned int c = xmit->buf[xmit->tail];
375157b9394SAndrei Pistirica 
376157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_UART_TX, c);
377157b9394SAndrei Pistirica 
37898fdebeeSIlpo Järvinen 		uart_xmit_advance(port, 1);
379157b9394SAndrei Pistirica 		if (uart_circ_empty(xmit))
380157b9394SAndrei Pistirica 			break;
381157b9394SAndrei Pistirica 		if (--max_count == 0)
382157b9394SAndrei Pistirica 			break;
383157b9394SAndrei Pistirica 	}
384157b9394SAndrei Pistirica 
385157b9394SAndrei Pistirica 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
386157b9394SAndrei Pistirica 		uart_write_wakeup(port);
387157b9394SAndrei Pistirica 
388157b9394SAndrei Pistirica 	if (uart_circ_empty(xmit))
389157b9394SAndrei Pistirica 		goto txq_empty;
390157b9394SAndrei Pistirica 
391157b9394SAndrei Pistirica 	return;
392157b9394SAndrei Pistirica 
393157b9394SAndrei Pistirica txq_empty:
394157b9394SAndrei Pistirica 	pic32_uart_irqtxen(sport, 0);
395157b9394SAndrei Pistirica }
396157b9394SAndrei Pistirica 
397157b9394SAndrei Pistirica /* RX interrupt handler */
398157b9394SAndrei Pistirica static irqreturn_t pic32_uart_rx_interrupt(int irq, void *dev_id)
399157b9394SAndrei Pistirica {
400157b9394SAndrei Pistirica 	struct uart_port *port = dev_id;
401157b9394SAndrei Pistirica 
402157b9394SAndrei Pistirica 	pic32_uart_do_rx(port);
403157b9394SAndrei Pistirica 
404157b9394SAndrei Pistirica 	return IRQ_HANDLED;
405157b9394SAndrei Pistirica }
406157b9394SAndrei Pistirica 
407157b9394SAndrei Pistirica /* TX interrupt handler */
408157b9394SAndrei Pistirica static irqreturn_t pic32_uart_tx_interrupt(int irq, void *dev_id)
409157b9394SAndrei Pistirica {
410157b9394SAndrei Pistirica 	struct uart_port *port = dev_id;
411157b9394SAndrei Pistirica 	unsigned long flags;
412157b9394SAndrei Pistirica 
4138975ed8cSThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
414157b9394SAndrei Pistirica 	pic32_uart_do_tx(port);
4158975ed8cSThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
416157b9394SAndrei Pistirica 
417157b9394SAndrei Pistirica 	return IRQ_HANDLED;
418157b9394SAndrei Pistirica }
419157b9394SAndrei Pistirica 
420157b9394SAndrei Pistirica /* FAULT interrupt handler */
421157b9394SAndrei Pistirica static irqreturn_t pic32_uart_fault_interrupt(int irq, void *dev_id)
422157b9394SAndrei Pistirica {
423157b9394SAndrei Pistirica 	/* do nothing: pic32_uart_do_rx() handles faults. */
424157b9394SAndrei Pistirica 	return IRQ_HANDLED;
425157b9394SAndrei Pistirica }
426157b9394SAndrei Pistirica 
427157b9394SAndrei Pistirica /* enable rx & tx operation on uart */
428157b9394SAndrei Pistirica static void pic32_uart_en_and_unmask(struct uart_port *port)
429157b9394SAndrei Pistirica {
430157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
431157b9394SAndrei Pistirica 
432157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_SET(PIC32_UART_STA),
433157b9394SAndrei Pistirica 				PIC32_UART_STA_UTXEN | PIC32_UART_STA_URXEN);
434157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
435157b9394SAndrei Pistirica 				PIC32_UART_MODE_ON);
436157b9394SAndrei Pistirica }
437157b9394SAndrei Pistirica 
438157b9394SAndrei Pistirica /* disable rx & tx operation on uart */
439157b9394SAndrei Pistirica static void pic32_uart_dsbl_and_mask(struct uart_port *port)
440157b9394SAndrei Pistirica {
441157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
442157b9394SAndrei Pistirica 
443157b9394SAndrei Pistirica 	/* wait for tx empty, otherwise chars will be lost or corrupted */
444157b9394SAndrei Pistirica 	pic32_wait_deplete_txbuf(sport);
445157b9394SAndrei Pistirica 
446157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
447157b9394SAndrei Pistirica 				PIC32_UART_STA_UTXEN | PIC32_UART_STA_URXEN);
448157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
449157b9394SAndrei Pistirica 				PIC32_UART_MODE_ON);
450157b9394SAndrei Pistirica }
451157b9394SAndrei Pistirica 
452157b9394SAndrei Pistirica /* serial core request to initialize uart and start rx operation */
453157b9394SAndrei Pistirica static int pic32_uart_startup(struct uart_port *port)
454157b9394SAndrei Pistirica {
455157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
456157b9394SAndrei Pistirica 	u32 dflt_baud = (port->uartclk / PIC32_UART_DFLT_BRATE / 16) - 1;
457157b9394SAndrei Pistirica 	unsigned long flags;
458157b9394SAndrei Pistirica 	int ret;
459157b9394SAndrei Pistirica 
460157b9394SAndrei Pistirica 	local_irq_save(flags);
461157b9394SAndrei Pistirica 
462bb2cff41SJiri Slaby 	ret = clk_prepare_enable(sport->clk);
463157b9394SAndrei Pistirica 	if (ret) {
464157b9394SAndrei Pistirica 		local_irq_restore(flags);
465157b9394SAndrei Pistirica 		goto out_done;
466157b9394SAndrei Pistirica 	}
467157b9394SAndrei Pistirica 
468157b9394SAndrei Pistirica 	/* clear status and mode registers */
469157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_UART_MODE, 0);
470157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_UART_STA, 0);
471157b9394SAndrei Pistirica 
472157b9394SAndrei Pistirica 	/* disable uart and mask all interrupts */
473157b9394SAndrei Pistirica 	pic32_uart_dsbl_and_mask(port);
474157b9394SAndrei Pistirica 
475157b9394SAndrei Pistirica 	/* set default baud */
476157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_UART_BRG, dflt_baud);
477157b9394SAndrei Pistirica 
478157b9394SAndrei Pistirica 	local_irq_restore(flags);
479157b9394SAndrei Pistirica 
480157b9394SAndrei Pistirica 	/* Each UART of a PIC32 has three interrupts therefore,
481157b9394SAndrei Pistirica 	 * we setup driver to register the 3 irqs for the device.
482157b9394SAndrei Pistirica 	 *
483157b9394SAndrei Pistirica 	 * For each irq request_irq() is called with interrupt disabled.
484157b9394SAndrei Pistirica 	 * And the irq is enabled as soon as we are ready to handle them.
485157b9394SAndrei Pistirica 	 */
486e8616bd0SJiri Slaby 	sport->enable_tx_irq = false;
487157b9394SAndrei Pistirica 
488157b9394SAndrei Pistirica 	sport->irq_fault_name = kasprintf(GFP_KERNEL, "%s%d-fault",
489157b9394SAndrei Pistirica 					  pic32_uart_type(port),
490157b9394SAndrei Pistirica 					  sport->idx);
491157b9394SAndrei Pistirica 	if (!sport->irq_fault_name) {
492157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: kasprintf err!", __func__);
493157b9394SAndrei Pistirica 		ret = -ENOMEM;
4946f3cdf2bSYang Yingliang 		goto out_disable_clk;
495157b9394SAndrei Pistirica 	}
496157b9394SAndrei Pistirica 	irq_set_status_flags(sport->irq_fault, IRQ_NOAUTOEN);
497157b9394SAndrei Pistirica 	ret = request_irq(sport->irq_fault, pic32_uart_fault_interrupt,
49808f643e0SJiri Slaby 			  IRQF_NO_THREAD, sport->irq_fault_name, port);
499157b9394SAndrei Pistirica 	if (ret) {
500157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
501157b9394SAndrei Pistirica 			__func__, sport->irq_fault, ret,
502157b9394SAndrei Pistirica 			pic32_uart_type(port));
503157b9394SAndrei Pistirica 		goto out_f;
504157b9394SAndrei Pistirica 	}
505157b9394SAndrei Pistirica 
506157b9394SAndrei Pistirica 	sport->irq_rx_name = kasprintf(GFP_KERNEL, "%s%d-rx",
507157b9394SAndrei Pistirica 				       pic32_uart_type(port),
508157b9394SAndrei Pistirica 				       sport->idx);
509157b9394SAndrei Pistirica 	if (!sport->irq_rx_name) {
510157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: kasprintf err!", __func__);
511157b9394SAndrei Pistirica 		ret = -ENOMEM;
512157b9394SAndrei Pistirica 		goto out_f;
513157b9394SAndrei Pistirica 	}
514157b9394SAndrei Pistirica 	irq_set_status_flags(sport->irq_rx, IRQ_NOAUTOEN);
515157b9394SAndrei Pistirica 	ret = request_irq(sport->irq_rx, pic32_uart_rx_interrupt,
51608f643e0SJiri Slaby 			  IRQF_NO_THREAD, sport->irq_rx_name, port);
517157b9394SAndrei Pistirica 	if (ret) {
518157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
519157b9394SAndrei Pistirica 			__func__, sport->irq_rx, ret,
520157b9394SAndrei Pistirica 			pic32_uart_type(port));
521157b9394SAndrei Pistirica 		goto out_r;
522157b9394SAndrei Pistirica 	}
523157b9394SAndrei Pistirica 
524157b9394SAndrei Pistirica 	sport->irq_tx_name = kasprintf(GFP_KERNEL, "%s%d-tx",
525157b9394SAndrei Pistirica 				       pic32_uart_type(port),
526157b9394SAndrei Pistirica 				       sport->idx);
527157b9394SAndrei Pistirica 	if (!sport->irq_tx_name) {
528157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: kasprintf err!", __func__);
529157b9394SAndrei Pistirica 		ret = -ENOMEM;
530157b9394SAndrei Pistirica 		goto out_r;
531157b9394SAndrei Pistirica 	}
532157b9394SAndrei Pistirica 	irq_set_status_flags(sport->irq_tx, IRQ_NOAUTOEN);
533157b9394SAndrei Pistirica 	ret = request_irq(sport->irq_tx, pic32_uart_tx_interrupt,
53408f643e0SJiri Slaby 			  IRQF_NO_THREAD, sport->irq_tx_name, port);
535157b9394SAndrei Pistirica 	if (ret) {
536157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: request irq(%d) err! ret:%d name:%s\n",
537157b9394SAndrei Pistirica 			__func__, sport->irq_tx, ret,
538157b9394SAndrei Pistirica 			pic32_uart_type(port));
539157b9394SAndrei Pistirica 		goto out_t;
540157b9394SAndrei Pistirica 	}
541157b9394SAndrei Pistirica 
542157b9394SAndrei Pistirica 	local_irq_save(flags);
543157b9394SAndrei Pistirica 
544157b9394SAndrei Pistirica 	/* set rx interrupt on first receive */
545157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
546157b9394SAndrei Pistirica 			PIC32_UART_STA_URXISEL1 | PIC32_UART_STA_URXISEL0);
547157b9394SAndrei Pistirica 
548157b9394SAndrei Pistirica 	/* set interrupt on empty */
549157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_STA),
550157b9394SAndrei Pistirica 			PIC32_UART_STA_UTXISEL1);
551157b9394SAndrei Pistirica 
552157b9394SAndrei Pistirica 	/* enable all interrupts and eanable uart */
553157b9394SAndrei Pistirica 	pic32_uart_en_and_unmask(port);
554157b9394SAndrei Pistirica 
555dfb9afb6SJiri Slaby 	local_irq_restore(flags);
556dfb9afb6SJiri Slaby 
557157b9394SAndrei Pistirica 	enable_irq(sport->irq_rx);
558157b9394SAndrei Pistirica 
559157b9394SAndrei Pistirica 	return 0;
560157b9394SAndrei Pistirica 
561157b9394SAndrei Pistirica out_t:
5622aaa9573SChristophe JAILLET 	free_irq(sport->irq_tx, port);
563fe36fa18SJiri Slaby 	kfree(sport->irq_tx_name);
564157b9394SAndrei Pistirica out_r:
5652aaa9573SChristophe JAILLET 	free_irq(sport->irq_rx, port);
566fe36fa18SJiri Slaby 	kfree(sport->irq_rx_name);
567157b9394SAndrei Pistirica out_f:
5682aaa9573SChristophe JAILLET 	free_irq(sport->irq_fault, port);
569fe36fa18SJiri Slaby 	kfree(sport->irq_fault_name);
5706f3cdf2bSYang Yingliang out_disable_clk:
5716f3cdf2bSYang Yingliang 	clk_disable_unprepare(sport->clk);
572157b9394SAndrei Pistirica out_done:
573157b9394SAndrei Pistirica 	return ret;
574157b9394SAndrei Pistirica }
575157b9394SAndrei Pistirica 
576157b9394SAndrei Pistirica /* serial core request to flush & disable uart */
577157b9394SAndrei Pistirica static void pic32_uart_shutdown(struct uart_port *port)
578157b9394SAndrei Pistirica {
579157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
580157b9394SAndrei Pistirica 	unsigned long flags;
581157b9394SAndrei Pistirica 
582157b9394SAndrei Pistirica 	/* disable uart */
5838975ed8cSThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
584157b9394SAndrei Pistirica 	pic32_uart_dsbl_and_mask(port);
5858975ed8cSThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
586bb2cff41SJiri Slaby 	clk_disable_unprepare(sport->clk);
587157b9394SAndrei Pistirica 
588157b9394SAndrei Pistirica 	/* free all 3 interrupts for this UART */
589157b9394SAndrei Pistirica 	free_irq(sport->irq_fault, port);
590fe36fa18SJiri Slaby 	kfree(sport->irq_fault_name);
591157b9394SAndrei Pistirica 	free_irq(sport->irq_tx, port);
592fe36fa18SJiri Slaby 	kfree(sport->irq_tx_name);
593157b9394SAndrei Pistirica 	free_irq(sport->irq_rx, port);
594fe36fa18SJiri Slaby 	kfree(sport->irq_rx_name);
595157b9394SAndrei Pistirica }
596157b9394SAndrei Pistirica 
597157b9394SAndrei Pistirica /* serial core request to change current uart setting */
598157b9394SAndrei Pistirica static void pic32_uart_set_termios(struct uart_port *port,
599157b9394SAndrei Pistirica 				   struct ktermios *new,
600bec5b814SIlpo Järvinen 				   const struct ktermios *old)
601157b9394SAndrei Pistirica {
602157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
603157b9394SAndrei Pistirica 	unsigned int baud;
604157b9394SAndrei Pistirica 	unsigned int quot;
605157b9394SAndrei Pistirica 	unsigned long flags;
606157b9394SAndrei Pistirica 
6078975ed8cSThomas Gleixner 	uart_port_lock_irqsave(port, &flags);
608157b9394SAndrei Pistirica 
609157b9394SAndrei Pistirica 	/* disable uart and mask all interrupts while changing speed */
610157b9394SAndrei Pistirica 	pic32_uart_dsbl_and_mask(port);
611157b9394SAndrei Pistirica 
612157b9394SAndrei Pistirica 	/* stop bit options */
613157b9394SAndrei Pistirica 	if (new->c_cflag & CSTOPB)
614157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
615157b9394SAndrei Pistirica 					PIC32_UART_MODE_STSEL);
616157b9394SAndrei Pistirica 	else
617157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
618157b9394SAndrei Pistirica 					PIC32_UART_MODE_STSEL);
619157b9394SAndrei Pistirica 
620157b9394SAndrei Pistirica 	/* parity options */
621157b9394SAndrei Pistirica 	if (new->c_cflag & PARENB) {
622157b9394SAndrei Pistirica 		if (new->c_cflag & PARODD) {
623157b9394SAndrei Pistirica 			pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
624157b9394SAndrei Pistirica 					PIC32_UART_MODE_PDSEL1);
625157b9394SAndrei Pistirica 			pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
626157b9394SAndrei Pistirica 					PIC32_UART_MODE_PDSEL0);
627157b9394SAndrei Pistirica 		} else {
628157b9394SAndrei Pistirica 			pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
629157b9394SAndrei Pistirica 					PIC32_UART_MODE_PDSEL0);
630157b9394SAndrei Pistirica 			pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
631157b9394SAndrei Pistirica 					PIC32_UART_MODE_PDSEL1);
632157b9394SAndrei Pistirica 		}
633157b9394SAndrei Pistirica 	} else {
634157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
635157b9394SAndrei Pistirica 					PIC32_UART_MODE_PDSEL1 |
636157b9394SAndrei Pistirica 					PIC32_UART_MODE_PDSEL0);
637157b9394SAndrei Pistirica 	}
638157b9394SAndrei Pistirica 	/* if hw flow ctrl, then the pins must be specified in device tree */
639e9c9d3bbSAndy Shevchenko 	if ((new->c_cflag & CRTSCTS) && sport->cts_gpiod) {
640157b9394SAndrei Pistirica 		/* enable hardware flow control */
641157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_SET(PIC32_UART_MODE),
642157b9394SAndrei Pistirica 					PIC32_UART_MODE_UEN1);
643157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
644157b9394SAndrei Pistirica 					PIC32_UART_MODE_UEN0);
645157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
646157b9394SAndrei Pistirica 					PIC32_UART_MODE_RTSMD);
647157b9394SAndrei Pistirica 	} else {
648157b9394SAndrei Pistirica 		/* disable hardware flow control */
649157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
650157b9394SAndrei Pistirica 					PIC32_UART_MODE_UEN1);
651157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
652157b9394SAndrei Pistirica 					PIC32_UART_MODE_UEN0);
653157b9394SAndrei Pistirica 		pic32_uart_writel(sport, PIC32_CLR(PIC32_UART_MODE),
654157b9394SAndrei Pistirica 					PIC32_UART_MODE_RTSMD);
655157b9394SAndrei Pistirica 	}
656157b9394SAndrei Pistirica 
657157b9394SAndrei Pistirica 	/* Always 8-bit */
658157b9394SAndrei Pistirica 	new->c_cflag |= CS8;
659157b9394SAndrei Pistirica 
660157b9394SAndrei Pistirica 	/* Mark/Space parity is not supported */
661157b9394SAndrei Pistirica 	new->c_cflag &= ~CMSPAR;
662157b9394SAndrei Pistirica 
663157b9394SAndrei Pistirica 	/* update baud */
664157b9394SAndrei Pistirica 	baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
665157b9394SAndrei Pistirica 	quot = uart_get_divisor(port, baud) - 1;
666157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_UART_BRG, quot);
667157b9394SAndrei Pistirica 	uart_update_timeout(port, new->c_cflag, baud);
668157b9394SAndrei Pistirica 
669157b9394SAndrei Pistirica 	if (tty_termios_baud_rate(new))
670157b9394SAndrei Pistirica 		tty_termios_encode_baud_rate(new, baud, baud);
671157b9394SAndrei Pistirica 
672157b9394SAndrei Pistirica 	/* enable uart */
673157b9394SAndrei Pistirica 	pic32_uart_en_and_unmask(port);
674157b9394SAndrei Pistirica 
6758975ed8cSThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
676157b9394SAndrei Pistirica }
677157b9394SAndrei Pistirica 
678157b9394SAndrei Pistirica /* serial core request to claim uart iomem */
679157b9394SAndrei Pistirica static int pic32_uart_request_port(struct uart_port *port)
680157b9394SAndrei Pistirica {
681157b9394SAndrei Pistirica 	struct platform_device *pdev = to_platform_device(port->dev);
682157b9394SAndrei Pistirica 	struct resource *res_mem;
683157b9394SAndrei Pistirica 
684157b9394SAndrei Pistirica 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
685157b9394SAndrei Pistirica 	if (unlikely(!res_mem))
686157b9394SAndrei Pistirica 		return -EINVAL;
687157b9394SAndrei Pistirica 
688157b9394SAndrei Pistirica 	if (!request_mem_region(port->mapbase, resource_size(res_mem),
689157b9394SAndrei Pistirica 				"pic32_uart_mem"))
690157b9394SAndrei Pistirica 		return -EBUSY;
691157b9394SAndrei Pistirica 
6924bdc0d67SChristoph Hellwig 	port->membase = devm_ioremap(port->dev, port->mapbase,
693157b9394SAndrei Pistirica 						resource_size(res_mem));
694157b9394SAndrei Pistirica 	if (!port->membase) {
695157b9394SAndrei Pistirica 		dev_err(port->dev, "Unable to map registers\n");
696157b9394SAndrei Pistirica 		release_mem_region(port->mapbase, resource_size(res_mem));
697157b9394SAndrei Pistirica 		return -ENOMEM;
698157b9394SAndrei Pistirica 	}
699157b9394SAndrei Pistirica 
700157b9394SAndrei Pistirica 	return 0;
701157b9394SAndrei Pistirica }
702157b9394SAndrei Pistirica 
703157b9394SAndrei Pistirica /* serial core request to release uart iomem */
704157b9394SAndrei Pistirica static void pic32_uart_release_port(struct uart_port *port)
705157b9394SAndrei Pistirica {
706157b9394SAndrei Pistirica 	struct platform_device *pdev = to_platform_device(port->dev);
707157b9394SAndrei Pistirica 	struct resource *res_mem;
708157b9394SAndrei Pistirica 	unsigned int res_size;
709157b9394SAndrei Pistirica 
710157b9394SAndrei Pistirica 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711157b9394SAndrei Pistirica 	if (unlikely(!res_mem))
712157b9394SAndrei Pistirica 		return;
713157b9394SAndrei Pistirica 	res_size = resource_size(res_mem);
714157b9394SAndrei Pistirica 
715157b9394SAndrei Pistirica 	release_mem_region(port->mapbase, res_size);
716157b9394SAndrei Pistirica }
717157b9394SAndrei Pistirica 
718157b9394SAndrei Pistirica /* serial core request to do any port required auto-configuration */
719157b9394SAndrei Pistirica static void pic32_uart_config_port(struct uart_port *port, int flags)
720157b9394SAndrei Pistirica {
721157b9394SAndrei Pistirica 	if (flags & UART_CONFIG_TYPE) {
722157b9394SAndrei Pistirica 		if (pic32_uart_request_port(port))
723157b9394SAndrei Pistirica 			return;
724157b9394SAndrei Pistirica 		port->type = PORT_PIC32;
725157b9394SAndrei Pistirica 	}
726157b9394SAndrei Pistirica }
727157b9394SAndrei Pistirica 
728157b9394SAndrei Pistirica /* serial core request to check that port information in serinfo are suitable */
729157b9394SAndrei Pistirica static int pic32_uart_verify_port(struct uart_port *port,
730157b9394SAndrei Pistirica 				  struct serial_struct *serinfo)
731157b9394SAndrei Pistirica {
732157b9394SAndrei Pistirica 	if (port->type != PORT_PIC32)
733157b9394SAndrei Pistirica 		return -EINVAL;
734157b9394SAndrei Pistirica 	if (port->irq != serinfo->irq)
735157b9394SAndrei Pistirica 		return -EINVAL;
736157b9394SAndrei Pistirica 	if (port->iotype != serinfo->io_type)
737157b9394SAndrei Pistirica 		return -EINVAL;
738157b9394SAndrei Pistirica 	if (port->mapbase != (unsigned long)serinfo->iomem_base)
739157b9394SAndrei Pistirica 		return -EINVAL;
740157b9394SAndrei Pistirica 
741157b9394SAndrei Pistirica 	return 0;
742157b9394SAndrei Pistirica }
743157b9394SAndrei Pistirica 
744157b9394SAndrei Pistirica /* serial core callbacks */
745157b9394SAndrei Pistirica static const struct uart_ops pic32_uart_ops = {
746157b9394SAndrei Pistirica 	.tx_empty	= pic32_uart_tx_empty,
747157b9394SAndrei Pistirica 	.get_mctrl	= pic32_uart_get_mctrl,
748157b9394SAndrei Pistirica 	.set_mctrl	= pic32_uart_set_mctrl,
749157b9394SAndrei Pistirica 	.start_tx	= pic32_uart_start_tx,
750157b9394SAndrei Pistirica 	.stop_tx	= pic32_uart_stop_tx,
751157b9394SAndrei Pistirica 	.stop_rx	= pic32_uart_stop_rx,
752157b9394SAndrei Pistirica 	.break_ctl	= pic32_uart_break_ctl,
753157b9394SAndrei Pistirica 	.startup	= pic32_uart_startup,
754157b9394SAndrei Pistirica 	.shutdown	= pic32_uart_shutdown,
755157b9394SAndrei Pistirica 	.set_termios	= pic32_uart_set_termios,
756157b9394SAndrei Pistirica 	.type		= pic32_uart_type,
757157b9394SAndrei Pistirica 	.release_port	= pic32_uart_release_port,
758157b9394SAndrei Pistirica 	.request_port	= pic32_uart_request_port,
759157b9394SAndrei Pistirica 	.config_port	= pic32_uart_config_port,
760157b9394SAndrei Pistirica 	.verify_port	= pic32_uart_verify_port,
761157b9394SAndrei Pistirica };
762157b9394SAndrei Pistirica 
763157b9394SAndrei Pistirica #ifdef CONFIG_SERIAL_PIC32_CONSOLE
764157b9394SAndrei Pistirica /* output given char */
7653f8bab17SJiri Slaby static void pic32_console_putchar(struct uart_port *port, unsigned char ch)
766157b9394SAndrei Pistirica {
767157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
768157b9394SAndrei Pistirica 
769157b9394SAndrei Pistirica 	if (!(pic32_uart_readl(sport, PIC32_UART_MODE) & PIC32_UART_MODE_ON))
770157b9394SAndrei Pistirica 		return;
771157b9394SAndrei Pistirica 
772157b9394SAndrei Pistirica 	if (!(pic32_uart_readl(sport, PIC32_UART_STA) & PIC32_UART_STA_UTXEN))
773157b9394SAndrei Pistirica 		return;
774157b9394SAndrei Pistirica 
775157b9394SAndrei Pistirica 	/* wait for tx empty */
776157b9394SAndrei Pistirica 	pic32_wait_deplete_txbuf(sport);
777157b9394SAndrei Pistirica 
778157b9394SAndrei Pistirica 	pic32_uart_writel(sport, PIC32_UART_TX, ch & 0xff);
779157b9394SAndrei Pistirica }
780157b9394SAndrei Pistirica 
781157b9394SAndrei Pistirica /* console core request to output given string */
782157b9394SAndrei Pistirica static void pic32_console_write(struct console *co, const char *s,
783157b9394SAndrei Pistirica 				unsigned int count)
784157b9394SAndrei Pistirica {
785157b9394SAndrei Pistirica 	struct pic32_sport *sport = pic32_sports[co->index];
786157b9394SAndrei Pistirica 
787157b9394SAndrei Pistirica 	/* call uart helper to deal with \r\n */
788343f23cfSJiri Slaby 	uart_console_write(&sport->port, s, count, pic32_console_putchar);
789157b9394SAndrei Pistirica }
790157b9394SAndrei Pistirica 
791157b9394SAndrei Pistirica /* console core request to setup given console, find matching uart
792157b9394SAndrei Pistirica  * port and setup it.
793157b9394SAndrei Pistirica  */
794157b9394SAndrei Pistirica static int pic32_console_setup(struct console *co, char *options)
795157b9394SAndrei Pistirica {
796157b9394SAndrei Pistirica 	struct pic32_sport *sport;
797157b9394SAndrei Pistirica 	int baud = 115200;
798157b9394SAndrei Pistirica 	int bits = 8;
799157b9394SAndrei Pistirica 	int parity = 'n';
800157b9394SAndrei Pistirica 	int flow = 'n';
801157b9394SAndrei Pistirica 	int ret = 0;
802157b9394SAndrei Pistirica 
803157b9394SAndrei Pistirica 	if (unlikely(co->index < 0 || co->index >= PIC32_MAX_UARTS))
804157b9394SAndrei Pistirica 		return -ENODEV;
805157b9394SAndrei Pistirica 
806157b9394SAndrei Pistirica 	sport = pic32_sports[co->index];
807157b9394SAndrei Pistirica 	if (!sport)
808157b9394SAndrei Pistirica 		return -ENODEV;
809157b9394SAndrei Pistirica 
810bb2cff41SJiri Slaby 	ret = clk_prepare_enable(sport->clk);
811157b9394SAndrei Pistirica 	if (ret)
812157b9394SAndrei Pistirica 		return ret;
813157b9394SAndrei Pistirica 
814157b9394SAndrei Pistirica 	if (options)
815157b9394SAndrei Pistirica 		uart_parse_options(options, &baud, &parity, &bits, &flow);
816157b9394SAndrei Pistirica 
817343f23cfSJiri Slaby 	return uart_set_options(&sport->port, co, baud, parity, bits, flow);
818157b9394SAndrei Pistirica }
819157b9394SAndrei Pistirica 
820157b9394SAndrei Pistirica static struct uart_driver pic32_uart_driver;
821157b9394SAndrei Pistirica static struct console pic32_console = {
822157b9394SAndrei Pistirica 	.name		= PIC32_SDEV_NAME,
823157b9394SAndrei Pistirica 	.write		= pic32_console_write,
824157b9394SAndrei Pistirica 	.device		= uart_console_device,
825157b9394SAndrei Pistirica 	.setup		= pic32_console_setup,
826157b9394SAndrei Pistirica 	.flags		= CON_PRINTBUFFER,
827157b9394SAndrei Pistirica 	.index		= -1,
828157b9394SAndrei Pistirica 	.data		= &pic32_uart_driver,
829157b9394SAndrei Pistirica };
830157b9394SAndrei Pistirica #define PIC32_SCONSOLE (&pic32_console)
831157b9394SAndrei Pistirica 
832157b9394SAndrei Pistirica static int __init pic32_console_init(void)
833157b9394SAndrei Pistirica {
834157b9394SAndrei Pistirica 	register_console(&pic32_console);
835157b9394SAndrei Pistirica 	return 0;
836157b9394SAndrei Pistirica }
837157b9394SAndrei Pistirica console_initcall(pic32_console_init);
838157b9394SAndrei Pistirica 
839157b9394SAndrei Pistirica /*
840157b9394SAndrei Pistirica  * Late console initialization.
841157b9394SAndrei Pistirica  */
842157b9394SAndrei Pistirica static int __init pic32_late_console_init(void)
843157b9394SAndrei Pistirica {
844f5bea480SJohn Ogness 	if (!console_is_registered(&pic32_console))
845157b9394SAndrei Pistirica 		register_console(&pic32_console);
846157b9394SAndrei Pistirica 
847157b9394SAndrei Pistirica 	return 0;
848157b9394SAndrei Pistirica }
849157b9394SAndrei Pistirica 
850157b9394SAndrei Pistirica core_initcall(pic32_late_console_init);
851157b9394SAndrei Pistirica 
852157b9394SAndrei Pistirica #else
853157b9394SAndrei Pistirica #define PIC32_SCONSOLE NULL
854157b9394SAndrei Pistirica #endif
855157b9394SAndrei Pistirica 
856157b9394SAndrei Pistirica static struct uart_driver pic32_uart_driver = {
857157b9394SAndrei Pistirica 	.owner			= THIS_MODULE,
858157b9394SAndrei Pistirica 	.driver_name		= PIC32_DEV_NAME,
859157b9394SAndrei Pistirica 	.dev_name		= PIC32_SDEV_NAME,
860157b9394SAndrei Pistirica 	.nr			= PIC32_MAX_UARTS,
861157b9394SAndrei Pistirica 	.cons			= PIC32_SCONSOLE,
862157b9394SAndrei Pistirica };
863157b9394SAndrei Pistirica 
864157b9394SAndrei Pistirica static int pic32_uart_probe(struct platform_device *pdev)
865157b9394SAndrei Pistirica {
866e9c9d3bbSAndy Shevchenko 	struct device *dev = &pdev->dev;
867e9c9d3bbSAndy Shevchenko 	struct device_node *np = dev->of_node;
868157b9394SAndrei Pistirica 	struct pic32_sport *sport;
869157b9394SAndrei Pistirica 	int uart_idx = 0;
870157b9394SAndrei Pistirica 	struct resource *res_mem;
871157b9394SAndrei Pistirica 	struct uart_port *port;
872157b9394SAndrei Pistirica 	int ret;
873157b9394SAndrei Pistirica 
874157b9394SAndrei Pistirica 	uart_idx = of_alias_get_id(np, "serial");
875157b9394SAndrei Pistirica 	if (uart_idx < 0 || uart_idx >= PIC32_MAX_UARTS)
876157b9394SAndrei Pistirica 		return -EINVAL;
877157b9394SAndrei Pistirica 
878157b9394SAndrei Pistirica 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
879157b9394SAndrei Pistirica 	if (!res_mem)
880157b9394SAndrei Pistirica 		return -EINVAL;
881157b9394SAndrei Pistirica 
882157b9394SAndrei Pistirica 	sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
883157b9394SAndrei Pistirica 	if (!sport)
884157b9394SAndrei Pistirica 		return -ENOMEM;
885157b9394SAndrei Pistirica 
886157b9394SAndrei Pistirica 	sport->idx		= uart_idx;
887157b9394SAndrei Pistirica 	sport->irq_fault	= irq_of_parse_and_map(np, 0);
888157b9394SAndrei Pistirica 	sport->irq_rx		= irq_of_parse_and_map(np, 1);
889157b9394SAndrei Pistirica 	sport->irq_tx		= irq_of_parse_and_map(np, 2);
890157b9394SAndrei Pistirica 	sport->clk		= devm_clk_get(&pdev->dev, NULL);
8913bec2f77SYuan Can 	if (IS_ERR(sport->clk))
8923bec2f77SYuan Can 		return PTR_ERR(sport->clk);
893157b9394SAndrei Pistirica 	sport->dev		= &pdev->dev;
894157b9394SAndrei Pistirica 
895157b9394SAndrei Pistirica 	/* Hardware flow control: gpios
896157b9394SAndrei Pistirica 	 * !Note: Basically, CTS is needed for reading the status.
897157b9394SAndrei Pistirica 	 */
898e9c9d3bbSAndy Shevchenko 	sport->cts_gpiod = devm_gpiod_get_optional(dev, "cts", GPIOD_IN);
899e9c9d3bbSAndy Shevchenko 	if (IS_ERR(sport->cts_gpiod))
900e9c9d3bbSAndy Shevchenko 		return dev_err_probe(dev, PTR_ERR(sport->cts_gpiod), "error requesting CTS GPIO\n");
901e9c9d3bbSAndy Shevchenko 	gpiod_set_consumer_name(sport->cts_gpiod, "CTS");
902157b9394SAndrei Pistirica 
903157b9394SAndrei Pistirica 	pic32_sports[uart_idx] = sport;
904157b9394SAndrei Pistirica 	port = &sport->port;
905157b9394SAndrei Pistirica 	port->iotype	= UPIO_MEM;
906157b9394SAndrei Pistirica 	port->mapbase	= res_mem->start;
907157b9394SAndrei Pistirica 	port->ops	= &pic32_uart_ops;
908157b9394SAndrei Pistirica 	port->flags	= UPF_BOOT_AUTOCONF;
909157b9394SAndrei Pistirica 	port->dev	= &pdev->dev;
910157b9394SAndrei Pistirica 	port->fifosize	= PIC32_UART_TX_FIFO_DEPTH;
911157b9394SAndrei Pistirica 	port->uartclk	= clk_get_rate(sport->clk);
912157b9394SAndrei Pistirica 	port->line	= uart_idx;
913157b9394SAndrei Pistirica 
914157b9394SAndrei Pistirica 	ret = uart_add_one_port(&pic32_uart_driver, port);
915157b9394SAndrei Pistirica 	if (ret) {
916157b9394SAndrei Pistirica 		port->membase = NULL;
917157b9394SAndrei Pistirica 		dev_err(port->dev, "%s: uart add port error!\n", __func__);
918157b9394SAndrei Pistirica 		goto err;
919157b9394SAndrei Pistirica 	}
920157b9394SAndrei Pistirica 
921157b9394SAndrei Pistirica #ifdef CONFIG_SERIAL_PIC32_CONSOLE
922452b9b24SJohn Ogness 	if (uart_console_registered(port)) {
923157b9394SAndrei Pistirica 		/* The peripheral clock has been enabled by console_setup,
924157b9394SAndrei Pistirica 		 * so disable it till the port is used.
925157b9394SAndrei Pistirica 		 */
926bb2cff41SJiri Slaby 		clk_disable_unprepare(sport->clk);
927157b9394SAndrei Pistirica 	}
928157b9394SAndrei Pistirica #endif
929157b9394SAndrei Pistirica 
930157b9394SAndrei Pistirica 	platform_set_drvdata(pdev, port);
931157b9394SAndrei Pistirica 
932157b9394SAndrei Pistirica 	dev_info(&pdev->dev, "%s: uart(%d) driver initialized.\n",
933157b9394SAndrei Pistirica 		 __func__, uart_idx);
934157b9394SAndrei Pistirica 
935157b9394SAndrei Pistirica 	return 0;
936157b9394SAndrei Pistirica err:
937157b9394SAndrei Pistirica 	/* automatic unroll of sport and gpios */
938157b9394SAndrei Pistirica 	return ret;
939157b9394SAndrei Pistirica }
940157b9394SAndrei Pistirica 
941*915fd7f3SUwe Kleine-König static void pic32_uart_remove(struct platform_device *pdev)
942157b9394SAndrei Pistirica {
943157b9394SAndrei Pistirica 	struct uart_port *port = platform_get_drvdata(pdev);
944157b9394SAndrei Pistirica 	struct pic32_sport *sport = to_pic32_sport(port);
945157b9394SAndrei Pistirica 
946157b9394SAndrei Pistirica 	uart_remove_one_port(&pic32_uart_driver, port);
947bb2cff41SJiri Slaby 	clk_disable_unprepare(sport->clk);
948157b9394SAndrei Pistirica 	platform_set_drvdata(pdev, NULL);
949157b9394SAndrei Pistirica 	pic32_sports[sport->idx] = NULL;
950157b9394SAndrei Pistirica }
951157b9394SAndrei Pistirica 
952157b9394SAndrei Pistirica static const struct of_device_id pic32_serial_dt_ids[] = {
953157b9394SAndrei Pistirica 	{ .compatible = "microchip,pic32mzda-uart" },
954157b9394SAndrei Pistirica 	{ /* sentinel */ }
955157b9394SAndrei Pistirica };
956157b9394SAndrei Pistirica MODULE_DEVICE_TABLE(of, pic32_serial_dt_ids);
957157b9394SAndrei Pistirica 
958157b9394SAndrei Pistirica static struct platform_driver pic32_uart_platform_driver = {
959157b9394SAndrei Pistirica 	.probe		= pic32_uart_probe,
960*915fd7f3SUwe Kleine-König 	.remove_new	= pic32_uart_remove,
961157b9394SAndrei Pistirica 	.driver		= {
962157b9394SAndrei Pistirica 		.name	= PIC32_DEV_NAME,
963157b9394SAndrei Pistirica 		.of_match_table	= of_match_ptr(pic32_serial_dt_ids),
96464609794SAnders Roxell 		.suppress_bind_attrs = IS_BUILTIN(CONFIG_SERIAL_PIC32),
965157b9394SAndrei Pistirica 	},
966157b9394SAndrei Pistirica };
967157b9394SAndrei Pistirica 
968157b9394SAndrei Pistirica static int __init pic32_uart_init(void)
969157b9394SAndrei Pistirica {
970157b9394SAndrei Pistirica 	int ret;
971157b9394SAndrei Pistirica 
972157b9394SAndrei Pistirica 	ret = uart_register_driver(&pic32_uart_driver);
973157b9394SAndrei Pistirica 	if (ret) {
974157b9394SAndrei Pistirica 		pr_err("failed to register %s:%d\n",
975157b9394SAndrei Pistirica 		       pic32_uart_driver.driver_name, ret);
976157b9394SAndrei Pistirica 		return ret;
977157b9394SAndrei Pistirica 	}
978157b9394SAndrei Pistirica 
979157b9394SAndrei Pistirica 	ret = platform_driver_register(&pic32_uart_platform_driver);
980157b9394SAndrei Pistirica 	if (ret) {
981157b9394SAndrei Pistirica 		pr_err("fail to register pic32 uart\n");
982157b9394SAndrei Pistirica 		uart_unregister_driver(&pic32_uart_driver);
983157b9394SAndrei Pistirica 	}
984157b9394SAndrei Pistirica 
985157b9394SAndrei Pistirica 	return ret;
986157b9394SAndrei Pistirica }
987157b9394SAndrei Pistirica arch_initcall(pic32_uart_init);
988157b9394SAndrei Pistirica 
989157b9394SAndrei Pistirica static void __exit pic32_uart_exit(void)
990157b9394SAndrei Pistirica {
991157b9394SAndrei Pistirica #ifdef CONFIG_SERIAL_PIC32_CONSOLE
992157b9394SAndrei Pistirica 	unregister_console(&pic32_console);
993157b9394SAndrei Pistirica #endif
994157b9394SAndrei Pistirica 	platform_driver_unregister(&pic32_uart_platform_driver);
995157b9394SAndrei Pistirica 	uart_unregister_driver(&pic32_uart_driver);
996157b9394SAndrei Pistirica }
997157b9394SAndrei Pistirica module_exit(pic32_uart_exit);
998157b9394SAndrei Pistirica 
999157b9394SAndrei Pistirica MODULE_AUTHOR("Sorin-Andrei Pistirica <andrei.pistirica@microchip.com>");
1000157b9394SAndrei Pistirica MODULE_DESCRIPTION("Microchip PIC32 integrated serial port driver");
1001157b9394SAndrei Pistirica MODULE_LICENSE("GPL v2");
1002