xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision 654a8d6c93e77ecff2256ca3ab2cd98967821f0a)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
4c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
5c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
6c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
7c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
83e4aaea7SAkash Asthana #include <linux/irq.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
11c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
12a5819b54SRajendra Nayak #include <linux/pm_opp.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
14f3974413SAkash Asthana #include <linux/pm_runtime.h>
158b7103f3SAkash Asthana #include <linux/pm_wakeirq.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/qcom-geni-se.h>
17c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
18c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
19c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
20c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
21c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
22c4f52879SKarthikeyan Ramasubramanian 
23c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
248a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
259fa3c4b1SRoja Rani Yarubandi #define SE_UART_IO_MACRO_CTRL		0x240
26c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
27c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
28c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
29c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
30c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
31c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
32c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
33c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
34c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
358a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
36c4f52879SKarthikeyan Ramasubramanian 
37c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
38c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN		BIT(0)
39c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK		BIT(1)
40c4f52879SKarthikeyan Ramasubramanian 
41c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_WORD_LEN */
42c4f52879SKarthikeyan Ramasubramanian #define TX_WORD_LEN_MSK		GENMASK(9, 0)
43c4f52879SKarthikeyan Ramasubramanian 
44c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
45c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_MSK	GENMASK(23, 0)
46c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1	0
47c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1_5	1
48c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2	2
49c4f52879SKarthikeyan Ramasubramanian 
50c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_TRANS_LEN */
51c4f52879SKarthikeyan Ramasubramanian #define TX_TRANS_LEN_MSK	GENMASK(23, 0)
52c4f52879SKarthikeyan Ramasubramanian 
53c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
54c4f52879SKarthikeyan Ramasubramanian #define UART_RX_INS_STATUS_BIT	BIT(2)
55c4f52879SKarthikeyan Ramasubramanian #define UART_RX_PAR_EN		BIT(3)
56c4f52879SKarthikeyan Ramasubramanian 
57c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_WORD_LEN */
58c4f52879SKarthikeyan Ramasubramanian #define RX_WORD_LEN_MASK	GENMASK(9, 0)
59c4f52879SKarthikeyan Ramasubramanian 
60c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_STALE_CNT */
61c4f52879SKarthikeyan Ramasubramanian #define RX_STALE_CNT		GENMASK(23, 0)
62c4f52879SKarthikeyan Ramasubramanian 
63c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
64c4f52879SKarthikeyan Ramasubramanian #define PAR_CALC_EN		BIT(0)
65c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_MSK		GENMASK(2, 1)
66c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_SHFT		1
67c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN		0x00
68c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD			0x01
69c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE		0x10
70c4f52879SKarthikeyan Ramasubramanian #define PAR_MARK		0x11
71c4f52879SKarthikeyan Ramasubramanian 
728a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
738a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN	BIT(31)
748a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY	BIT(1)
758a8a66a1SGirish Mahadevan #define UART_RFR_READY		BIT(0)
768a8a66a1SGirish Mahadevan 
77c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
78c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX		0x1
79c4f52879SKarthikeyan Ramasubramanian #define UART_START_BREAK	0x4
80c4f52879SKarthikeyan Ramasubramanian #define UART_STOP_BREAK		0x5
81c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
82c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ		0x1
83c4f52879SKarthikeyan Ramasubramanian #define UART_PARAM		0x1
84c4f52879SKarthikeyan Ramasubramanian 
85c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING	32
86c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT		16
87c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR	10
88c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS	1
898a8a66a1SGirish Mahadevan #define GENI_UART_PORTS		3
90c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS	16
91c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM		2
92c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS	32
93a85fb9ceSRyan Case #define UART_RX_WM		2
9469bd1a4fSAkash Asthana 
9569bd1a4fSAkash Asthana /* SE_UART_LOOPBACK_CFG */
9669bd1a4fSAkash Asthana #define RX_TX_SORTED	BIT(0)
9769bd1a4fSAkash Asthana #define CTS_RTS_SORTED	BIT(1)
9869bd1a4fSAkash Asthana #define RX_TX_CTS_RTS_SORTED	(RX_TX_SORTED | CTS_RTS_SORTED)
99c4f52879SKarthikeyan Ramasubramanian 
1009fa3c4b1SRoja Rani Yarubandi /* UART pin swap value */
1019fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO0_IO1_MASK		GENMASK(3, 0)
1029fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO0_SEL		0x3
1039fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO2_IO3_MASK		GENMASK(15, 4)
1049fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO2_IO3_SWAP		0x4640
1059fa3c4b1SRoja Rani Yarubandi 
106650c8bd3SDouglas Anderson /* We always configure 4 bytes per FIFO word */
107650c8bd3SDouglas Anderson #define BYTES_PER_FIFO_WORD		4
108650c8bd3SDouglas Anderson 
109e42d6c3eSDouglas Anderson struct qcom_geni_private_data {
110e42d6c3eSDouglas Anderson 	/* NOTE: earlycon port will have NULL here */
111e42d6c3eSDouglas Anderson 	struct uart_driver *drv;
112e42d6c3eSDouglas Anderson 
113e42d6c3eSDouglas Anderson 	u32 poll_cached_bytes;
114e42d6c3eSDouglas Anderson 	unsigned int poll_cached_bytes_cnt;
115650c8bd3SDouglas Anderson 
116650c8bd3SDouglas Anderson 	u32 write_cached_bytes;
117650c8bd3SDouglas Anderson 	unsigned int write_cached_bytes_cnt;
118e42d6c3eSDouglas Anderson };
119c4f52879SKarthikeyan Ramasubramanian 
120c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
121c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
122c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
123f3974413SAkash Asthana 	const char *name;
124c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
125c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
126c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
127c4f52879SKarthikeyan Ramasubramanian 	bool setup;
128c4f52879SKarthikeyan Ramasubramanian 	int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
129c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
130f9d690b6Ssatya priya 	void *rx_fifo;
1318a8a66a1SGirish Mahadevan 	u32 loopback;
132c4f52879SKarthikeyan Ramasubramanian 	bool brk;
133a1fee899SRyan Case 
134a1fee899SRyan Case 	unsigned int tx_remaining;
1358b7103f3SAkash Asthana 	int wakeup_irq;
1369fa3c4b1SRoja Rani Yarubandi 	bool rx_tx_swap;
1379fa3c4b1SRoja Rani Yarubandi 	bool cts_rts_swap;
138e42d6c3eSDouglas Anderson 
139e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data private_data;
140c4f52879SKarthikeyan Ramasubramanian };
141c4f52879SKarthikeyan Ramasubramanian 
142f7371750SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops;
1438a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops;
144c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver;
1458a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver;
146c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
1478a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
148c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
149c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport);
150679aac5eSsatya priya static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
151c4f52879SKarthikeyan Ramasubramanian 
152c4f52879SKarthikeyan Ramasubramanian #define to_dev_port(ptr, member) \
153c4f52879SKarthikeyan Ramasubramanian 		container_of(ptr, struct qcom_geni_serial_port, member)
154c4f52879SKarthikeyan Ramasubramanian 
1558a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1568a8a66a1SGirish Mahadevan 	[0] = {
1578a8a66a1SGirish Mahadevan 		.uport = {
1588a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1598a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1608a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1618a8a66a1SGirish Mahadevan 				.line = 0,
1628a8a66a1SGirish Mahadevan 		},
1638a8a66a1SGirish Mahadevan 	},
1648a8a66a1SGirish Mahadevan 	[1] = {
1658a8a66a1SGirish Mahadevan 		.uport = {
1668a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1678a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1688a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1698a8a66a1SGirish Mahadevan 				.line = 1,
1708a8a66a1SGirish Mahadevan 		},
1718a8a66a1SGirish Mahadevan 	},
1728a8a66a1SGirish Mahadevan 	[2] = {
1738a8a66a1SGirish Mahadevan 		.uport = {
1748a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1758a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1768a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1778a8a66a1SGirish Mahadevan 				.line = 2,
1788a8a66a1SGirish Mahadevan 		},
1798a8a66a1SGirish Mahadevan 	},
1808a8a66a1SGirish Mahadevan };
1818a8a66a1SGirish Mahadevan 
182f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
183f7371750SKarthikeyan Ramasubramanian 	.uport = {
184f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
185f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
186f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
187f7371750SKarthikeyan Ramasubramanian 		.line = 0,
188f7371750SKarthikeyan Ramasubramanian 	},
189f7371750SKarthikeyan Ramasubramanian };
190c4f52879SKarthikeyan Ramasubramanian 
191c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
192c4f52879SKarthikeyan Ramasubramanian {
193c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
194c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
195c4f52879SKarthikeyan Ramasubramanian 
19644e60d52SYueHaibing 	uport->membase = devm_platform_ioremap_resource(pdev, 0);
197c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
198c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
199c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
200c4f52879SKarthikeyan Ramasubramanian 	return 0;
201c4f52879SKarthikeyan Ramasubramanian }
202c4f52879SKarthikeyan Ramasubramanian 
203c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
204c4f52879SKarthikeyan Ramasubramanian {
205c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
206c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
207c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
208c4f52879SKarthikeyan Ramasubramanian 	}
209c4f52879SKarthikeyan Ramasubramanian }
210c4f52879SKarthikeyan Ramasubramanian 
2118a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
212c4f52879SKarthikeyan Ramasubramanian {
2138a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2148a8a66a1SGirish Mahadevan 	u32 geni_ios;
2158a8a66a1SGirish Mahadevan 
216e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport)) {
2178a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2188a8a66a1SGirish Mahadevan 	} else {
2199e06d55fSRyan Case 		geni_ios = readl(uport->membase + SE_GENI_IOS);
2208a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2218a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
222c4f52879SKarthikeyan Ramasubramanian 	}
223c4f52879SKarthikeyan Ramasubramanian 
2248a8a66a1SGirish Mahadevan 	return mctrl;
2258a8a66a1SGirish Mahadevan }
2268a8a66a1SGirish Mahadevan 
2278a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
228c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
229c4f52879SKarthikeyan Ramasubramanian {
2308a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
23169bd1a4fSAkash Asthana 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
2328a8a66a1SGirish Mahadevan 
233e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport))
2348a8a66a1SGirish Mahadevan 		return;
2358a8a66a1SGirish Mahadevan 
23669bd1a4fSAkash Asthana 	if (mctrl & TIOCM_LOOP)
23769bd1a4fSAkash Asthana 		port->loopback = RX_TX_CTS_RTS_SORTED;
23869bd1a4fSAkash Asthana 
239a4ced376Ssatya priya 	if (!(mctrl & TIOCM_RTS) && !uport->suspended)
2408a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2419e06d55fSRyan Case 	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
242c4f52879SKarthikeyan Ramasubramanian }
243c4f52879SKarthikeyan Ramasubramanian 
244c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
245c4f52879SKarthikeyan Ramasubramanian {
246c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
247c4f52879SKarthikeyan Ramasubramanian }
248c4f52879SKarthikeyan Ramasubramanian 
2498a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
250c4f52879SKarthikeyan Ramasubramanian {
2518a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2528a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2538a8a66a1SGirish Mahadevan 
2548a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
255c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2568a8a66a1SGirish Mahadevan 
2578a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2588a8a66a1SGirish Mahadevan 	return port;
259c4f52879SKarthikeyan Ramasubramanian }
260c4f52879SKarthikeyan Ramasubramanian 
261c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
262c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
263c4f52879SKarthikeyan Ramasubramanian {
264c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
265c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
266c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
267c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
268c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
269e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
270c4f52879SKarthikeyan Ramasubramanian 
271e42d6c3eSDouglas Anderson 	if (private_data->drv) {
272c4f52879SKarthikeyan Ramasubramanian 		port = to_dev_port(uport, uport);
273c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
274c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
275c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
276c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
277c4f52879SKarthikeyan Ramasubramanian 		/*
278c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
279c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
280c4f52879SKarthikeyan Ramasubramanian 		 */
281c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
282c4f52879SKarthikeyan Ramasubramanian 	}
283c4f52879SKarthikeyan Ramasubramanian 
28443f1831bSKarthikeyan Ramasubramanian 	/*
28543f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
28643f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
28743f1831bSKarthikeyan Ramasubramanian 	 */
28843f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
28943f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
2909e06d55fSRyan Case 		reg = readl(uport->membase + offset);
29143f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29243f1831bSKarthikeyan Ramasubramanian 			return true;
29343f1831bSKarthikeyan Ramasubramanian 		udelay(10);
29443f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
29543f1831bSKarthikeyan Ramasubramanian 	}
29643f1831bSKarthikeyan Ramasubramanian 	return false;
297c4f52879SKarthikeyan Ramasubramanian }
298c4f52879SKarthikeyan Ramasubramanian 
299c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
300c4f52879SKarthikeyan Ramasubramanian {
301c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
302c4f52879SKarthikeyan Ramasubramanian 
3039e06d55fSRyan Case 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
304c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
305c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
306c4f52879SKarthikeyan Ramasubramanian }
307c4f52879SKarthikeyan Ramasubramanian 
308c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
309c4f52879SKarthikeyan Ramasubramanian {
310c4f52879SKarthikeyan Ramasubramanian 	int done;
311c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
312c4f52879SKarthikeyan Ramasubramanian 
313c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
314c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
315c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
3169e06d55fSRyan Case 		writel(M_GENI_CMD_ABORT, uport->membase +
317c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
318c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
319c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
320c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
321c4f52879SKarthikeyan Ramasubramanian 	}
3229e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
323c4f52879SKarthikeyan Ramasubramanian }
324c4f52879SKarthikeyan Ramasubramanian 
325c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
326c4f52879SKarthikeyan Ramasubramanian {
327c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
328c4f52879SKarthikeyan Ramasubramanian 
329c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
330c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
331c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
3329e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
3339e06d55fSRyan Case 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
334c4f52879SKarthikeyan Ramasubramanian }
335c4f52879SKarthikeyan Ramasubramanian 
336c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
337e42d6c3eSDouglas Anderson 
338c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
339c4f52879SKarthikeyan Ramasubramanian {
340e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
341c4f52879SKarthikeyan Ramasubramanian 	u32 status;
342e42d6c3eSDouglas Anderson 	u32 word_cnt;
343e42d6c3eSDouglas Anderson 	int ret;
344c4f52879SKarthikeyan Ramasubramanian 
345e42d6c3eSDouglas Anderson 	if (!private_data->poll_cached_bytes_cnt) {
3469e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
3479e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
348c4f52879SKarthikeyan Ramasubramanian 
3499e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
3509e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
351c4f52879SKarthikeyan Ramasubramanian 
3529e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
353e42d6c3eSDouglas Anderson 		word_cnt = status & RX_FIFO_WC_MSK;
354e42d6c3eSDouglas Anderson 		if (!word_cnt)
355c4f52879SKarthikeyan Ramasubramanian 			return NO_POLL_CHAR;
356c4f52879SKarthikeyan Ramasubramanian 
357e42d6c3eSDouglas Anderson 		if (word_cnt == 1 && (status & RX_LAST))
358d681a6e4SDouglas Anderson 			/*
359d681a6e4SDouglas Anderson 			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
360d681a6e4SDouglas Anderson 			 * treated as if it was BYTES_PER_FIFO_WORD.
361d681a6e4SDouglas Anderson 			 */
362e42d6c3eSDouglas Anderson 			private_data->poll_cached_bytes_cnt =
363e42d6c3eSDouglas Anderson 				(status & RX_LAST_BYTE_VALID_MSK) >>
364e42d6c3eSDouglas Anderson 				RX_LAST_BYTE_VALID_SHFT;
365d681a6e4SDouglas Anderson 
366d681a6e4SDouglas Anderson 		if (private_data->poll_cached_bytes_cnt == 0)
367d681a6e4SDouglas Anderson 			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
368e42d6c3eSDouglas Anderson 
369e42d6c3eSDouglas Anderson 		private_data->poll_cached_bytes =
370e42d6c3eSDouglas Anderson 			readl(uport->membase + SE_GENI_RX_FIFOn);
371e42d6c3eSDouglas Anderson 	}
372e42d6c3eSDouglas Anderson 
373e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes_cnt--;
374e42d6c3eSDouglas Anderson 	ret = private_data->poll_cached_bytes & 0xff;
375e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes >>= 8;
376e42d6c3eSDouglas Anderson 
377e42d6c3eSDouglas Anderson 	return ret;
378c4f52879SKarthikeyan Ramasubramanian }
379c4f52879SKarthikeyan Ramasubramanian 
380c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
381c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
382c4f52879SKarthikeyan Ramasubramanian {
383a85fb9ceSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
384c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
385c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
386c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
3879e06d55fSRyan Case 	writel(c, uport->membase + SE_GENI_TX_FIFOn);
3889e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
389c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
390c4f52879SKarthikeyan Ramasubramanian }
391c4f52879SKarthikeyan Ramasubramanian #endif
392c4f52879SKarthikeyan Ramasubramanian 
393c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
3943f8bab17SJiri Slaby static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
395c4f52879SKarthikeyan Ramasubramanian {
396650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
397650c8bd3SDouglas Anderson 
398650c8bd3SDouglas Anderson 	private_data->write_cached_bytes =
399650c8bd3SDouglas Anderson 		(private_data->write_cached_bytes >> 8) | (ch << 24);
400650c8bd3SDouglas Anderson 	private_data->write_cached_bytes_cnt++;
401650c8bd3SDouglas Anderson 
402650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
403650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
404650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
405650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
406650c8bd3SDouglas Anderson 	}
407c4f52879SKarthikeyan Ramasubramanian }
408c4f52879SKarthikeyan Ramasubramanian 
409c4f52879SKarthikeyan Ramasubramanian static void
410c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
411c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
412c4f52879SKarthikeyan Ramasubramanian {
413650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
414650c8bd3SDouglas Anderson 
415c4f52879SKarthikeyan Ramasubramanian 	int i;
416c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
417c4f52879SKarthikeyan Ramasubramanian 
418c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
419f0262568SKarthikeyan Ramasubramanian 		/*
420f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
421f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
422f0262568SKarthikeyan Ramasubramanian 		 */
423c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
424c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
425c4f52879SKarthikeyan Ramasubramanian 	}
426c4f52879SKarthikeyan Ramasubramanian 
4279e06d55fSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
428c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
429c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
430c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
431c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
432c4f52879SKarthikeyan Ramasubramanian 
433c4f52879SKarthikeyan Ramasubramanian 		/*
434c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
435c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
436c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
437c4f52879SKarthikeyan Ramasubramanian 		 * lost.
438c4f52879SKarthikeyan Ramasubramanian 		 */
439c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
440c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
441c4f52879SKarthikeyan Ramasubramanian 			break;
4426a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
443c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
444c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
4459e06d55fSRyan Case 		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
446c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
447c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
448c4f52879SKarthikeyan Ramasubramanian 	}
449650c8bd3SDouglas Anderson 
450650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt) {
451650c8bd3SDouglas Anderson 		private_data->write_cached_bytes >>= BITS_PER_BYTE *
452650c8bd3SDouglas Anderson 			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
453650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
454650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
455650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
456650c8bd3SDouglas Anderson 	}
457650c8bd3SDouglas Anderson 
458c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
459c4f52879SKarthikeyan Ramasubramanian }
460c4f52879SKarthikeyan Ramasubramanian 
461c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
462c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
463c4f52879SKarthikeyan Ramasubramanian {
464c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
465c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
466c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
467c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
468a1fee899SRyan Case 	u32 geni_status;
469663abb1aSRyan Case 	u32 irq_en;
470c4f52879SKarthikeyan Ramasubramanian 
471c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
472c4f52879SKarthikeyan Ramasubramanian 
4738a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
474c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
475c4f52879SKarthikeyan Ramasubramanian 		return;
476c4f52879SKarthikeyan Ramasubramanian 
477c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
478c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
479c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
480c4f52879SKarthikeyan Ramasubramanian 	else
481c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
482c4f52879SKarthikeyan Ramasubramanian 
4839e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
484a1fee899SRyan Case 
485c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
486c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
487c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
488c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
489c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
490c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
491c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
492c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
4939e06d55fSRyan Case 			writel(M_CMD_ABORT_EN, uport->membase +
494c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
495c4f52879SKarthikeyan Ramasubramanian 		}
4969e06d55fSRyan Case 		writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
497a1fee899SRyan Case 	} else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
498a1fee899SRyan Case 		/*
499a1fee899SRyan Case 		 * It seems we can't interrupt existing transfers if all data
500a1fee899SRyan Case 		 * has been sent, in which case we need to look for done first.
501a1fee899SRyan Case 		 */
502a1fee899SRyan Case 		qcom_geni_serial_poll_tx_done(uport);
503663abb1aSRyan Case 
504d2b574c0SJiri Slaby 		if (!uart_circ_empty(&uport->state->xmit)) {
5059e06d55fSRyan Case 			irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
5069e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
507663abb1aSRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
508663abb1aSRyan Case 		}
509c4f52879SKarthikeyan Ramasubramanian 	}
510c4f52879SKarthikeyan Ramasubramanian 
511c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
512a1fee899SRyan Case 
513a1fee899SRyan Case 	if (port->tx_remaining)
514a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, port->tx_remaining);
515a1fee899SRyan Case 
516c4f52879SKarthikeyan Ramasubramanian 	if (locked)
517c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
518c4f52879SKarthikeyan Ramasubramanian }
519c4f52879SKarthikeyan Ramasubramanian 
520c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
521c4f52879SKarthikeyan Ramasubramanian {
522c4f52879SKarthikeyan Ramasubramanian 	u32 i;
523c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
524c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
525c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
526c4f52879SKarthikeyan Ramasubramanian 
527c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
528c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
529c4f52879SKarthikeyan Ramasubramanian 		int c;
530650c8bd3SDouglas Anderson 		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
531c4f52879SKarthikeyan Ramasubramanian 
532c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
533c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
534c4f52879SKarthikeyan Ramasubramanian 		if (drop)
535c4f52879SKarthikeyan Ramasubramanian 			continue;
536c4f52879SKarthikeyan Ramasubramanian 
537c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
538c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
539c4f52879SKarthikeyan Ramasubramanian 
540c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
541c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
542c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
543c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
544c4f52879SKarthikeyan Ramasubramanian 					continue;
545c4f52879SKarthikeyan Ramasubramanian 			}
546c4f52879SKarthikeyan Ramasubramanian 
547336447b3SDouglas Anderson 			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
548babeca85SDouglas Anderson 
549c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
550c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
551c4f52879SKarthikeyan Ramasubramanian 		}
552c4f52879SKarthikeyan Ramasubramanian 	}
553c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
554c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
555c4f52879SKarthikeyan Ramasubramanian 	return 0;
556c4f52879SKarthikeyan Ramasubramanian }
557c4f52879SKarthikeyan Ramasubramanian #else
558c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
559c4f52879SKarthikeyan Ramasubramanian {
560c4f52879SKarthikeyan Ramasubramanian 	return -EPERM;
561c4f52879SKarthikeyan Ramasubramanian }
562c4f52879SKarthikeyan Ramasubramanian 
563c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
564c4f52879SKarthikeyan Ramasubramanian 
5658a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5668a8a66a1SGirish Mahadevan {
5678a8a66a1SGirish Mahadevan 	struct tty_port *tport;
5688a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
5698a8a66a1SGirish Mahadevan 	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
5708a8a66a1SGirish Mahadevan 	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
5718a8a66a1SGirish Mahadevan 	int ret;
5728a8a66a1SGirish Mahadevan 
5738a8a66a1SGirish Mahadevan 	tport = &uport->state->port;
5748a8a66a1SGirish Mahadevan 	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
5758a8a66a1SGirish Mahadevan 	if (drop)
5768a8a66a1SGirish Mahadevan 		return 0;
5778a8a66a1SGirish Mahadevan 
578f9d690b6Ssatya priya 	ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
5798a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5808a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5818a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5828a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5838a8a66a1SGirish Mahadevan 	}
5848a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5858a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5868a8a66a1SGirish Mahadevan 	return ret;
5878a8a66a1SGirish Mahadevan }
5888a8a66a1SGirish Mahadevan 
589c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_tx(struct uart_port *uport)
590c4f52879SKarthikeyan Ramasubramanian {
591c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
592c4f52879SKarthikeyan Ramasubramanian 	u32 status;
593c4f52879SKarthikeyan Ramasubramanian 
5947fb5b880SKarthikeyan Ramasubramanian 	status = readl(uport->membase + SE_GENI_STATUS);
595c4f52879SKarthikeyan Ramasubramanian 	if (status & M_GENI_CMD_ACTIVE)
596c4f52879SKarthikeyan Ramasubramanian 		return;
597c4f52879SKarthikeyan Ramasubramanian 
598c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_tx_empty(uport))
599c4f52879SKarthikeyan Ramasubramanian 		return;
600c4f52879SKarthikeyan Ramasubramanian 
6019e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
602c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
603c4f52879SKarthikeyan Ramasubramanian 
604bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
6059e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
606c4f52879SKarthikeyan Ramasubramanian }
607c4f52879SKarthikeyan Ramasubramanian 
608c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_tx(struct uart_port *uport)
609c4f52879SKarthikeyan Ramasubramanian {
610c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
611c4f52879SKarthikeyan Ramasubramanian 	u32 status;
612c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
613c4f52879SKarthikeyan Ramasubramanian 
6149e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
615bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
616bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
6179e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
6189e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
619c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
620c4f52879SKarthikeyan Ramasubramanian 	if (!(status & M_GENI_CMD_ACTIVE))
621c4f52879SKarthikeyan Ramasubramanian 		return;
622c4f52879SKarthikeyan Ramasubramanian 
623c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
624c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
625c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
626c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
627c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
628c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
6299e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
630c4f52879SKarthikeyan Ramasubramanian 	}
6319e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
632c4f52879SKarthikeyan Ramasubramanian }
633c4f52879SKarthikeyan Ramasubramanian 
634c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_rx(struct uart_port *uport)
635c4f52879SKarthikeyan Ramasubramanian {
636c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
637c4f52879SKarthikeyan Ramasubramanian 	u32 status;
638c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
639c4f52879SKarthikeyan Ramasubramanian 
6409e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
641c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
642c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_rx(uport);
643c4f52879SKarthikeyan Ramasubramanian 
644c4f52879SKarthikeyan Ramasubramanian 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
645c4f52879SKarthikeyan Ramasubramanian 
6469e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
647c4f52879SKarthikeyan Ramasubramanian 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
6489e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
649c4f52879SKarthikeyan Ramasubramanian 
6509e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
651c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
6529e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
653c4f52879SKarthikeyan Ramasubramanian }
654c4f52879SKarthikeyan Ramasubramanian 
655c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport)
656c4f52879SKarthikeyan Ramasubramanian {
657c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
658c4f52879SKarthikeyan Ramasubramanian 	u32 status;
659c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
660679aac5eSsatya priya 	u32 s_irq_status;
661c4f52879SKarthikeyan Ramasubramanian 
6629e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
663c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
6649e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
665c4f52879SKarthikeyan Ramasubramanian 
6669e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
667c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
6689e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
669c4f52879SKarthikeyan Ramasubramanian 
6709e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
671c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop rx is called multiple times. */
672c4f52879SKarthikeyan Ramasubramanian 	if (!(status & S_GENI_CMD_ACTIVE))
673c4f52879SKarthikeyan Ramasubramanian 		return;
674c4f52879SKarthikeyan Ramasubramanian 
675c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
676679aac5eSsatya priya 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
677679aac5eSsatya priya 					S_CMD_CANCEL_EN, true);
678679aac5eSsatya priya 	/*
679679aac5eSsatya priya 	 * If timeout occurs secondary engine remains active
680679aac5eSsatya priya 	 * and Abort sequence is executed.
681679aac5eSsatya priya 	 */
682679aac5eSsatya priya 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
683679aac5eSsatya priya 	/* Flush the Rx buffer */
684679aac5eSsatya priya 	if (s_irq_status & S_RX_FIFO_LAST_EN)
685679aac5eSsatya priya 		qcom_geni_serial_handle_rx(uport, true);
686679aac5eSsatya priya 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
687679aac5eSsatya priya 
6889e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
689c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
690c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
691c4f52879SKarthikeyan Ramasubramanian }
692c4f52879SKarthikeyan Ramasubramanian 
693c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
694c4f52879SKarthikeyan Ramasubramanian {
695c4f52879SKarthikeyan Ramasubramanian 	u32 status;
696c4f52879SKarthikeyan Ramasubramanian 	u32 word_cnt;
697c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_byte_cnt;
698c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_partial;
699c4f52879SKarthikeyan Ramasubramanian 	u32 total_bytes;
700c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
701c4f52879SKarthikeyan Ramasubramanian 
7029e06d55fSRyan Case 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
703c4f52879SKarthikeyan Ramasubramanian 	word_cnt = status & RX_FIFO_WC_MSK;
704c4f52879SKarthikeyan Ramasubramanian 	last_word_partial = status & RX_LAST;
705c4f52879SKarthikeyan Ramasubramanian 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
706c4f52879SKarthikeyan Ramasubramanian 						RX_LAST_BYTE_VALID_SHFT;
707c4f52879SKarthikeyan Ramasubramanian 
708c4f52879SKarthikeyan Ramasubramanian 	if (!word_cnt)
709c4f52879SKarthikeyan Ramasubramanian 		return;
710650c8bd3SDouglas Anderson 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
711c4f52879SKarthikeyan Ramasubramanian 	if (last_word_partial && last_word_byte_cnt)
712c4f52879SKarthikeyan Ramasubramanian 		total_bytes += last_word_byte_cnt;
713c4f52879SKarthikeyan Ramasubramanian 	else
714650c8bd3SDouglas Anderson 		total_bytes += BYTES_PER_FIFO_WORD;
715c4f52879SKarthikeyan Ramasubramanian 	port->handle_rx(uport, total_bytes, drop);
716c4f52879SKarthikeyan Ramasubramanian }
717c4f52879SKarthikeyan Ramasubramanian 
718a1fee899SRyan Case static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
719a1fee899SRyan Case 		bool active)
720c4f52879SKarthikeyan Ramasubramanian {
721c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
722c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
723c4f52879SKarthikeyan Ramasubramanian 	size_t avail;
724c4f52879SKarthikeyan Ramasubramanian 	size_t remaining;
725a1fee899SRyan Case 	size_t pending;
726c4f52879SKarthikeyan Ramasubramanian 	int i;
727c4f52879SKarthikeyan Ramasubramanian 	u32 status;
72864a42807SRyan Case 	u32 irq_en;
729c4f52879SKarthikeyan Ramasubramanian 	unsigned int chunk;
730c4f52879SKarthikeyan Ramasubramanian 	int tail;
731c4f52879SKarthikeyan Ramasubramanian 
7329e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
733a1fee899SRyan Case 
734a1fee899SRyan Case 	/* Complete the current tx command before taking newly added data */
735a1fee899SRyan Case 	if (active)
736a1fee899SRyan Case 		pending = port->tx_remaining;
737a1fee899SRyan Case 	else
738a1fee899SRyan Case 		pending = uart_circ_chars_pending(xmit);
739a1fee899SRyan Case 
740a1fee899SRyan Case 	/* All data has been transmitted and acknowledged as received */
741a1fee899SRyan Case 	if (!pending && !status && done) {
742c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_tx(uport);
743c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
744c4f52879SKarthikeyan Ramasubramanian 	}
745c4f52879SKarthikeyan Ramasubramanian 
746a1fee899SRyan Case 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
747650c8bd3SDouglas Anderson 	avail *= BYTES_PER_FIFO_WORD;
7488a8a66a1SGirish Mahadevan 
749638a6f4eSEvan Green 	tail = xmit->tail;
7503c66eb4bSMatthias Kaehlcke 	chunk = min(avail, pending);
751c4f52879SKarthikeyan Ramasubramanian 	if (!chunk)
752c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
753c4f52879SKarthikeyan Ramasubramanian 
754a1fee899SRyan Case 	if (!port->tx_remaining) {
755a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, pending);
756a1fee899SRyan Case 		port->tx_remaining = pending;
75764a42807SRyan Case 
7589e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
75964a42807SRyan Case 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
7609e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
76164a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
762a1fee899SRyan Case 	}
763c4f52879SKarthikeyan Ramasubramanian 
764c4f52879SKarthikeyan Ramasubramanian 	remaining = chunk;
765c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < chunk; ) {
766c4f52879SKarthikeyan Ramasubramanian 		unsigned int tx_bytes;
76769736b57SKarthikeyan Ramasubramanian 		u8 buf[sizeof(u32)];
768c4f52879SKarthikeyan Ramasubramanian 		int c;
769c4f52879SKarthikeyan Ramasubramanian 
7703550f897SDan Carpenter 		memset(buf, 0, sizeof(buf));
771650c8bd3SDouglas Anderson 		tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
7723c66eb4bSMatthias Kaehlcke 
7733c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
7743c66eb4bSMatthias Kaehlcke 			buf[c] = xmit->buf[tail++];
7753c66eb4bSMatthias Kaehlcke 			tail &= UART_XMIT_SIZE - 1;
7763c66eb4bSMatthias Kaehlcke 		}
777c4f52879SKarthikeyan Ramasubramanian 
77869736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
779c4f52879SKarthikeyan Ramasubramanian 
780c4f52879SKarthikeyan Ramasubramanian 		i += tx_bytes;
781c4f52879SKarthikeyan Ramasubramanian 		uport->icount.tx += tx_bytes;
782c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
783a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
784c4f52879SKarthikeyan Ramasubramanian 	}
785638a6f4eSEvan Green 
7863c66eb4bSMatthias Kaehlcke 	xmit->tail = tail;
78764a42807SRyan Case 
78864a42807SRyan Case 	/*
78964a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
79064a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
79164a42807SRyan Case 	 * so we must clear it again here after our writes.
79264a42807SRyan Case 	 */
7939e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
79464a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
79564a42807SRyan Case 
796c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
79764a42807SRyan Case 	if (!port->tx_remaining) {
7989e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
79964a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
8009e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
80164a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
80264a42807SRyan Case 	}
80364a42807SRyan Case 
804638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
805c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
806c4f52879SKarthikeyan Ramasubramanian }
807c4f52879SKarthikeyan Ramasubramanian 
808c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
809c4f52879SKarthikeyan Ramasubramanian {
810385298abSRyan Case 	u32 m_irq_en;
811385298abSRyan Case 	u32 m_irq_status;
812385298abSRyan Case 	u32 s_irq_status;
813385298abSRyan Case 	u32 geni_status;
814c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
815c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
816c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
817c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
818c4f52879SKarthikeyan Ramasubramanian 
819c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
820ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
821c4f52879SKarthikeyan Ramasubramanian 
82275f4e830SJohan Hovold 	spin_lock(&uport->lock);
82375f4e830SJohan Hovold 
8249e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
8259e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
8269e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
8279e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
8289e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
8299e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
830c4f52879SKarthikeyan Ramasubramanian 
831c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
832c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
833c4f52879SKarthikeyan Ramasubramanian 
834c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
835c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
836c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
837c4f52879SKarthikeyan Ramasubramanian 	}
838c4f52879SKarthikeyan Ramasubramanian 
83964a42807SRyan Case 	if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
840a1fee899SRyan Case 		qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
841a1fee899SRyan Case 					geni_status & M_GENI_CMD_ACTIVE);
842c4f52879SKarthikeyan Ramasubramanian 
843c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
844c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
845c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
846c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
847c4f52879SKarthikeyan Ramasubramanian 	} else if (s_irq_status & S_GP_IRQ_2_EN ||
848c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_GP_IRQ_3_EN) {
849c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
850c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
851c4f52879SKarthikeyan Ramasubramanian 	}
852c4f52879SKarthikeyan Ramasubramanian 
853c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
854c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_RX_FIFO_LAST_EN)
855c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_rx(uport, drop_rx);
856c4f52879SKarthikeyan Ramasubramanian 
857c4f52879SKarthikeyan Ramasubramanian out_unlock:
85875f4e830SJohan Hovold 	uart_unlock_and_check_sysrq(uport);
859336447b3SDouglas Anderson 
860c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
861c4f52879SKarthikeyan Ramasubramanian }
862c4f52879SKarthikeyan Ramasubramanian 
8636a10635eSKarthikeyan Ramasubramanian static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
864c4f52879SKarthikeyan Ramasubramanian {
865c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
866c4f52879SKarthikeyan Ramasubramanian 
867c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
868c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
869c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
870c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
871c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
872c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
873c4f52879SKarthikeyan Ramasubramanian }
874c4f52879SKarthikeyan Ramasubramanian 
875c4f52879SKarthikeyan Ramasubramanian 
876c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
877c4f52879SKarthikeyan Ramasubramanian {
8783e4aaea7SAkash Asthana 	disable_irq(uport->irq);
879c4f52879SKarthikeyan Ramasubramanian }
880c4f52879SKarthikeyan Ramasubramanian 
881c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
882c4f52879SKarthikeyan Ramasubramanian {
883c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
884385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
885c362272bSDouglas Anderson 	u32 proto;
8869fa3c4b1SRoja Rani Yarubandi 	u32 pin_swap;
887c362272bSDouglas Anderson 
888c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
889c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
890c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
891c362272bSDouglas Anderson 		return -ENXIO;
892c362272bSDouglas Anderson 	}
893c362272bSDouglas Anderson 
894c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
895c362272bSDouglas Anderson 
896c362272bSDouglas Anderson 	get_tx_fifo_size(port);
897c4f52879SKarthikeyan Ramasubramanian 
8989e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
8999fa3c4b1SRoja Rani Yarubandi 
9009fa3c4b1SRoja Rani Yarubandi 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
9019fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap) {
9029fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
9039fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
9049fa3c4b1SRoja Rani Yarubandi 	}
9059fa3c4b1SRoja Rani Yarubandi 	if (port->cts_rts_swap) {
9069fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
9079fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO0_SEL;
9089fa3c4b1SRoja Rani Yarubandi 	}
9099fa3c4b1SRoja Rani Yarubandi 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
9109fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap || port->cts_rts_swap)
9119fa3c4b1SRoja Rani Yarubandi 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
9129fa3c4b1SRoja Rani Yarubandi 
913c4f52879SKarthikeyan Ramasubramanian 	/*
914c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
915c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
916c4f52879SKarthikeyan Ramasubramanian 	 */
9178a8a66a1SGirish Mahadevan 	if (uart_console(uport))
918c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
919650c8bd3SDouglas Anderson 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
920650c8bd3SDouglas Anderson 			       false, true, true);
921a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
922bdc05a8aSRyan Case 	geni_se_select_mode(&port->se, GENI_SE_FIFO);
923c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
924c362272bSDouglas Anderson 
925c4f52879SKarthikeyan Ramasubramanian 	return 0;
926c4f52879SKarthikeyan Ramasubramanian }
927c4f52879SKarthikeyan Ramasubramanian 
928c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
929c4f52879SKarthikeyan Ramasubramanian {
930c4f52879SKarthikeyan Ramasubramanian 	int ret;
931c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
932c4f52879SKarthikeyan Ramasubramanian 
933c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
934c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
935c4f52879SKarthikeyan Ramasubramanian 		if (ret)
936c4f52879SKarthikeyan Ramasubramanian 			return ret;
937c4f52879SKarthikeyan Ramasubramanian 	}
9383e4aaea7SAkash Asthana 	enable_irq(uport->irq);
939c4f52879SKarthikeyan Ramasubramanian 
9403e4aaea7SAkash Asthana 	return 0;
941c4f52879SKarthikeyan Ramasubramanian }
942c4f52879SKarthikeyan Ramasubramanian 
943c2194bc9SVijaya Krishna Nivarthi static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
944ce734600SVivek Gautam 			unsigned int sampling_rate, unsigned int *clk_div)
945c4f52879SKarthikeyan Ramasubramanian {
946c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
947c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
948c2194bc9SVijaya Krishna Nivarthi 	unsigned long freq, prev;
949c2194bc9SVijaya Krishna Nivarthi 	unsigned long div, maxdiv;
950c2194bc9SVijaya Krishna Nivarthi 	int64_t mult;
951c4f52879SKarthikeyan Ramasubramanian 
952ce734600SVivek Gautam 	desired_clk = baud * sampling_rate;
953c2194bc9SVijaya Krishna Nivarthi 	if (!desired_clk) {
954c2194bc9SVijaya Krishna Nivarthi 		pr_err("%s: Invalid frequency\n", __func__);
955c2194bc9SVijaya Krishna Nivarthi 		return 0;
956c2194bc9SVijaya Krishna Nivarthi 	}
957c2194bc9SVijaya Krishna Nivarthi 
958c2194bc9SVijaya Krishna Nivarthi 	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
959c2194bc9SVijaya Krishna Nivarthi 	prev = 0;
960c2194bc9SVijaya Krishna Nivarthi 
961c2194bc9SVijaya Krishna Nivarthi 	for (div = 1; div <= maxdiv; div++) {
962c2194bc9SVijaya Krishna Nivarthi 		mult = div * desired_clk;
963c2194bc9SVijaya Krishna Nivarthi 		if (mult > ULONG_MAX)
964c2194bc9SVijaya Krishna Nivarthi 			break;
965c2194bc9SVijaya Krishna Nivarthi 
966c2194bc9SVijaya Krishna Nivarthi 		freq = clk_round_rate(clk, (unsigned long)mult);
967c2194bc9SVijaya Krishna Nivarthi 		if (!(freq % desired_clk)) {
968c2194bc9SVijaya Krishna Nivarthi 			ser_clk = freq;
969c2194bc9SVijaya Krishna Nivarthi 			break;
970c2194bc9SVijaya Krishna Nivarthi 		}
971c2194bc9SVijaya Krishna Nivarthi 
972c2194bc9SVijaya Krishna Nivarthi 		if (!prev)
973c2194bc9SVijaya Krishna Nivarthi 			ser_clk = freq;
974c2194bc9SVijaya Krishna Nivarthi 		else if (prev == freq)
975c2194bc9SVijaya Krishna Nivarthi 			break;
976c2194bc9SVijaya Krishna Nivarthi 
977c2194bc9SVijaya Krishna Nivarthi 		prev = freq;
978c2194bc9SVijaya Krishna Nivarthi 	}
979c2194bc9SVijaya Krishna Nivarthi 
980c4f52879SKarthikeyan Ramasubramanian 	if (!ser_clk) {
981c4f52879SKarthikeyan Ramasubramanian 		pr_err("%s: Can't find matching DFS entry for baud %d\n",
982c4f52879SKarthikeyan Ramasubramanian 								__func__, baud);
983c4f52879SKarthikeyan Ramasubramanian 		return ser_clk;
984c4f52879SKarthikeyan Ramasubramanian 	}
985c4f52879SKarthikeyan Ramasubramanian 
986c4f52879SKarthikeyan Ramasubramanian 	*clk_div = ser_clk / desired_clk;
987c2194bc9SVijaya Krishna Nivarthi 	if (!(*clk_div))
988c2194bc9SVijaya Krishna Nivarthi 		*clk_div = 1;
989c2194bc9SVijaya Krishna Nivarthi 
990c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
991c4f52879SKarthikeyan Ramasubramanian }
992c4f52879SKarthikeyan Ramasubramanian 
993c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
994c4f52879SKarthikeyan Ramasubramanian 				struct ktermios *termios, struct ktermios *old)
995c4f52879SKarthikeyan Ramasubramanian {
996c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
997385298abSRyan Case 	u32 bits_per_char;
998385298abSRyan Case 	u32 tx_trans_cfg;
999385298abSRyan Case 	u32 tx_parity_cfg;
1000385298abSRyan Case 	u32 rx_trans_cfg;
1001385298abSRyan Case 	u32 rx_parity_cfg;
1002385298abSRyan Case 	u32 stop_bit_len;
1003c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
1004385298abSRyan Case 	u32 ser_clk_cfg;
1005c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1006c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
1007ce734600SVivek Gautam 	u32 ver, sampling_rate;
10087cf563b2SAkash Asthana 	unsigned int avg_bw_core;
1009c4f52879SKarthikeyan Ramasubramanian 
1010c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
1011c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
1012c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1013c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
1014ce734600SVivek Gautam 
1015ce734600SVivek Gautam 	sampling_rate = UART_OVERSAMPLING;
1016ce734600SVivek Gautam 	/* Sampling rate is halved for IP versions >= 2.5 */
1017ce734600SVivek Gautam 	ver = geni_se_get_qup_hw_version(&port->se);
1018c9ca43d4SParas Sharma 	if (ver >= QUP_SE_VERSION_2_5)
1019ce734600SVivek Gautam 		sampling_rate /= 2;
1020ce734600SVivek Gautam 
1021c2194bc9SVijaya Krishna Nivarthi 	clk_rate = get_clk_div_rate(port->se.clk, baud,
1022c2194bc9SVijaya Krishna Nivarthi 		sampling_rate, &clk_div);
1023c4f52879SKarthikeyan Ramasubramanian 	if (!clk_rate)
1024c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
1025c4f52879SKarthikeyan Ramasubramanian 
1026c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
1027a5819b54SRajendra Nayak 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1028c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
1029c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1030c4f52879SKarthikeyan Ramasubramanian 
10317cf563b2SAkash Asthana 	/*
10327cf563b2SAkash Asthana 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
10337cf563b2SAkash Asthana 	 * only.
10347cf563b2SAkash Asthana 	 */
10357cf563b2SAkash Asthana 	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
10367cf563b2SAkash Asthana 						: GENI_DEFAULT_BW;
10377cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
10387cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
10397cf563b2SAkash Asthana 	geni_icc_set_bw(&port->se);
10407cf563b2SAkash Asthana 
1041c4f52879SKarthikeyan Ramasubramanian 	/* parity */
10429e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
10439e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
10449e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
10459e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1046c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
1047c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
1048c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
1049c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
1050c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
1051c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
1052c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
1053c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
1054c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
1055c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
1056c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
1057c4f52879SKarthikeyan Ramasubramanian 		} else {
1058c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
1059c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
1060c4f52879SKarthikeyan Ramasubramanian 		}
1061c4f52879SKarthikeyan Ramasubramanian 	} else {
1062c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1063c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1064c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1065c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1066c4f52879SKarthikeyan Ramasubramanian 	}
1067c4f52879SKarthikeyan Ramasubramanian 
1068c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
10693ec2ff37SJiri Slaby 	bits_per_char = tty_get_char_size(termios->c_cflag);
1070c4f52879SKarthikeyan Ramasubramanian 
1071c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1072c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1073c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1074c4f52879SKarthikeyan Ramasubramanian 	else
1075c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1076c4f52879SKarthikeyan Ramasubramanian 
1077c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1078c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1079c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1080c4f52879SKarthikeyan Ramasubramanian 	else
1081c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1082c4f52879SKarthikeyan Ramasubramanian 
1083c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1084c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1085c4f52879SKarthikeyan Ramasubramanian 
10868a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
10879e06d55fSRyan Case 		writel(port->loopback,
10888a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
10899e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
10909e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
10919e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
10929e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
10939e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
10949e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
10959e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
10969e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
10979e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1098c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1099c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1100c4f52879SKarthikeyan Ramasubramanian }
1101c4f52879SKarthikeyan Ramasubramanian 
1102c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1103c4f52879SKarthikeyan Ramasubramanian {
11047fb5b880SKarthikeyan Ramasubramanian 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1105c4f52879SKarthikeyan Ramasubramanian }
1106c4f52879SKarthikeyan Ramasubramanian 
1107c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1108975efc66SJohn Stultz static int qcom_geni_console_setup(struct console *co, char *options)
1109c4f52879SKarthikeyan Ramasubramanian {
1110c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1111c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
11122ec812a0SDouglas Anderson 	int baud = 115200;
1113c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1114c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1115c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1116c362272bSDouglas Anderson 	int ret;
1117c4f52879SKarthikeyan Ramasubramanian 
1118c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1119c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1120c4f52879SKarthikeyan Ramasubramanian 
11218a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1122c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
11236a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1124c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1125c4f52879SKarthikeyan Ramasubramanian 	}
1126c4f52879SKarthikeyan Ramasubramanian 
1127c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1128c4f52879SKarthikeyan Ramasubramanian 
1129c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1130c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1131c4f52879SKarthikeyan Ramasubramanian 
1132c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1133c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1134c362272bSDouglas Anderson 		if (ret)
1135c362272bSDouglas Anderson 			return ret;
1136c4f52879SKarthikeyan Ramasubramanian 	}
1137c4f52879SKarthikeyan Ramasubramanian 
1138c4f52879SKarthikeyan Ramasubramanian 	if (options)
1139c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1140c4f52879SKarthikeyan Ramasubramanian 
1141c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1142c4f52879SKarthikeyan Ramasubramanian }
1143c4f52879SKarthikeyan Ramasubramanian 
114443f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
114543f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
114643f1831bSKarthikeyan Ramasubramanian {
114743f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
114843f1831bSKarthikeyan Ramasubramanian 
114943f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
115043f1831bSKarthikeyan Ramasubramanian }
115143f1831bSKarthikeyan Ramasubramanian 
1152205b5bddSDouglas Anderson #ifdef CONFIG_CONSOLE_POLL
1153205b5bddSDouglas Anderson static int qcom_geni_serial_earlycon_read(struct console *con,
1154205b5bddSDouglas Anderson 					  char *s, unsigned int n)
1155205b5bddSDouglas Anderson {
1156205b5bddSDouglas Anderson 	struct earlycon_device *dev = con->data;
1157205b5bddSDouglas Anderson 	struct uart_port *uport = &dev->port;
1158205b5bddSDouglas Anderson 	int num_read = 0;
1159205b5bddSDouglas Anderson 	int ch;
1160205b5bddSDouglas Anderson 
1161205b5bddSDouglas Anderson 	while (num_read < n) {
1162205b5bddSDouglas Anderson 		ch = qcom_geni_serial_get_char(uport);
1163205b5bddSDouglas Anderson 		if (ch == NO_POLL_CHAR)
1164205b5bddSDouglas Anderson 			break;
1165205b5bddSDouglas Anderson 		s[num_read++] = ch;
1166205b5bddSDouglas Anderson 	}
1167205b5bddSDouglas Anderson 
1168205b5bddSDouglas Anderson 	return num_read;
1169205b5bddSDouglas Anderson }
1170205b5bddSDouglas Anderson 
1171205b5bddSDouglas Anderson static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1172205b5bddSDouglas Anderson 						      struct console *con)
1173205b5bddSDouglas Anderson {
1174205b5bddSDouglas Anderson 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1175205b5bddSDouglas Anderson 	con->read = qcom_geni_serial_earlycon_read;
1176205b5bddSDouglas Anderson }
1177205b5bddSDouglas Anderson #else
1178205b5bddSDouglas Anderson static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1179205b5bddSDouglas Anderson 						      struct console *con) { }
1180205b5bddSDouglas Anderson #endif
1181205b5bddSDouglas Anderson 
1182e42d6c3eSDouglas Anderson static struct qcom_geni_private_data earlycon_private_data;
1183e42d6c3eSDouglas Anderson 
118443f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
118543f1831bSKarthikeyan Ramasubramanian 								const char *opt)
118643f1831bSKarthikeyan Ramasubramanian {
118743f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
118843f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
118943f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
119043f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
119143f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
119243f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
119343f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
119443f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
119543f1831bSKarthikeyan Ramasubramanian 
119643f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
119743f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
119843f1831bSKarthikeyan Ramasubramanian 
1199e42d6c3eSDouglas Anderson 	uport->private_data = &earlycon_private_data;
1200e42d6c3eSDouglas Anderson 
120143f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
120243f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
120343f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
120443f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
120543f1831bSKarthikeyan Ramasubramanian 	/*
120643f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
120743f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
120843f1831bSKarthikeyan Ramasubramanian 	 */
120943f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
121043f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
121143f1831bSKarthikeyan Ramasubramanian 
121243f1831bSKarthikeyan Ramasubramanian 	/*
121343f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
121443f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
121543f1831bSKarthikeyan Ramasubramanian 	 */
121643f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
121743f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
1218650c8bd3SDouglas Anderson 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1219650c8bd3SDouglas Anderson 			       false, true, true);
122043f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
122143f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
122243f1831bSKarthikeyan Ramasubramanian 
12239e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
12249e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
12259e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
12269e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
12279e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
12289e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
12299e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
123043f1831bSKarthikeyan Ramasubramanian 
123143f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
123243f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
1233205b5bddSDouglas Anderson 	qcom_geni_serial_enable_early_read(&se, dev->con);
1234205b5bddSDouglas Anderson 
123543f1831bSKarthikeyan Ramasubramanian 	return 0;
123643f1831bSKarthikeyan Ramasubramanian }
123743f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
123843f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
123943f1831bSKarthikeyan Ramasubramanian 
1240c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1241c4f52879SKarthikeyan Ramasubramanian {
1242c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1243c4f52879SKarthikeyan Ramasubramanian }
1244c4f52879SKarthikeyan Ramasubramanian 
1245c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1246c4f52879SKarthikeyan Ramasubramanian {
1247c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1248c4f52879SKarthikeyan Ramasubramanian }
1249c4f52879SKarthikeyan Ramasubramanian 
1250c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1251c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1252c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1253c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1254c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1255c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1256c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1257c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1258c4f52879SKarthikeyan Ramasubramanian };
1259c4f52879SKarthikeyan Ramasubramanian 
1260c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1261c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1262c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1263c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1264c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1265c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1266c4f52879SKarthikeyan Ramasubramanian };
1267c4f52879SKarthikeyan Ramasubramanian #else
1268c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1269c4f52879SKarthikeyan Ramasubramanian {
1270c4f52879SKarthikeyan Ramasubramanian 	return 0;
1271c4f52879SKarthikeyan Ramasubramanian }
1272c4f52879SKarthikeyan Ramasubramanian 
1273c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1274c4f52879SKarthikeyan Ramasubramanian {
1275c4f52879SKarthikeyan Ramasubramanian }
1276c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1277c4f52879SKarthikeyan Ramasubramanian 
12788a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
12798a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
12808a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
12818a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
12828a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
12838a8a66a1SGirish Mahadevan };
12848a8a66a1SGirish Mahadevan 
12858a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1286c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1287c4f52879SKarthikeyan Ramasubramanian {
1288c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1289c4f52879SKarthikeyan Ramasubramanian 
1290c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1291c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1292c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1293c362272bSDouglas Anderson 
12947cf563b2SAkash Asthana 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
12957cf563b2SAkash Asthana 		geni_icc_enable(&port->se);
1296c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
12977cf563b2SAkash Asthana 	} else if (new_state == UART_PM_STATE_OFF &&
12987cf563b2SAkash Asthana 			old_state == UART_PM_STATE_ON) {
1299c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
13007cf563b2SAkash Asthana 		geni_icc_disable(&port->se);
13017cf563b2SAkash Asthana 	}
1302c4f52879SKarthikeyan Ramasubramanian }
1303c4f52879SKarthikeyan Ramasubramanian 
1304c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1305c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
1306c4f52879SKarthikeyan Ramasubramanian 	.stop_tx = qcom_geni_serial_stop_tx,
1307c4f52879SKarthikeyan Ramasubramanian 	.start_tx = qcom_geni_serial_start_tx,
1308c4f52879SKarthikeyan Ramasubramanian 	.stop_rx = qcom_geni_serial_stop_rx,
1309*654a8d6cSVijaya Krishna Nivarthi 	.start_rx = qcom_geni_serial_start_rx,
1310c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1311c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1312c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1313c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1314c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1315c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
13168a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
13178a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1318c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1319c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1320c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1321c4f52879SKarthikeyan Ramasubramanian #endif
13228a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
13238a8a66a1SGirish Mahadevan };
13248a8a66a1SGirish Mahadevan 
13258a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
13268a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
13278a8a66a1SGirish Mahadevan 	.stop_tx = qcom_geni_serial_stop_tx,
13288a8a66a1SGirish Mahadevan 	.start_tx = qcom_geni_serial_start_tx,
13298a8a66a1SGirish Mahadevan 	.stop_rx = qcom_geni_serial_stop_rx,
13308a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
13318a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
13328a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
13338a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
13348a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
13358a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
13368a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
13378a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
13388a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1339c4f52879SKarthikeyan Ramasubramanian };
1340c4f52879SKarthikeyan Ramasubramanian 
1341c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1342c4f52879SKarthikeyan Ramasubramanian {
1343c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
134471581242SColin Ian King 	int line;
1345c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1346c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1347c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1348066cd1c4SKarthikeyan Ramasubramanian 	int irq;
13498a8a66a1SGirish Mahadevan 	bool console = false;
13508a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
1351c4f52879SKarthikeyan Ramasubramanian 
13528a8a66a1SGirish Mahadevan 	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
13538a8a66a1SGirish Mahadevan 		console = true;
13548a8a66a1SGirish Mahadevan 
13558a8a66a1SGirish Mahadevan 	if (console) {
13568a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1357c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
13588a8a66a1SGirish Mahadevan 	} else {
13598a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
136008b0adb1SDmitry Baryshkov 		line = of_alias_get_id(pdev->dev.of_node, "serial");
136108b0adb1SDmitry Baryshkov 		if (line == -ENODEV) /* compat with non-standard aliases */
13628a8a66a1SGirish Mahadevan 			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
13638a8a66a1SGirish Mahadevan 	}
1364c4f52879SKarthikeyan Ramasubramanian 
13658a8a66a1SGirish Mahadevan 	port = get_port_from_line(line, console);
1366c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
13676a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
13686a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1369c4f52879SKarthikeyan Ramasubramanian 	}
1370c4f52879SKarthikeyan Ramasubramanian 
1371c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1372c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1373c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1374c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1375c4f52879SKarthikeyan Ramasubramanian 
1376c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
1377c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1378c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1379c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1380c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1381c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1382c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1383c4f52879SKarthikeyan Ramasubramanian 		return ret;
1384c4f52879SKarthikeyan Ramasubramanian 	}
1385c4f52879SKarthikeyan Ramasubramanian 
1386c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13877693b331SWei Yongjun 	if (!res)
13887693b331SWei Yongjun 		return -EINVAL;
1389c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1390c4f52879SKarthikeyan Ramasubramanian 
1391c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1392c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1393c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1394c4f52879SKarthikeyan Ramasubramanian 
1395f9d690b6Ssatya priya 	if (!console) {
1396f9d690b6Ssatya priya 		port->rx_fifo = devm_kcalloc(uport->dev,
1397f9d690b6Ssatya priya 			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
1398f9d690b6Ssatya priya 		if (!port->rx_fifo)
1399f9d690b6Ssatya priya 			return -ENOMEM;
1400f9d690b6Ssatya priya 	}
1401f9d690b6Ssatya priya 
14027cf563b2SAkash Asthana 	ret = geni_icc_get(&port->se, NULL);
14037cf563b2SAkash Asthana 	if (ret)
14047cf563b2SAkash Asthana 		return ret;
14057cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
14067cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
14077cf563b2SAkash Asthana 
14087cf563b2SAkash Asthana 	/* Set BW for register access */
14097cf563b2SAkash Asthana 	ret = geni_icc_set_bw(&port->se);
14107cf563b2SAkash Asthana 	if (ret)
14117cf563b2SAkash Asthana 		return ret;
14127cf563b2SAkash Asthana 
1413f3974413SAkash Asthana 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1414f3974413SAkash Asthana 			"qcom_geni_serial_%s%d",
1415f3974413SAkash Asthana 			uart_console(uport) ? "console" : "uart", uport->line);
1416f3974413SAkash Asthana 	if (!port->name)
1417f3974413SAkash Asthana 		return -ENOMEM;
1418f3974413SAkash Asthana 
1419066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
14201df21786SStephen Boyd 	if (irq < 0)
1421066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1422066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
14238f122698SDmitry Safonov 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1424c4f52879SKarthikeyan Ramasubramanian 
1425f3974413SAkash Asthana 	if (!console)
1426f3974413SAkash Asthana 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
14273e4aaea7SAkash Asthana 
14289fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
14299fa3c4b1SRoja Rani Yarubandi 		port->rx_tx_swap = true;
14309fa3c4b1SRoja Rani Yarubandi 
14319fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
14329fa3c4b1SRoja Rani Yarubandi 		port->cts_rts_swap = true;
14339fa3c4b1SRoja Rani Yarubandi 
1434300894a6SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1435300894a6SYangtao Li 	if (ret)
1436300894a6SYangtao Li 		return ret;
1437a5819b54SRajendra Nayak 	/* OPP table is optional */
1438300894a6SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
1439c7ac46daSViresh Kumar 	if (ret && ret != -ENODEV) {
1440a5819b54SRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1441300894a6SYangtao Li 		return ret;
1442a5819b54SRajendra Nayak 	}
1443a5819b54SRajendra Nayak 
1444e42d6c3eSDouglas Anderson 	port->private_data.drv = drv;
1445e42d6c3eSDouglas Anderson 	uport->private_data = &port->private_data;
1446c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
14478a8a66a1SGirish Mahadevan 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
1448f3974413SAkash Asthana 
1449f3974413SAkash Asthana 	ret = uart_add_one_port(drv, uport);
1450f3974413SAkash Asthana 	if (ret)
1451300894a6SYangtao Li 		return ret;
1452f3974413SAkash Asthana 
1453f3974413SAkash Asthana 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1454f3974413SAkash Asthana 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1455f3974413SAkash Asthana 			IRQF_TRIGGER_HIGH, port->name, uport);
1456f3974413SAkash Asthana 	if (ret) {
1457f3974413SAkash Asthana 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1458f3974413SAkash Asthana 		uart_remove_one_port(drv, uport);
1459300894a6SYangtao Li 		return ret;
1460f3974413SAkash Asthana 	}
1461f3974413SAkash Asthana 
1462f3974413SAkash Asthana 	/*
1463f3974413SAkash Asthana 	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1464f3974413SAkash Asthana 	 * enabled/disabled from dev_pm_arm_wake_irq during system
1465f3974413SAkash Asthana 	 * suspend/resume respectively.
1466f3974413SAkash Asthana 	 */
1467f3974413SAkash Asthana 	pm_runtime_set_active(&pdev->dev);
1468f3974413SAkash Asthana 
1469f3974413SAkash Asthana 	if (port->wakeup_irq > 0) {
1470f3974413SAkash Asthana 		device_init_wakeup(&pdev->dev, true);
1471f3974413SAkash Asthana 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1472f3974413SAkash Asthana 						port->wakeup_irq);
1473f3974413SAkash Asthana 		if (ret) {
1474f3974413SAkash Asthana 			device_init_wakeup(&pdev->dev, false);
1475f3974413SAkash Asthana 			uart_remove_one_port(drv, uport);
1476300894a6SYangtao Li 			return ret;
1477f3974413SAkash Asthana 		}
1478f3974413SAkash Asthana 	}
1479f3974413SAkash Asthana 
1480f3974413SAkash Asthana 	return 0;
1481c4f52879SKarthikeyan Ramasubramanian }
1482c4f52879SKarthikeyan Ramasubramanian 
1483c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1484c4f52879SKarthikeyan Ramasubramanian {
1485c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1486e42d6c3eSDouglas Anderson 	struct uart_driver *drv = port->private_data.drv;
1487c4f52879SKarthikeyan Ramasubramanian 
1488f3974413SAkash Asthana 	dev_pm_clear_wake_irq(&pdev->dev);
1489f3974413SAkash Asthana 	device_init_wakeup(&pdev->dev, false);
1490c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1491f3974413SAkash Asthana 
1492c4f52879SKarthikeyan Ramasubramanian 	return 0;
1493c4f52879SKarthikeyan Ramasubramanian }
1494c4f52879SKarthikeyan Ramasubramanian 
1495b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1496c4f52879SKarthikeyan Ramasubramanian {
1497a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1498c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1499e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1500c4f52879SKarthikeyan Ramasubramanian 
15014a3107f6SRajendra Nayak 	/*
15024a3107f6SRajendra Nayak 	 * This is done so we can hit the lowest possible state in suspend
15034a3107f6SRajendra Nayak 	 * even with no_console_suspend
15044a3107f6SRajendra Nayak 	 */
15054a3107f6SRajendra Nayak 	if (uart_console(uport)) {
15064a3107f6SRajendra Nayak 		geni_icc_set_tag(&port->se, 0x3);
15074a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
15084a3107f6SRajendra Nayak 	}
1509e42d6c3eSDouglas Anderson 	return uart_suspend_port(private_data->drv, uport);
15108a8a66a1SGirish Mahadevan }
15118a8a66a1SGirish Mahadevan 
1512b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
1513c4f52879SKarthikeyan Ramasubramanian {
15144a3107f6SRajendra Nayak 	int ret;
1515a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1516c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1517e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1518c4f52879SKarthikeyan Ramasubramanian 
15194a3107f6SRajendra Nayak 	ret = uart_resume_port(private_data->drv, uport);
15204a3107f6SRajendra Nayak 	if (uart_console(uport)) {
15214a3107f6SRajendra Nayak 		geni_icc_set_tag(&port->se, 0x7);
15224a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
15234a3107f6SRajendra Nayak 	}
15244a3107f6SRajendra Nayak 	return ret;
1525c4f52879SKarthikeyan Ramasubramanian }
1526c4f52879SKarthikeyan Ramasubramanian 
1527c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1528b1f84dd3SMukesh Kumar Savaliya 	SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1529b1f84dd3SMukesh Kumar Savaliya 					qcom_geni_serial_sys_resume)
1530c4f52879SKarthikeyan Ramasubramanian };
1531c4f52879SKarthikeyan Ramasubramanian 
1532c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
1533c4f52879SKarthikeyan Ramasubramanian 	{ .compatible = "qcom,geni-debug-uart", },
15348a8a66a1SGirish Mahadevan 	{ .compatible = "qcom,geni-uart", },
1535c4f52879SKarthikeyan Ramasubramanian 	{}
1536c4f52879SKarthikeyan Ramasubramanian };
1537c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1538c4f52879SKarthikeyan Ramasubramanian 
1539c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1540c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1541c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1542c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1543c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1544c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1545c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1546c4f52879SKarthikeyan Ramasubramanian 	},
1547c4f52879SKarthikeyan Ramasubramanian };
1548c4f52879SKarthikeyan Ramasubramanian 
1549c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1550c4f52879SKarthikeyan Ramasubramanian {
1551c4f52879SKarthikeyan Ramasubramanian 	int ret;
1552c4f52879SKarthikeyan Ramasubramanian 
1553c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1554c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1555c4f52879SKarthikeyan Ramasubramanian 		return ret;
1556c4f52879SKarthikeyan Ramasubramanian 
15578a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
15588a8a66a1SGirish Mahadevan 	if (ret) {
1559c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1560c4f52879SKarthikeyan Ramasubramanian 		return ret;
1561c4f52879SKarthikeyan Ramasubramanian 	}
15628a8a66a1SGirish Mahadevan 
15638a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
15648a8a66a1SGirish Mahadevan 	if (ret) {
15658a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
15668a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
15678a8a66a1SGirish Mahadevan 	}
15688a8a66a1SGirish Mahadevan 	return ret;
15698a8a66a1SGirish Mahadevan }
1570c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1571c4f52879SKarthikeyan Ramasubramanian 
1572c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1573c4f52879SKarthikeyan Ramasubramanian {
1574c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1575c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
15768a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1577c4f52879SKarthikeyan Ramasubramanian }
1578c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1579c4f52879SKarthikeyan Ramasubramanian 
1580c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1581c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1582