xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision bec5b814d46c2a704c3c8148752e62a33e9fa6dc)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
460457d5eSSai Prakash Ranjan /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
560457d5eSSai Prakash Ranjan #define __DISABLE_TRACE_MMIO__
660457d5eSSai Prakash Ranjan 
7c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
8c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
113e4aaea7SAkash Asthana #include <linux/irq.h>
12c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
14c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
15a5819b54SRajendra Nayak #include <linux/pm_opp.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
17f3974413SAkash Asthana #include <linux/pm_runtime.h>
188b7103f3SAkash Asthana #include <linux/pm_wakeirq.h>
19c4f52879SKarthikeyan Ramasubramanian #include <linux/qcom-geni-se.h>
20c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
21c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
22c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
23c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
24c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
25c4f52879SKarthikeyan Ramasubramanian 
26c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
278a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
289fa3c4b1SRoja Rani Yarubandi #define SE_UART_IO_MACRO_CTRL		0x240
29c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
30c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
31c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
32c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
33c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
34c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
35c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
36c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
37c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
388a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
39c4f52879SKarthikeyan Ramasubramanian 
40c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
41c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN		BIT(0)
42c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK		BIT(1)
43c4f52879SKarthikeyan Ramasubramanian 
44c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_WORD_LEN */
45c4f52879SKarthikeyan Ramasubramanian #define TX_WORD_LEN_MSK		GENMASK(9, 0)
46c4f52879SKarthikeyan Ramasubramanian 
47c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
48c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_MSK	GENMASK(23, 0)
49c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1	0
50c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1_5	1
51c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2	2
52c4f52879SKarthikeyan Ramasubramanian 
53c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_TRANS_LEN */
54c4f52879SKarthikeyan Ramasubramanian #define TX_TRANS_LEN_MSK	GENMASK(23, 0)
55c4f52879SKarthikeyan Ramasubramanian 
56c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
57c4f52879SKarthikeyan Ramasubramanian #define UART_RX_INS_STATUS_BIT	BIT(2)
58c4f52879SKarthikeyan Ramasubramanian #define UART_RX_PAR_EN		BIT(3)
59c4f52879SKarthikeyan Ramasubramanian 
60c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_WORD_LEN */
61c4f52879SKarthikeyan Ramasubramanian #define RX_WORD_LEN_MASK	GENMASK(9, 0)
62c4f52879SKarthikeyan Ramasubramanian 
63c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_STALE_CNT */
64c4f52879SKarthikeyan Ramasubramanian #define RX_STALE_CNT		GENMASK(23, 0)
65c4f52879SKarthikeyan Ramasubramanian 
66c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
67c4f52879SKarthikeyan Ramasubramanian #define PAR_CALC_EN		BIT(0)
68c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_MSK		GENMASK(2, 1)
69c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_SHFT		1
70c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN		0x00
71c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD			0x01
72c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE		0x10
73c4f52879SKarthikeyan Ramasubramanian #define PAR_MARK		0x11
74c4f52879SKarthikeyan Ramasubramanian 
758a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
768a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN	BIT(31)
778a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY	BIT(1)
788a8a66a1SGirish Mahadevan #define UART_RFR_READY		BIT(0)
798a8a66a1SGirish Mahadevan 
80c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
81c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX		0x1
82c4f52879SKarthikeyan Ramasubramanian #define UART_START_BREAK	0x4
83c4f52879SKarthikeyan Ramasubramanian #define UART_STOP_BREAK		0x5
84c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
85c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ		0x1
86c4f52879SKarthikeyan Ramasubramanian #define UART_PARAM		0x1
87c4f52879SKarthikeyan Ramasubramanian 
88c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING	32
89c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT		16
90c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR	10
91c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS	1
928a8a66a1SGirish Mahadevan #define GENI_UART_PORTS		3
93c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS	16
94c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM		2
95c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS	32
96a85fb9ceSRyan Case #define UART_RX_WM		2
9769bd1a4fSAkash Asthana 
9869bd1a4fSAkash Asthana /* SE_UART_LOOPBACK_CFG */
9969bd1a4fSAkash Asthana #define RX_TX_SORTED	BIT(0)
10069bd1a4fSAkash Asthana #define CTS_RTS_SORTED	BIT(1)
10169bd1a4fSAkash Asthana #define RX_TX_CTS_RTS_SORTED	(RX_TX_SORTED | CTS_RTS_SORTED)
102c4f52879SKarthikeyan Ramasubramanian 
1039fa3c4b1SRoja Rani Yarubandi /* UART pin swap value */
1049fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO0_IO1_MASK		GENMASK(3, 0)
1059fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO0_SEL		0x3
1069fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO2_IO3_MASK		GENMASK(15, 4)
1079fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO2_IO3_SWAP		0x4640
1089fa3c4b1SRoja Rani Yarubandi 
109650c8bd3SDouglas Anderson /* We always configure 4 bytes per FIFO word */
110650c8bd3SDouglas Anderson #define BYTES_PER_FIFO_WORD		4
111650c8bd3SDouglas Anderson 
112e42d6c3eSDouglas Anderson struct qcom_geni_private_data {
113e42d6c3eSDouglas Anderson 	/* NOTE: earlycon port will have NULL here */
114e42d6c3eSDouglas Anderson 	struct uart_driver *drv;
115e42d6c3eSDouglas Anderson 
116e42d6c3eSDouglas Anderson 	u32 poll_cached_bytes;
117e42d6c3eSDouglas Anderson 	unsigned int poll_cached_bytes_cnt;
118650c8bd3SDouglas Anderson 
119650c8bd3SDouglas Anderson 	u32 write_cached_bytes;
120650c8bd3SDouglas Anderson 	unsigned int write_cached_bytes_cnt;
121e42d6c3eSDouglas Anderson };
122c4f52879SKarthikeyan Ramasubramanian 
123c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
124c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
125c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
126f3974413SAkash Asthana 	const char *name;
127c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
128c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
129c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
130c4f52879SKarthikeyan Ramasubramanian 	bool setup;
131c4f52879SKarthikeyan Ramasubramanian 	int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
132c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
133f9d690b6Ssatya priya 	void *rx_fifo;
1348a8a66a1SGirish Mahadevan 	u32 loopback;
135c4f52879SKarthikeyan Ramasubramanian 	bool brk;
136a1fee899SRyan Case 
137a1fee899SRyan Case 	unsigned int tx_remaining;
1388b7103f3SAkash Asthana 	int wakeup_irq;
1399fa3c4b1SRoja Rani Yarubandi 	bool rx_tx_swap;
1409fa3c4b1SRoja Rani Yarubandi 	bool cts_rts_swap;
141e42d6c3eSDouglas Anderson 
142e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data private_data;
143c4f52879SKarthikeyan Ramasubramanian };
144c4f52879SKarthikeyan Ramasubramanian 
145f7371750SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops;
1468a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops;
147c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver;
1488a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver;
149c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
1508a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
151c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
152c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport);
153679aac5eSsatya priya static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
154c4f52879SKarthikeyan Ramasubramanian 
155c4f52879SKarthikeyan Ramasubramanian #define to_dev_port(ptr, member) \
156c4f52879SKarthikeyan Ramasubramanian 		container_of(ptr, struct qcom_geni_serial_port, member)
157c4f52879SKarthikeyan Ramasubramanian 
1588a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1598a8a66a1SGirish Mahadevan 	[0] = {
1608a8a66a1SGirish Mahadevan 		.uport = {
1618a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1628a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1638a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1648a8a66a1SGirish Mahadevan 				.line = 0,
1658a8a66a1SGirish Mahadevan 		},
1668a8a66a1SGirish Mahadevan 	},
1678a8a66a1SGirish Mahadevan 	[1] = {
1688a8a66a1SGirish Mahadevan 		.uport = {
1698a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1708a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1718a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1728a8a66a1SGirish Mahadevan 				.line = 1,
1738a8a66a1SGirish Mahadevan 		},
1748a8a66a1SGirish Mahadevan 	},
1758a8a66a1SGirish Mahadevan 	[2] = {
1768a8a66a1SGirish Mahadevan 		.uport = {
1778a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1788a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1798a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1808a8a66a1SGirish Mahadevan 				.line = 2,
1818a8a66a1SGirish Mahadevan 		},
1828a8a66a1SGirish Mahadevan 	},
1838a8a66a1SGirish Mahadevan };
1848a8a66a1SGirish Mahadevan 
185f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
186f7371750SKarthikeyan Ramasubramanian 	.uport = {
187f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
188f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
189f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
190f7371750SKarthikeyan Ramasubramanian 		.line = 0,
191f7371750SKarthikeyan Ramasubramanian 	},
192f7371750SKarthikeyan Ramasubramanian };
193c4f52879SKarthikeyan Ramasubramanian 
194c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
195c4f52879SKarthikeyan Ramasubramanian {
196c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
197c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
198c4f52879SKarthikeyan Ramasubramanian 
19944e60d52SYueHaibing 	uport->membase = devm_platform_ioremap_resource(pdev, 0);
200c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
201c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
202c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
203c4f52879SKarthikeyan Ramasubramanian 	return 0;
204c4f52879SKarthikeyan Ramasubramanian }
205c4f52879SKarthikeyan Ramasubramanian 
206c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
207c4f52879SKarthikeyan Ramasubramanian {
208c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
209c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
210c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
211c4f52879SKarthikeyan Ramasubramanian 	}
212c4f52879SKarthikeyan Ramasubramanian }
213c4f52879SKarthikeyan Ramasubramanian 
2148a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
215c4f52879SKarthikeyan Ramasubramanian {
2168a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2178a8a66a1SGirish Mahadevan 	u32 geni_ios;
2188a8a66a1SGirish Mahadevan 
219e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport)) {
2208a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2218a8a66a1SGirish Mahadevan 	} else {
2229e06d55fSRyan Case 		geni_ios = readl(uport->membase + SE_GENI_IOS);
2238a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2248a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
225c4f52879SKarthikeyan Ramasubramanian 	}
226c4f52879SKarthikeyan Ramasubramanian 
2278a8a66a1SGirish Mahadevan 	return mctrl;
2288a8a66a1SGirish Mahadevan }
2298a8a66a1SGirish Mahadevan 
2308a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
231c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
232c4f52879SKarthikeyan Ramasubramanian {
2338a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
23469bd1a4fSAkash Asthana 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
2358a8a66a1SGirish Mahadevan 
236e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport))
2378a8a66a1SGirish Mahadevan 		return;
2388a8a66a1SGirish Mahadevan 
23969bd1a4fSAkash Asthana 	if (mctrl & TIOCM_LOOP)
24069bd1a4fSAkash Asthana 		port->loopback = RX_TX_CTS_RTS_SORTED;
24169bd1a4fSAkash Asthana 
242a4ced376Ssatya priya 	if (!(mctrl & TIOCM_RTS) && !uport->suspended)
2438a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2449e06d55fSRyan Case 	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
245c4f52879SKarthikeyan Ramasubramanian }
246c4f52879SKarthikeyan Ramasubramanian 
247c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
248c4f52879SKarthikeyan Ramasubramanian {
249c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
250c4f52879SKarthikeyan Ramasubramanian }
251c4f52879SKarthikeyan Ramasubramanian 
2528a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
253c4f52879SKarthikeyan Ramasubramanian {
2548a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2558a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2568a8a66a1SGirish Mahadevan 
2578a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
258c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2598a8a66a1SGirish Mahadevan 
2608a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2618a8a66a1SGirish Mahadevan 	return port;
262c4f52879SKarthikeyan Ramasubramanian }
263c4f52879SKarthikeyan Ramasubramanian 
264c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
265c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
266c4f52879SKarthikeyan Ramasubramanian {
267c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
268c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
269c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
270c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
271c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
272e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
273c4f52879SKarthikeyan Ramasubramanian 
274e42d6c3eSDouglas Anderson 	if (private_data->drv) {
275c4f52879SKarthikeyan Ramasubramanian 		port = to_dev_port(uport, uport);
276c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
277c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
278c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
279c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
280c4f52879SKarthikeyan Ramasubramanian 		/*
281c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
282c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
283c4f52879SKarthikeyan Ramasubramanian 		 */
284c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
285c4f52879SKarthikeyan Ramasubramanian 	}
286c4f52879SKarthikeyan Ramasubramanian 
28743f1831bSKarthikeyan Ramasubramanian 	/*
28843f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
28943f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29043f1831bSKarthikeyan Ramasubramanian 	 */
29143f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29243f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
2939e06d55fSRyan Case 		reg = readl(uport->membase + offset);
29443f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29543f1831bSKarthikeyan Ramasubramanian 			return true;
29643f1831bSKarthikeyan Ramasubramanian 		udelay(10);
29743f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
29843f1831bSKarthikeyan Ramasubramanian 	}
29943f1831bSKarthikeyan Ramasubramanian 	return false;
300c4f52879SKarthikeyan Ramasubramanian }
301c4f52879SKarthikeyan Ramasubramanian 
302c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
303c4f52879SKarthikeyan Ramasubramanian {
304c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
305c4f52879SKarthikeyan Ramasubramanian 
3069e06d55fSRyan Case 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
307c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
308c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
309c4f52879SKarthikeyan Ramasubramanian }
310c4f52879SKarthikeyan Ramasubramanian 
311c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
312c4f52879SKarthikeyan Ramasubramanian {
313c4f52879SKarthikeyan Ramasubramanian 	int done;
314c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
315c4f52879SKarthikeyan Ramasubramanian 
316c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
317c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
318c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
3199e06d55fSRyan Case 		writel(M_GENI_CMD_ABORT, uport->membase +
320c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
321c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
322c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
323c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
324c4f52879SKarthikeyan Ramasubramanian 	}
3259e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
326c4f52879SKarthikeyan Ramasubramanian }
327c4f52879SKarthikeyan Ramasubramanian 
328c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
329c4f52879SKarthikeyan Ramasubramanian {
330c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
331c4f52879SKarthikeyan Ramasubramanian 
332c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
333c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
334c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
3359e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
3369e06d55fSRyan Case 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
337c4f52879SKarthikeyan Ramasubramanian }
338c4f52879SKarthikeyan Ramasubramanian 
339c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
340e42d6c3eSDouglas Anderson 
341c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
342c4f52879SKarthikeyan Ramasubramanian {
343e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
344c4f52879SKarthikeyan Ramasubramanian 	u32 status;
345e42d6c3eSDouglas Anderson 	u32 word_cnt;
346e42d6c3eSDouglas Anderson 	int ret;
347c4f52879SKarthikeyan Ramasubramanian 
348e42d6c3eSDouglas Anderson 	if (!private_data->poll_cached_bytes_cnt) {
3499e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
3509e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
351c4f52879SKarthikeyan Ramasubramanian 
3529e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
3539e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
354c4f52879SKarthikeyan Ramasubramanian 
3559e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
356e42d6c3eSDouglas Anderson 		word_cnt = status & RX_FIFO_WC_MSK;
357e42d6c3eSDouglas Anderson 		if (!word_cnt)
358c4f52879SKarthikeyan Ramasubramanian 			return NO_POLL_CHAR;
359c4f52879SKarthikeyan Ramasubramanian 
360e42d6c3eSDouglas Anderson 		if (word_cnt == 1 && (status & RX_LAST))
361d681a6e4SDouglas Anderson 			/*
362d681a6e4SDouglas Anderson 			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
363d681a6e4SDouglas Anderson 			 * treated as if it was BYTES_PER_FIFO_WORD.
364d681a6e4SDouglas Anderson 			 */
365e42d6c3eSDouglas Anderson 			private_data->poll_cached_bytes_cnt =
366e42d6c3eSDouglas Anderson 				(status & RX_LAST_BYTE_VALID_MSK) >>
367e42d6c3eSDouglas Anderson 				RX_LAST_BYTE_VALID_SHFT;
368d681a6e4SDouglas Anderson 
369d681a6e4SDouglas Anderson 		if (private_data->poll_cached_bytes_cnt == 0)
370d681a6e4SDouglas Anderson 			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
371e42d6c3eSDouglas Anderson 
372e42d6c3eSDouglas Anderson 		private_data->poll_cached_bytes =
373e42d6c3eSDouglas Anderson 			readl(uport->membase + SE_GENI_RX_FIFOn);
374e42d6c3eSDouglas Anderson 	}
375e42d6c3eSDouglas Anderson 
376e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes_cnt--;
377e42d6c3eSDouglas Anderson 	ret = private_data->poll_cached_bytes & 0xff;
378e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes >>= 8;
379e42d6c3eSDouglas Anderson 
380e42d6c3eSDouglas Anderson 	return ret;
381c4f52879SKarthikeyan Ramasubramanian }
382c4f52879SKarthikeyan Ramasubramanian 
383c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
384c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
385c4f52879SKarthikeyan Ramasubramanian {
386a85fb9ceSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
387c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
388c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
389c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
3909e06d55fSRyan Case 	writel(c, uport->membase + SE_GENI_TX_FIFOn);
3919e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
392c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
393c4f52879SKarthikeyan Ramasubramanian }
394c4f52879SKarthikeyan Ramasubramanian #endif
395c4f52879SKarthikeyan Ramasubramanian 
396c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
3973f8bab17SJiri Slaby static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
398c4f52879SKarthikeyan Ramasubramanian {
399650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
400650c8bd3SDouglas Anderson 
401650c8bd3SDouglas Anderson 	private_data->write_cached_bytes =
402650c8bd3SDouglas Anderson 		(private_data->write_cached_bytes >> 8) | (ch << 24);
403650c8bd3SDouglas Anderson 	private_data->write_cached_bytes_cnt++;
404650c8bd3SDouglas Anderson 
405650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
406650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
407650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
408650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
409650c8bd3SDouglas Anderson 	}
410c4f52879SKarthikeyan Ramasubramanian }
411c4f52879SKarthikeyan Ramasubramanian 
412c4f52879SKarthikeyan Ramasubramanian static void
413c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
414c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
415c4f52879SKarthikeyan Ramasubramanian {
416650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
417650c8bd3SDouglas Anderson 
418c4f52879SKarthikeyan Ramasubramanian 	int i;
419c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
420c4f52879SKarthikeyan Ramasubramanian 
421c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
422f0262568SKarthikeyan Ramasubramanian 		/*
423f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
424f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
425f0262568SKarthikeyan Ramasubramanian 		 */
426c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
427c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
428c4f52879SKarthikeyan Ramasubramanian 	}
429c4f52879SKarthikeyan Ramasubramanian 
4309e06d55fSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
431c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
432c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
433c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
434c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
435c4f52879SKarthikeyan Ramasubramanian 
436c4f52879SKarthikeyan Ramasubramanian 		/*
437c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
438c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
439c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
440c4f52879SKarthikeyan Ramasubramanian 		 * lost.
441c4f52879SKarthikeyan Ramasubramanian 		 */
442c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
443c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
444c4f52879SKarthikeyan Ramasubramanian 			break;
4456a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
446c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
447c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
4489e06d55fSRyan Case 		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
449c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
450c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
451c4f52879SKarthikeyan Ramasubramanian 	}
452650c8bd3SDouglas Anderson 
453650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt) {
454650c8bd3SDouglas Anderson 		private_data->write_cached_bytes >>= BITS_PER_BYTE *
455650c8bd3SDouglas Anderson 			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
456650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
457650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
458650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
459650c8bd3SDouglas Anderson 	}
460650c8bd3SDouglas Anderson 
461c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
462c4f52879SKarthikeyan Ramasubramanian }
463c4f52879SKarthikeyan Ramasubramanian 
464c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
465c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
466c4f52879SKarthikeyan Ramasubramanian {
467c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
468c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
469c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
470c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
471a1fee899SRyan Case 	u32 geni_status;
472663abb1aSRyan Case 	u32 irq_en;
473c4f52879SKarthikeyan Ramasubramanian 
474c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
475c4f52879SKarthikeyan Ramasubramanian 
4768a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
477c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
478c4f52879SKarthikeyan Ramasubramanian 		return;
479c4f52879SKarthikeyan Ramasubramanian 
480c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
481c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
482c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
483c4f52879SKarthikeyan Ramasubramanian 	else
484c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
485c4f52879SKarthikeyan Ramasubramanian 
4869e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
487a1fee899SRyan Case 
488c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
489c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
490c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
491c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
492c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
493c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
494c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
495c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
4969e06d55fSRyan Case 			writel(M_CMD_ABORT_EN, uport->membase +
497c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
498c4f52879SKarthikeyan Ramasubramanian 		}
4999e06d55fSRyan Case 		writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
500a1fee899SRyan Case 	} else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
501a1fee899SRyan Case 		/*
502a1fee899SRyan Case 		 * It seems we can't interrupt existing transfers if all data
503a1fee899SRyan Case 		 * has been sent, in which case we need to look for done first.
504a1fee899SRyan Case 		 */
505a1fee899SRyan Case 		qcom_geni_serial_poll_tx_done(uport);
506663abb1aSRyan Case 
507d2b574c0SJiri Slaby 		if (!uart_circ_empty(&uport->state->xmit)) {
5089e06d55fSRyan Case 			irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
5099e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
510663abb1aSRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
511663abb1aSRyan Case 		}
512c4f52879SKarthikeyan Ramasubramanian 	}
513c4f52879SKarthikeyan Ramasubramanian 
514c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
515a1fee899SRyan Case 
516a1fee899SRyan Case 	if (port->tx_remaining)
517a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, port->tx_remaining);
518a1fee899SRyan Case 
519c4f52879SKarthikeyan Ramasubramanian 	if (locked)
520c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
521c4f52879SKarthikeyan Ramasubramanian }
522c4f52879SKarthikeyan Ramasubramanian 
523c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
524c4f52879SKarthikeyan Ramasubramanian {
525c4f52879SKarthikeyan Ramasubramanian 	u32 i;
526c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
527c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
528c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
529c4f52879SKarthikeyan Ramasubramanian 
530c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
531c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
532c4f52879SKarthikeyan Ramasubramanian 		int c;
533650c8bd3SDouglas Anderson 		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
534c4f52879SKarthikeyan Ramasubramanian 
535c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
536c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
537c4f52879SKarthikeyan Ramasubramanian 		if (drop)
538c4f52879SKarthikeyan Ramasubramanian 			continue;
539c4f52879SKarthikeyan Ramasubramanian 
540c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
541c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
542c4f52879SKarthikeyan Ramasubramanian 
543c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
544c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
545c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
546c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
547c4f52879SKarthikeyan Ramasubramanian 					continue;
548c4f52879SKarthikeyan Ramasubramanian 			}
549c4f52879SKarthikeyan Ramasubramanian 
550336447b3SDouglas Anderson 			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
551babeca85SDouglas Anderson 
552c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
553c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
554c4f52879SKarthikeyan Ramasubramanian 		}
555c4f52879SKarthikeyan Ramasubramanian 	}
556c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
557c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
558c4f52879SKarthikeyan Ramasubramanian 	return 0;
559c4f52879SKarthikeyan Ramasubramanian }
560c4f52879SKarthikeyan Ramasubramanian #else
561c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
562c4f52879SKarthikeyan Ramasubramanian {
563c4f52879SKarthikeyan Ramasubramanian 	return -EPERM;
564c4f52879SKarthikeyan Ramasubramanian }
565c4f52879SKarthikeyan Ramasubramanian 
566c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
567c4f52879SKarthikeyan Ramasubramanian 
5688a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5698a8a66a1SGirish Mahadevan {
5708a8a66a1SGirish Mahadevan 	struct tty_port *tport;
5718a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
5728a8a66a1SGirish Mahadevan 	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
5738a8a66a1SGirish Mahadevan 	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
5748a8a66a1SGirish Mahadevan 	int ret;
5758a8a66a1SGirish Mahadevan 
5768a8a66a1SGirish Mahadevan 	tport = &uport->state->port;
5778a8a66a1SGirish Mahadevan 	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
5788a8a66a1SGirish Mahadevan 	if (drop)
5798a8a66a1SGirish Mahadevan 		return 0;
5808a8a66a1SGirish Mahadevan 
581f9d690b6Ssatya priya 	ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
5828a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5838a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5848a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5858a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5868a8a66a1SGirish Mahadevan 	}
5878a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5888a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5898a8a66a1SGirish Mahadevan 	return ret;
5908a8a66a1SGirish Mahadevan }
5918a8a66a1SGirish Mahadevan 
592c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_tx(struct uart_port *uport)
593c4f52879SKarthikeyan Ramasubramanian {
594c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
595c4f52879SKarthikeyan Ramasubramanian 	u32 status;
596c4f52879SKarthikeyan Ramasubramanian 
5977fb5b880SKarthikeyan Ramasubramanian 	status = readl(uport->membase + SE_GENI_STATUS);
598c4f52879SKarthikeyan Ramasubramanian 	if (status & M_GENI_CMD_ACTIVE)
599c4f52879SKarthikeyan Ramasubramanian 		return;
600c4f52879SKarthikeyan Ramasubramanian 
601c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_tx_empty(uport))
602c4f52879SKarthikeyan Ramasubramanian 		return;
603c4f52879SKarthikeyan Ramasubramanian 
6049e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
605c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
606c4f52879SKarthikeyan Ramasubramanian 
607bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
6089e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
609c4f52879SKarthikeyan Ramasubramanian }
610c4f52879SKarthikeyan Ramasubramanian 
611c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_tx(struct uart_port *uport)
612c4f52879SKarthikeyan Ramasubramanian {
613c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
614c4f52879SKarthikeyan Ramasubramanian 	u32 status;
615c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
616c4f52879SKarthikeyan Ramasubramanian 
6179e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
618bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
619bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
6209e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
6219e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
622c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
623c4f52879SKarthikeyan Ramasubramanian 	if (!(status & M_GENI_CMD_ACTIVE))
624c4f52879SKarthikeyan Ramasubramanian 		return;
625c4f52879SKarthikeyan Ramasubramanian 
626c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
627c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
628c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
629c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
630c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
631c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
6329e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
633c4f52879SKarthikeyan Ramasubramanian 	}
6349e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
635c4f52879SKarthikeyan Ramasubramanian }
636c4f52879SKarthikeyan Ramasubramanian 
637c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_rx(struct uart_port *uport)
638c4f52879SKarthikeyan Ramasubramanian {
639c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
640c4f52879SKarthikeyan Ramasubramanian 	u32 status;
641c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
642c4f52879SKarthikeyan Ramasubramanian 
6439e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
644c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
645c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_rx(uport);
646c4f52879SKarthikeyan Ramasubramanian 
647c4f52879SKarthikeyan Ramasubramanian 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
648c4f52879SKarthikeyan Ramasubramanian 
6499e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
650c4f52879SKarthikeyan Ramasubramanian 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
6519e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
652c4f52879SKarthikeyan Ramasubramanian 
6539e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
654c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
6559e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
656c4f52879SKarthikeyan Ramasubramanian }
657c4f52879SKarthikeyan Ramasubramanian 
658c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport)
659c4f52879SKarthikeyan Ramasubramanian {
660c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
661c4f52879SKarthikeyan Ramasubramanian 	u32 status;
662c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
663679aac5eSsatya priya 	u32 s_irq_status;
664c4f52879SKarthikeyan Ramasubramanian 
6659e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
666c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
6679e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
668c4f52879SKarthikeyan Ramasubramanian 
6699e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
670c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
6719e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
672c4f52879SKarthikeyan Ramasubramanian 
6739e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
674c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop rx is called multiple times. */
675c4f52879SKarthikeyan Ramasubramanian 	if (!(status & S_GENI_CMD_ACTIVE))
676c4f52879SKarthikeyan Ramasubramanian 		return;
677c4f52879SKarthikeyan Ramasubramanian 
678c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
679679aac5eSsatya priya 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
680679aac5eSsatya priya 					S_CMD_CANCEL_EN, true);
681679aac5eSsatya priya 	/*
682679aac5eSsatya priya 	 * If timeout occurs secondary engine remains active
683679aac5eSsatya priya 	 * and Abort sequence is executed.
684679aac5eSsatya priya 	 */
685679aac5eSsatya priya 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
686679aac5eSsatya priya 	/* Flush the Rx buffer */
687679aac5eSsatya priya 	if (s_irq_status & S_RX_FIFO_LAST_EN)
688679aac5eSsatya priya 		qcom_geni_serial_handle_rx(uport, true);
689679aac5eSsatya priya 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
690679aac5eSsatya priya 
6919e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
692c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
693c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
694c4f52879SKarthikeyan Ramasubramanian }
695c4f52879SKarthikeyan Ramasubramanian 
696c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
697c4f52879SKarthikeyan Ramasubramanian {
698c4f52879SKarthikeyan Ramasubramanian 	u32 status;
699c4f52879SKarthikeyan Ramasubramanian 	u32 word_cnt;
700c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_byte_cnt;
701c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_partial;
702c4f52879SKarthikeyan Ramasubramanian 	u32 total_bytes;
703c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
704c4f52879SKarthikeyan Ramasubramanian 
7059e06d55fSRyan Case 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
706c4f52879SKarthikeyan Ramasubramanian 	word_cnt = status & RX_FIFO_WC_MSK;
707c4f52879SKarthikeyan Ramasubramanian 	last_word_partial = status & RX_LAST;
708c4f52879SKarthikeyan Ramasubramanian 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
709c4f52879SKarthikeyan Ramasubramanian 						RX_LAST_BYTE_VALID_SHFT;
710c4f52879SKarthikeyan Ramasubramanian 
711c4f52879SKarthikeyan Ramasubramanian 	if (!word_cnt)
712c4f52879SKarthikeyan Ramasubramanian 		return;
713650c8bd3SDouglas Anderson 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
714c4f52879SKarthikeyan Ramasubramanian 	if (last_word_partial && last_word_byte_cnt)
715c4f52879SKarthikeyan Ramasubramanian 		total_bytes += last_word_byte_cnt;
716c4f52879SKarthikeyan Ramasubramanian 	else
717650c8bd3SDouglas Anderson 		total_bytes += BYTES_PER_FIFO_WORD;
718c4f52879SKarthikeyan Ramasubramanian 	port->handle_rx(uport, total_bytes, drop);
719c4f52879SKarthikeyan Ramasubramanian }
720c4f52879SKarthikeyan Ramasubramanian 
721a1fee899SRyan Case static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
722a1fee899SRyan Case 		bool active)
723c4f52879SKarthikeyan Ramasubramanian {
724c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
725c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
726c4f52879SKarthikeyan Ramasubramanian 	size_t avail;
727c4f52879SKarthikeyan Ramasubramanian 	size_t remaining;
728a1fee899SRyan Case 	size_t pending;
729c4f52879SKarthikeyan Ramasubramanian 	int i;
730c4f52879SKarthikeyan Ramasubramanian 	u32 status;
73164a42807SRyan Case 	u32 irq_en;
732c4f52879SKarthikeyan Ramasubramanian 	unsigned int chunk;
733c4f52879SKarthikeyan Ramasubramanian 	int tail;
734c4f52879SKarthikeyan Ramasubramanian 
7359e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
736a1fee899SRyan Case 
737a1fee899SRyan Case 	/* Complete the current tx command before taking newly added data */
738a1fee899SRyan Case 	if (active)
739a1fee899SRyan Case 		pending = port->tx_remaining;
740a1fee899SRyan Case 	else
741a1fee899SRyan Case 		pending = uart_circ_chars_pending(xmit);
742a1fee899SRyan Case 
743a1fee899SRyan Case 	/* All data has been transmitted and acknowledged as received */
744a1fee899SRyan Case 	if (!pending && !status && done) {
745c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_tx(uport);
746c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
747c4f52879SKarthikeyan Ramasubramanian 	}
748c4f52879SKarthikeyan Ramasubramanian 
749a1fee899SRyan Case 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
750650c8bd3SDouglas Anderson 	avail *= BYTES_PER_FIFO_WORD;
7518a8a66a1SGirish Mahadevan 
752638a6f4eSEvan Green 	tail = xmit->tail;
7533c66eb4bSMatthias Kaehlcke 	chunk = min(avail, pending);
754c4f52879SKarthikeyan Ramasubramanian 	if (!chunk)
755c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
756c4f52879SKarthikeyan Ramasubramanian 
757a1fee899SRyan Case 	if (!port->tx_remaining) {
758a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, pending);
759a1fee899SRyan Case 		port->tx_remaining = pending;
76064a42807SRyan Case 
7619e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
76264a42807SRyan Case 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
7639e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
76464a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
765a1fee899SRyan Case 	}
766c4f52879SKarthikeyan Ramasubramanian 
767c4f52879SKarthikeyan Ramasubramanian 	remaining = chunk;
768c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < chunk; ) {
769c4f52879SKarthikeyan Ramasubramanian 		unsigned int tx_bytes;
77069736b57SKarthikeyan Ramasubramanian 		u8 buf[sizeof(u32)];
771c4f52879SKarthikeyan Ramasubramanian 		int c;
772c4f52879SKarthikeyan Ramasubramanian 
7733550f897SDan Carpenter 		memset(buf, 0, sizeof(buf));
774650c8bd3SDouglas Anderson 		tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
7753c66eb4bSMatthias Kaehlcke 
7763c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
7773c66eb4bSMatthias Kaehlcke 			buf[c] = xmit->buf[tail++];
7783c66eb4bSMatthias Kaehlcke 			tail &= UART_XMIT_SIZE - 1;
7793c66eb4bSMatthias Kaehlcke 		}
780c4f52879SKarthikeyan Ramasubramanian 
78169736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
782c4f52879SKarthikeyan Ramasubramanian 
783c4f52879SKarthikeyan Ramasubramanian 		i += tx_bytes;
784c4f52879SKarthikeyan Ramasubramanian 		uport->icount.tx += tx_bytes;
785c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
786a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
787c4f52879SKarthikeyan Ramasubramanian 	}
788638a6f4eSEvan Green 
7893c66eb4bSMatthias Kaehlcke 	xmit->tail = tail;
79064a42807SRyan Case 
79164a42807SRyan Case 	/*
79264a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
79364a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
79464a42807SRyan Case 	 * so we must clear it again here after our writes.
79564a42807SRyan Case 	 */
7969e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
79764a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
79864a42807SRyan Case 
799c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
80064a42807SRyan Case 	if (!port->tx_remaining) {
8019e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
80264a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
8039e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
80464a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
80564a42807SRyan Case 	}
80664a42807SRyan Case 
807638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
808c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
809c4f52879SKarthikeyan Ramasubramanian }
810c4f52879SKarthikeyan Ramasubramanian 
811c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
812c4f52879SKarthikeyan Ramasubramanian {
813385298abSRyan Case 	u32 m_irq_en;
814385298abSRyan Case 	u32 m_irq_status;
815385298abSRyan Case 	u32 s_irq_status;
816385298abSRyan Case 	u32 geni_status;
817c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
818c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
819c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
820c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
821c4f52879SKarthikeyan Ramasubramanian 
822c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
823ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
824c4f52879SKarthikeyan Ramasubramanian 
82575f4e830SJohan Hovold 	spin_lock(&uport->lock);
82675f4e830SJohan Hovold 
8279e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
8289e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
8299e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
8309e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
8319e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
8329e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
833c4f52879SKarthikeyan Ramasubramanian 
834c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
835c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
836c4f52879SKarthikeyan Ramasubramanian 
837c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
838c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
839c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
840c4f52879SKarthikeyan Ramasubramanian 	}
841c4f52879SKarthikeyan Ramasubramanian 
84264a42807SRyan Case 	if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
843a1fee899SRyan Case 		qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
844a1fee899SRyan Case 					geni_status & M_GENI_CMD_ACTIVE);
845c4f52879SKarthikeyan Ramasubramanian 
846c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
847c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
848c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
849c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
850c4f52879SKarthikeyan Ramasubramanian 	} else if (s_irq_status & S_GP_IRQ_2_EN ||
851c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_GP_IRQ_3_EN) {
852c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
853c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
854c4f52879SKarthikeyan Ramasubramanian 	}
855c4f52879SKarthikeyan Ramasubramanian 
856c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
857c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_RX_FIFO_LAST_EN)
858c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_rx(uport, drop_rx);
859c4f52879SKarthikeyan Ramasubramanian 
860c4f52879SKarthikeyan Ramasubramanian out_unlock:
86175f4e830SJohan Hovold 	uart_unlock_and_check_sysrq(uport);
862336447b3SDouglas Anderson 
863c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
864c4f52879SKarthikeyan Ramasubramanian }
865c4f52879SKarthikeyan Ramasubramanian 
8666a10635eSKarthikeyan Ramasubramanian static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
867c4f52879SKarthikeyan Ramasubramanian {
868c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
869c4f52879SKarthikeyan Ramasubramanian 
870c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
871c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
872c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
873c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
874c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
875c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
876c4f52879SKarthikeyan Ramasubramanian }
877c4f52879SKarthikeyan Ramasubramanian 
878c4f52879SKarthikeyan Ramasubramanian 
879c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
880c4f52879SKarthikeyan Ramasubramanian {
8813e4aaea7SAkash Asthana 	disable_irq(uport->irq);
882c4f52879SKarthikeyan Ramasubramanian }
883c4f52879SKarthikeyan Ramasubramanian 
884c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
885c4f52879SKarthikeyan Ramasubramanian {
886c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
887385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
888c362272bSDouglas Anderson 	u32 proto;
8899fa3c4b1SRoja Rani Yarubandi 	u32 pin_swap;
890c362272bSDouglas Anderson 
891c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
892c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
893c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
894c362272bSDouglas Anderson 		return -ENXIO;
895c362272bSDouglas Anderson 	}
896c362272bSDouglas Anderson 
897c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
898c362272bSDouglas Anderson 
899c362272bSDouglas Anderson 	get_tx_fifo_size(port);
900c4f52879SKarthikeyan Ramasubramanian 
9019e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
9029fa3c4b1SRoja Rani Yarubandi 
9039fa3c4b1SRoja Rani Yarubandi 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
9049fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap) {
9059fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
9069fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
9079fa3c4b1SRoja Rani Yarubandi 	}
9089fa3c4b1SRoja Rani Yarubandi 	if (port->cts_rts_swap) {
9099fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
9109fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO0_SEL;
9119fa3c4b1SRoja Rani Yarubandi 	}
9129fa3c4b1SRoja Rani Yarubandi 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
9139fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap || port->cts_rts_swap)
9149fa3c4b1SRoja Rani Yarubandi 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
9159fa3c4b1SRoja Rani Yarubandi 
916c4f52879SKarthikeyan Ramasubramanian 	/*
917c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
918c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
919c4f52879SKarthikeyan Ramasubramanian 	 */
9208a8a66a1SGirish Mahadevan 	if (uart_console(uport))
921c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
922650c8bd3SDouglas Anderson 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
923650c8bd3SDouglas Anderson 			       false, true, true);
924a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
925bdc05a8aSRyan Case 	geni_se_select_mode(&port->se, GENI_SE_FIFO);
926c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
927c362272bSDouglas Anderson 
928c4f52879SKarthikeyan Ramasubramanian 	return 0;
929c4f52879SKarthikeyan Ramasubramanian }
930c4f52879SKarthikeyan Ramasubramanian 
931c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
932c4f52879SKarthikeyan Ramasubramanian {
933c4f52879SKarthikeyan Ramasubramanian 	int ret;
934c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
935c4f52879SKarthikeyan Ramasubramanian 
936c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
937c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
938c4f52879SKarthikeyan Ramasubramanian 		if (ret)
939c4f52879SKarthikeyan Ramasubramanian 			return ret;
940c4f52879SKarthikeyan Ramasubramanian 	}
9413e4aaea7SAkash Asthana 	enable_irq(uport->irq);
942c4f52879SKarthikeyan Ramasubramanian 
9433e4aaea7SAkash Asthana 	return 0;
944c4f52879SKarthikeyan Ramasubramanian }
945c4f52879SKarthikeyan Ramasubramanian 
946c474c775SVijaya Krishna Nivarthi static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
947c474c775SVijaya Krishna Nivarthi 			unsigned int *clk_div, unsigned int percent_tol)
948c474c775SVijaya Krishna Nivarthi {
949c474c775SVijaya Krishna Nivarthi 	unsigned long freq;
950c474c775SVijaya Krishna Nivarthi 	unsigned long div, maxdiv;
951c474c775SVijaya Krishna Nivarthi 	u64 mult;
952c474c775SVijaya Krishna Nivarthi 	unsigned long offset, abs_tol, achieved;
953c474c775SVijaya Krishna Nivarthi 
954c474c775SVijaya Krishna Nivarthi 	abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
955c474c775SVijaya Krishna Nivarthi 	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
956c474c775SVijaya Krishna Nivarthi 	div = 1;
957c474c775SVijaya Krishna Nivarthi 	while (div <= maxdiv) {
958c474c775SVijaya Krishna Nivarthi 		mult = (u64)div * desired_clk;
959c474c775SVijaya Krishna Nivarthi 		if (mult != (unsigned long)mult)
960c474c775SVijaya Krishna Nivarthi 			break;
961c474c775SVijaya Krishna Nivarthi 
962c474c775SVijaya Krishna Nivarthi 		offset = div * abs_tol;
963c474c775SVijaya Krishna Nivarthi 		freq = clk_round_rate(clk, mult - offset);
964c474c775SVijaya Krishna Nivarthi 
965c474c775SVijaya Krishna Nivarthi 		/* Can only get lower if we're done */
966c474c775SVijaya Krishna Nivarthi 		if (freq < mult - offset)
967c474c775SVijaya Krishna Nivarthi 			break;
968c474c775SVijaya Krishna Nivarthi 
969c474c775SVijaya Krishna Nivarthi 		/*
970c474c775SVijaya Krishna Nivarthi 		 * Re-calculate div in case rounding skipped rates but we
971c474c775SVijaya Krishna Nivarthi 		 * ended up at a good one, then check for a match.
972c474c775SVijaya Krishna Nivarthi 		 */
973c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_CLOSEST(freq, desired_clk);
974c474c775SVijaya Krishna Nivarthi 		achieved = DIV_ROUND_CLOSEST(freq, div);
975c474c775SVijaya Krishna Nivarthi 		if (achieved <= desired_clk + abs_tol &&
976c474c775SVijaya Krishna Nivarthi 		    achieved >= desired_clk - abs_tol) {
977c474c775SVijaya Krishna Nivarthi 			*clk_div = div;
978c474c775SVijaya Krishna Nivarthi 			return freq;
979c474c775SVijaya Krishna Nivarthi 		}
980c474c775SVijaya Krishna Nivarthi 
981c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_UP(freq, desired_clk);
982c474c775SVijaya Krishna Nivarthi 	}
983c474c775SVijaya Krishna Nivarthi 
984c474c775SVijaya Krishna Nivarthi 	return 0;
985c474c775SVijaya Krishna Nivarthi }
986c474c775SVijaya Krishna Nivarthi 
987c2194bc9SVijaya Krishna Nivarthi static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
988ce734600SVivek Gautam 			unsigned int sampling_rate, unsigned int *clk_div)
989c4f52879SKarthikeyan Ramasubramanian {
990c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
991c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
992c4f52879SKarthikeyan Ramasubramanian 
993ce734600SVivek Gautam 	desired_clk = baud * sampling_rate;
994c474c775SVijaya Krishna Nivarthi 	if (!desired_clk)
995c2194bc9SVijaya Krishna Nivarthi 		return 0;
996c2194bc9SVijaya Krishna Nivarthi 
997c474c775SVijaya Krishna Nivarthi 	/*
998c474c775SVijaya Krishna Nivarthi 	 * try to find a clock rate within 2% tolerance, then within 5%
999c474c775SVijaya Krishna Nivarthi 	 */
1000c474c775SVijaya Krishna Nivarthi 	ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1001c474c775SVijaya Krishna Nivarthi 	if (!ser_clk)
1002c474c775SVijaya Krishna Nivarthi 		ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1003c2194bc9SVijaya Krishna Nivarthi 
1004c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
1005c4f52879SKarthikeyan Ramasubramanian }
1006c4f52879SKarthikeyan Ramasubramanian 
1007c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
1008*bec5b814SIlpo Järvinen 					 struct ktermios *termios,
1009*bec5b814SIlpo Järvinen 					 const struct ktermios *old)
1010c4f52879SKarthikeyan Ramasubramanian {
1011c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
1012385298abSRyan Case 	u32 bits_per_char;
1013385298abSRyan Case 	u32 tx_trans_cfg;
1014385298abSRyan Case 	u32 tx_parity_cfg;
1015385298abSRyan Case 	u32 rx_trans_cfg;
1016385298abSRyan Case 	u32 rx_parity_cfg;
1017385298abSRyan Case 	u32 stop_bit_len;
1018c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
1019385298abSRyan Case 	u32 ser_clk_cfg;
1020c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1021c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
1022ce734600SVivek Gautam 	u32 ver, sampling_rate;
10237cf563b2SAkash Asthana 	unsigned int avg_bw_core;
1024c4f52879SKarthikeyan Ramasubramanian 
1025c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
1026c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
1027c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1028c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
1029ce734600SVivek Gautam 
1030ce734600SVivek Gautam 	sampling_rate = UART_OVERSAMPLING;
1031ce734600SVivek Gautam 	/* Sampling rate is halved for IP versions >= 2.5 */
1032ce734600SVivek Gautam 	ver = geni_se_get_qup_hw_version(&port->se);
1033c9ca43d4SParas Sharma 	if (ver >= QUP_SE_VERSION_2_5)
1034ce734600SVivek Gautam 		sampling_rate /= 2;
1035ce734600SVivek Gautam 
1036c2194bc9SVijaya Krishna Nivarthi 	clk_rate = get_clk_div_rate(port->se.clk, baud,
1037c2194bc9SVijaya Krishna Nivarthi 		sampling_rate, &clk_div);
1038c474c775SVijaya Krishna Nivarthi 	if (!clk_rate) {
1039c474c775SVijaya Krishna Nivarthi 		dev_err(port->se.dev,
10400fec5180SDouglas Anderson 			"Couldn't find suitable clock rate for %u\n",
1041c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate);
1042c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
1043c474c775SVijaya Krishna Nivarthi 	}
1044c474c775SVijaya Krishna Nivarthi 
10450fec5180SDouglas Anderson 	dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
1046c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate, clk_rate, clk_div);
1047c4f52879SKarthikeyan Ramasubramanian 
1048c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
1049a5819b54SRajendra Nayak 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1050c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
1051c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1052c4f52879SKarthikeyan Ramasubramanian 
10537cf563b2SAkash Asthana 	/*
10547cf563b2SAkash Asthana 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
10557cf563b2SAkash Asthana 	 * only.
10567cf563b2SAkash Asthana 	 */
10577cf563b2SAkash Asthana 	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
10587cf563b2SAkash Asthana 						: GENI_DEFAULT_BW;
10597cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
10607cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
10617cf563b2SAkash Asthana 	geni_icc_set_bw(&port->se);
10627cf563b2SAkash Asthana 
1063c4f52879SKarthikeyan Ramasubramanian 	/* parity */
10649e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
10659e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
10669e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
10679e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1068c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
1069c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
1070c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
1071c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
1072c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
1073c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
1074c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
1075c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
1076c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
1077c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
1078c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
1079c4f52879SKarthikeyan Ramasubramanian 		} else {
1080c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
1081c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
1082c4f52879SKarthikeyan Ramasubramanian 		}
1083c4f52879SKarthikeyan Ramasubramanian 	} else {
1084c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1085c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1086c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1087c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1088c4f52879SKarthikeyan Ramasubramanian 	}
1089c4f52879SKarthikeyan Ramasubramanian 
1090c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
10913ec2ff37SJiri Slaby 	bits_per_char = tty_get_char_size(termios->c_cflag);
1092c4f52879SKarthikeyan Ramasubramanian 
1093c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1094c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1095c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1096c4f52879SKarthikeyan Ramasubramanian 	else
1097c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1098c4f52879SKarthikeyan Ramasubramanian 
1099c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1100c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1101c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1102c4f52879SKarthikeyan Ramasubramanian 	else
1103c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1104c4f52879SKarthikeyan Ramasubramanian 
1105c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1106c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1107c4f52879SKarthikeyan Ramasubramanian 
11088a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
11099e06d55fSRyan Case 		writel(port->loopback,
11108a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
11119e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
11129e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
11139e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
11149e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
11159e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
11169e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
11179e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
11189e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
11199e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1120c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1121c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1122c4f52879SKarthikeyan Ramasubramanian }
1123c4f52879SKarthikeyan Ramasubramanian 
1124c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1125c4f52879SKarthikeyan Ramasubramanian {
11267fb5b880SKarthikeyan Ramasubramanian 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1127c4f52879SKarthikeyan Ramasubramanian }
1128c4f52879SKarthikeyan Ramasubramanian 
1129c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1130975efc66SJohn Stultz static int qcom_geni_console_setup(struct console *co, char *options)
1131c4f52879SKarthikeyan Ramasubramanian {
1132c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1133c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
11342ec812a0SDouglas Anderson 	int baud = 115200;
1135c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1136c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1137c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1138c362272bSDouglas Anderson 	int ret;
1139c4f52879SKarthikeyan Ramasubramanian 
1140c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1141c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1142c4f52879SKarthikeyan Ramasubramanian 
11438a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1144c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
11456a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1146c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1147c4f52879SKarthikeyan Ramasubramanian 	}
1148c4f52879SKarthikeyan Ramasubramanian 
1149c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1150c4f52879SKarthikeyan Ramasubramanian 
1151c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1152c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1153c4f52879SKarthikeyan Ramasubramanian 
1154c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1155c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1156c362272bSDouglas Anderson 		if (ret)
1157c362272bSDouglas Anderson 			return ret;
1158c4f52879SKarthikeyan Ramasubramanian 	}
1159c4f52879SKarthikeyan Ramasubramanian 
1160c4f52879SKarthikeyan Ramasubramanian 	if (options)
1161c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1162c4f52879SKarthikeyan Ramasubramanian 
1163c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1164c4f52879SKarthikeyan Ramasubramanian }
1165c4f52879SKarthikeyan Ramasubramanian 
116643f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
116743f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
116843f1831bSKarthikeyan Ramasubramanian {
116943f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
117043f1831bSKarthikeyan Ramasubramanian 
117143f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
117243f1831bSKarthikeyan Ramasubramanian }
117343f1831bSKarthikeyan Ramasubramanian 
1174205b5bddSDouglas Anderson #ifdef CONFIG_CONSOLE_POLL
1175205b5bddSDouglas Anderson static int qcom_geni_serial_earlycon_read(struct console *con,
1176205b5bddSDouglas Anderson 					  char *s, unsigned int n)
1177205b5bddSDouglas Anderson {
1178205b5bddSDouglas Anderson 	struct earlycon_device *dev = con->data;
1179205b5bddSDouglas Anderson 	struct uart_port *uport = &dev->port;
1180205b5bddSDouglas Anderson 	int num_read = 0;
1181205b5bddSDouglas Anderson 	int ch;
1182205b5bddSDouglas Anderson 
1183205b5bddSDouglas Anderson 	while (num_read < n) {
1184205b5bddSDouglas Anderson 		ch = qcom_geni_serial_get_char(uport);
1185205b5bddSDouglas Anderson 		if (ch == NO_POLL_CHAR)
1186205b5bddSDouglas Anderson 			break;
1187205b5bddSDouglas Anderson 		s[num_read++] = ch;
1188205b5bddSDouglas Anderson 	}
1189205b5bddSDouglas Anderson 
1190205b5bddSDouglas Anderson 	return num_read;
1191205b5bddSDouglas Anderson }
1192205b5bddSDouglas Anderson 
1193205b5bddSDouglas Anderson static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1194205b5bddSDouglas Anderson 						      struct console *con)
1195205b5bddSDouglas Anderson {
1196205b5bddSDouglas Anderson 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1197205b5bddSDouglas Anderson 	con->read = qcom_geni_serial_earlycon_read;
1198205b5bddSDouglas Anderson }
1199205b5bddSDouglas Anderson #else
1200205b5bddSDouglas Anderson static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1201205b5bddSDouglas Anderson 						      struct console *con) { }
1202205b5bddSDouglas Anderson #endif
1203205b5bddSDouglas Anderson 
1204e42d6c3eSDouglas Anderson static struct qcom_geni_private_data earlycon_private_data;
1205e42d6c3eSDouglas Anderson 
120643f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
120743f1831bSKarthikeyan Ramasubramanian 								const char *opt)
120843f1831bSKarthikeyan Ramasubramanian {
120943f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
121043f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
121143f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
121243f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
121343f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
121443f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
121543f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
121643f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
121743f1831bSKarthikeyan Ramasubramanian 
121843f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
121943f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
122043f1831bSKarthikeyan Ramasubramanian 
1221e42d6c3eSDouglas Anderson 	uport->private_data = &earlycon_private_data;
1222e42d6c3eSDouglas Anderson 
122343f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
122443f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
122543f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
122643f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
122743f1831bSKarthikeyan Ramasubramanian 	/*
122843f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
122943f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
123043f1831bSKarthikeyan Ramasubramanian 	 */
123143f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
123243f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
123343f1831bSKarthikeyan Ramasubramanian 
123443f1831bSKarthikeyan Ramasubramanian 	/*
123543f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
123643f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
123743f1831bSKarthikeyan Ramasubramanian 	 */
123843f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
123943f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
1240650c8bd3SDouglas Anderson 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1241650c8bd3SDouglas Anderson 			       false, true, true);
124243f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
124343f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
124443f1831bSKarthikeyan Ramasubramanian 
12459e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
12469e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
12479e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
12489e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
12499e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
12509e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
12519e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
125243f1831bSKarthikeyan Ramasubramanian 
125343f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
125443f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
1255205b5bddSDouglas Anderson 	qcom_geni_serial_enable_early_read(&se, dev->con);
1256205b5bddSDouglas Anderson 
125743f1831bSKarthikeyan Ramasubramanian 	return 0;
125843f1831bSKarthikeyan Ramasubramanian }
125943f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
126043f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
126143f1831bSKarthikeyan Ramasubramanian 
1262c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1263c4f52879SKarthikeyan Ramasubramanian {
1264c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1265c4f52879SKarthikeyan Ramasubramanian }
1266c4f52879SKarthikeyan Ramasubramanian 
1267c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1268c4f52879SKarthikeyan Ramasubramanian {
1269c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1270c4f52879SKarthikeyan Ramasubramanian }
1271c4f52879SKarthikeyan Ramasubramanian 
1272c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1273c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1274c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1275c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1276c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1277c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1278c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1279c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1280c4f52879SKarthikeyan Ramasubramanian };
1281c4f52879SKarthikeyan Ramasubramanian 
1282c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1283c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1284c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1285c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1286c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1287c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1288c4f52879SKarthikeyan Ramasubramanian };
1289c4f52879SKarthikeyan Ramasubramanian #else
1290c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1291c4f52879SKarthikeyan Ramasubramanian {
1292c4f52879SKarthikeyan Ramasubramanian 	return 0;
1293c4f52879SKarthikeyan Ramasubramanian }
1294c4f52879SKarthikeyan Ramasubramanian 
1295c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1296c4f52879SKarthikeyan Ramasubramanian {
1297c4f52879SKarthikeyan Ramasubramanian }
1298c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1299c4f52879SKarthikeyan Ramasubramanian 
13008a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
13018a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
13028a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
13038a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
13048a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
13058a8a66a1SGirish Mahadevan };
13068a8a66a1SGirish Mahadevan 
13078a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1308c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1309c4f52879SKarthikeyan Ramasubramanian {
1310c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1311c4f52879SKarthikeyan Ramasubramanian 
1312c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1313c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1314c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1315c362272bSDouglas Anderson 
13167cf563b2SAkash Asthana 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
13177cf563b2SAkash Asthana 		geni_icc_enable(&port->se);
1318c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
13197cf563b2SAkash Asthana 	} else if (new_state == UART_PM_STATE_OFF &&
13207cf563b2SAkash Asthana 			old_state == UART_PM_STATE_ON) {
1321c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
13227cf563b2SAkash Asthana 		geni_icc_disable(&port->se);
13237cf563b2SAkash Asthana 	}
1324c4f52879SKarthikeyan Ramasubramanian }
1325c4f52879SKarthikeyan Ramasubramanian 
1326c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1327c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
1328c4f52879SKarthikeyan Ramasubramanian 	.stop_tx = qcom_geni_serial_stop_tx,
1329c4f52879SKarthikeyan Ramasubramanian 	.start_tx = qcom_geni_serial_start_tx,
1330c4f52879SKarthikeyan Ramasubramanian 	.stop_rx = qcom_geni_serial_stop_rx,
1331654a8d6cSVijaya Krishna Nivarthi 	.start_rx = qcom_geni_serial_start_rx,
1332c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1333c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1334c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1335c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1336c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1337c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
13388a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
13398a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1340c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1341c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1342c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1343c4f52879SKarthikeyan Ramasubramanian #endif
13448a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
13458a8a66a1SGirish Mahadevan };
13468a8a66a1SGirish Mahadevan 
13478a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
13488a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
13498a8a66a1SGirish Mahadevan 	.stop_tx = qcom_geni_serial_stop_tx,
13508a8a66a1SGirish Mahadevan 	.start_tx = qcom_geni_serial_start_tx,
13518a8a66a1SGirish Mahadevan 	.stop_rx = qcom_geni_serial_stop_rx,
13528a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
13538a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
13548a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
13558a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
13568a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
13578a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
13588a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
13598a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
13608a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1361c4f52879SKarthikeyan Ramasubramanian };
1362c4f52879SKarthikeyan Ramasubramanian 
1363c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1364c4f52879SKarthikeyan Ramasubramanian {
1365c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
136671581242SColin Ian King 	int line;
1367c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1368c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1369c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1370066cd1c4SKarthikeyan Ramasubramanian 	int irq;
13718a8a66a1SGirish Mahadevan 	bool console = false;
13728a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
1373c4f52879SKarthikeyan Ramasubramanian 
13748a8a66a1SGirish Mahadevan 	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
13758a8a66a1SGirish Mahadevan 		console = true;
13768a8a66a1SGirish Mahadevan 
13778a8a66a1SGirish Mahadevan 	if (console) {
13788a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1379c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
13808a8a66a1SGirish Mahadevan 	} else {
13818a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
138208b0adb1SDmitry Baryshkov 		line = of_alias_get_id(pdev->dev.of_node, "serial");
138308b0adb1SDmitry Baryshkov 		if (line == -ENODEV) /* compat with non-standard aliases */
13848a8a66a1SGirish Mahadevan 			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
13858a8a66a1SGirish Mahadevan 	}
1386c4f52879SKarthikeyan Ramasubramanian 
13878a8a66a1SGirish Mahadevan 	port = get_port_from_line(line, console);
1388c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
13896a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
13906a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1391c4f52879SKarthikeyan Ramasubramanian 	}
1392c4f52879SKarthikeyan Ramasubramanian 
1393c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1394c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1395c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1396c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1397c4f52879SKarthikeyan Ramasubramanian 
1398c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
1399c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1400c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1401c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1402c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1403c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1404c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1405c4f52879SKarthikeyan Ramasubramanian 		return ret;
1406c4f52879SKarthikeyan Ramasubramanian 	}
1407c4f52879SKarthikeyan Ramasubramanian 
1408c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
14097693b331SWei Yongjun 	if (!res)
14107693b331SWei Yongjun 		return -EINVAL;
1411c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1412c4f52879SKarthikeyan Ramasubramanian 
1413c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1414c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1415c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1416c4f52879SKarthikeyan Ramasubramanian 
1417f9d690b6Ssatya priya 	if (!console) {
1418f9d690b6Ssatya priya 		port->rx_fifo = devm_kcalloc(uport->dev,
1419f9d690b6Ssatya priya 			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
1420f9d690b6Ssatya priya 		if (!port->rx_fifo)
1421f9d690b6Ssatya priya 			return -ENOMEM;
1422f9d690b6Ssatya priya 	}
1423f9d690b6Ssatya priya 
14247cf563b2SAkash Asthana 	ret = geni_icc_get(&port->se, NULL);
14257cf563b2SAkash Asthana 	if (ret)
14267cf563b2SAkash Asthana 		return ret;
14277cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
14287cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
14297cf563b2SAkash Asthana 
14307cf563b2SAkash Asthana 	/* Set BW for register access */
14317cf563b2SAkash Asthana 	ret = geni_icc_set_bw(&port->se);
14327cf563b2SAkash Asthana 	if (ret)
14337cf563b2SAkash Asthana 		return ret;
14347cf563b2SAkash Asthana 
1435f3974413SAkash Asthana 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1436f3974413SAkash Asthana 			"qcom_geni_serial_%s%d",
1437f3974413SAkash Asthana 			uart_console(uport) ? "console" : "uart", uport->line);
1438f3974413SAkash Asthana 	if (!port->name)
1439f3974413SAkash Asthana 		return -ENOMEM;
1440f3974413SAkash Asthana 
1441066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
14421df21786SStephen Boyd 	if (irq < 0)
1443066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1444066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
14458f122698SDmitry Safonov 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1446c4f52879SKarthikeyan Ramasubramanian 
1447f3974413SAkash Asthana 	if (!console)
1448f3974413SAkash Asthana 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
14493e4aaea7SAkash Asthana 
14509fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
14519fa3c4b1SRoja Rani Yarubandi 		port->rx_tx_swap = true;
14529fa3c4b1SRoja Rani Yarubandi 
14539fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
14549fa3c4b1SRoja Rani Yarubandi 		port->cts_rts_swap = true;
14559fa3c4b1SRoja Rani Yarubandi 
1456300894a6SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1457300894a6SYangtao Li 	if (ret)
1458300894a6SYangtao Li 		return ret;
1459a5819b54SRajendra Nayak 	/* OPP table is optional */
1460300894a6SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
1461c7ac46daSViresh Kumar 	if (ret && ret != -ENODEV) {
1462a5819b54SRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1463300894a6SYangtao Li 		return ret;
1464a5819b54SRajendra Nayak 	}
1465a5819b54SRajendra Nayak 
1466e42d6c3eSDouglas Anderson 	port->private_data.drv = drv;
1467e42d6c3eSDouglas Anderson 	uport->private_data = &port->private_data;
1468c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
14698a8a66a1SGirish Mahadevan 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
1470f3974413SAkash Asthana 
1471f3974413SAkash Asthana 	ret = uart_add_one_port(drv, uport);
1472f3974413SAkash Asthana 	if (ret)
1473300894a6SYangtao Li 		return ret;
1474f3974413SAkash Asthana 
1475f3974413SAkash Asthana 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1476f3974413SAkash Asthana 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1477f3974413SAkash Asthana 			IRQF_TRIGGER_HIGH, port->name, uport);
1478f3974413SAkash Asthana 	if (ret) {
1479f3974413SAkash Asthana 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1480f3974413SAkash Asthana 		uart_remove_one_port(drv, uport);
1481300894a6SYangtao Li 		return ret;
1482f3974413SAkash Asthana 	}
1483f3974413SAkash Asthana 
1484f3974413SAkash Asthana 	/*
1485f3974413SAkash Asthana 	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1486f3974413SAkash Asthana 	 * enabled/disabled from dev_pm_arm_wake_irq during system
1487f3974413SAkash Asthana 	 * suspend/resume respectively.
1488f3974413SAkash Asthana 	 */
1489f3974413SAkash Asthana 	pm_runtime_set_active(&pdev->dev);
1490f3974413SAkash Asthana 
1491f3974413SAkash Asthana 	if (port->wakeup_irq > 0) {
1492f3974413SAkash Asthana 		device_init_wakeup(&pdev->dev, true);
1493f3974413SAkash Asthana 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1494f3974413SAkash Asthana 						port->wakeup_irq);
1495f3974413SAkash Asthana 		if (ret) {
1496f3974413SAkash Asthana 			device_init_wakeup(&pdev->dev, false);
1497f3974413SAkash Asthana 			uart_remove_one_port(drv, uport);
1498300894a6SYangtao Li 			return ret;
1499f3974413SAkash Asthana 		}
1500f3974413SAkash Asthana 	}
1501f3974413SAkash Asthana 
1502f3974413SAkash Asthana 	return 0;
1503c4f52879SKarthikeyan Ramasubramanian }
1504c4f52879SKarthikeyan Ramasubramanian 
1505c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1506c4f52879SKarthikeyan Ramasubramanian {
1507c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1508e42d6c3eSDouglas Anderson 	struct uart_driver *drv = port->private_data.drv;
1509c4f52879SKarthikeyan Ramasubramanian 
1510f3974413SAkash Asthana 	dev_pm_clear_wake_irq(&pdev->dev);
1511f3974413SAkash Asthana 	device_init_wakeup(&pdev->dev, false);
1512c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1513f3974413SAkash Asthana 
1514c4f52879SKarthikeyan Ramasubramanian 	return 0;
1515c4f52879SKarthikeyan Ramasubramanian }
1516c4f52879SKarthikeyan Ramasubramanian 
1517b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1518c4f52879SKarthikeyan Ramasubramanian {
1519a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1520c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1521e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1522c4f52879SKarthikeyan Ramasubramanian 
15234a3107f6SRajendra Nayak 	/*
15244a3107f6SRajendra Nayak 	 * This is done so we can hit the lowest possible state in suspend
15254a3107f6SRajendra Nayak 	 * even with no_console_suspend
15264a3107f6SRajendra Nayak 	 */
15274a3107f6SRajendra Nayak 	if (uart_console(uport)) {
15284a3107f6SRajendra Nayak 		geni_icc_set_tag(&port->se, 0x3);
15294a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
15304a3107f6SRajendra Nayak 	}
1531e42d6c3eSDouglas Anderson 	return uart_suspend_port(private_data->drv, uport);
15328a8a66a1SGirish Mahadevan }
15338a8a66a1SGirish Mahadevan 
1534b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
1535c4f52879SKarthikeyan Ramasubramanian {
15364a3107f6SRajendra Nayak 	int ret;
1537a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1538c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1539e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1540c4f52879SKarthikeyan Ramasubramanian 
15414a3107f6SRajendra Nayak 	ret = uart_resume_port(private_data->drv, uport);
15424a3107f6SRajendra Nayak 	if (uart_console(uport)) {
15434a3107f6SRajendra Nayak 		geni_icc_set_tag(&port->se, 0x7);
15444a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
15454a3107f6SRajendra Nayak 	}
15464a3107f6SRajendra Nayak 	return ret;
1547c4f52879SKarthikeyan Ramasubramanian }
1548c4f52879SKarthikeyan Ramasubramanian 
1549c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1550b1f84dd3SMukesh Kumar Savaliya 	SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1551b1f84dd3SMukesh Kumar Savaliya 					qcom_geni_serial_sys_resume)
1552c4f52879SKarthikeyan Ramasubramanian };
1553c4f52879SKarthikeyan Ramasubramanian 
1554c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
1555c4f52879SKarthikeyan Ramasubramanian 	{ .compatible = "qcom,geni-debug-uart", },
15568a8a66a1SGirish Mahadevan 	{ .compatible = "qcom,geni-uart", },
1557c4f52879SKarthikeyan Ramasubramanian 	{}
1558c4f52879SKarthikeyan Ramasubramanian };
1559c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1560c4f52879SKarthikeyan Ramasubramanian 
1561c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1562c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1563c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1564c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1565c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1566c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1567c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1568c4f52879SKarthikeyan Ramasubramanian 	},
1569c4f52879SKarthikeyan Ramasubramanian };
1570c4f52879SKarthikeyan Ramasubramanian 
1571c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1572c4f52879SKarthikeyan Ramasubramanian {
1573c4f52879SKarthikeyan Ramasubramanian 	int ret;
1574c4f52879SKarthikeyan Ramasubramanian 
1575c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1576c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1577c4f52879SKarthikeyan Ramasubramanian 		return ret;
1578c4f52879SKarthikeyan Ramasubramanian 
15798a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
15808a8a66a1SGirish Mahadevan 	if (ret) {
1581c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1582c4f52879SKarthikeyan Ramasubramanian 		return ret;
1583c4f52879SKarthikeyan Ramasubramanian 	}
15848a8a66a1SGirish Mahadevan 
15858a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
15868a8a66a1SGirish Mahadevan 	if (ret) {
15878a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
15888a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
15898a8a66a1SGirish Mahadevan 	}
15908a8a66a1SGirish Mahadevan 	return ret;
15918a8a66a1SGirish Mahadevan }
1592c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1593c4f52879SKarthikeyan Ramasubramanian 
1594c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1595c4f52879SKarthikeyan Ramasubramanian {
1596c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1597c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
15988a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1599c4f52879SKarthikeyan Ramasubramanian }
1600c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1601c4f52879SKarthikeyan Ramasubramanian 
1602c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1603c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1604