xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision d681a6e4e353958cba22faf2c48474ad1efa20c8)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
4c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
5c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
6c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
7c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
83e4aaea7SAkash Asthana #include <linux/irq.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
11c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
12a5819b54SRajendra Nayak #include <linux/pm_opp.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
14f3974413SAkash Asthana #include <linux/pm_runtime.h>
158b7103f3SAkash Asthana #include <linux/pm_wakeirq.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/qcom-geni-se.h>
17c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
18c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
19c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
20c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
21c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
22c4f52879SKarthikeyan Ramasubramanian 
23c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
248a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
259fa3c4b1SRoja Rani Yarubandi #define SE_UART_IO_MACRO_CTRL		0x240
26c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
27c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
28c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
29c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
30c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
31c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
32c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
33c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
34c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
358a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
36c4f52879SKarthikeyan Ramasubramanian 
37c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
38c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN		BIT(0)
39c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK		BIT(1)
40c4f52879SKarthikeyan Ramasubramanian 
41c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_WORD_LEN */
42c4f52879SKarthikeyan Ramasubramanian #define TX_WORD_LEN_MSK		GENMASK(9, 0)
43c4f52879SKarthikeyan Ramasubramanian 
44c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
45c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_MSK	GENMASK(23, 0)
46c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1	0
47c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1_5	1
48c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2	2
49c4f52879SKarthikeyan Ramasubramanian 
50c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_TRANS_LEN */
51c4f52879SKarthikeyan Ramasubramanian #define TX_TRANS_LEN_MSK	GENMASK(23, 0)
52c4f52879SKarthikeyan Ramasubramanian 
53c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
54c4f52879SKarthikeyan Ramasubramanian #define UART_RX_INS_STATUS_BIT	BIT(2)
55c4f52879SKarthikeyan Ramasubramanian #define UART_RX_PAR_EN		BIT(3)
56c4f52879SKarthikeyan Ramasubramanian 
57c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_WORD_LEN */
58c4f52879SKarthikeyan Ramasubramanian #define RX_WORD_LEN_MASK	GENMASK(9, 0)
59c4f52879SKarthikeyan Ramasubramanian 
60c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_STALE_CNT */
61c4f52879SKarthikeyan Ramasubramanian #define RX_STALE_CNT		GENMASK(23, 0)
62c4f52879SKarthikeyan Ramasubramanian 
63c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
64c4f52879SKarthikeyan Ramasubramanian #define PAR_CALC_EN		BIT(0)
65c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_MSK		GENMASK(2, 1)
66c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_SHFT		1
67c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN		0x00
68c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD			0x01
69c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE		0x10
70c4f52879SKarthikeyan Ramasubramanian #define PAR_MARK		0x11
71c4f52879SKarthikeyan Ramasubramanian 
728a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
738a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN	BIT(31)
748a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY	BIT(1)
758a8a66a1SGirish Mahadevan #define UART_RFR_READY		BIT(0)
768a8a66a1SGirish Mahadevan 
77c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
78c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX		0x1
79c4f52879SKarthikeyan Ramasubramanian #define UART_START_BREAK	0x4
80c4f52879SKarthikeyan Ramasubramanian #define UART_STOP_BREAK		0x5
81c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
82c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ		0x1
83c4f52879SKarthikeyan Ramasubramanian #define UART_PARAM		0x1
84c4f52879SKarthikeyan Ramasubramanian 
85c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING	32
86c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT		16
87c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR	10
88c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS	1
898a8a66a1SGirish Mahadevan #define GENI_UART_PORTS		3
90c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS	16
91c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM		2
92c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS	32
93a85fb9ceSRyan Case #define UART_RX_WM		2
9469bd1a4fSAkash Asthana 
9569bd1a4fSAkash Asthana /* SE_UART_LOOPBACK_CFG */
9669bd1a4fSAkash Asthana #define RX_TX_SORTED	BIT(0)
9769bd1a4fSAkash Asthana #define CTS_RTS_SORTED	BIT(1)
9869bd1a4fSAkash Asthana #define RX_TX_CTS_RTS_SORTED	(RX_TX_SORTED | CTS_RTS_SORTED)
99c4f52879SKarthikeyan Ramasubramanian 
1009fa3c4b1SRoja Rani Yarubandi /* UART pin swap value */
1019fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO0_IO1_MASK		GENMASK(3, 0)
1029fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO0_SEL		0x3
1039fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO2_IO3_MASK		GENMASK(15, 4)
1049fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO2_IO3_SWAP		0x4640
1059fa3c4b1SRoja Rani Yarubandi 
106650c8bd3SDouglas Anderson /* We always configure 4 bytes per FIFO word */
107650c8bd3SDouglas Anderson #define BYTES_PER_FIFO_WORD		4
108650c8bd3SDouglas Anderson 
109e42d6c3eSDouglas Anderson struct qcom_geni_private_data {
110e42d6c3eSDouglas Anderson 	/* NOTE: earlycon port will have NULL here */
111e42d6c3eSDouglas Anderson 	struct uart_driver *drv;
112e42d6c3eSDouglas Anderson 
113e42d6c3eSDouglas Anderson 	u32 poll_cached_bytes;
114e42d6c3eSDouglas Anderson 	unsigned int poll_cached_bytes_cnt;
115650c8bd3SDouglas Anderson 
116650c8bd3SDouglas Anderson 	u32 write_cached_bytes;
117650c8bd3SDouglas Anderson 	unsigned int write_cached_bytes_cnt;
118e42d6c3eSDouglas Anderson };
119c4f52879SKarthikeyan Ramasubramanian 
120c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
121c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
122c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
123f3974413SAkash Asthana 	const char *name;
124c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
125c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
126c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
127c4f52879SKarthikeyan Ramasubramanian 	bool setup;
128c4f52879SKarthikeyan Ramasubramanian 	int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
129c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
130f9d690b6Ssatya priya 	void *rx_fifo;
1318a8a66a1SGirish Mahadevan 	u32 loopback;
132c4f52879SKarthikeyan Ramasubramanian 	bool brk;
133a1fee899SRyan Case 
134a1fee899SRyan Case 	unsigned int tx_remaining;
1358b7103f3SAkash Asthana 	int wakeup_irq;
1369fa3c4b1SRoja Rani Yarubandi 	bool rx_tx_swap;
1379fa3c4b1SRoja Rani Yarubandi 	bool cts_rts_swap;
138e42d6c3eSDouglas Anderson 
139e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data private_data;
140c4f52879SKarthikeyan Ramasubramanian };
141c4f52879SKarthikeyan Ramasubramanian 
142f7371750SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops;
1438a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops;
144c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver;
1458a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver;
146c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
1478a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
148c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
149c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport);
150679aac5eSsatya priya static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
151c4f52879SKarthikeyan Ramasubramanian 
152c4f52879SKarthikeyan Ramasubramanian static const unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
153a1b44ea3Ssatya priya 					32000000, 48000000, 51200000, 64000000,
154a1b44ea3Ssatya priya 					80000000, 96000000, 100000000,
155a1b44ea3Ssatya priya 					102400000, 112000000, 120000000,
156a1b44ea3Ssatya priya 					128000000};
157c4f52879SKarthikeyan Ramasubramanian 
158c4f52879SKarthikeyan Ramasubramanian #define to_dev_port(ptr, member) \
159c4f52879SKarthikeyan Ramasubramanian 		container_of(ptr, struct qcom_geni_serial_port, member)
160c4f52879SKarthikeyan Ramasubramanian 
1618a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1628a8a66a1SGirish Mahadevan 	[0] = {
1638a8a66a1SGirish Mahadevan 		.uport = {
1648a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1658a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1668a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1678a8a66a1SGirish Mahadevan 				.line = 0,
1688a8a66a1SGirish Mahadevan 		},
1698a8a66a1SGirish Mahadevan 	},
1708a8a66a1SGirish Mahadevan 	[1] = {
1718a8a66a1SGirish Mahadevan 		.uport = {
1728a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1738a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1748a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1758a8a66a1SGirish Mahadevan 				.line = 1,
1768a8a66a1SGirish Mahadevan 		},
1778a8a66a1SGirish Mahadevan 	},
1788a8a66a1SGirish Mahadevan 	[2] = {
1798a8a66a1SGirish Mahadevan 		.uport = {
1808a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1818a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1828a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1838a8a66a1SGirish Mahadevan 				.line = 2,
1848a8a66a1SGirish Mahadevan 		},
1858a8a66a1SGirish Mahadevan 	},
1868a8a66a1SGirish Mahadevan };
1878a8a66a1SGirish Mahadevan 
188f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
189f7371750SKarthikeyan Ramasubramanian 	.uport = {
190f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
191f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
192f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
193f7371750SKarthikeyan Ramasubramanian 		.line = 0,
194f7371750SKarthikeyan Ramasubramanian 	},
195f7371750SKarthikeyan Ramasubramanian };
196c4f52879SKarthikeyan Ramasubramanian 
197c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
198c4f52879SKarthikeyan Ramasubramanian {
199c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
200c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
201c4f52879SKarthikeyan Ramasubramanian 
20244e60d52SYueHaibing 	uport->membase = devm_platform_ioremap_resource(pdev, 0);
203c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
204c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
205c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
206c4f52879SKarthikeyan Ramasubramanian 	return 0;
207c4f52879SKarthikeyan Ramasubramanian }
208c4f52879SKarthikeyan Ramasubramanian 
209c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
210c4f52879SKarthikeyan Ramasubramanian {
211c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
212c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
213c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
214c4f52879SKarthikeyan Ramasubramanian 	}
215c4f52879SKarthikeyan Ramasubramanian }
216c4f52879SKarthikeyan Ramasubramanian 
2178a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
218c4f52879SKarthikeyan Ramasubramanian {
2198a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2208a8a66a1SGirish Mahadevan 	u32 geni_ios;
2218a8a66a1SGirish Mahadevan 
222e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport)) {
2238a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2248a8a66a1SGirish Mahadevan 	} else {
2259e06d55fSRyan Case 		geni_ios = readl(uport->membase + SE_GENI_IOS);
2268a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2278a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
228c4f52879SKarthikeyan Ramasubramanian 	}
229c4f52879SKarthikeyan Ramasubramanian 
2308a8a66a1SGirish Mahadevan 	return mctrl;
2318a8a66a1SGirish Mahadevan }
2328a8a66a1SGirish Mahadevan 
2338a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
234c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
235c4f52879SKarthikeyan Ramasubramanian {
2368a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
23769bd1a4fSAkash Asthana 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
2388a8a66a1SGirish Mahadevan 
239e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport))
2408a8a66a1SGirish Mahadevan 		return;
2418a8a66a1SGirish Mahadevan 
24269bd1a4fSAkash Asthana 	if (mctrl & TIOCM_LOOP)
24369bd1a4fSAkash Asthana 		port->loopback = RX_TX_CTS_RTS_SORTED;
24469bd1a4fSAkash Asthana 
2458a8a66a1SGirish Mahadevan 	if (!(mctrl & TIOCM_RTS))
2468a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2479e06d55fSRyan Case 	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
248c4f52879SKarthikeyan Ramasubramanian }
249c4f52879SKarthikeyan Ramasubramanian 
250c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
251c4f52879SKarthikeyan Ramasubramanian {
252c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
253c4f52879SKarthikeyan Ramasubramanian }
254c4f52879SKarthikeyan Ramasubramanian 
2558a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
256c4f52879SKarthikeyan Ramasubramanian {
2578a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2588a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2598a8a66a1SGirish Mahadevan 
2608a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
261c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2628a8a66a1SGirish Mahadevan 
2638a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2648a8a66a1SGirish Mahadevan 	return port;
265c4f52879SKarthikeyan Ramasubramanian }
266c4f52879SKarthikeyan Ramasubramanian 
267c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
268c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
269c4f52879SKarthikeyan Ramasubramanian {
270c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
271c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
272c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
273c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
274c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
275e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
276c4f52879SKarthikeyan Ramasubramanian 
277e42d6c3eSDouglas Anderson 	if (private_data->drv) {
278c4f52879SKarthikeyan Ramasubramanian 		port = to_dev_port(uport, uport);
279c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
280c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
281c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
282c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
283c4f52879SKarthikeyan Ramasubramanian 		/*
284c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
285c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
286c4f52879SKarthikeyan Ramasubramanian 		 */
287c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
288c4f52879SKarthikeyan Ramasubramanian 	}
289c4f52879SKarthikeyan Ramasubramanian 
29043f1831bSKarthikeyan Ramasubramanian 	/*
29143f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
29243f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29343f1831bSKarthikeyan Ramasubramanian 	 */
29443f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29543f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
2969e06d55fSRyan Case 		reg = readl(uport->membase + offset);
29743f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29843f1831bSKarthikeyan Ramasubramanian 			return true;
29943f1831bSKarthikeyan Ramasubramanian 		udelay(10);
30043f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
30143f1831bSKarthikeyan Ramasubramanian 	}
30243f1831bSKarthikeyan Ramasubramanian 	return false;
303c4f52879SKarthikeyan Ramasubramanian }
304c4f52879SKarthikeyan Ramasubramanian 
305c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
306c4f52879SKarthikeyan Ramasubramanian {
307c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
308c4f52879SKarthikeyan Ramasubramanian 
3099e06d55fSRyan Case 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
310c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
311c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
312c4f52879SKarthikeyan Ramasubramanian }
313c4f52879SKarthikeyan Ramasubramanian 
314c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
315c4f52879SKarthikeyan Ramasubramanian {
316c4f52879SKarthikeyan Ramasubramanian 	int done;
317c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
318c4f52879SKarthikeyan Ramasubramanian 
319c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
320c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
321c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
3229e06d55fSRyan Case 		writel(M_GENI_CMD_ABORT, uport->membase +
323c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
324c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
325c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
326c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
327c4f52879SKarthikeyan Ramasubramanian 	}
3289e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
329c4f52879SKarthikeyan Ramasubramanian }
330c4f52879SKarthikeyan Ramasubramanian 
331c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
332c4f52879SKarthikeyan Ramasubramanian {
333c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
334c4f52879SKarthikeyan Ramasubramanian 
335c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
336c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
337c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
3389e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
3399e06d55fSRyan Case 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
340c4f52879SKarthikeyan Ramasubramanian }
341c4f52879SKarthikeyan Ramasubramanian 
342c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
343e42d6c3eSDouglas Anderson 
344c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
345c4f52879SKarthikeyan Ramasubramanian {
346e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
347c4f52879SKarthikeyan Ramasubramanian 	u32 status;
348e42d6c3eSDouglas Anderson 	u32 word_cnt;
349e42d6c3eSDouglas Anderson 	int ret;
350c4f52879SKarthikeyan Ramasubramanian 
351e42d6c3eSDouglas Anderson 	if (!private_data->poll_cached_bytes_cnt) {
3529e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
3539e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
354c4f52879SKarthikeyan Ramasubramanian 
3559e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
3569e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
357c4f52879SKarthikeyan Ramasubramanian 
3589e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
359e42d6c3eSDouglas Anderson 		word_cnt = status & RX_FIFO_WC_MSK;
360e42d6c3eSDouglas Anderson 		if (!word_cnt)
361c4f52879SKarthikeyan Ramasubramanian 			return NO_POLL_CHAR;
362c4f52879SKarthikeyan Ramasubramanian 
363e42d6c3eSDouglas Anderson 		if (word_cnt == 1 && (status & RX_LAST))
364*d681a6e4SDouglas Anderson 			/*
365*d681a6e4SDouglas Anderson 			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
366*d681a6e4SDouglas Anderson 			 * treated as if it was BYTES_PER_FIFO_WORD.
367*d681a6e4SDouglas Anderson 			 */
368e42d6c3eSDouglas Anderson 			private_data->poll_cached_bytes_cnt =
369e42d6c3eSDouglas Anderson 				(status & RX_LAST_BYTE_VALID_MSK) >>
370e42d6c3eSDouglas Anderson 				RX_LAST_BYTE_VALID_SHFT;
371*d681a6e4SDouglas Anderson 
372*d681a6e4SDouglas Anderson 		if (private_data->poll_cached_bytes_cnt == 0)
373*d681a6e4SDouglas Anderson 			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
374e42d6c3eSDouglas Anderson 
375e42d6c3eSDouglas Anderson 		private_data->poll_cached_bytes =
376e42d6c3eSDouglas Anderson 			readl(uport->membase + SE_GENI_RX_FIFOn);
377e42d6c3eSDouglas Anderson 	}
378e42d6c3eSDouglas Anderson 
379e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes_cnt--;
380e42d6c3eSDouglas Anderson 	ret = private_data->poll_cached_bytes & 0xff;
381e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes >>= 8;
382e42d6c3eSDouglas Anderson 
383e42d6c3eSDouglas Anderson 	return ret;
384c4f52879SKarthikeyan Ramasubramanian }
385c4f52879SKarthikeyan Ramasubramanian 
386c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
387c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
388c4f52879SKarthikeyan Ramasubramanian {
389a85fb9ceSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
390c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
391c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
392c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
3939e06d55fSRyan Case 	writel(c, uport->membase + SE_GENI_TX_FIFOn);
3949e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
395c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
396c4f52879SKarthikeyan Ramasubramanian }
397c4f52879SKarthikeyan Ramasubramanian #endif
398c4f52879SKarthikeyan Ramasubramanian 
399c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
400c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch)
401c4f52879SKarthikeyan Ramasubramanian {
402650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
403650c8bd3SDouglas Anderson 
404650c8bd3SDouglas Anderson 	private_data->write_cached_bytes =
405650c8bd3SDouglas Anderson 		(private_data->write_cached_bytes >> 8) | (ch << 24);
406650c8bd3SDouglas Anderson 	private_data->write_cached_bytes_cnt++;
407650c8bd3SDouglas Anderson 
408650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
409650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
410650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
411650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
412650c8bd3SDouglas Anderson 	}
413c4f52879SKarthikeyan Ramasubramanian }
414c4f52879SKarthikeyan Ramasubramanian 
415c4f52879SKarthikeyan Ramasubramanian static void
416c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
417c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
418c4f52879SKarthikeyan Ramasubramanian {
419650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
420650c8bd3SDouglas Anderson 
421c4f52879SKarthikeyan Ramasubramanian 	int i;
422c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
423c4f52879SKarthikeyan Ramasubramanian 
424c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
425f0262568SKarthikeyan Ramasubramanian 		/*
426f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
427f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
428f0262568SKarthikeyan Ramasubramanian 		 */
429c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
430c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
431c4f52879SKarthikeyan Ramasubramanian 	}
432c4f52879SKarthikeyan Ramasubramanian 
4339e06d55fSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
434c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
435c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
436c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
437c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
438c4f52879SKarthikeyan Ramasubramanian 
439c4f52879SKarthikeyan Ramasubramanian 		/*
440c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
441c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
442c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
443c4f52879SKarthikeyan Ramasubramanian 		 * lost.
444c4f52879SKarthikeyan Ramasubramanian 		 */
445c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
446c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
447c4f52879SKarthikeyan Ramasubramanian 			break;
4486a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
449c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
450c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
4519e06d55fSRyan Case 		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
452c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
453c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
454c4f52879SKarthikeyan Ramasubramanian 	}
455650c8bd3SDouglas Anderson 
456650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt) {
457650c8bd3SDouglas Anderson 		private_data->write_cached_bytes >>= BITS_PER_BYTE *
458650c8bd3SDouglas Anderson 			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
459650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
460650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
461650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
462650c8bd3SDouglas Anderson 	}
463650c8bd3SDouglas Anderson 
464c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
465c4f52879SKarthikeyan Ramasubramanian }
466c4f52879SKarthikeyan Ramasubramanian 
467c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
468c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
469c4f52879SKarthikeyan Ramasubramanian {
470c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
471c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
472c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
473c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
474a1fee899SRyan Case 	u32 geni_status;
475663abb1aSRyan Case 	u32 irq_en;
476c4f52879SKarthikeyan Ramasubramanian 
477c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
478c4f52879SKarthikeyan Ramasubramanian 
4798a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
480c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
481c4f52879SKarthikeyan Ramasubramanian 		return;
482c4f52879SKarthikeyan Ramasubramanian 
483c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
484c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
485c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
486c4f52879SKarthikeyan Ramasubramanian 	else
487c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
488c4f52879SKarthikeyan Ramasubramanian 
4899e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
490a1fee899SRyan Case 
491c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
492c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
493c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
494c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
495c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
496c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
497c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
498c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
4999e06d55fSRyan Case 			writel(M_CMD_ABORT_EN, uport->membase +
500c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
501c4f52879SKarthikeyan Ramasubramanian 		}
5029e06d55fSRyan Case 		writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
503a1fee899SRyan Case 	} else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
504a1fee899SRyan Case 		/*
505a1fee899SRyan Case 		 * It seems we can't interrupt existing transfers if all data
506a1fee899SRyan Case 		 * has been sent, in which case we need to look for done first.
507a1fee899SRyan Case 		 */
508a1fee899SRyan Case 		qcom_geni_serial_poll_tx_done(uport);
509663abb1aSRyan Case 
510663abb1aSRyan Case 		if (uart_circ_chars_pending(&uport->state->xmit)) {
5119e06d55fSRyan Case 			irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
5129e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
513663abb1aSRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
514663abb1aSRyan Case 		}
515c4f52879SKarthikeyan Ramasubramanian 	}
516c4f52879SKarthikeyan Ramasubramanian 
517c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
518a1fee899SRyan Case 
519a1fee899SRyan Case 	if (port->tx_remaining)
520a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, port->tx_remaining);
521a1fee899SRyan Case 
522c4f52879SKarthikeyan Ramasubramanian 	if (locked)
523c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
524c4f52879SKarthikeyan Ramasubramanian }
525c4f52879SKarthikeyan Ramasubramanian 
526c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
527c4f52879SKarthikeyan Ramasubramanian {
528c4f52879SKarthikeyan Ramasubramanian 	u32 i;
529c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
530c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
531c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
532c4f52879SKarthikeyan Ramasubramanian 
533c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
534c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
535c4f52879SKarthikeyan Ramasubramanian 		int c;
536650c8bd3SDouglas Anderson 		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
537c4f52879SKarthikeyan Ramasubramanian 
538c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
539c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
540c4f52879SKarthikeyan Ramasubramanian 		if (drop)
541c4f52879SKarthikeyan Ramasubramanian 			continue;
542c4f52879SKarthikeyan Ramasubramanian 
543c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
544c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
545c4f52879SKarthikeyan Ramasubramanian 
546c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
547c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
548c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
549c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
550c4f52879SKarthikeyan Ramasubramanian 					continue;
551c4f52879SKarthikeyan Ramasubramanian 			}
552c4f52879SKarthikeyan Ramasubramanian 
553336447b3SDouglas Anderson 			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
554babeca85SDouglas Anderson 
555c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
556c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
557c4f52879SKarthikeyan Ramasubramanian 		}
558c4f52879SKarthikeyan Ramasubramanian 	}
559c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
560c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
561c4f52879SKarthikeyan Ramasubramanian 	return 0;
562c4f52879SKarthikeyan Ramasubramanian }
563c4f52879SKarthikeyan Ramasubramanian #else
564c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
565c4f52879SKarthikeyan Ramasubramanian {
566c4f52879SKarthikeyan Ramasubramanian 	return -EPERM;
567c4f52879SKarthikeyan Ramasubramanian }
568c4f52879SKarthikeyan Ramasubramanian 
569c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
570c4f52879SKarthikeyan Ramasubramanian 
5718a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5728a8a66a1SGirish Mahadevan {
5738a8a66a1SGirish Mahadevan 	struct tty_port *tport;
5748a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
5758a8a66a1SGirish Mahadevan 	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
5768a8a66a1SGirish Mahadevan 	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
5778a8a66a1SGirish Mahadevan 	int ret;
5788a8a66a1SGirish Mahadevan 
5798a8a66a1SGirish Mahadevan 	tport = &uport->state->port;
5808a8a66a1SGirish Mahadevan 	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
5818a8a66a1SGirish Mahadevan 	if (drop)
5828a8a66a1SGirish Mahadevan 		return 0;
5838a8a66a1SGirish Mahadevan 
584f9d690b6Ssatya priya 	ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
5858a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5868a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5878a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5888a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5898a8a66a1SGirish Mahadevan 	}
5908a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5918a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5928a8a66a1SGirish Mahadevan 	return ret;
5938a8a66a1SGirish Mahadevan }
5948a8a66a1SGirish Mahadevan 
595c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_tx(struct uart_port *uport)
596c4f52879SKarthikeyan Ramasubramanian {
597c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
598c4f52879SKarthikeyan Ramasubramanian 	u32 status;
599c4f52879SKarthikeyan Ramasubramanian 
6007fb5b880SKarthikeyan Ramasubramanian 	status = readl(uport->membase + SE_GENI_STATUS);
601c4f52879SKarthikeyan Ramasubramanian 	if (status & M_GENI_CMD_ACTIVE)
602c4f52879SKarthikeyan Ramasubramanian 		return;
603c4f52879SKarthikeyan Ramasubramanian 
604c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_tx_empty(uport))
605c4f52879SKarthikeyan Ramasubramanian 		return;
606c4f52879SKarthikeyan Ramasubramanian 
6079e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
608c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
609c4f52879SKarthikeyan Ramasubramanian 
610bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
6119e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
612c4f52879SKarthikeyan Ramasubramanian }
613c4f52879SKarthikeyan Ramasubramanian 
614c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_tx(struct uart_port *uport)
615c4f52879SKarthikeyan Ramasubramanian {
616c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
617c4f52879SKarthikeyan Ramasubramanian 	u32 status;
618c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
619c4f52879SKarthikeyan Ramasubramanian 
6209e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
621bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
622bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
6239e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
6249e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
625c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
626c4f52879SKarthikeyan Ramasubramanian 	if (!(status & M_GENI_CMD_ACTIVE))
627c4f52879SKarthikeyan Ramasubramanian 		return;
628c4f52879SKarthikeyan Ramasubramanian 
629c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
630c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
631c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
632c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
633c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
634c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
6359e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
636c4f52879SKarthikeyan Ramasubramanian 	}
6379e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
638c4f52879SKarthikeyan Ramasubramanian }
639c4f52879SKarthikeyan Ramasubramanian 
640c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_rx(struct uart_port *uport)
641c4f52879SKarthikeyan Ramasubramanian {
642c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
643c4f52879SKarthikeyan Ramasubramanian 	u32 status;
644c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
645c4f52879SKarthikeyan Ramasubramanian 
6469e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
647c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
648c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_rx(uport);
649c4f52879SKarthikeyan Ramasubramanian 
650c4f52879SKarthikeyan Ramasubramanian 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
651c4f52879SKarthikeyan Ramasubramanian 
6529e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
653c4f52879SKarthikeyan Ramasubramanian 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
6549e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
655c4f52879SKarthikeyan Ramasubramanian 
6569e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
657c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
6589e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
659c4f52879SKarthikeyan Ramasubramanian }
660c4f52879SKarthikeyan Ramasubramanian 
661c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport)
662c4f52879SKarthikeyan Ramasubramanian {
663c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
664c4f52879SKarthikeyan Ramasubramanian 	u32 status;
665c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
666679aac5eSsatya priya 	u32 s_irq_status;
667c4f52879SKarthikeyan Ramasubramanian 
6689e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
669c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
6709e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
671c4f52879SKarthikeyan Ramasubramanian 
6729e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
673c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
6749e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
675c4f52879SKarthikeyan Ramasubramanian 
6769e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
677c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop rx is called multiple times. */
678c4f52879SKarthikeyan Ramasubramanian 	if (!(status & S_GENI_CMD_ACTIVE))
679c4f52879SKarthikeyan Ramasubramanian 		return;
680c4f52879SKarthikeyan Ramasubramanian 
681c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
682679aac5eSsatya priya 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
683679aac5eSsatya priya 					S_CMD_CANCEL_EN, true);
684679aac5eSsatya priya 	/*
685679aac5eSsatya priya 	 * If timeout occurs secondary engine remains active
686679aac5eSsatya priya 	 * and Abort sequence is executed.
687679aac5eSsatya priya 	 */
688679aac5eSsatya priya 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
689679aac5eSsatya priya 	/* Flush the Rx buffer */
690679aac5eSsatya priya 	if (s_irq_status & S_RX_FIFO_LAST_EN)
691679aac5eSsatya priya 		qcom_geni_serial_handle_rx(uport, true);
692679aac5eSsatya priya 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
693679aac5eSsatya priya 
6949e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
695c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
696c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
697c4f52879SKarthikeyan Ramasubramanian }
698c4f52879SKarthikeyan Ramasubramanian 
699c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
700c4f52879SKarthikeyan Ramasubramanian {
701c4f52879SKarthikeyan Ramasubramanian 	u32 status;
702c4f52879SKarthikeyan Ramasubramanian 	u32 word_cnt;
703c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_byte_cnt;
704c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_partial;
705c4f52879SKarthikeyan Ramasubramanian 	u32 total_bytes;
706c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
707c4f52879SKarthikeyan Ramasubramanian 
7089e06d55fSRyan Case 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
709c4f52879SKarthikeyan Ramasubramanian 	word_cnt = status & RX_FIFO_WC_MSK;
710c4f52879SKarthikeyan Ramasubramanian 	last_word_partial = status & RX_LAST;
711c4f52879SKarthikeyan Ramasubramanian 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
712c4f52879SKarthikeyan Ramasubramanian 						RX_LAST_BYTE_VALID_SHFT;
713c4f52879SKarthikeyan Ramasubramanian 
714c4f52879SKarthikeyan Ramasubramanian 	if (!word_cnt)
715c4f52879SKarthikeyan Ramasubramanian 		return;
716650c8bd3SDouglas Anderson 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
717c4f52879SKarthikeyan Ramasubramanian 	if (last_word_partial && last_word_byte_cnt)
718c4f52879SKarthikeyan Ramasubramanian 		total_bytes += last_word_byte_cnt;
719c4f52879SKarthikeyan Ramasubramanian 	else
720650c8bd3SDouglas Anderson 		total_bytes += BYTES_PER_FIFO_WORD;
721c4f52879SKarthikeyan Ramasubramanian 	port->handle_rx(uport, total_bytes, drop);
722c4f52879SKarthikeyan Ramasubramanian }
723c4f52879SKarthikeyan Ramasubramanian 
724a1fee899SRyan Case static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
725a1fee899SRyan Case 		bool active)
726c4f52879SKarthikeyan Ramasubramanian {
727c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
728c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
729c4f52879SKarthikeyan Ramasubramanian 	size_t avail;
730c4f52879SKarthikeyan Ramasubramanian 	size_t remaining;
731a1fee899SRyan Case 	size_t pending;
732c4f52879SKarthikeyan Ramasubramanian 	int i;
733c4f52879SKarthikeyan Ramasubramanian 	u32 status;
73464a42807SRyan Case 	u32 irq_en;
735c4f52879SKarthikeyan Ramasubramanian 	unsigned int chunk;
736c4f52879SKarthikeyan Ramasubramanian 	int tail;
737c4f52879SKarthikeyan Ramasubramanian 
7389e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
739a1fee899SRyan Case 
740a1fee899SRyan Case 	/* Complete the current tx command before taking newly added data */
741a1fee899SRyan Case 	if (active)
742a1fee899SRyan Case 		pending = port->tx_remaining;
743a1fee899SRyan Case 	else
744a1fee899SRyan Case 		pending = uart_circ_chars_pending(xmit);
745a1fee899SRyan Case 
746a1fee899SRyan Case 	/* All data has been transmitted and acknowledged as received */
747a1fee899SRyan Case 	if (!pending && !status && done) {
748c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_tx(uport);
749c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
750c4f52879SKarthikeyan Ramasubramanian 	}
751c4f52879SKarthikeyan Ramasubramanian 
752a1fee899SRyan Case 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
753650c8bd3SDouglas Anderson 	avail *= BYTES_PER_FIFO_WORD;
7548a8a66a1SGirish Mahadevan 
755638a6f4eSEvan Green 	tail = xmit->tail;
7563c66eb4bSMatthias Kaehlcke 	chunk = min(avail, pending);
757c4f52879SKarthikeyan Ramasubramanian 	if (!chunk)
758c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
759c4f52879SKarthikeyan Ramasubramanian 
760a1fee899SRyan Case 	if (!port->tx_remaining) {
761a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, pending);
762a1fee899SRyan Case 		port->tx_remaining = pending;
76364a42807SRyan Case 
7649e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
76564a42807SRyan Case 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
7669e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
76764a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
768a1fee899SRyan Case 	}
769c4f52879SKarthikeyan Ramasubramanian 
770c4f52879SKarthikeyan Ramasubramanian 	remaining = chunk;
771c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < chunk; ) {
772c4f52879SKarthikeyan Ramasubramanian 		unsigned int tx_bytes;
77369736b57SKarthikeyan Ramasubramanian 		u8 buf[sizeof(u32)];
774c4f52879SKarthikeyan Ramasubramanian 		int c;
775c4f52879SKarthikeyan Ramasubramanian 
7763550f897SDan Carpenter 		memset(buf, 0, sizeof(buf));
777650c8bd3SDouglas Anderson 		tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
7783c66eb4bSMatthias Kaehlcke 
7793c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
7803c66eb4bSMatthias Kaehlcke 			buf[c] = xmit->buf[tail++];
7813c66eb4bSMatthias Kaehlcke 			tail &= UART_XMIT_SIZE - 1;
7823c66eb4bSMatthias Kaehlcke 		}
783c4f52879SKarthikeyan Ramasubramanian 
78469736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
785c4f52879SKarthikeyan Ramasubramanian 
786c4f52879SKarthikeyan Ramasubramanian 		i += tx_bytes;
787c4f52879SKarthikeyan Ramasubramanian 		uport->icount.tx += tx_bytes;
788c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
789a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
790c4f52879SKarthikeyan Ramasubramanian 	}
791638a6f4eSEvan Green 
7923c66eb4bSMatthias Kaehlcke 	xmit->tail = tail;
79364a42807SRyan Case 
79464a42807SRyan Case 	/*
79564a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
79664a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
79764a42807SRyan Case 	 * so we must clear it again here after our writes.
79864a42807SRyan Case 	 */
7999e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
80064a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
80164a42807SRyan Case 
802c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
80364a42807SRyan Case 	if (!port->tx_remaining) {
8049e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
80564a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
8069e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
80764a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
80864a42807SRyan Case 	}
80964a42807SRyan Case 
810638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
811c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
812c4f52879SKarthikeyan Ramasubramanian }
813c4f52879SKarthikeyan Ramasubramanian 
814c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
815c4f52879SKarthikeyan Ramasubramanian {
816385298abSRyan Case 	u32 m_irq_en;
817385298abSRyan Case 	u32 m_irq_status;
818385298abSRyan Case 	u32 s_irq_status;
819385298abSRyan Case 	u32 geni_status;
820c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
821c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
822c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
823c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
824c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
825c4f52879SKarthikeyan Ramasubramanian 
826c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
827ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
828c4f52879SKarthikeyan Ramasubramanian 
829c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
8309e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
8319e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
8329e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
8339e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
8349e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
8359e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
836c4f52879SKarthikeyan Ramasubramanian 
837c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
838c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
839c4f52879SKarthikeyan Ramasubramanian 
840c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
841c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
842c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
843c4f52879SKarthikeyan Ramasubramanian 	}
844c4f52879SKarthikeyan Ramasubramanian 
84564a42807SRyan Case 	if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
846a1fee899SRyan Case 		qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
847a1fee899SRyan Case 					geni_status & M_GENI_CMD_ACTIVE);
848c4f52879SKarthikeyan Ramasubramanian 
849c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
850c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
851c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
852c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
853c4f52879SKarthikeyan Ramasubramanian 	} else if (s_irq_status & S_GP_IRQ_2_EN ||
854c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_GP_IRQ_3_EN) {
855c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
856c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
857c4f52879SKarthikeyan Ramasubramanian 	}
858c4f52879SKarthikeyan Ramasubramanian 
859c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
860c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_RX_FIFO_LAST_EN)
861c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_rx(uport, drop_rx);
862c4f52879SKarthikeyan Ramasubramanian 
863c4f52879SKarthikeyan Ramasubramanian out_unlock:
864336447b3SDouglas Anderson 	uart_unlock_and_check_sysrq(uport, flags);
865336447b3SDouglas Anderson 
866c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
867c4f52879SKarthikeyan Ramasubramanian }
868c4f52879SKarthikeyan Ramasubramanian 
8696a10635eSKarthikeyan Ramasubramanian static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
870c4f52879SKarthikeyan Ramasubramanian {
871c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
872c4f52879SKarthikeyan Ramasubramanian 
873c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
874c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
875c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
876c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
877c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
878c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
879c4f52879SKarthikeyan Ramasubramanian }
880c4f52879SKarthikeyan Ramasubramanian 
881c4f52879SKarthikeyan Ramasubramanian 
882c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
883c4f52879SKarthikeyan Ramasubramanian {
8843e4aaea7SAkash Asthana 	disable_irq(uport->irq);
885c4f52879SKarthikeyan Ramasubramanian }
886c4f52879SKarthikeyan Ramasubramanian 
887c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
888c4f52879SKarthikeyan Ramasubramanian {
889c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
890385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
891c362272bSDouglas Anderson 	u32 proto;
8929fa3c4b1SRoja Rani Yarubandi 	u32 pin_swap;
893c362272bSDouglas Anderson 
894c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
895c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
896c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
897c362272bSDouglas Anderson 		return -ENXIO;
898c362272bSDouglas Anderson 	}
899c362272bSDouglas Anderson 
900c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
901c362272bSDouglas Anderson 
902c362272bSDouglas Anderson 	get_tx_fifo_size(port);
903c4f52879SKarthikeyan Ramasubramanian 
9049e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
9059fa3c4b1SRoja Rani Yarubandi 
9069fa3c4b1SRoja Rani Yarubandi 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
9079fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap) {
9089fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
9099fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
9109fa3c4b1SRoja Rani Yarubandi 	}
9119fa3c4b1SRoja Rani Yarubandi 	if (port->cts_rts_swap) {
9129fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
9139fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO0_SEL;
9149fa3c4b1SRoja Rani Yarubandi 	}
9159fa3c4b1SRoja Rani Yarubandi 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
9169fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap || port->cts_rts_swap)
9179fa3c4b1SRoja Rani Yarubandi 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
9189fa3c4b1SRoja Rani Yarubandi 
919c4f52879SKarthikeyan Ramasubramanian 	/*
920c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
921c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
922c4f52879SKarthikeyan Ramasubramanian 	 */
9238a8a66a1SGirish Mahadevan 	if (uart_console(uport))
924c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
925650c8bd3SDouglas Anderson 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
926650c8bd3SDouglas Anderson 			       false, true, true);
927a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
928bdc05a8aSRyan Case 	geni_se_select_mode(&port->se, GENI_SE_FIFO);
929c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
930c362272bSDouglas Anderson 
931c4f52879SKarthikeyan Ramasubramanian 	return 0;
932c4f52879SKarthikeyan Ramasubramanian }
933c4f52879SKarthikeyan Ramasubramanian 
934c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
935c4f52879SKarthikeyan Ramasubramanian {
936c4f52879SKarthikeyan Ramasubramanian 	int ret;
937c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
938c4f52879SKarthikeyan Ramasubramanian 
939c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
940c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
941c4f52879SKarthikeyan Ramasubramanian 		if (ret)
942c4f52879SKarthikeyan Ramasubramanian 			return ret;
943c4f52879SKarthikeyan Ramasubramanian 	}
9443e4aaea7SAkash Asthana 	enable_irq(uport->irq);
945c4f52879SKarthikeyan Ramasubramanian 
9463e4aaea7SAkash Asthana 	return 0;
947c4f52879SKarthikeyan Ramasubramanian }
948c4f52879SKarthikeyan Ramasubramanian 
949c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_cfg(unsigned long clk_freq)
950c4f52879SKarthikeyan Ramasubramanian {
951c4f52879SKarthikeyan Ramasubramanian 	int i;
952c4f52879SKarthikeyan Ramasubramanian 
953c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
954c4f52879SKarthikeyan Ramasubramanian 		if (!(root_freq[i] % clk_freq))
955c4f52879SKarthikeyan Ramasubramanian 			return root_freq[i];
956c4f52879SKarthikeyan Ramasubramanian 	}
957c4f52879SKarthikeyan Ramasubramanian 	return 0;
958c4f52879SKarthikeyan Ramasubramanian }
959c4f52879SKarthikeyan Ramasubramanian 
960ce734600SVivek Gautam static unsigned long get_clk_div_rate(unsigned int baud,
961ce734600SVivek Gautam 			unsigned int sampling_rate, unsigned int *clk_div)
962c4f52879SKarthikeyan Ramasubramanian {
963c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
964c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
965c4f52879SKarthikeyan Ramasubramanian 
966ce734600SVivek Gautam 	desired_clk = baud * sampling_rate;
967c4f52879SKarthikeyan Ramasubramanian 	ser_clk = get_clk_cfg(desired_clk);
968c4f52879SKarthikeyan Ramasubramanian 	if (!ser_clk) {
969c4f52879SKarthikeyan Ramasubramanian 		pr_err("%s: Can't find matching DFS entry for baud %d\n",
970c4f52879SKarthikeyan Ramasubramanian 								__func__, baud);
971c4f52879SKarthikeyan Ramasubramanian 		return ser_clk;
972c4f52879SKarthikeyan Ramasubramanian 	}
973c4f52879SKarthikeyan Ramasubramanian 
974c4f52879SKarthikeyan Ramasubramanian 	*clk_div = ser_clk / desired_clk;
975c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
976c4f52879SKarthikeyan Ramasubramanian }
977c4f52879SKarthikeyan Ramasubramanian 
978c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
979c4f52879SKarthikeyan Ramasubramanian 				struct ktermios *termios, struct ktermios *old)
980c4f52879SKarthikeyan Ramasubramanian {
981c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
982385298abSRyan Case 	u32 bits_per_char;
983385298abSRyan Case 	u32 tx_trans_cfg;
984385298abSRyan Case 	u32 tx_parity_cfg;
985385298abSRyan Case 	u32 rx_trans_cfg;
986385298abSRyan Case 	u32 rx_parity_cfg;
987385298abSRyan Case 	u32 stop_bit_len;
988c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
989385298abSRyan Case 	u32 ser_clk_cfg;
990c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
991c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
992ce734600SVivek Gautam 	u32 ver, sampling_rate;
9937cf563b2SAkash Asthana 	unsigned int avg_bw_core;
994c4f52879SKarthikeyan Ramasubramanian 
995c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
996c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
997c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
998c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
999ce734600SVivek Gautam 
1000ce734600SVivek Gautam 	sampling_rate = UART_OVERSAMPLING;
1001ce734600SVivek Gautam 	/* Sampling rate is halved for IP versions >= 2.5 */
1002ce734600SVivek Gautam 	ver = geni_se_get_qup_hw_version(&port->se);
1003ce734600SVivek Gautam 	if (GENI_SE_VERSION_MAJOR(ver) >= 2 && GENI_SE_VERSION_MINOR(ver) >= 5)
1004ce734600SVivek Gautam 		sampling_rate /= 2;
1005ce734600SVivek Gautam 
1006ce734600SVivek Gautam 	clk_rate = get_clk_div_rate(baud, sampling_rate, &clk_div);
1007c4f52879SKarthikeyan Ramasubramanian 	if (!clk_rate)
1008c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
1009c4f52879SKarthikeyan Ramasubramanian 
1010c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
1011a5819b54SRajendra Nayak 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1012c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
1013c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1014c4f52879SKarthikeyan Ramasubramanian 
10157cf563b2SAkash Asthana 	/*
10167cf563b2SAkash Asthana 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
10177cf563b2SAkash Asthana 	 * only.
10187cf563b2SAkash Asthana 	 */
10197cf563b2SAkash Asthana 	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
10207cf563b2SAkash Asthana 						: GENI_DEFAULT_BW;
10217cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
10227cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
10237cf563b2SAkash Asthana 	geni_icc_set_bw(&port->se);
10247cf563b2SAkash Asthana 
1025c4f52879SKarthikeyan Ramasubramanian 	/* parity */
10269e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
10279e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
10289e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
10299e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1030c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
1031c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
1032c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
1033c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
1034c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
1035c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
1036c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
1037c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
1038c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
1039c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
1040c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
1041c4f52879SKarthikeyan Ramasubramanian 		} else {
1042c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
1043c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
1044c4f52879SKarthikeyan Ramasubramanian 		}
1045c4f52879SKarthikeyan Ramasubramanian 	} else {
1046c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1047c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1048c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1049c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1050c4f52879SKarthikeyan Ramasubramanian 	}
1051c4f52879SKarthikeyan Ramasubramanian 
1052c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
1053c4f52879SKarthikeyan Ramasubramanian 	switch (termios->c_cflag & CSIZE) {
1054c4f52879SKarthikeyan Ramasubramanian 	case CS5:
1055c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 5;
1056c4f52879SKarthikeyan Ramasubramanian 		break;
1057c4f52879SKarthikeyan Ramasubramanian 	case CS6:
1058c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 6;
1059c4f52879SKarthikeyan Ramasubramanian 		break;
1060c4f52879SKarthikeyan Ramasubramanian 	case CS7:
1061c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 7;
1062c4f52879SKarthikeyan Ramasubramanian 		break;
1063c4f52879SKarthikeyan Ramasubramanian 	case CS8:
1064c4f52879SKarthikeyan Ramasubramanian 	default:
1065c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 8;
1066c4f52879SKarthikeyan Ramasubramanian 		break;
1067c4f52879SKarthikeyan Ramasubramanian 	}
1068c4f52879SKarthikeyan Ramasubramanian 
1069c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1070c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1071c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1072c4f52879SKarthikeyan Ramasubramanian 	else
1073c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1074c4f52879SKarthikeyan Ramasubramanian 
1075c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1076c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1077c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1078c4f52879SKarthikeyan Ramasubramanian 	else
1079c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1080c4f52879SKarthikeyan Ramasubramanian 
1081c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1082c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1083c4f52879SKarthikeyan Ramasubramanian 
10848a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
10859e06d55fSRyan Case 		writel(port->loopback,
10868a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
10879e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
10889e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
10899e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
10909e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
10919e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
10929e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
10939e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
10949e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
10959e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1096c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1097c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1098c4f52879SKarthikeyan Ramasubramanian }
1099c4f52879SKarthikeyan Ramasubramanian 
1100c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1101c4f52879SKarthikeyan Ramasubramanian {
11027fb5b880SKarthikeyan Ramasubramanian 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1103c4f52879SKarthikeyan Ramasubramanian }
1104c4f52879SKarthikeyan Ramasubramanian 
1105c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1106c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_console_setup(struct console *co, char *options)
1107c4f52879SKarthikeyan Ramasubramanian {
1108c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1109c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1110c5cbc78aSNathan Chancellor 	int baud = 9600;
1111c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1112c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1113c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1114c362272bSDouglas Anderson 	int ret;
1115c4f52879SKarthikeyan Ramasubramanian 
1116c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1117c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1118c4f52879SKarthikeyan Ramasubramanian 
11198a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1120c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
11216a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1122c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1123c4f52879SKarthikeyan Ramasubramanian 	}
1124c4f52879SKarthikeyan Ramasubramanian 
1125c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1126c4f52879SKarthikeyan Ramasubramanian 
1127c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1128c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1129c4f52879SKarthikeyan Ramasubramanian 
1130c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1131c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1132c362272bSDouglas Anderson 		if (ret)
1133c362272bSDouglas Anderson 			return ret;
1134c4f52879SKarthikeyan Ramasubramanian 	}
1135c4f52879SKarthikeyan Ramasubramanian 
1136c4f52879SKarthikeyan Ramasubramanian 	if (options)
1137c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1138c4f52879SKarthikeyan Ramasubramanian 
1139c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1140c4f52879SKarthikeyan Ramasubramanian }
1141c4f52879SKarthikeyan Ramasubramanian 
114243f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
114343f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
114443f1831bSKarthikeyan Ramasubramanian {
114543f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
114643f1831bSKarthikeyan Ramasubramanian 
114743f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
114843f1831bSKarthikeyan Ramasubramanian }
114943f1831bSKarthikeyan Ramasubramanian 
1150205b5bddSDouglas Anderson #ifdef CONFIG_CONSOLE_POLL
1151205b5bddSDouglas Anderson static int qcom_geni_serial_earlycon_read(struct console *con,
1152205b5bddSDouglas Anderson 					  char *s, unsigned int n)
1153205b5bddSDouglas Anderson {
1154205b5bddSDouglas Anderson 	struct earlycon_device *dev = con->data;
1155205b5bddSDouglas Anderson 	struct uart_port *uport = &dev->port;
1156205b5bddSDouglas Anderson 	int num_read = 0;
1157205b5bddSDouglas Anderson 	int ch;
1158205b5bddSDouglas Anderson 
1159205b5bddSDouglas Anderson 	while (num_read < n) {
1160205b5bddSDouglas Anderson 		ch = qcom_geni_serial_get_char(uport);
1161205b5bddSDouglas Anderson 		if (ch == NO_POLL_CHAR)
1162205b5bddSDouglas Anderson 			break;
1163205b5bddSDouglas Anderson 		s[num_read++] = ch;
1164205b5bddSDouglas Anderson 	}
1165205b5bddSDouglas Anderson 
1166205b5bddSDouglas Anderson 	return num_read;
1167205b5bddSDouglas Anderson }
1168205b5bddSDouglas Anderson 
1169205b5bddSDouglas Anderson static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1170205b5bddSDouglas Anderson 						      struct console *con)
1171205b5bddSDouglas Anderson {
1172205b5bddSDouglas Anderson 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1173205b5bddSDouglas Anderson 	con->read = qcom_geni_serial_earlycon_read;
1174205b5bddSDouglas Anderson }
1175205b5bddSDouglas Anderson #else
1176205b5bddSDouglas Anderson static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1177205b5bddSDouglas Anderson 						      struct console *con) { }
1178205b5bddSDouglas Anderson #endif
1179205b5bddSDouglas Anderson 
1180048eb908SAkash Asthana static int qcom_geni_serial_earlycon_exit(struct console *con)
1181048eb908SAkash Asthana {
1182048eb908SAkash Asthana 	geni_remove_earlycon_icc_vote();
1183048eb908SAkash Asthana 	return 0;
1184048eb908SAkash Asthana }
1185048eb908SAkash Asthana 
1186e42d6c3eSDouglas Anderson static struct qcom_geni_private_data earlycon_private_data;
1187e42d6c3eSDouglas Anderson 
118843f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
118943f1831bSKarthikeyan Ramasubramanian 								const char *opt)
119043f1831bSKarthikeyan Ramasubramanian {
119143f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
119243f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
119343f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
119443f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
119543f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
119643f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
119743f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
119843f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
119943f1831bSKarthikeyan Ramasubramanian 
120043f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
120143f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
120243f1831bSKarthikeyan Ramasubramanian 
1203e42d6c3eSDouglas Anderson 	uport->private_data = &earlycon_private_data;
1204e42d6c3eSDouglas Anderson 
120543f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
120643f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
120743f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
120843f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
120943f1831bSKarthikeyan Ramasubramanian 	/*
121043f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
121143f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
121243f1831bSKarthikeyan Ramasubramanian 	 */
121343f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
121443f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
121543f1831bSKarthikeyan Ramasubramanian 
121643f1831bSKarthikeyan Ramasubramanian 	/*
121743f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
121843f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
121943f1831bSKarthikeyan Ramasubramanian 	 */
122043f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
122143f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
1222650c8bd3SDouglas Anderson 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1223650c8bd3SDouglas Anderson 			       false, true, true);
122443f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
122543f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
122643f1831bSKarthikeyan Ramasubramanian 
12279e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
12289e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
12299e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
12309e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
12319e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
12329e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
12339e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
123443f1831bSKarthikeyan Ramasubramanian 
123543f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
1236048eb908SAkash Asthana 	dev->con->exit = qcom_geni_serial_earlycon_exit;
123743f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
1238205b5bddSDouglas Anderson 	qcom_geni_serial_enable_early_read(&se, dev->con);
1239205b5bddSDouglas Anderson 
124043f1831bSKarthikeyan Ramasubramanian 	return 0;
124143f1831bSKarthikeyan Ramasubramanian }
124243f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
124343f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
124443f1831bSKarthikeyan Ramasubramanian 
1245c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1246c4f52879SKarthikeyan Ramasubramanian {
1247c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1248c4f52879SKarthikeyan Ramasubramanian }
1249c4f52879SKarthikeyan Ramasubramanian 
1250c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1251c4f52879SKarthikeyan Ramasubramanian {
1252c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1253c4f52879SKarthikeyan Ramasubramanian }
1254c4f52879SKarthikeyan Ramasubramanian 
1255c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1256c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1257c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1258c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1259c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1260c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1261c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1262c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1263c4f52879SKarthikeyan Ramasubramanian };
1264c4f52879SKarthikeyan Ramasubramanian 
1265c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1266c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1267c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1268c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1269c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1270c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1271c4f52879SKarthikeyan Ramasubramanian };
1272c4f52879SKarthikeyan Ramasubramanian #else
1273c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1274c4f52879SKarthikeyan Ramasubramanian {
1275c4f52879SKarthikeyan Ramasubramanian 	return 0;
1276c4f52879SKarthikeyan Ramasubramanian }
1277c4f52879SKarthikeyan Ramasubramanian 
1278c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1279c4f52879SKarthikeyan Ramasubramanian {
1280c4f52879SKarthikeyan Ramasubramanian }
1281c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1282c4f52879SKarthikeyan Ramasubramanian 
12838a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
12848a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
12858a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
12868a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
12878a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
12888a8a66a1SGirish Mahadevan };
12898a8a66a1SGirish Mahadevan 
12908a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1291c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1292c4f52879SKarthikeyan Ramasubramanian {
1293c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1294c4f52879SKarthikeyan Ramasubramanian 
1295c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1296c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1297c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1298c362272bSDouglas Anderson 
12997cf563b2SAkash Asthana 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
13007cf563b2SAkash Asthana 		geni_icc_enable(&port->se);
1301c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
13027cf563b2SAkash Asthana 	} else if (new_state == UART_PM_STATE_OFF &&
13037cf563b2SAkash Asthana 			old_state == UART_PM_STATE_ON) {
1304c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
13057cf563b2SAkash Asthana 		geni_icc_disable(&port->se);
13067cf563b2SAkash Asthana 	}
1307c4f52879SKarthikeyan Ramasubramanian }
1308c4f52879SKarthikeyan Ramasubramanian 
1309c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1310c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
1311c4f52879SKarthikeyan Ramasubramanian 	.stop_tx = qcom_geni_serial_stop_tx,
1312c4f52879SKarthikeyan Ramasubramanian 	.start_tx = qcom_geni_serial_start_tx,
1313c4f52879SKarthikeyan Ramasubramanian 	.stop_rx = qcom_geni_serial_stop_rx,
1314c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1315c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1316c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1317c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1318c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1319c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
13208a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
13218a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1322c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1323c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1324c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1325c4f52879SKarthikeyan Ramasubramanian #endif
13268a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
13278a8a66a1SGirish Mahadevan };
13288a8a66a1SGirish Mahadevan 
13298a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
13308a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
13318a8a66a1SGirish Mahadevan 	.stop_tx = qcom_geni_serial_stop_tx,
13328a8a66a1SGirish Mahadevan 	.start_tx = qcom_geni_serial_start_tx,
13338a8a66a1SGirish Mahadevan 	.stop_rx = qcom_geni_serial_stop_rx,
13348a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
13358a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
13368a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
13378a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
13388a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
13398a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
13408a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
13418a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
13428a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1343c4f52879SKarthikeyan Ramasubramanian };
1344c4f52879SKarthikeyan Ramasubramanian 
1345c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1346c4f52879SKarthikeyan Ramasubramanian {
1347c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
1348c4f52879SKarthikeyan Ramasubramanian 	int line = -1;
1349c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1350c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1351c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1352066cd1c4SKarthikeyan Ramasubramanian 	int irq;
13538a8a66a1SGirish Mahadevan 	bool console = false;
13548a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
1355c4f52879SKarthikeyan Ramasubramanian 
13568a8a66a1SGirish Mahadevan 	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
13578a8a66a1SGirish Mahadevan 		console = true;
13588a8a66a1SGirish Mahadevan 
13598a8a66a1SGirish Mahadevan 	if (console) {
13608a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1361c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
13628a8a66a1SGirish Mahadevan 	} else {
13638a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
13648a8a66a1SGirish Mahadevan 		line = of_alias_get_id(pdev->dev.of_node, "hsuart");
13658a8a66a1SGirish Mahadevan 	}
1366c4f52879SKarthikeyan Ramasubramanian 
13678a8a66a1SGirish Mahadevan 	port = get_port_from_line(line, console);
1368c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
13696a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
13706a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1371c4f52879SKarthikeyan Ramasubramanian 	}
1372c4f52879SKarthikeyan Ramasubramanian 
1373c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1374c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1375c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1376c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1377c4f52879SKarthikeyan Ramasubramanian 
1378c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
1379c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1380c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1381c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1382c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1383c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1384c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1385c4f52879SKarthikeyan Ramasubramanian 		return ret;
1386c4f52879SKarthikeyan Ramasubramanian 	}
1387c4f52879SKarthikeyan Ramasubramanian 
1388c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13897693b331SWei Yongjun 	if (!res)
13907693b331SWei Yongjun 		return -EINVAL;
1391c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1392c4f52879SKarthikeyan Ramasubramanian 
1393c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1394c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1395c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1396c4f52879SKarthikeyan Ramasubramanian 
1397f9d690b6Ssatya priya 	if (!console) {
1398f9d690b6Ssatya priya 		port->rx_fifo = devm_kcalloc(uport->dev,
1399f9d690b6Ssatya priya 			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
1400f9d690b6Ssatya priya 		if (!port->rx_fifo)
1401f9d690b6Ssatya priya 			return -ENOMEM;
1402f9d690b6Ssatya priya 	}
1403f9d690b6Ssatya priya 
14047cf563b2SAkash Asthana 	ret = geni_icc_get(&port->se, NULL);
14057cf563b2SAkash Asthana 	if (ret)
14067cf563b2SAkash Asthana 		return ret;
14077cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
14087cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
14097cf563b2SAkash Asthana 
14107cf563b2SAkash Asthana 	/* Set BW for register access */
14117cf563b2SAkash Asthana 	ret = geni_icc_set_bw(&port->se);
14127cf563b2SAkash Asthana 	if (ret)
14137cf563b2SAkash Asthana 		return ret;
14147cf563b2SAkash Asthana 
1415f3974413SAkash Asthana 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1416f3974413SAkash Asthana 			"qcom_geni_serial_%s%d",
1417f3974413SAkash Asthana 			uart_console(uport) ? "console" : "uart", uport->line);
1418f3974413SAkash Asthana 	if (!port->name)
1419f3974413SAkash Asthana 		return -ENOMEM;
1420f3974413SAkash Asthana 
1421066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
14221df21786SStephen Boyd 	if (irq < 0)
1423066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1424066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
14258f122698SDmitry Safonov 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1426c4f52879SKarthikeyan Ramasubramanian 
1427f3974413SAkash Asthana 	if (!console)
1428f3974413SAkash Asthana 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
14293e4aaea7SAkash Asthana 
14309fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
14319fa3c4b1SRoja Rani Yarubandi 		port->rx_tx_swap = true;
14329fa3c4b1SRoja Rani Yarubandi 
14339fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
14349fa3c4b1SRoja Rani Yarubandi 		port->cts_rts_swap = true;
14359fa3c4b1SRoja Rani Yarubandi 
1436a5819b54SRajendra Nayak 	port->se.opp_table = dev_pm_opp_set_clkname(&pdev->dev, "se");
1437a5819b54SRajendra Nayak 	if (IS_ERR(port->se.opp_table))
1438a5819b54SRajendra Nayak 		return PTR_ERR(port->se.opp_table);
1439a5819b54SRajendra Nayak 	/* OPP table is optional */
1440a5819b54SRajendra Nayak 	ret = dev_pm_opp_of_add_table(&pdev->dev);
1441a5819b54SRajendra Nayak 	if (!ret) {
1442a5819b54SRajendra Nayak 		port->se.has_opp_table = true;
1443a5819b54SRajendra Nayak 	} else if (ret != -ENODEV) {
1444a5819b54SRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1445a5819b54SRajendra Nayak 		return ret;
1446a5819b54SRajendra Nayak 	}
1447a5819b54SRajendra Nayak 
1448e42d6c3eSDouglas Anderson 	port->private_data.drv = drv;
1449e42d6c3eSDouglas Anderson 	uport->private_data = &port->private_data;
1450c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
14518a8a66a1SGirish Mahadevan 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
1452f3974413SAkash Asthana 
1453f3974413SAkash Asthana 	ret = uart_add_one_port(drv, uport);
1454f3974413SAkash Asthana 	if (ret)
1455a5819b54SRajendra Nayak 		goto err;
1456f3974413SAkash Asthana 
1457f3974413SAkash Asthana 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1458f3974413SAkash Asthana 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1459f3974413SAkash Asthana 			IRQF_TRIGGER_HIGH, port->name, uport);
1460f3974413SAkash Asthana 	if (ret) {
1461f3974413SAkash Asthana 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1462f3974413SAkash Asthana 		uart_remove_one_port(drv, uport);
1463a5819b54SRajendra Nayak 		goto err;
1464f3974413SAkash Asthana 	}
1465f3974413SAkash Asthana 
1466f3974413SAkash Asthana 	/*
1467f3974413SAkash Asthana 	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1468f3974413SAkash Asthana 	 * enabled/disabled from dev_pm_arm_wake_irq during system
1469f3974413SAkash Asthana 	 * suspend/resume respectively.
1470f3974413SAkash Asthana 	 */
1471f3974413SAkash Asthana 	pm_runtime_set_active(&pdev->dev);
1472f3974413SAkash Asthana 
1473f3974413SAkash Asthana 	if (port->wakeup_irq > 0) {
1474f3974413SAkash Asthana 		device_init_wakeup(&pdev->dev, true);
1475f3974413SAkash Asthana 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1476f3974413SAkash Asthana 						port->wakeup_irq);
1477f3974413SAkash Asthana 		if (ret) {
1478f3974413SAkash Asthana 			device_init_wakeup(&pdev->dev, false);
1479f3974413SAkash Asthana 			uart_remove_one_port(drv, uport);
1480a5819b54SRajendra Nayak 			goto err;
1481f3974413SAkash Asthana 		}
1482f3974413SAkash Asthana 	}
1483f3974413SAkash Asthana 
1484f3974413SAkash Asthana 	return 0;
1485a5819b54SRajendra Nayak err:
1486a5819b54SRajendra Nayak 	if (port->se.has_opp_table)
1487a5819b54SRajendra Nayak 		dev_pm_opp_of_remove_table(&pdev->dev);
1488a5819b54SRajendra Nayak 	dev_pm_opp_put_clkname(port->se.opp_table);
1489a5819b54SRajendra Nayak 	return ret;
1490c4f52879SKarthikeyan Ramasubramanian }
1491c4f52879SKarthikeyan Ramasubramanian 
1492c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1493c4f52879SKarthikeyan Ramasubramanian {
1494c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1495e42d6c3eSDouglas Anderson 	struct uart_driver *drv = port->private_data.drv;
1496c4f52879SKarthikeyan Ramasubramanian 
1497a5819b54SRajendra Nayak 	if (port->se.has_opp_table)
1498a5819b54SRajendra Nayak 		dev_pm_opp_of_remove_table(&pdev->dev);
1499a5819b54SRajendra Nayak 	dev_pm_opp_put_clkname(port->se.opp_table);
1500f3974413SAkash Asthana 	dev_pm_clear_wake_irq(&pdev->dev);
1501f3974413SAkash Asthana 	device_init_wakeup(&pdev->dev, false);
1502c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1503f3974413SAkash Asthana 
1504c4f52879SKarthikeyan Ramasubramanian 	return 0;
1505c4f52879SKarthikeyan Ramasubramanian }
1506c4f52879SKarthikeyan Ramasubramanian 
1507b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1508c4f52879SKarthikeyan Ramasubramanian {
1509a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1510c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1511e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1512c4f52879SKarthikeyan Ramasubramanian 
15134a3107f6SRajendra Nayak 	/*
15144a3107f6SRajendra Nayak 	 * This is done so we can hit the lowest possible state in suspend
15154a3107f6SRajendra Nayak 	 * even with no_console_suspend
15164a3107f6SRajendra Nayak 	 */
15174a3107f6SRajendra Nayak 	if (uart_console(uport)) {
15184a3107f6SRajendra Nayak 		geni_icc_set_tag(&port->se, 0x3);
15194a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
15204a3107f6SRajendra Nayak 	}
1521e42d6c3eSDouglas Anderson 	return uart_suspend_port(private_data->drv, uport);
15228a8a66a1SGirish Mahadevan }
15238a8a66a1SGirish Mahadevan 
1524b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
1525c4f52879SKarthikeyan Ramasubramanian {
15264a3107f6SRajendra Nayak 	int ret;
1527a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1528c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1529e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1530c4f52879SKarthikeyan Ramasubramanian 
15314a3107f6SRajendra Nayak 	ret = uart_resume_port(private_data->drv, uport);
15324a3107f6SRajendra Nayak 	if (uart_console(uport)) {
15334a3107f6SRajendra Nayak 		geni_icc_set_tag(&port->se, 0x7);
15344a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
15354a3107f6SRajendra Nayak 	}
15364a3107f6SRajendra Nayak 	return ret;
1537c4f52879SKarthikeyan Ramasubramanian }
1538c4f52879SKarthikeyan Ramasubramanian 
1539c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1540b1f84dd3SMukesh Kumar Savaliya 	SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1541b1f84dd3SMukesh Kumar Savaliya 					qcom_geni_serial_sys_resume)
1542c4f52879SKarthikeyan Ramasubramanian };
1543c4f52879SKarthikeyan Ramasubramanian 
1544c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
1545c4f52879SKarthikeyan Ramasubramanian 	{ .compatible = "qcom,geni-debug-uart", },
15468a8a66a1SGirish Mahadevan 	{ .compatible = "qcom,geni-uart", },
1547c4f52879SKarthikeyan Ramasubramanian 	{}
1548c4f52879SKarthikeyan Ramasubramanian };
1549c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1550c4f52879SKarthikeyan Ramasubramanian 
1551c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1552c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1553c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1554c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1555c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1556c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1557c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1558c4f52879SKarthikeyan Ramasubramanian 	},
1559c4f52879SKarthikeyan Ramasubramanian };
1560c4f52879SKarthikeyan Ramasubramanian 
1561c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1562c4f52879SKarthikeyan Ramasubramanian {
1563c4f52879SKarthikeyan Ramasubramanian 	int ret;
1564c4f52879SKarthikeyan Ramasubramanian 
1565c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1566c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1567c4f52879SKarthikeyan Ramasubramanian 		return ret;
1568c4f52879SKarthikeyan Ramasubramanian 
15698a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
15708a8a66a1SGirish Mahadevan 	if (ret) {
1571c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1572c4f52879SKarthikeyan Ramasubramanian 		return ret;
1573c4f52879SKarthikeyan Ramasubramanian 	}
15748a8a66a1SGirish Mahadevan 
15758a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
15768a8a66a1SGirish Mahadevan 	if (ret) {
15778a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
15788a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
15798a8a66a1SGirish Mahadevan 	}
15808a8a66a1SGirish Mahadevan 	return ret;
15818a8a66a1SGirish Mahadevan }
1582c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1583c4f52879SKarthikeyan Ramasubramanian 
1584c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1585c4f52879SKarthikeyan Ramasubramanian {
1586c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1587c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
15888a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1589c4f52879SKarthikeyan Ramasubramanian }
1590c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1591c4f52879SKarthikeyan Ramasubramanian 
1592c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1593c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1594