xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision 97820780b723197d1b472f2bd39fd8593b5d4edc)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
460457d5eSSai Prakash Ranjan /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
560457d5eSSai Prakash Ranjan #define __DISABLE_TRACE_MMIO__
660457d5eSSai Prakash Ranjan 
7c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
8c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
113e4aaea7SAkash Asthana #include <linux/irq.h>
12c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
14c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
15a5819b54SRajendra Nayak #include <linux/pm_opp.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
17f3974413SAkash Asthana #include <linux/pm_runtime.h>
188b7103f3SAkash Asthana #include <linux/pm_wakeirq.h>
19491581f4SElliot Berman #include <linux/soc/qcom/geni-se.h>
20c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
21c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
22c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
23c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
24c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
25408e532eSVijaya Krishna Nivarthi #include <dt-bindings/interconnect/qcom,icc.h>
26c4f52879SKarthikeyan Ramasubramanian 
27c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
288a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
299fa3c4b1SRoja Rani Yarubandi #define SE_UART_IO_MACRO_CTRL		0x240
30c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
31c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
32c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
33c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
34c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
35c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
36c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
37c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
38c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
398a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
40c4f52879SKarthikeyan Ramasubramanian 
41c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
42c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN			BIT(0)
43c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK			BIT(1)
44c4f52879SKarthikeyan Ramasubramanian 
45c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
46c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1		0
47c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2		2
48c4f52879SKarthikeyan Ramasubramanian 
49c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
50c4f52879SKarthikeyan Ramasubramanian #define UART_RX_PAR_EN			BIT(3)
51c4f52879SKarthikeyan Ramasubramanian 
52c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_WORD_LEN */
53c4f52879SKarthikeyan Ramasubramanian #define RX_WORD_LEN_MASK		GENMASK(9, 0)
54c4f52879SKarthikeyan Ramasubramanian 
55c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_STALE_CNT */
56c4f52879SKarthikeyan Ramasubramanian #define RX_STALE_CNT			GENMASK(23, 0)
57c4f52879SKarthikeyan Ramasubramanian 
58c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
59c4f52879SKarthikeyan Ramasubramanian #define PAR_CALC_EN			BIT(0)
60c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN			0x00
61c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD				0x01
62c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE			0x10
63c4f52879SKarthikeyan Ramasubramanian 
648a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
658a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN		BIT(31)
668a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY		BIT(1)
678a8a66a1SGirish Mahadevan #define UART_RFR_READY			BIT(0)
688a8a66a1SGirish Mahadevan 
69c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
70c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX			0x1
71c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
72c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ			0x1
732aaa43c7SBartosz Golaszewski #define UART_PARAM			0x1
742aaa43c7SBartosz Golaszewski #define UART_PARAM_RFR_OPEN		BIT(7)
75c4f52879SKarthikeyan Ramasubramanian 
76c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING		32
77c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT			16
78c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR		10
79c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS		1
808a8a66a1SGirish Mahadevan #define GENI_UART_PORTS			3
81c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS		16
82c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM			2
83c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS		32
84a85fb9ceSRyan Case #define UART_RX_WM			2
8569bd1a4fSAkash Asthana 
8669bd1a4fSAkash Asthana /* SE_UART_LOOPBACK_CFG */
8769bd1a4fSAkash Asthana #define RX_TX_SORTED			BIT(0)
8869bd1a4fSAkash Asthana #define CTS_RTS_SORTED			BIT(1)
8969bd1a4fSAkash Asthana #define RX_TX_CTS_RTS_SORTED		(RX_TX_SORTED | CTS_RTS_SORTED)
90c4f52879SKarthikeyan Ramasubramanian 
919fa3c4b1SRoja Rani Yarubandi /* UART pin swap value */
929fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO0_IO1_MASK	GENMASK(3, 0)
939fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO0_SEL		0x3
949fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO2_IO3_MASK	GENMASK(15, 4)
959fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO2_IO3_SWAP		0x4640
969fa3c4b1SRoja Rani Yarubandi 
97650c8bd3SDouglas Anderson /* We always configure 4 bytes per FIFO word */
98bd795584SBartosz Golaszewski #define BYTES_PER_FIFO_WORD		4U
99650c8bd3SDouglas Anderson 
1002aaa43c7SBartosz Golaszewski #define DMA_RX_BUF_SIZE		2048
1012aaa43c7SBartosz Golaszewski 
10240ec6d41SBartosz Golaszewski struct qcom_geni_device_data {
10340ec6d41SBartosz Golaszewski 	bool console;
1042aaa43c7SBartosz Golaszewski 	enum geni_se_xfer_mode mode;
10540ec6d41SBartosz Golaszewski };
10640ec6d41SBartosz Golaszewski 
107e42d6c3eSDouglas Anderson struct qcom_geni_private_data {
108e42d6c3eSDouglas Anderson 	/* NOTE: earlycon port will have NULL here */
109e42d6c3eSDouglas Anderson 	struct uart_driver *drv;
110e42d6c3eSDouglas Anderson 
111e42d6c3eSDouglas Anderson 	u32 poll_cached_bytes;
112e42d6c3eSDouglas Anderson 	unsigned int poll_cached_bytes_cnt;
113650c8bd3SDouglas Anderson 
114650c8bd3SDouglas Anderson 	u32 write_cached_bytes;
115650c8bd3SDouglas Anderson 	unsigned int write_cached_bytes_cnt;
116e42d6c3eSDouglas Anderson };
117c4f52879SKarthikeyan Ramasubramanian 
118c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
119c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
120c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
121f3974413SAkash Asthana 	const char *name;
122c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
123c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
124c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
1252aaa43c7SBartosz Golaszewski 	dma_addr_t tx_dma_addr;
1262aaa43c7SBartosz Golaszewski 	dma_addr_t rx_dma_addr;
127c4f52879SKarthikeyan Ramasubramanian 	bool setup;
128c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
1292aaa43c7SBartosz Golaszewski 	void *rx_buf;
1308a8a66a1SGirish Mahadevan 	u32 loopback;
131c4f52879SKarthikeyan Ramasubramanian 	bool brk;
132a1fee899SRyan Case 
133a1fee899SRyan Case 	unsigned int tx_remaining;
1348b7103f3SAkash Asthana 	int wakeup_irq;
1359fa3c4b1SRoja Rani Yarubandi 	bool rx_tx_swap;
1369fa3c4b1SRoja Rani Yarubandi 	bool cts_rts_swap;
137e42d6c3eSDouglas Anderson 
138e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data private_data;
13940ec6d41SBartosz Golaszewski 	const struct qcom_geni_device_data *dev_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 
14700ce7c6eSBartosz Golaszewski static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
14800ce7c6eSBartosz Golaszewski {
14900ce7c6eSBartosz Golaszewski 	return container_of(uport, struct qcom_geni_serial_port, uport);
15000ce7c6eSBartosz Golaszewski }
151c4f52879SKarthikeyan Ramasubramanian 
1528a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1538a8a66a1SGirish Mahadevan 	[0] = {
1548a8a66a1SGirish Mahadevan 		.uport = {
1558a8a66a1SGirish Mahadevan 			.iotype = UPIO_MEM,
1568a8a66a1SGirish Mahadevan 			.ops = &qcom_geni_uart_pops,
1578a8a66a1SGirish Mahadevan 			.flags = UPF_BOOT_AUTOCONF,
1588a8a66a1SGirish Mahadevan 			.line = 0,
1598a8a66a1SGirish Mahadevan 		},
1608a8a66a1SGirish Mahadevan 	},
1618a8a66a1SGirish Mahadevan 	[1] = {
1628a8a66a1SGirish Mahadevan 		.uport = {
1638a8a66a1SGirish Mahadevan 			.iotype = UPIO_MEM,
1648a8a66a1SGirish Mahadevan 			.ops = &qcom_geni_uart_pops,
1658a8a66a1SGirish Mahadevan 			.flags = UPF_BOOT_AUTOCONF,
1668a8a66a1SGirish Mahadevan 			.line = 1,
1678a8a66a1SGirish Mahadevan 		},
1688a8a66a1SGirish Mahadevan 	},
1698a8a66a1SGirish Mahadevan 	[2] = {
1708a8a66a1SGirish Mahadevan 		.uport = {
1718a8a66a1SGirish Mahadevan 			.iotype = UPIO_MEM,
1728a8a66a1SGirish Mahadevan 			.ops = &qcom_geni_uart_pops,
1738a8a66a1SGirish Mahadevan 			.flags = UPF_BOOT_AUTOCONF,
1748a8a66a1SGirish Mahadevan 			.line = 2,
1758a8a66a1SGirish Mahadevan 		},
1768a8a66a1SGirish Mahadevan 	},
1778a8a66a1SGirish Mahadevan };
1788a8a66a1SGirish Mahadevan 
179f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
180f7371750SKarthikeyan Ramasubramanian 	.uport = {
181f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
182f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
183f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
184f7371750SKarthikeyan Ramasubramanian 		.line = 0,
185f7371750SKarthikeyan Ramasubramanian 	},
186f7371750SKarthikeyan Ramasubramanian };
187c4f52879SKarthikeyan Ramasubramanian 
188c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
189c4f52879SKarthikeyan Ramasubramanian {
190c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
19100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
192c4f52879SKarthikeyan Ramasubramanian 
19344e60d52SYueHaibing 	uport->membase = devm_platform_ioremap_resource(pdev, 0);
194c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
195c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
196c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
197c4f52879SKarthikeyan Ramasubramanian 	return 0;
198c4f52879SKarthikeyan Ramasubramanian }
199c4f52879SKarthikeyan Ramasubramanian 
200c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
201c4f52879SKarthikeyan Ramasubramanian {
202c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
203c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
204c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
205c4f52879SKarthikeyan Ramasubramanian 	}
206c4f52879SKarthikeyan Ramasubramanian }
207c4f52879SKarthikeyan Ramasubramanian 
2088a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
209c4f52879SKarthikeyan Ramasubramanian {
2108a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2118a8a66a1SGirish Mahadevan 	u32 geni_ios;
2128a8a66a1SGirish Mahadevan 
213e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport)) {
2148a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2158a8a66a1SGirish Mahadevan 	} else {
2169e06d55fSRyan Case 		geni_ios = readl(uport->membase + SE_GENI_IOS);
2178a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2188a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
219c4f52879SKarthikeyan Ramasubramanian 	}
220c4f52879SKarthikeyan Ramasubramanian 
2218a8a66a1SGirish Mahadevan 	return mctrl;
2228a8a66a1SGirish Mahadevan }
2238a8a66a1SGirish Mahadevan 
2248a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
225c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
226c4f52879SKarthikeyan Ramasubramanian {
2278a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
22800ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
2298a8a66a1SGirish Mahadevan 
230e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport))
2318a8a66a1SGirish Mahadevan 		return;
2328a8a66a1SGirish Mahadevan 
23369bd1a4fSAkash Asthana 	if (mctrl & TIOCM_LOOP)
23469bd1a4fSAkash Asthana 		port->loopback = RX_TX_CTS_RTS_SORTED;
23569bd1a4fSAkash Asthana 
236a4ced376Ssatya priya 	if (!(mctrl & TIOCM_RTS) && !uport->suspended)
2378a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2389e06d55fSRyan Case 	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
239c4f52879SKarthikeyan Ramasubramanian }
240c4f52879SKarthikeyan Ramasubramanian 
241c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
242c4f52879SKarthikeyan Ramasubramanian {
243c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
244c4f52879SKarthikeyan Ramasubramanian }
245c4f52879SKarthikeyan Ramasubramanian 
2468a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
247c4f52879SKarthikeyan Ramasubramanian {
2488a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2498a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2508a8a66a1SGirish Mahadevan 
2518a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
252c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2538a8a66a1SGirish Mahadevan 
2548a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2558a8a66a1SGirish Mahadevan 	return port;
256c4f52879SKarthikeyan Ramasubramanian }
257c4f52879SKarthikeyan Ramasubramanian 
2582aaa43c7SBartosz Golaszewski static bool qcom_geni_serial_main_active(struct uart_port *uport)
2592aaa43c7SBartosz Golaszewski {
2602aaa43c7SBartosz Golaszewski 	return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
2612aaa43c7SBartosz Golaszewski }
2622aaa43c7SBartosz Golaszewski 
2632aaa43c7SBartosz Golaszewski static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
2642aaa43c7SBartosz Golaszewski {
2652aaa43c7SBartosz Golaszewski 	return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
2662aaa43c7SBartosz Golaszewski }
2672aaa43c7SBartosz Golaszewski 
268c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
269c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
270c4f52879SKarthikeyan Ramasubramanian {
271c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
272c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
273c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
274c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
275c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
276e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
277c4f52879SKarthikeyan Ramasubramanian 
278e42d6c3eSDouglas Anderson 	if (private_data->drv) {
27900ce7c6eSBartosz Golaszewski 		port = to_dev_port(uport);
280c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
281c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
282c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
283c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
284c4f52879SKarthikeyan Ramasubramanian 		/*
285c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
286c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
287c4f52879SKarthikeyan Ramasubramanian 		 */
288c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
289c4f52879SKarthikeyan Ramasubramanian 	}
290c4f52879SKarthikeyan Ramasubramanian 
29143f1831bSKarthikeyan Ramasubramanian 	/*
29243f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
29343f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29443f1831bSKarthikeyan Ramasubramanian 	 */
29543f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29643f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
2979e06d55fSRyan Case 		reg = readl(uport->membase + offset);
29843f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29943f1831bSKarthikeyan Ramasubramanian 			return true;
30043f1831bSKarthikeyan Ramasubramanian 		udelay(10);
30143f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
30243f1831bSKarthikeyan Ramasubramanian 	}
30343f1831bSKarthikeyan Ramasubramanian 	return false;
304c4f52879SKarthikeyan Ramasubramanian }
305c4f52879SKarthikeyan Ramasubramanian 
306c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
307c4f52879SKarthikeyan Ramasubramanian {
308c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
309c4f52879SKarthikeyan Ramasubramanian 
3109e06d55fSRyan Case 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
311c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
312c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
313c4f52879SKarthikeyan Ramasubramanian }
314c4f52879SKarthikeyan Ramasubramanian 
315c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
316c4f52879SKarthikeyan Ramasubramanian {
317c4f52879SKarthikeyan Ramasubramanian 	int done;
318c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
319c4f52879SKarthikeyan Ramasubramanian 
320c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
321c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
322c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
3239e06d55fSRyan Case 		writel(M_GENI_CMD_ABORT, uport->membase +
324c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
325c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
326c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
327c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
328c4f52879SKarthikeyan Ramasubramanian 	}
3299e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
330c4f52879SKarthikeyan Ramasubramanian }
331c4f52879SKarthikeyan Ramasubramanian 
332c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
333c4f52879SKarthikeyan Ramasubramanian {
334c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
335c4f52879SKarthikeyan Ramasubramanian 
336c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
337c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
338c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
3399e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
3409e06d55fSRyan Case 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
341c4f52879SKarthikeyan Ramasubramanian }
342c4f52879SKarthikeyan Ramasubramanian 
343c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
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))
364d681a6e4SDouglas Anderson 			/*
365d681a6e4SDouglas Anderson 			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
366d681a6e4SDouglas Anderson 			 * treated as if it was BYTES_PER_FIFO_WORD.
367d681a6e4SDouglas Anderson 			 */
368e42d6c3eSDouglas Anderson 			private_data->poll_cached_bytes_cnt =
369e42d6c3eSDouglas Anderson 				(status & RX_LAST_BYTE_VALID_MSK) >>
370e42d6c3eSDouglas Anderson 				RX_LAST_BYTE_VALID_SHFT;
371d681a6e4SDouglas Anderson 
372d681a6e4SDouglas Anderson 		if (private_data->poll_cached_bytes_cnt == 0)
373d681a6e4SDouglas 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
4003f8bab17SJiri Slaby static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char 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 
510d2b574c0SJiri Slaby 		if (!uart_circ_empty(&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 
5260626afe5SBartosz Golaszewski static void 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;
53100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(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 }
562c4f52879SKarthikeyan Ramasubramanian #else
5630626afe5SBartosz Golaszewski static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
564c4f52879SKarthikeyan Ramasubramanian {
5650626afe5SBartosz Golaszewski 
566c4f52879SKarthikeyan Ramasubramanian }
567c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
568c4f52879SKarthikeyan Ramasubramanian 
5690626afe5SBartosz Golaszewski static void handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5708a8a66a1SGirish Mahadevan {
57100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
5722aaa43c7SBartosz Golaszewski 	struct tty_port *tport = &uport->state->port;
5738a8a66a1SGirish Mahadevan 	int ret;
5748a8a66a1SGirish Mahadevan 
5752aaa43c7SBartosz Golaszewski 	ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
5768a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5778a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5788a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5798a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5808a8a66a1SGirish Mahadevan 	}
5818a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5828a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5838a8a66a1SGirish Mahadevan }
5848a8a66a1SGirish Mahadevan 
585d0fabb0dSBartosz Golaszewski static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
586d0fabb0dSBartosz Golaszewski {
587d0fabb0dSBartosz Golaszewski 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
588d0fabb0dSBartosz Golaszewski }
589d0fabb0dSBartosz Golaszewski 
5902aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
591c4f52879SKarthikeyan Ramasubramanian {
5922aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
5932aaa43c7SBartosz Golaszewski 	bool done;
5942aaa43c7SBartosz Golaszewski 	u32 m_irq_en;
595c4f52879SKarthikeyan Ramasubramanian 
5962aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_main_active(uport))
597c4f52879SKarthikeyan Ramasubramanian 		return;
598c4f52879SKarthikeyan Ramasubramanian 
59995fcfc08SJohan Hovold 	if (port->tx_dma_addr) {
6002aaa43c7SBartosz Golaszewski 		geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
6012aaa43c7SBartosz Golaszewski 				      port->tx_remaining);
6022aaa43c7SBartosz Golaszewski 		port->tx_dma_addr = 0;
6032aaa43c7SBartosz Golaszewski 		port->tx_remaining = 0;
6042aaa43c7SBartosz Golaszewski 	}
6052aaa43c7SBartosz Golaszewski 
6062aaa43c7SBartosz Golaszewski 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
6072aaa43c7SBartosz Golaszewski 	writel(m_irq_en, uport->membase + SE_GENI_M_IRQ_EN);
6082aaa43c7SBartosz Golaszewski 	geni_se_cancel_m_cmd(&port->se);
6092aaa43c7SBartosz Golaszewski 
6102aaa43c7SBartosz Golaszewski 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
6112aaa43c7SBartosz Golaszewski 					 S_CMD_CANCEL_EN, true);
6122aaa43c7SBartosz Golaszewski 	if (!done) {
6132aaa43c7SBartosz Golaszewski 		geni_se_abort_m_cmd(&port->se);
6142aaa43c7SBartosz Golaszewski 		done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
6152aaa43c7SBartosz Golaszewski 						 M_CMD_ABORT_EN, true);
6162aaa43c7SBartosz Golaszewski 		if (!done)
6172aaa43c7SBartosz Golaszewski 			dev_err_ratelimited(uport->dev, "M_CMD_ABORT_EN not set");
6182aaa43c7SBartosz Golaszewski 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
6192aaa43c7SBartosz Golaszewski 	}
6202aaa43c7SBartosz Golaszewski 
6212aaa43c7SBartosz Golaszewski 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
6222aaa43c7SBartosz Golaszewski }
6232aaa43c7SBartosz Golaszewski 
6242aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
6252aaa43c7SBartosz Golaszewski {
6262aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
6272aaa43c7SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
6282aaa43c7SBartosz Golaszewski 	unsigned int xmit_size;
6292aaa43c7SBartosz Golaszewski 	int ret;
6302aaa43c7SBartosz Golaszewski 
6312aaa43c7SBartosz Golaszewski 	if (port->tx_dma_addr)
6322aaa43c7SBartosz Golaszewski 		return;
6332aaa43c7SBartosz Golaszewski 
634*97820780SJohan Hovold 	if (uart_circ_empty(xmit))
635*97820780SJohan Hovold 		return;
636*97820780SJohan Hovold 
6372aaa43c7SBartosz Golaszewski 	xmit_size = uart_circ_chars_pending(xmit);
6382aaa43c7SBartosz Golaszewski 	if (xmit_size < WAKEUP_CHARS)
6392aaa43c7SBartosz Golaszewski 		uart_write_wakeup(uport);
6402aaa43c7SBartosz Golaszewski 
6412aaa43c7SBartosz Golaszewski 	xmit_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
6422aaa43c7SBartosz Golaszewski 
6432aaa43c7SBartosz Golaszewski 	qcom_geni_serial_setup_tx(uport, xmit_size);
6442aaa43c7SBartosz Golaszewski 
6452aaa43c7SBartosz Golaszewski 	ret = geni_se_tx_dma_prep(&port->se, &xmit->buf[xmit->tail],
6462aaa43c7SBartosz Golaszewski 				  xmit_size, &port->tx_dma_addr);
6472aaa43c7SBartosz Golaszewski 	if (ret) {
6482aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
6492aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_tx_dma(uport);
6502aaa43c7SBartosz Golaszewski 		return;
6512aaa43c7SBartosz Golaszewski 	}
6522aaa43c7SBartosz Golaszewski 
6532aaa43c7SBartosz Golaszewski 	port->tx_remaining = xmit_size;
6542aaa43c7SBartosz Golaszewski }
6552aaa43c7SBartosz Golaszewski 
6562aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
6572aaa43c7SBartosz Golaszewski {
6582aaa43c7SBartosz Golaszewski 	u32 irq_en;
6592aaa43c7SBartosz Golaszewski 
6602aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_main_active(uport) ||
6612aaa43c7SBartosz Golaszewski 	    !qcom_geni_serial_tx_empty(uport))
662c4f52879SKarthikeyan Ramasubramanian 		return;
663c4f52879SKarthikeyan Ramasubramanian 
6649e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
665c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
666c4f52879SKarthikeyan Ramasubramanian 
667bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
6689e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
669c4f52879SKarthikeyan Ramasubramanian }
670c4f52879SKarthikeyan Ramasubramanian 
6712aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
672c4f52879SKarthikeyan Ramasubramanian {
673c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
67400ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
675c4f52879SKarthikeyan Ramasubramanian 
6769e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
677bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
678bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
6799e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
680c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
6812aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_main_active(uport))
682c4f52879SKarthikeyan Ramasubramanian 		return;
683c4f52879SKarthikeyan Ramasubramanian 
684c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
685c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
686c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
687c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
688c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
689c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
6909e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
691c4f52879SKarthikeyan Ramasubramanian 	}
6929e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
693c4f52879SKarthikeyan Ramasubramanian }
694c4f52879SKarthikeyan Ramasubramanian 
6952aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
696c4f52879SKarthikeyan Ramasubramanian {
697c4f52879SKarthikeyan Ramasubramanian 	u32 status;
698d0fabb0dSBartosz Golaszewski 	u32 word_cnt;
699d0fabb0dSBartosz Golaszewski 	u32 last_word_byte_cnt;
700d0fabb0dSBartosz Golaszewski 	u32 last_word_partial;
701d0fabb0dSBartosz Golaszewski 	u32 total_bytes;
702c4f52879SKarthikeyan Ramasubramanian 
703d0fabb0dSBartosz Golaszewski 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
704d0fabb0dSBartosz Golaszewski 	word_cnt = status & RX_FIFO_WC_MSK;
705d0fabb0dSBartosz Golaszewski 	last_word_partial = status & RX_LAST;
706d0fabb0dSBartosz Golaszewski 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
707d0fabb0dSBartosz Golaszewski 						RX_LAST_BYTE_VALID_SHFT;
708c4f52879SKarthikeyan Ramasubramanian 
709d0fabb0dSBartosz Golaszewski 	if (!word_cnt)
710d0fabb0dSBartosz Golaszewski 		return;
711d0fabb0dSBartosz Golaszewski 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
712d0fabb0dSBartosz Golaszewski 	if (last_word_partial && last_word_byte_cnt)
713d0fabb0dSBartosz Golaszewski 		total_bytes += last_word_byte_cnt;
714d0fabb0dSBartosz Golaszewski 	else
715d0fabb0dSBartosz Golaszewski 		total_bytes += BYTES_PER_FIFO_WORD;
7162aaa43c7SBartosz Golaszewski 	handle_rx_console(uport, total_bytes, drop);
717c4f52879SKarthikeyan Ramasubramanian }
718c4f52879SKarthikeyan Ramasubramanian 
7192aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
720c4f52879SKarthikeyan Ramasubramanian {
721c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
72200ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
723679aac5eSsatya priya 	u32 s_irq_status;
724c4f52879SKarthikeyan Ramasubramanian 
7259e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
726c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
7279e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
728c4f52879SKarthikeyan Ramasubramanian 
7299e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
730c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
7319e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
732c4f52879SKarthikeyan Ramasubramanian 
7332aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
734c4f52879SKarthikeyan Ramasubramanian 		return;
735c4f52879SKarthikeyan Ramasubramanian 
736c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
737679aac5eSsatya priya 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
738679aac5eSsatya priya 					S_CMD_CANCEL_EN, true);
739679aac5eSsatya priya 	/*
740679aac5eSsatya priya 	 * If timeout occurs secondary engine remains active
741679aac5eSsatya priya 	 * and Abort sequence is executed.
742679aac5eSsatya priya 	 */
743679aac5eSsatya priya 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
744679aac5eSsatya priya 	/* Flush the Rx buffer */
745679aac5eSsatya priya 	if (s_irq_status & S_RX_FIFO_LAST_EN)
7462aaa43c7SBartosz Golaszewski 		qcom_geni_serial_handle_rx_fifo(uport, true);
747679aac5eSsatya priya 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
748679aac5eSsatya priya 
7492aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
750c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
751c4f52879SKarthikeyan Ramasubramanian }
752c4f52879SKarthikeyan Ramasubramanian 
7532aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
754c4f52879SKarthikeyan Ramasubramanian {
755d0fabb0dSBartosz Golaszewski 	u32 irq_en;
75600ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
757c4f52879SKarthikeyan Ramasubramanian 
7582aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7592aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_fifo(uport);
760c4f52879SKarthikeyan Ramasubramanian 
761d0fabb0dSBartosz Golaszewski 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
762d0fabb0dSBartosz Golaszewski 
763d0fabb0dSBartosz Golaszewski 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
764d0fabb0dSBartosz Golaszewski 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
765d0fabb0dSBartosz Golaszewski 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
766d0fabb0dSBartosz Golaszewski 
767d0fabb0dSBartosz Golaszewski 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
768d0fabb0dSBartosz Golaszewski 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
769d0fabb0dSBartosz Golaszewski 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
770c4f52879SKarthikeyan Ramasubramanian }
771c4f52879SKarthikeyan Ramasubramanian 
7722aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
7732aaa43c7SBartosz Golaszewski {
7742aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
7752aaa43c7SBartosz Golaszewski 
7762aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
7772aaa43c7SBartosz Golaszewski 		return;
7782aaa43c7SBartosz Golaszewski 
7792aaa43c7SBartosz Golaszewski 	geni_se_cancel_s_cmd(&port->se);
7802aaa43c7SBartosz Golaszewski 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
7812aaa43c7SBartosz Golaszewski 				  S_CMD_CANCEL_EN, true);
7822aaa43c7SBartosz Golaszewski 
7832aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7842aaa43c7SBartosz Golaszewski 		qcom_geni_serial_abort_rx(uport);
7852aaa43c7SBartosz Golaszewski 
7862aaa43c7SBartosz Golaszewski 	if (port->rx_dma_addr) {
7872aaa43c7SBartosz Golaszewski 		geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
7882aaa43c7SBartosz Golaszewski 				      DMA_RX_BUF_SIZE);
7892aaa43c7SBartosz Golaszewski 		port->rx_dma_addr = 0;
7902aaa43c7SBartosz Golaszewski 	}
7912aaa43c7SBartosz Golaszewski }
7922aaa43c7SBartosz Golaszewski 
7932aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
7942aaa43c7SBartosz Golaszewski {
7952aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
7962aaa43c7SBartosz Golaszewski 	int ret;
7972aaa43c7SBartosz Golaszewski 
7982aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7992aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8002aaa43c7SBartosz Golaszewski 
8012aaa43c7SBartosz Golaszewski 	geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
8022aaa43c7SBartosz Golaszewski 
8032aaa43c7SBartosz Golaszewski 	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
8042aaa43c7SBartosz Golaszewski 				  DMA_RX_BUF_SIZE,
8052aaa43c7SBartosz Golaszewski 				  &port->rx_dma_addr);
8062aaa43c7SBartosz Golaszewski 	if (ret) {
8072aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
8082aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8092aaa43c7SBartosz Golaszewski 	}
8102aaa43c7SBartosz Golaszewski }
8112aaa43c7SBartosz Golaszewski 
8122aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
8132aaa43c7SBartosz Golaszewski {
8142aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
8152aaa43c7SBartosz Golaszewski 	u32 rx_in;
8162aaa43c7SBartosz Golaszewski 	int ret;
8172aaa43c7SBartosz Golaszewski 
8182aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
8192aaa43c7SBartosz Golaszewski 		return;
8202aaa43c7SBartosz Golaszewski 
8212aaa43c7SBartosz Golaszewski 	if (!port->rx_dma_addr)
8222aaa43c7SBartosz Golaszewski 		return;
8232aaa43c7SBartosz Golaszewski 
8242aaa43c7SBartosz Golaszewski 	geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
8252aaa43c7SBartosz Golaszewski 	port->rx_dma_addr = 0;
8262aaa43c7SBartosz Golaszewski 
8272aaa43c7SBartosz Golaszewski 	rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
8282aaa43c7SBartosz Golaszewski 	if (!rx_in) {
8292aaa43c7SBartosz Golaszewski 		dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
8302aaa43c7SBartosz Golaszewski 		return;
8312aaa43c7SBartosz Golaszewski 	}
8322aaa43c7SBartosz Golaszewski 
8332aaa43c7SBartosz Golaszewski 	if (!drop)
8342aaa43c7SBartosz Golaszewski 		handle_rx_uart(uport, rx_in, drop);
8352aaa43c7SBartosz Golaszewski 
8362aaa43c7SBartosz Golaszewski 	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
8372aaa43c7SBartosz Golaszewski 				  DMA_RX_BUF_SIZE,
8382aaa43c7SBartosz Golaszewski 				  &port->rx_dma_addr);
8392aaa43c7SBartosz Golaszewski 	if (ret) {
8402aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
8412aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8422aaa43c7SBartosz Golaszewski 	}
8432aaa43c7SBartosz Golaszewski }
8442aaa43c7SBartosz Golaszewski 
8452aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx(struct uart_port *uport)
8462aaa43c7SBartosz Golaszewski {
8472aaa43c7SBartosz Golaszewski 	uport->ops->start_rx(uport);
8482aaa43c7SBartosz Golaszewski }
8492aaa43c7SBartosz Golaszewski 
8502aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx(struct uart_port *uport)
8512aaa43c7SBartosz Golaszewski {
8522aaa43c7SBartosz Golaszewski 	uport->ops->stop_rx(uport);
8532aaa43c7SBartosz Golaszewski }
8542aaa43c7SBartosz Golaszewski 
8552aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx(struct uart_port *uport)
8562aaa43c7SBartosz Golaszewski {
8572aaa43c7SBartosz Golaszewski 	uport->ops->stop_tx(uport);
8582aaa43c7SBartosz Golaszewski }
8592aaa43c7SBartosz Golaszewski 
860bd795584SBartosz Golaszewski static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
861d420fb49SBartosz Golaszewski 					     unsigned int chunk)
862c4f52879SKarthikeyan Ramasubramanian {
86300ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
864c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
865bd795584SBartosz Golaszewski 	unsigned int tx_bytes, c, remaining = chunk;
866bd795584SBartosz Golaszewski 	u8 buf[BYTES_PER_FIFO_WORD];
867c4f52879SKarthikeyan Ramasubramanian 
868bd795584SBartosz Golaszewski 	while (remaining) {
8693550f897SDan Carpenter 		memset(buf, 0, sizeof(buf));
870bd795584SBartosz Golaszewski 		tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
8713c66eb4bSMatthias Kaehlcke 
8723c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
873bd795584SBartosz Golaszewski 			buf[c] = xmit->buf[xmit->tail];
874bd795584SBartosz Golaszewski 			uart_xmit_advance(uport, 1);
8753c66eb4bSMatthias Kaehlcke 		}
876c4f52879SKarthikeyan Ramasubramanian 
87769736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
878c4f52879SKarthikeyan Ramasubramanian 
879c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
880a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
881c4f52879SKarthikeyan Ramasubramanian 	}
882d420fb49SBartosz Golaszewski }
883d420fb49SBartosz Golaszewski 
8842aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
8852aaa43c7SBartosz Golaszewski 					    bool done, bool active)
886d420fb49SBartosz Golaszewski {
887d420fb49SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
888d420fb49SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
889d420fb49SBartosz Golaszewski 	size_t avail;
890d420fb49SBartosz Golaszewski 	size_t pending;
891d420fb49SBartosz Golaszewski 	u32 status;
892d420fb49SBartosz Golaszewski 	u32 irq_en;
893d420fb49SBartosz Golaszewski 	unsigned int chunk;
894d420fb49SBartosz Golaszewski 
895d420fb49SBartosz Golaszewski 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
896d420fb49SBartosz Golaszewski 
897d420fb49SBartosz Golaszewski 	/* Complete the current tx command before taking newly added data */
898d420fb49SBartosz Golaszewski 	if (active)
899d420fb49SBartosz Golaszewski 		pending = port->tx_remaining;
900d420fb49SBartosz Golaszewski 	else
901d420fb49SBartosz Golaszewski 		pending = uart_circ_chars_pending(xmit);
902d420fb49SBartosz Golaszewski 
903d420fb49SBartosz Golaszewski 	/* All data has been transmitted and acknowledged as received */
904d420fb49SBartosz Golaszewski 	if (!pending && !status && done) {
9052aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_tx_fifo(uport);
906d420fb49SBartosz Golaszewski 		goto out_write_wakeup;
907d420fb49SBartosz Golaszewski 	}
908d420fb49SBartosz Golaszewski 
909d420fb49SBartosz Golaszewski 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
910d420fb49SBartosz Golaszewski 	avail *= BYTES_PER_FIFO_WORD;
911d420fb49SBartosz Golaszewski 
912d420fb49SBartosz Golaszewski 	chunk = min(avail, pending);
913d420fb49SBartosz Golaszewski 	if (!chunk)
914d420fb49SBartosz Golaszewski 		goto out_write_wakeup;
915d420fb49SBartosz Golaszewski 
916d420fb49SBartosz Golaszewski 	if (!port->tx_remaining) {
917d420fb49SBartosz Golaszewski 		qcom_geni_serial_setup_tx(uport, pending);
918d420fb49SBartosz Golaszewski 		port->tx_remaining = pending;
919d420fb49SBartosz Golaszewski 
920d420fb49SBartosz Golaszewski 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
921d420fb49SBartosz Golaszewski 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
922d420fb49SBartosz Golaszewski 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
923d420fb49SBartosz Golaszewski 					uport->membase + SE_GENI_M_IRQ_EN);
924d420fb49SBartosz Golaszewski 	}
925d420fb49SBartosz Golaszewski 
926bd795584SBartosz Golaszewski 	qcom_geni_serial_send_chunk_fifo(uport, chunk);
92764a42807SRyan Case 
92864a42807SRyan Case 	/*
92964a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
93064a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
93164a42807SRyan Case 	 * so we must clear it again here after our writes.
93264a42807SRyan Case 	 */
9339e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
93464a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
93564a42807SRyan Case 
936c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
93764a42807SRyan Case 	if (!port->tx_remaining) {
9389e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
93964a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
9409e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
94164a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
94264a42807SRyan Case 	}
94364a42807SRyan Case 
944638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
945c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
946c4f52879SKarthikeyan Ramasubramanian }
947c4f52879SKarthikeyan Ramasubramanian 
9482aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
9492aaa43c7SBartosz Golaszewski {
9502aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
9512aaa43c7SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
9522aaa43c7SBartosz Golaszewski 
9532aaa43c7SBartosz Golaszewski 	uart_xmit_advance(uport, port->tx_remaining);
9542aaa43c7SBartosz Golaszewski 	geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
9552aaa43c7SBartosz Golaszewski 	port->tx_dma_addr = 0;
9562aaa43c7SBartosz Golaszewski 	port->tx_remaining = 0;
9572aaa43c7SBartosz Golaszewski 
9582aaa43c7SBartosz Golaszewski 	if (!uart_circ_empty(xmit))
9592aaa43c7SBartosz Golaszewski 		qcom_geni_serial_start_tx_dma(uport);
9602aaa43c7SBartosz Golaszewski 
9612aaa43c7SBartosz Golaszewski 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
9622aaa43c7SBartosz Golaszewski 		uart_write_wakeup(uport);
9632aaa43c7SBartosz Golaszewski }
9642aaa43c7SBartosz Golaszewski 
965c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
966c4f52879SKarthikeyan Ramasubramanian {
967385298abSRyan Case 	u32 m_irq_en;
968385298abSRyan Case 	u32 m_irq_status;
969385298abSRyan Case 	u32 s_irq_status;
970385298abSRyan Case 	u32 geni_status;
9712aaa43c7SBartosz Golaszewski 	u32 dma;
9722aaa43c7SBartosz Golaszewski 	u32 dma_tx_status;
9732aaa43c7SBartosz Golaszewski 	u32 dma_rx_status;
974c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
975c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
976c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
97700ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
978c4f52879SKarthikeyan Ramasubramanian 
979c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
980ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
981c4f52879SKarthikeyan Ramasubramanian 
98275f4e830SJohan Hovold 	spin_lock(&uport->lock);
98375f4e830SJohan Hovold 
9849e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
9859e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
9862aaa43c7SBartosz Golaszewski 	dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
9872aaa43c7SBartosz Golaszewski 	dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
9889e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
9892aaa43c7SBartosz Golaszewski 	dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
9909e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
9919e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
9929e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
9932aaa43c7SBartosz Golaszewski 	writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
9942aaa43c7SBartosz Golaszewski 	writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
995c4f52879SKarthikeyan Ramasubramanian 
996c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
997c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
998c4f52879SKarthikeyan Ramasubramanian 
999c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
1000c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
1001c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1002c4f52879SKarthikeyan Ramasubramanian 	}
1003c4f52879SKarthikeyan Ramasubramanian 
1004fe6a00e8SBartosz Golaszewski 	if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
1005c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
1006c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
1007c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
1008fe6a00e8SBartosz Golaszewski 	} else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
1009c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
1010c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
1011c4f52879SKarthikeyan Ramasubramanian 	}
1012c4f52879SKarthikeyan Ramasubramanian 
10132aaa43c7SBartosz Golaszewski 	if (dma) {
10142aaa43c7SBartosz Golaszewski 		if (dma_tx_status & TX_DMA_DONE)
10152aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_tx_dma(uport);
10162aaa43c7SBartosz Golaszewski 
10172aaa43c7SBartosz Golaszewski 		if (dma_rx_status) {
10182aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_RESET_DONE)
10192aaa43c7SBartosz Golaszewski 				goto out_unlock;
10202aaa43c7SBartosz Golaszewski 
10212aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_DMA_PARITY_ERR) {
10222aaa43c7SBartosz Golaszewski 				uport->icount.parity++;
10232aaa43c7SBartosz Golaszewski 				drop_rx = true;
10242aaa43c7SBartosz Golaszewski 			}
10252aaa43c7SBartosz Golaszewski 
10262aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_DMA_BREAK)
10272aaa43c7SBartosz Golaszewski 				uport->icount.brk++;
10282aaa43c7SBartosz Golaszewski 
10292aaa43c7SBartosz Golaszewski 			if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
10302aaa43c7SBartosz Golaszewski 				qcom_geni_serial_handle_rx_dma(uport, drop_rx);
10312aaa43c7SBartosz Golaszewski 		}
10322aaa43c7SBartosz Golaszewski 	} else {
10332aaa43c7SBartosz Golaszewski 		if (m_irq_status & m_irq_en &
10342aaa43c7SBartosz Golaszewski 		    (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
10352aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_tx_fifo(uport,
10362aaa43c7SBartosz Golaszewski 					m_irq_status & M_CMD_DONE_EN,
10372aaa43c7SBartosz Golaszewski 					geni_status & M_GENI_CMD_ACTIVE);
10382aaa43c7SBartosz Golaszewski 
1039fe6a00e8SBartosz Golaszewski 		if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
10402aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
10412aaa43c7SBartosz Golaszewski 	}
1042c4f52879SKarthikeyan Ramasubramanian 
1043c4f52879SKarthikeyan Ramasubramanian out_unlock:
104475f4e830SJohan Hovold 	uart_unlock_and_check_sysrq(uport);
1045336447b3SDouglas Anderson 
1046c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
1047c4f52879SKarthikeyan Ramasubramanian }
1048c4f52879SKarthikeyan Ramasubramanian 
1049b8caf69aSKrzysztof Kozlowski static int setup_fifos(struct qcom_geni_serial_port *port)
1050c4f52879SKarthikeyan Ramasubramanian {
1051c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1052b8caf69aSKrzysztof Kozlowski 	u32 old_rx_fifo_depth = port->rx_fifo_depth;
1053c4f52879SKarthikeyan Ramasubramanian 
1054c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1055c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1056c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1057c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1058c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
1059c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1060b8caf69aSKrzysztof Kozlowski 
1061a3cf6b94SIlpo Järvinen 	if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
1062a3cf6b94SIlpo Järvinen 		port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
1063b8caf69aSKrzysztof Kozlowski 					     port->rx_fifo_depth * sizeof(u32),
1064b8caf69aSKrzysztof Kozlowski 					     GFP_KERNEL);
1065a3cf6b94SIlpo Järvinen 		if (!port->rx_buf)
1066b8caf69aSKrzysztof Kozlowski 			return -ENOMEM;
1067b8caf69aSKrzysztof Kozlowski 	}
1068b8caf69aSKrzysztof Kozlowski 
1069b8caf69aSKrzysztof Kozlowski 	return 0;
1070c4f52879SKarthikeyan Ramasubramanian }
1071c4f52879SKarthikeyan Ramasubramanian 
1072c4f52879SKarthikeyan Ramasubramanian 
1073c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
1074c4f52879SKarthikeyan Ramasubramanian {
10753e4aaea7SAkash Asthana 	disable_irq(uport->irq);
10769aff74ccSJohan Hovold 
10779aff74ccSJohan Hovold 	if (uart_console(uport))
10789aff74ccSJohan Hovold 		return;
10799aff74ccSJohan Hovold 
1080d8aca2f9SBartosz Golaszewski 	qcom_geni_serial_stop_tx(uport);
1081d8aca2f9SBartosz Golaszewski 	qcom_geni_serial_stop_rx(uport);
1082c4f52879SKarthikeyan Ramasubramanian }
1083c4f52879SKarthikeyan Ramasubramanian 
1084c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
1085c4f52879SKarthikeyan Ramasubramanian {
108600ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1087385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1088c362272bSDouglas Anderson 	u32 proto;
10899fa3c4b1SRoja Rani Yarubandi 	u32 pin_swap;
1090b8caf69aSKrzysztof Kozlowski 	int ret;
1091c362272bSDouglas Anderson 
1092c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
1093c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
1094c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1095c362272bSDouglas Anderson 		return -ENXIO;
1096c362272bSDouglas Anderson 	}
1097c362272bSDouglas Anderson 
1098c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
1099c362272bSDouglas Anderson 
1100b8caf69aSKrzysztof Kozlowski 	ret = setup_fifos(port);
1101b8caf69aSKrzysztof Kozlowski 	if (ret)
1102b8caf69aSKrzysztof Kozlowski 		return ret;
1103c4f52879SKarthikeyan Ramasubramanian 
11049e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
11059fa3c4b1SRoja Rani Yarubandi 
11069fa3c4b1SRoja Rani Yarubandi 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
11079fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap) {
11089fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
11099fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
11109fa3c4b1SRoja Rani Yarubandi 	}
11119fa3c4b1SRoja Rani Yarubandi 	if (port->cts_rts_swap) {
11129fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
11139fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO0_SEL;
11149fa3c4b1SRoja Rani Yarubandi 	}
11159fa3c4b1SRoja Rani Yarubandi 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
11169fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap || port->cts_rts_swap)
11179fa3c4b1SRoja Rani Yarubandi 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
11189fa3c4b1SRoja Rani Yarubandi 
1119c4f52879SKarthikeyan Ramasubramanian 	/*
1120c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
1121c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
1122c4f52879SKarthikeyan Ramasubramanian 	 */
11238a8a66a1SGirish Mahadevan 	if (uart_console(uport))
1124c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
1125650c8bd3SDouglas Anderson 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1126650c8bd3SDouglas Anderson 			       false, true, true);
1127a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
11282aaa43c7SBartosz Golaszewski 	geni_se_select_mode(&port->se, port->dev_data->mode);
112935781d83SAniket Randive 	qcom_geni_serial_start_rx(uport);
1130c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
1131c362272bSDouglas Anderson 
1132c4f52879SKarthikeyan Ramasubramanian 	return 0;
1133c4f52879SKarthikeyan Ramasubramanian }
1134c4f52879SKarthikeyan Ramasubramanian 
1135c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
1136c4f52879SKarthikeyan Ramasubramanian {
1137c4f52879SKarthikeyan Ramasubramanian 	int ret;
113800ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1139c4f52879SKarthikeyan Ramasubramanian 
1140c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1141c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
1142c4f52879SKarthikeyan Ramasubramanian 		if (ret)
1143c4f52879SKarthikeyan Ramasubramanian 			return ret;
1144c4f52879SKarthikeyan Ramasubramanian 	}
11453e4aaea7SAkash Asthana 	enable_irq(uport->irq);
1146c4f52879SKarthikeyan Ramasubramanian 
11473e4aaea7SAkash Asthana 	return 0;
1148c4f52879SKarthikeyan Ramasubramanian }
1149c4f52879SKarthikeyan Ramasubramanian 
1150c474c775SVijaya Krishna Nivarthi static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
1151c474c775SVijaya Krishna Nivarthi 			unsigned int *clk_div, unsigned int percent_tol)
1152c474c775SVijaya Krishna Nivarthi {
1153c474c775SVijaya Krishna Nivarthi 	unsigned long freq;
1154c474c775SVijaya Krishna Nivarthi 	unsigned long div, maxdiv;
1155c474c775SVijaya Krishna Nivarthi 	u64 mult;
1156c474c775SVijaya Krishna Nivarthi 	unsigned long offset, abs_tol, achieved;
1157c474c775SVijaya Krishna Nivarthi 
1158c474c775SVijaya Krishna Nivarthi 	abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
1159c474c775SVijaya Krishna Nivarthi 	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
1160c474c775SVijaya Krishna Nivarthi 	div = 1;
1161c474c775SVijaya Krishna Nivarthi 	while (div <= maxdiv) {
1162c474c775SVijaya Krishna Nivarthi 		mult = (u64)div * desired_clk;
1163c474c775SVijaya Krishna Nivarthi 		if (mult != (unsigned long)mult)
1164c474c775SVijaya Krishna Nivarthi 			break;
1165c474c775SVijaya Krishna Nivarthi 
1166c474c775SVijaya Krishna Nivarthi 		offset = div * abs_tol;
1167c474c775SVijaya Krishna Nivarthi 		freq = clk_round_rate(clk, mult - offset);
1168c474c775SVijaya Krishna Nivarthi 
1169c474c775SVijaya Krishna Nivarthi 		/* Can only get lower if we're done */
1170c474c775SVijaya Krishna Nivarthi 		if (freq < mult - offset)
1171c474c775SVijaya Krishna Nivarthi 			break;
1172c474c775SVijaya Krishna Nivarthi 
1173c474c775SVijaya Krishna Nivarthi 		/*
1174c474c775SVijaya Krishna Nivarthi 		 * Re-calculate div in case rounding skipped rates but we
1175c474c775SVijaya Krishna Nivarthi 		 * ended up at a good one, then check for a match.
1176c474c775SVijaya Krishna Nivarthi 		 */
1177c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_CLOSEST(freq, desired_clk);
1178c474c775SVijaya Krishna Nivarthi 		achieved = DIV_ROUND_CLOSEST(freq, div);
1179c474c775SVijaya Krishna Nivarthi 		if (achieved <= desired_clk + abs_tol &&
1180c474c775SVijaya Krishna Nivarthi 		    achieved >= desired_clk - abs_tol) {
1181c474c775SVijaya Krishna Nivarthi 			*clk_div = div;
1182c474c775SVijaya Krishna Nivarthi 			return freq;
1183c474c775SVijaya Krishna Nivarthi 		}
1184c474c775SVijaya Krishna Nivarthi 
1185c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_UP(freq, desired_clk);
1186c474c775SVijaya Krishna Nivarthi 	}
1187c474c775SVijaya Krishna Nivarthi 
1188c474c775SVijaya Krishna Nivarthi 	return 0;
1189c474c775SVijaya Krishna Nivarthi }
1190c474c775SVijaya Krishna Nivarthi 
1191c2194bc9SVijaya Krishna Nivarthi static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1192ce734600SVivek Gautam 			unsigned int sampling_rate, unsigned int *clk_div)
1193c4f52879SKarthikeyan Ramasubramanian {
1194c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
1195c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
1196c4f52879SKarthikeyan Ramasubramanian 
1197ce734600SVivek Gautam 	desired_clk = baud * sampling_rate;
1198c474c775SVijaya Krishna Nivarthi 	if (!desired_clk)
1199c2194bc9SVijaya Krishna Nivarthi 		return 0;
1200c2194bc9SVijaya Krishna Nivarthi 
1201c474c775SVijaya Krishna Nivarthi 	/*
1202c474c775SVijaya Krishna Nivarthi 	 * try to find a clock rate within 2% tolerance, then within 5%
1203c474c775SVijaya Krishna Nivarthi 	 */
1204c474c775SVijaya Krishna Nivarthi 	ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1205c474c775SVijaya Krishna Nivarthi 	if (!ser_clk)
1206c474c775SVijaya Krishna Nivarthi 		ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1207c2194bc9SVijaya Krishna Nivarthi 
1208c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
1209c4f52879SKarthikeyan Ramasubramanian }
1210c4f52879SKarthikeyan Ramasubramanian 
1211c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
1212bec5b814SIlpo Järvinen 					 struct ktermios *termios,
1213bec5b814SIlpo Järvinen 					 const struct ktermios *old)
1214c4f52879SKarthikeyan Ramasubramanian {
1215c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
1216385298abSRyan Case 	u32 bits_per_char;
1217385298abSRyan Case 	u32 tx_trans_cfg;
1218385298abSRyan Case 	u32 tx_parity_cfg;
1219385298abSRyan Case 	u32 rx_trans_cfg;
1220385298abSRyan Case 	u32 rx_parity_cfg;
1221385298abSRyan Case 	u32 stop_bit_len;
1222c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
1223385298abSRyan Case 	u32 ser_clk_cfg;
122400ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1225c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
1226ce734600SVivek Gautam 	u32 ver, sampling_rate;
12277cf563b2SAkash Asthana 	unsigned int avg_bw_core;
1228c4f52879SKarthikeyan Ramasubramanian 
1229c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
1230c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
1231c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1232c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
1233ce734600SVivek Gautam 
1234ce734600SVivek Gautam 	sampling_rate = UART_OVERSAMPLING;
1235ce734600SVivek Gautam 	/* Sampling rate is halved for IP versions >= 2.5 */
1236ce734600SVivek Gautam 	ver = geni_se_get_qup_hw_version(&port->se);
1237c9ca43d4SParas Sharma 	if (ver >= QUP_SE_VERSION_2_5)
1238ce734600SVivek Gautam 		sampling_rate /= 2;
1239ce734600SVivek Gautam 
1240c2194bc9SVijaya Krishna Nivarthi 	clk_rate = get_clk_div_rate(port->se.clk, baud,
1241c2194bc9SVijaya Krishna Nivarthi 		sampling_rate, &clk_div);
1242c474c775SVijaya Krishna Nivarthi 	if (!clk_rate) {
1243c474c775SVijaya Krishna Nivarthi 		dev_err(port->se.dev,
12440fec5180SDouglas Anderson 			"Couldn't find suitable clock rate for %u\n",
1245c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate);
1246c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
1247c474c775SVijaya Krishna Nivarthi 	}
1248c474c775SVijaya Krishna Nivarthi 
12490fec5180SDouglas Anderson 	dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
1250c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate, clk_rate, clk_div);
1251c4f52879SKarthikeyan Ramasubramanian 
1252c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
1253a5819b54SRajendra Nayak 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1254c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
1255c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1256c4f52879SKarthikeyan Ramasubramanian 
12577cf563b2SAkash Asthana 	/*
12587cf563b2SAkash Asthana 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
12597cf563b2SAkash Asthana 	 * only.
12607cf563b2SAkash Asthana 	 */
12617cf563b2SAkash Asthana 	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
12627cf563b2SAkash Asthana 						: GENI_DEFAULT_BW;
12637cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
12647cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
12657cf563b2SAkash Asthana 	geni_icc_set_bw(&port->se);
12667cf563b2SAkash Asthana 
1267c4f52879SKarthikeyan Ramasubramanian 	/* parity */
12689e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
12699e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
12709e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
12719e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1272c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
1273c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
1274c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
1275c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
1276c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
1277c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
1278c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
1279c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
1280c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
1281c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
1282c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
1283c4f52879SKarthikeyan Ramasubramanian 		} else {
1284c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
1285c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
1286c4f52879SKarthikeyan Ramasubramanian 		}
1287c4f52879SKarthikeyan Ramasubramanian 	} else {
1288c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1289c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1290c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1291c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1292c4f52879SKarthikeyan Ramasubramanian 	}
1293c4f52879SKarthikeyan Ramasubramanian 
1294c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
12953ec2ff37SJiri Slaby 	bits_per_char = tty_get_char_size(termios->c_cflag);
1296c4f52879SKarthikeyan Ramasubramanian 
1297c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1298c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1299c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1300c4f52879SKarthikeyan Ramasubramanian 	else
1301c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1302c4f52879SKarthikeyan Ramasubramanian 
1303c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1304c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1305c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1306c4f52879SKarthikeyan Ramasubramanian 	else
1307c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1308c4f52879SKarthikeyan Ramasubramanian 
1309c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1310c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1311c4f52879SKarthikeyan Ramasubramanian 
13128a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
13139e06d55fSRyan Case 		writel(port->loopback,
13148a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
13159e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
13169e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
13179e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
13189e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
13199e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
13209e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
13219e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
13229e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
13239e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1324c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1325c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1326c4f52879SKarthikeyan Ramasubramanian }
1327c4f52879SKarthikeyan Ramasubramanian 
1328c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1329975efc66SJohn Stultz static int qcom_geni_console_setup(struct console *co, char *options)
1330c4f52879SKarthikeyan Ramasubramanian {
1331c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1332c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
13332ec812a0SDouglas Anderson 	int baud = 115200;
1334c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1335c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1336c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1337c362272bSDouglas Anderson 	int ret;
1338c4f52879SKarthikeyan Ramasubramanian 
1339c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1340c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1341c4f52879SKarthikeyan Ramasubramanian 
13428a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1343c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
13446a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1345c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1346c4f52879SKarthikeyan Ramasubramanian 	}
1347c4f52879SKarthikeyan Ramasubramanian 
1348c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1349c4f52879SKarthikeyan Ramasubramanian 
1350c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1351c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1352c4f52879SKarthikeyan Ramasubramanian 
1353c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1354c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1355c362272bSDouglas Anderson 		if (ret)
1356c362272bSDouglas Anderson 			return ret;
1357c4f52879SKarthikeyan Ramasubramanian 	}
1358c4f52879SKarthikeyan Ramasubramanian 
1359c4f52879SKarthikeyan Ramasubramanian 	if (options)
1360c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1361c4f52879SKarthikeyan Ramasubramanian 
1362c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1363c4f52879SKarthikeyan Ramasubramanian }
1364c4f52879SKarthikeyan Ramasubramanian 
136543f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
136643f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
136743f1831bSKarthikeyan Ramasubramanian {
136843f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
136943f1831bSKarthikeyan Ramasubramanian 
137043f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
137143f1831bSKarthikeyan Ramasubramanian }
137243f1831bSKarthikeyan Ramasubramanian 
1373205b5bddSDouglas Anderson #ifdef CONFIG_CONSOLE_POLL
1374205b5bddSDouglas Anderson static int qcom_geni_serial_earlycon_read(struct console *con,
1375205b5bddSDouglas Anderson 					  char *s, unsigned int n)
1376205b5bddSDouglas Anderson {
1377205b5bddSDouglas Anderson 	struct earlycon_device *dev = con->data;
1378205b5bddSDouglas Anderson 	struct uart_port *uport = &dev->port;
1379205b5bddSDouglas Anderson 	int num_read = 0;
1380205b5bddSDouglas Anderson 	int ch;
1381205b5bddSDouglas Anderson 
1382205b5bddSDouglas Anderson 	while (num_read < n) {
1383205b5bddSDouglas Anderson 		ch = qcom_geni_serial_get_char(uport);
1384205b5bddSDouglas Anderson 		if (ch == NO_POLL_CHAR)
1385205b5bddSDouglas Anderson 			break;
1386205b5bddSDouglas Anderson 		s[num_read++] = ch;
1387205b5bddSDouglas Anderson 	}
1388205b5bddSDouglas Anderson 
1389205b5bddSDouglas Anderson 	return num_read;
1390205b5bddSDouglas Anderson }
1391205b5bddSDouglas Anderson 
1392205b5bddSDouglas Anderson static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1393205b5bddSDouglas Anderson 						      struct console *con)
1394205b5bddSDouglas Anderson {
1395205b5bddSDouglas Anderson 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1396205b5bddSDouglas Anderson 	con->read = qcom_geni_serial_earlycon_read;
1397205b5bddSDouglas Anderson }
1398205b5bddSDouglas Anderson #else
1399205b5bddSDouglas Anderson static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1400205b5bddSDouglas Anderson 						      struct console *con) { }
1401205b5bddSDouglas Anderson #endif
1402205b5bddSDouglas Anderson 
1403e42d6c3eSDouglas Anderson static struct qcom_geni_private_data earlycon_private_data;
1404e42d6c3eSDouglas Anderson 
140543f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
140643f1831bSKarthikeyan Ramasubramanian 								const char *opt)
140743f1831bSKarthikeyan Ramasubramanian {
140843f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
140943f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
141043f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
141143f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
141243f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
141343f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
141443f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
141543f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
141643f1831bSKarthikeyan Ramasubramanian 
141743f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
141843f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
141943f1831bSKarthikeyan Ramasubramanian 
1420e42d6c3eSDouglas Anderson 	uport->private_data = &earlycon_private_data;
1421e42d6c3eSDouglas Anderson 
142243f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
142343f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
142443f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
142543f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
142643f1831bSKarthikeyan Ramasubramanian 	/*
142743f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
142843f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
142943f1831bSKarthikeyan Ramasubramanian 	 */
143043f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
143143f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
143243f1831bSKarthikeyan Ramasubramanian 
143343f1831bSKarthikeyan Ramasubramanian 	/*
143443f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
143543f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
143643f1831bSKarthikeyan Ramasubramanian 	 */
143743f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
143843f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
1439650c8bd3SDouglas Anderson 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1440650c8bd3SDouglas Anderson 			       false, true, true);
144143f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
144243f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
144343f1831bSKarthikeyan Ramasubramanian 
14449e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
14459e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
14469e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
14479e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
14489e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
14499e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
14509e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
145143f1831bSKarthikeyan Ramasubramanian 
145243f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
145343f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
1454205b5bddSDouglas Anderson 	qcom_geni_serial_enable_early_read(&se, dev->con);
1455205b5bddSDouglas Anderson 
145643f1831bSKarthikeyan Ramasubramanian 	return 0;
145743f1831bSKarthikeyan Ramasubramanian }
145843f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
145943f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
146043f1831bSKarthikeyan Ramasubramanian 
1461c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1462c4f52879SKarthikeyan Ramasubramanian {
1463c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1464c4f52879SKarthikeyan Ramasubramanian }
1465c4f52879SKarthikeyan Ramasubramanian 
1466c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1467c4f52879SKarthikeyan Ramasubramanian {
1468c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1469c4f52879SKarthikeyan Ramasubramanian }
1470c4f52879SKarthikeyan Ramasubramanian 
1471c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1472c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1473c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1474c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1475c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1476c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1477c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1478c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1479c4f52879SKarthikeyan Ramasubramanian };
1480c4f52879SKarthikeyan Ramasubramanian 
1481c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1482c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1483c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1484c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1485c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1486c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1487c4f52879SKarthikeyan Ramasubramanian };
1488c4f52879SKarthikeyan Ramasubramanian #else
1489c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1490c4f52879SKarthikeyan Ramasubramanian {
1491c4f52879SKarthikeyan Ramasubramanian 	return 0;
1492c4f52879SKarthikeyan Ramasubramanian }
1493c4f52879SKarthikeyan Ramasubramanian 
1494c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1495c4f52879SKarthikeyan Ramasubramanian {
1496c4f52879SKarthikeyan Ramasubramanian }
1497c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1498c4f52879SKarthikeyan Ramasubramanian 
14998a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
15008a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
15018a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
15028a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
15038a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
15048a8a66a1SGirish Mahadevan };
15058a8a66a1SGirish Mahadevan 
15068a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1507c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1508c4f52879SKarthikeyan Ramasubramanian {
150900ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1510c4f52879SKarthikeyan Ramasubramanian 
1511c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1512c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1513c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1514c362272bSDouglas Anderson 
15157cf563b2SAkash Asthana 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
15167cf563b2SAkash Asthana 		geni_icc_enable(&port->se);
1517c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
15187cf563b2SAkash Asthana 	} else if (new_state == UART_PM_STATE_OFF &&
15197cf563b2SAkash Asthana 			old_state == UART_PM_STATE_ON) {
1520c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
15217cf563b2SAkash Asthana 		geni_icc_disable(&port->se);
15227cf563b2SAkash Asthana 	}
1523c4f52879SKarthikeyan Ramasubramanian }
1524c4f52879SKarthikeyan Ramasubramanian 
1525c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1526c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
15272aaa43c7SBartosz Golaszewski 	.stop_tx = qcom_geni_serial_stop_tx_fifo,
15282aaa43c7SBartosz Golaszewski 	.start_tx = qcom_geni_serial_start_tx_fifo,
15292aaa43c7SBartosz Golaszewski 	.stop_rx = qcom_geni_serial_stop_rx_fifo,
15302aaa43c7SBartosz Golaszewski 	.start_rx = qcom_geni_serial_start_rx_fifo,
1531c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1532c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1533c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1534c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1535c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1536c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
15378a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
15388a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1539c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1540c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1541c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1542c4f52879SKarthikeyan Ramasubramanian #endif
15438a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
15448a8a66a1SGirish Mahadevan };
15458a8a66a1SGirish Mahadevan 
15468a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
15478a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
15482aaa43c7SBartosz Golaszewski 	.stop_tx = qcom_geni_serial_stop_tx_dma,
15492aaa43c7SBartosz Golaszewski 	.start_tx = qcom_geni_serial_start_tx_dma,
15502aaa43c7SBartosz Golaszewski 	.start_rx = qcom_geni_serial_start_rx_dma,
15512aaa43c7SBartosz Golaszewski 	.stop_rx = qcom_geni_serial_stop_rx_dma,
15528a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
15538a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
15548a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
15558a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
15568a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
15578a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
15588a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
15598a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
15608a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1561c4f52879SKarthikeyan Ramasubramanian };
1562c4f52879SKarthikeyan Ramasubramanian 
1563c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1564c4f52879SKarthikeyan Ramasubramanian {
1565c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
156671581242SColin Ian King 	int line;
1567c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1568c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1569c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1570066cd1c4SKarthikeyan Ramasubramanian 	int irq;
15718a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
157240ec6d41SBartosz Golaszewski 	const struct qcom_geni_device_data *data;
1573c4f52879SKarthikeyan Ramasubramanian 
157440ec6d41SBartosz Golaszewski 	data = of_device_get_match_data(&pdev->dev);
157540ec6d41SBartosz Golaszewski 	if (!data)
157640ec6d41SBartosz Golaszewski 		return -EINVAL;
15778a8a66a1SGirish Mahadevan 
157840ec6d41SBartosz Golaszewski 	if (data->console) {
15798a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1580c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
15818a8a66a1SGirish Mahadevan 	} else {
15828a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
158308b0adb1SDmitry Baryshkov 		line = of_alias_get_id(pdev->dev.of_node, "serial");
158408b0adb1SDmitry Baryshkov 		if (line == -ENODEV) /* compat with non-standard aliases */
15858a8a66a1SGirish Mahadevan 			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
15868a8a66a1SGirish Mahadevan 	}
1587c4f52879SKarthikeyan Ramasubramanian 
158840ec6d41SBartosz Golaszewski 	port = get_port_from_line(line, data->console);
1589c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
15906a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
15916a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1592c4f52879SKarthikeyan Ramasubramanian 	}
1593c4f52879SKarthikeyan Ramasubramanian 
1594c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1595c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1596c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1597c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1598c4f52879SKarthikeyan Ramasubramanian 
1599c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
160040ec6d41SBartosz Golaszewski 	port->dev_data = data;
1601c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1602c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1603c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1604c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1605c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1606c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1607c4f52879SKarthikeyan Ramasubramanian 		return ret;
1608c4f52879SKarthikeyan Ramasubramanian 	}
1609c4f52879SKarthikeyan Ramasubramanian 
1610c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16117693b331SWei Yongjun 	if (!res)
16127693b331SWei Yongjun 		return -EINVAL;
1613c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1614c4f52879SKarthikeyan Ramasubramanian 
1615c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1616c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1617c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1618c4f52879SKarthikeyan Ramasubramanian 
161940ec6d41SBartosz Golaszewski 	if (!data->console) {
16202aaa43c7SBartosz Golaszewski 		port->rx_buf = devm_kzalloc(uport->dev,
16212aaa43c7SBartosz Golaszewski 					    DMA_RX_BUF_SIZE, GFP_KERNEL);
16222aaa43c7SBartosz Golaszewski 		if (!port->rx_buf)
1623f9d690b6Ssatya priya 			return -ENOMEM;
1624f9d690b6Ssatya priya 	}
1625f9d690b6Ssatya priya 
16267cf563b2SAkash Asthana 	ret = geni_icc_get(&port->se, NULL);
16277cf563b2SAkash Asthana 	if (ret)
16287cf563b2SAkash Asthana 		return ret;
16297cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
16307cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
16317cf563b2SAkash Asthana 
16327cf563b2SAkash Asthana 	/* Set BW for register access */
16337cf563b2SAkash Asthana 	ret = geni_icc_set_bw(&port->se);
16347cf563b2SAkash Asthana 	if (ret)
16357cf563b2SAkash Asthana 		return ret;
16367cf563b2SAkash Asthana 
1637f3974413SAkash Asthana 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1638f3974413SAkash Asthana 			"qcom_geni_serial_%s%d",
1639f3974413SAkash Asthana 			uart_console(uport) ? "console" : "uart", uport->line);
1640f3974413SAkash Asthana 	if (!port->name)
1641f3974413SAkash Asthana 		return -ENOMEM;
1642f3974413SAkash Asthana 
1643066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
16441df21786SStephen Boyd 	if (irq < 0)
1645066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1646066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
16478f122698SDmitry Safonov 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1648c4f52879SKarthikeyan Ramasubramanian 
164940ec6d41SBartosz Golaszewski 	if (!data->console)
1650f3974413SAkash Asthana 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
16513e4aaea7SAkash Asthana 
16529fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
16539fa3c4b1SRoja Rani Yarubandi 		port->rx_tx_swap = true;
16549fa3c4b1SRoja Rani Yarubandi 
16559fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
16569fa3c4b1SRoja Rani Yarubandi 		port->cts_rts_swap = true;
16579fa3c4b1SRoja Rani Yarubandi 
1658300894a6SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1659300894a6SYangtao Li 	if (ret)
1660300894a6SYangtao Li 		return ret;
1661a5819b54SRajendra Nayak 	/* OPP table is optional */
1662300894a6SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
1663c7ac46daSViresh Kumar 	if (ret && ret != -ENODEV) {
1664a5819b54SRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1665300894a6SYangtao Li 		return ret;
1666a5819b54SRajendra Nayak 	}
1667a5819b54SRajendra Nayak 
1668e42d6c3eSDouglas Anderson 	port->private_data.drv = drv;
1669e42d6c3eSDouglas Anderson 	uport->private_data = &port->private_data;
1670c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
1671f3974413SAkash Asthana 
1672f3974413SAkash Asthana 	ret = uart_add_one_port(drv, uport);
1673f3974413SAkash Asthana 	if (ret)
1674300894a6SYangtao Li 		return ret;
1675f3974413SAkash Asthana 
1676f3974413SAkash Asthana 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1677f3974413SAkash Asthana 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1678f3974413SAkash Asthana 			IRQF_TRIGGER_HIGH, port->name, uport);
1679f3974413SAkash Asthana 	if (ret) {
1680f3974413SAkash Asthana 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1681f3974413SAkash Asthana 		uart_remove_one_port(drv, uport);
1682300894a6SYangtao Li 		return ret;
1683f3974413SAkash Asthana 	}
1684f3974413SAkash Asthana 
1685f3974413SAkash Asthana 	/*
1686f3974413SAkash Asthana 	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1687f3974413SAkash Asthana 	 * enabled/disabled from dev_pm_arm_wake_irq during system
1688f3974413SAkash Asthana 	 * suspend/resume respectively.
1689f3974413SAkash Asthana 	 */
1690f3974413SAkash Asthana 	pm_runtime_set_active(&pdev->dev);
1691f3974413SAkash Asthana 
1692f3974413SAkash Asthana 	if (port->wakeup_irq > 0) {
1693f3974413SAkash Asthana 		device_init_wakeup(&pdev->dev, true);
1694f3974413SAkash Asthana 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1695f3974413SAkash Asthana 						port->wakeup_irq);
1696f3974413SAkash Asthana 		if (ret) {
1697f3974413SAkash Asthana 			device_init_wakeup(&pdev->dev, false);
1698f3974413SAkash Asthana 			uart_remove_one_port(drv, uport);
1699300894a6SYangtao Li 			return ret;
1700f3974413SAkash Asthana 		}
1701f3974413SAkash Asthana 	}
1702f3974413SAkash Asthana 
1703f3974413SAkash Asthana 	return 0;
1704c4f52879SKarthikeyan Ramasubramanian }
1705c4f52879SKarthikeyan Ramasubramanian 
1706c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1707c4f52879SKarthikeyan Ramasubramanian {
1708c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1709e42d6c3eSDouglas Anderson 	struct uart_driver *drv = port->private_data.drv;
1710c4f52879SKarthikeyan Ramasubramanian 
1711f3974413SAkash Asthana 	dev_pm_clear_wake_irq(&pdev->dev);
1712f3974413SAkash Asthana 	device_init_wakeup(&pdev->dev, false);
1713c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1714f3974413SAkash Asthana 
1715c4f52879SKarthikeyan Ramasubramanian 	return 0;
1716c4f52879SKarthikeyan Ramasubramanian }
1717c4f52879SKarthikeyan Ramasubramanian 
17185342ab0aSArnd Bergmann static int qcom_geni_serial_sys_suspend(struct device *dev)
1719c4f52879SKarthikeyan Ramasubramanian {
1720a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1721c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1722e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1723c4f52879SKarthikeyan Ramasubramanian 
17244a3107f6SRajendra Nayak 	/*
17254a3107f6SRajendra Nayak 	 * This is done so we can hit the lowest possible state in suspend
17264a3107f6SRajendra Nayak 	 * even with no_console_suspend
17274a3107f6SRajendra Nayak 	 */
17284a3107f6SRajendra Nayak 	if (uart_console(uport)) {
1729408e532eSVijaya Krishna Nivarthi 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
17304a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
17314a3107f6SRajendra Nayak 	}
1732e42d6c3eSDouglas Anderson 	return uart_suspend_port(private_data->drv, uport);
17338a8a66a1SGirish Mahadevan }
17348a8a66a1SGirish Mahadevan 
17355342ab0aSArnd Bergmann static int qcom_geni_serial_sys_resume(struct device *dev)
1736c4f52879SKarthikeyan Ramasubramanian {
17374a3107f6SRajendra Nayak 	int ret;
1738a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1739c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1740e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1741c4f52879SKarthikeyan Ramasubramanian 
17424a3107f6SRajendra Nayak 	ret = uart_resume_port(private_data->drv, uport);
17434a3107f6SRajendra Nayak 	if (uart_console(uport)) {
1744408e532eSVijaya Krishna Nivarthi 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
17454a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
17464a3107f6SRajendra Nayak 	}
17474a3107f6SRajendra Nayak 	return ret;
1748c4f52879SKarthikeyan Ramasubramanian }
1749c4f52879SKarthikeyan Ramasubramanian 
175035781d83SAniket Randive static int qcom_geni_serial_sys_hib_resume(struct device *dev)
175135781d83SAniket Randive {
175235781d83SAniket Randive 	int ret = 0;
175335781d83SAniket Randive 	struct uart_port *uport;
175435781d83SAniket Randive 	struct qcom_geni_private_data *private_data;
175535781d83SAniket Randive 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
175635781d83SAniket Randive 
175735781d83SAniket Randive 	uport = &port->uport;
175835781d83SAniket Randive 	private_data = uport->private_data;
175935781d83SAniket Randive 
176035781d83SAniket Randive 	if (uart_console(uport)) {
176135781d83SAniket Randive 		geni_icc_set_tag(&port->se, 0x7);
176235781d83SAniket Randive 		geni_icc_set_bw(&port->se);
176335781d83SAniket Randive 		ret = uart_resume_port(private_data->drv, uport);
176435781d83SAniket Randive 		/*
176535781d83SAniket Randive 		 * For hibernation usecase clients for
176635781d83SAniket Randive 		 * console UART won't call port setup during restore,
176735781d83SAniket Randive 		 * hence call port setup for console uart.
176835781d83SAniket Randive 		 */
176935781d83SAniket Randive 		qcom_geni_serial_port_setup(uport);
177035781d83SAniket Randive 	} else {
177135781d83SAniket Randive 		/*
177235781d83SAniket Randive 		 * Peripheral register settings are lost during hibernation.
177335781d83SAniket Randive 		 * Update setup flag such that port setup happens again
177435781d83SAniket Randive 		 * during next session. Clients of HS-UART will close and
177535781d83SAniket Randive 		 * open the port during hibernation.
177635781d83SAniket Randive 		 */
177735781d83SAniket Randive 		port->setup = false;
177835781d83SAniket Randive 	}
177935781d83SAniket Randive 	return ret;
178035781d83SAniket Randive }
178135781d83SAniket Randive 
178240ec6d41SBartosz Golaszewski static const struct qcom_geni_device_data qcom_geni_console_data = {
178340ec6d41SBartosz Golaszewski 	.console = true,
17842aaa43c7SBartosz Golaszewski 	.mode = GENI_SE_FIFO,
178540ec6d41SBartosz Golaszewski };
178640ec6d41SBartosz Golaszewski 
178740ec6d41SBartosz Golaszewski static const struct qcom_geni_device_data qcom_geni_uart_data = {
178840ec6d41SBartosz Golaszewski 	.console = false,
17892aaa43c7SBartosz Golaszewski 	.mode = GENI_SE_DMA,
179040ec6d41SBartosz Golaszewski };
179140ec6d41SBartosz Golaszewski 
1792c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
17935342ab0aSArnd Bergmann 	.suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17945342ab0aSArnd Bergmann 	.resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
17955342ab0aSArnd Bergmann 	.freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17965342ab0aSArnd Bergmann 	.poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17975342ab0aSArnd Bergmann 	.restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
17985342ab0aSArnd Bergmann 	.thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1799c4f52879SKarthikeyan Ramasubramanian };
1800c4f52879SKarthikeyan Ramasubramanian 
1801c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
180240ec6d41SBartosz Golaszewski 	{
180340ec6d41SBartosz Golaszewski 		.compatible = "qcom,geni-debug-uart",
180440ec6d41SBartosz Golaszewski 		.data = &qcom_geni_console_data,
180540ec6d41SBartosz Golaszewski 	},
180640ec6d41SBartosz Golaszewski 	{
180740ec6d41SBartosz Golaszewski 		.compatible = "qcom,geni-uart",
180840ec6d41SBartosz Golaszewski 		.data = &qcom_geni_uart_data,
180940ec6d41SBartosz Golaszewski 	},
1810c4f52879SKarthikeyan Ramasubramanian 	{}
1811c4f52879SKarthikeyan Ramasubramanian };
1812c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1813c4f52879SKarthikeyan Ramasubramanian 
1814c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1815c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1816c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1817c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1818c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1819c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1820c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1821c4f52879SKarthikeyan Ramasubramanian 	},
1822c4f52879SKarthikeyan Ramasubramanian };
1823c4f52879SKarthikeyan Ramasubramanian 
1824c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1825c4f52879SKarthikeyan Ramasubramanian {
1826c4f52879SKarthikeyan Ramasubramanian 	int ret;
1827c4f52879SKarthikeyan Ramasubramanian 
1828c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1829c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1830c4f52879SKarthikeyan Ramasubramanian 		return ret;
1831c4f52879SKarthikeyan Ramasubramanian 
18328a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
18338a8a66a1SGirish Mahadevan 	if (ret) {
1834c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1835c4f52879SKarthikeyan Ramasubramanian 		return ret;
1836c4f52879SKarthikeyan Ramasubramanian 	}
18378a8a66a1SGirish Mahadevan 
18388a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
18398a8a66a1SGirish Mahadevan 	if (ret) {
18408a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
18418a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
18428a8a66a1SGirish Mahadevan 	}
18438a8a66a1SGirish Mahadevan 	return ret;
18448a8a66a1SGirish Mahadevan }
1845c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1846c4f52879SKarthikeyan Ramasubramanian 
1847c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1848c4f52879SKarthikeyan Ramasubramanian {
1849c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1850c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
18518a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1852c4f52879SKarthikeyan Ramasubramanian }
1853c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1854c4f52879SKarthikeyan Ramasubramanian 
1855c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1856c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1857