xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision 491581f40e4c52c4b36c83e8c75b2210ed7c358a)
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>
19*491581f4SElliot 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 
5992aaa43c7SBartosz Golaszewski 	if (port->rx_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 
6342aaa43c7SBartosz Golaszewski 	xmit_size = uart_circ_chars_pending(xmit);
6352aaa43c7SBartosz Golaszewski 	if (xmit_size < WAKEUP_CHARS)
6362aaa43c7SBartosz Golaszewski 		uart_write_wakeup(uport);
6372aaa43c7SBartosz Golaszewski 
6382aaa43c7SBartosz Golaszewski 	xmit_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
6392aaa43c7SBartosz Golaszewski 
6402aaa43c7SBartosz Golaszewski 	qcom_geni_serial_setup_tx(uport, xmit_size);
6412aaa43c7SBartosz Golaszewski 
6422aaa43c7SBartosz Golaszewski 	ret = geni_se_tx_dma_prep(&port->se, &xmit->buf[xmit->tail],
6432aaa43c7SBartosz Golaszewski 				  xmit_size, &port->tx_dma_addr);
6442aaa43c7SBartosz Golaszewski 	if (ret) {
6452aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
6462aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_tx_dma(uport);
6472aaa43c7SBartosz Golaszewski 		return;
6482aaa43c7SBartosz Golaszewski 	}
6492aaa43c7SBartosz Golaszewski 
6502aaa43c7SBartosz Golaszewski 	port->tx_remaining = xmit_size;
6512aaa43c7SBartosz Golaszewski }
6522aaa43c7SBartosz Golaszewski 
6532aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
6542aaa43c7SBartosz Golaszewski {
6552aaa43c7SBartosz Golaszewski 	u32 irq_en;
6562aaa43c7SBartosz Golaszewski 
6572aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_main_active(uport) ||
6582aaa43c7SBartosz Golaszewski 	    !qcom_geni_serial_tx_empty(uport))
659c4f52879SKarthikeyan Ramasubramanian 		return;
660c4f52879SKarthikeyan Ramasubramanian 
6619e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
662c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
663c4f52879SKarthikeyan Ramasubramanian 
664bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
6659e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
666c4f52879SKarthikeyan Ramasubramanian }
667c4f52879SKarthikeyan Ramasubramanian 
6682aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
669c4f52879SKarthikeyan Ramasubramanian {
670c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
67100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
672c4f52879SKarthikeyan Ramasubramanian 
6739e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
674bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
675bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
6769e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
677c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
6782aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_main_active(uport))
679c4f52879SKarthikeyan Ramasubramanian 		return;
680c4f52879SKarthikeyan Ramasubramanian 
681c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
682c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
683c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
684c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
685c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
686c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
6879e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
688c4f52879SKarthikeyan Ramasubramanian 	}
6899e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
690c4f52879SKarthikeyan Ramasubramanian }
691c4f52879SKarthikeyan Ramasubramanian 
6922aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
693c4f52879SKarthikeyan Ramasubramanian {
694c4f52879SKarthikeyan Ramasubramanian 	u32 status;
695d0fabb0dSBartosz Golaszewski 	u32 word_cnt;
696d0fabb0dSBartosz Golaszewski 	u32 last_word_byte_cnt;
697d0fabb0dSBartosz Golaszewski 	u32 last_word_partial;
698d0fabb0dSBartosz Golaszewski 	u32 total_bytes;
699c4f52879SKarthikeyan Ramasubramanian 
700d0fabb0dSBartosz Golaszewski 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
701d0fabb0dSBartosz Golaszewski 	word_cnt = status & RX_FIFO_WC_MSK;
702d0fabb0dSBartosz Golaszewski 	last_word_partial = status & RX_LAST;
703d0fabb0dSBartosz Golaszewski 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
704d0fabb0dSBartosz Golaszewski 						RX_LAST_BYTE_VALID_SHFT;
705c4f52879SKarthikeyan Ramasubramanian 
706d0fabb0dSBartosz Golaszewski 	if (!word_cnt)
707d0fabb0dSBartosz Golaszewski 		return;
708d0fabb0dSBartosz Golaszewski 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
709d0fabb0dSBartosz Golaszewski 	if (last_word_partial && last_word_byte_cnt)
710d0fabb0dSBartosz Golaszewski 		total_bytes += last_word_byte_cnt;
711d0fabb0dSBartosz Golaszewski 	else
712d0fabb0dSBartosz Golaszewski 		total_bytes += BYTES_PER_FIFO_WORD;
7132aaa43c7SBartosz Golaszewski 	handle_rx_console(uport, total_bytes, drop);
714c4f52879SKarthikeyan Ramasubramanian }
715c4f52879SKarthikeyan Ramasubramanian 
7162aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
717c4f52879SKarthikeyan Ramasubramanian {
718c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
71900ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
720679aac5eSsatya priya 	u32 s_irq_status;
721c4f52879SKarthikeyan Ramasubramanian 
7229e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
723c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
7249e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
725c4f52879SKarthikeyan Ramasubramanian 
7269e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
727c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
7289e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
729c4f52879SKarthikeyan Ramasubramanian 
7302aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
731c4f52879SKarthikeyan Ramasubramanian 		return;
732c4f52879SKarthikeyan Ramasubramanian 
733c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
734679aac5eSsatya priya 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
735679aac5eSsatya priya 					S_CMD_CANCEL_EN, true);
736679aac5eSsatya priya 	/*
737679aac5eSsatya priya 	 * If timeout occurs secondary engine remains active
738679aac5eSsatya priya 	 * and Abort sequence is executed.
739679aac5eSsatya priya 	 */
740679aac5eSsatya priya 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
741679aac5eSsatya priya 	/* Flush the Rx buffer */
742679aac5eSsatya priya 	if (s_irq_status & S_RX_FIFO_LAST_EN)
7432aaa43c7SBartosz Golaszewski 		qcom_geni_serial_handle_rx_fifo(uport, true);
744679aac5eSsatya priya 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
745679aac5eSsatya priya 
7462aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
747c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
748c4f52879SKarthikeyan Ramasubramanian }
749c4f52879SKarthikeyan Ramasubramanian 
7502aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
751c4f52879SKarthikeyan Ramasubramanian {
752d0fabb0dSBartosz Golaszewski 	u32 irq_en;
75300ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
754c4f52879SKarthikeyan Ramasubramanian 
7552aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7562aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_fifo(uport);
757c4f52879SKarthikeyan Ramasubramanian 
758d0fabb0dSBartosz Golaszewski 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
759d0fabb0dSBartosz Golaszewski 
760d0fabb0dSBartosz Golaszewski 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
761d0fabb0dSBartosz Golaszewski 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
762d0fabb0dSBartosz Golaszewski 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
763d0fabb0dSBartosz Golaszewski 
764d0fabb0dSBartosz Golaszewski 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
765d0fabb0dSBartosz Golaszewski 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
766d0fabb0dSBartosz Golaszewski 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
767c4f52879SKarthikeyan Ramasubramanian }
768c4f52879SKarthikeyan Ramasubramanian 
7692aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
7702aaa43c7SBartosz Golaszewski {
7712aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
7722aaa43c7SBartosz Golaszewski 
7732aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
7742aaa43c7SBartosz Golaszewski 		return;
7752aaa43c7SBartosz Golaszewski 
7762aaa43c7SBartosz Golaszewski 	geni_se_cancel_s_cmd(&port->se);
7772aaa43c7SBartosz Golaszewski 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
7782aaa43c7SBartosz Golaszewski 				  S_CMD_CANCEL_EN, true);
7792aaa43c7SBartosz Golaszewski 
7802aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7812aaa43c7SBartosz Golaszewski 		qcom_geni_serial_abort_rx(uport);
7822aaa43c7SBartosz Golaszewski 
7832aaa43c7SBartosz Golaszewski 	if (port->rx_dma_addr) {
7842aaa43c7SBartosz Golaszewski 		geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
7852aaa43c7SBartosz Golaszewski 				      DMA_RX_BUF_SIZE);
7862aaa43c7SBartosz Golaszewski 		port->rx_dma_addr = 0;
7872aaa43c7SBartosz Golaszewski 	}
7882aaa43c7SBartosz Golaszewski }
7892aaa43c7SBartosz Golaszewski 
7902aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
7912aaa43c7SBartosz Golaszewski {
7922aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
7932aaa43c7SBartosz Golaszewski 	int ret;
7942aaa43c7SBartosz Golaszewski 
7952aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7962aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
7972aaa43c7SBartosz Golaszewski 
7982aaa43c7SBartosz Golaszewski 	geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
7992aaa43c7SBartosz Golaszewski 
8002aaa43c7SBartosz Golaszewski 	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
8012aaa43c7SBartosz Golaszewski 				  DMA_RX_BUF_SIZE,
8022aaa43c7SBartosz Golaszewski 				  &port->rx_dma_addr);
8032aaa43c7SBartosz Golaszewski 	if (ret) {
8042aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
8052aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8062aaa43c7SBartosz Golaszewski 	}
8072aaa43c7SBartosz Golaszewski }
8082aaa43c7SBartosz Golaszewski 
8092aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
8102aaa43c7SBartosz Golaszewski {
8112aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
8122aaa43c7SBartosz Golaszewski 	u32 rx_in;
8132aaa43c7SBartosz Golaszewski 	int ret;
8142aaa43c7SBartosz Golaszewski 
8152aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
8162aaa43c7SBartosz Golaszewski 		return;
8172aaa43c7SBartosz Golaszewski 
8182aaa43c7SBartosz Golaszewski 	if (!port->rx_dma_addr)
8192aaa43c7SBartosz Golaszewski 		return;
8202aaa43c7SBartosz Golaszewski 
8212aaa43c7SBartosz Golaszewski 	geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
8222aaa43c7SBartosz Golaszewski 	port->rx_dma_addr = 0;
8232aaa43c7SBartosz Golaszewski 
8242aaa43c7SBartosz Golaszewski 	rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
8252aaa43c7SBartosz Golaszewski 	if (!rx_in) {
8262aaa43c7SBartosz Golaszewski 		dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
8272aaa43c7SBartosz Golaszewski 		return;
8282aaa43c7SBartosz Golaszewski 	}
8292aaa43c7SBartosz Golaszewski 
8302aaa43c7SBartosz Golaszewski 	if (!drop)
8312aaa43c7SBartosz Golaszewski 		handle_rx_uart(uport, rx_in, drop);
8322aaa43c7SBartosz Golaszewski 
8332aaa43c7SBartosz Golaszewski 	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
8342aaa43c7SBartosz Golaszewski 				  DMA_RX_BUF_SIZE,
8352aaa43c7SBartosz Golaszewski 				  &port->rx_dma_addr);
8362aaa43c7SBartosz Golaszewski 	if (ret) {
8372aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
8382aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8392aaa43c7SBartosz Golaszewski 	}
8402aaa43c7SBartosz Golaszewski }
8412aaa43c7SBartosz Golaszewski 
8422aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx(struct uart_port *uport)
8432aaa43c7SBartosz Golaszewski {
8442aaa43c7SBartosz Golaszewski 	uport->ops->start_rx(uport);
8452aaa43c7SBartosz Golaszewski }
8462aaa43c7SBartosz Golaszewski 
8472aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx(struct uart_port *uport)
8482aaa43c7SBartosz Golaszewski {
8492aaa43c7SBartosz Golaszewski 	uport->ops->stop_rx(uport);
8502aaa43c7SBartosz Golaszewski }
8512aaa43c7SBartosz Golaszewski 
8522aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx(struct uart_port *uport)
8532aaa43c7SBartosz Golaszewski {
8542aaa43c7SBartosz Golaszewski 	uport->ops->stop_tx(uport);
8552aaa43c7SBartosz Golaszewski }
8562aaa43c7SBartosz Golaszewski 
857bd795584SBartosz Golaszewski static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
858d420fb49SBartosz Golaszewski 					     unsigned int chunk)
859c4f52879SKarthikeyan Ramasubramanian {
86000ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
861c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
862bd795584SBartosz Golaszewski 	unsigned int tx_bytes, c, remaining = chunk;
863bd795584SBartosz Golaszewski 	u8 buf[BYTES_PER_FIFO_WORD];
864c4f52879SKarthikeyan Ramasubramanian 
865bd795584SBartosz Golaszewski 	while (remaining) {
8663550f897SDan Carpenter 		memset(buf, 0, sizeof(buf));
867bd795584SBartosz Golaszewski 		tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
8683c66eb4bSMatthias Kaehlcke 
8693c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
870bd795584SBartosz Golaszewski 			buf[c] = xmit->buf[xmit->tail];
871bd795584SBartosz Golaszewski 			uart_xmit_advance(uport, 1);
8723c66eb4bSMatthias Kaehlcke 		}
873c4f52879SKarthikeyan Ramasubramanian 
87469736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
875c4f52879SKarthikeyan Ramasubramanian 
876c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
877a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
878c4f52879SKarthikeyan Ramasubramanian 	}
879d420fb49SBartosz Golaszewski }
880d420fb49SBartosz Golaszewski 
8812aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
8822aaa43c7SBartosz Golaszewski 					    bool done, bool active)
883d420fb49SBartosz Golaszewski {
884d420fb49SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
885d420fb49SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
886d420fb49SBartosz Golaszewski 	size_t avail;
887d420fb49SBartosz Golaszewski 	size_t pending;
888d420fb49SBartosz Golaszewski 	u32 status;
889d420fb49SBartosz Golaszewski 	u32 irq_en;
890d420fb49SBartosz Golaszewski 	unsigned int chunk;
891d420fb49SBartosz Golaszewski 
892d420fb49SBartosz Golaszewski 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
893d420fb49SBartosz Golaszewski 
894d420fb49SBartosz Golaszewski 	/* Complete the current tx command before taking newly added data */
895d420fb49SBartosz Golaszewski 	if (active)
896d420fb49SBartosz Golaszewski 		pending = port->tx_remaining;
897d420fb49SBartosz Golaszewski 	else
898d420fb49SBartosz Golaszewski 		pending = uart_circ_chars_pending(xmit);
899d420fb49SBartosz Golaszewski 
900d420fb49SBartosz Golaszewski 	/* All data has been transmitted and acknowledged as received */
901d420fb49SBartosz Golaszewski 	if (!pending && !status && done) {
9022aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_tx_fifo(uport);
903d420fb49SBartosz Golaszewski 		goto out_write_wakeup;
904d420fb49SBartosz Golaszewski 	}
905d420fb49SBartosz Golaszewski 
906d420fb49SBartosz Golaszewski 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
907d420fb49SBartosz Golaszewski 	avail *= BYTES_PER_FIFO_WORD;
908d420fb49SBartosz Golaszewski 
909d420fb49SBartosz Golaszewski 	chunk = min(avail, pending);
910d420fb49SBartosz Golaszewski 	if (!chunk)
911d420fb49SBartosz Golaszewski 		goto out_write_wakeup;
912d420fb49SBartosz Golaszewski 
913d420fb49SBartosz Golaszewski 	if (!port->tx_remaining) {
914d420fb49SBartosz Golaszewski 		qcom_geni_serial_setup_tx(uport, pending);
915d420fb49SBartosz Golaszewski 		port->tx_remaining = pending;
916d420fb49SBartosz Golaszewski 
917d420fb49SBartosz Golaszewski 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
918d420fb49SBartosz Golaszewski 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
919d420fb49SBartosz Golaszewski 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
920d420fb49SBartosz Golaszewski 					uport->membase + SE_GENI_M_IRQ_EN);
921d420fb49SBartosz Golaszewski 	}
922d420fb49SBartosz Golaszewski 
923bd795584SBartosz Golaszewski 	qcom_geni_serial_send_chunk_fifo(uport, chunk);
92464a42807SRyan Case 
92564a42807SRyan Case 	/*
92664a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
92764a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
92864a42807SRyan Case 	 * so we must clear it again here after our writes.
92964a42807SRyan Case 	 */
9309e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
93164a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
93264a42807SRyan Case 
933c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
93464a42807SRyan Case 	if (!port->tx_remaining) {
9359e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
93664a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
9379e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
93864a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
93964a42807SRyan Case 	}
94064a42807SRyan Case 
941638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
942c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
943c4f52879SKarthikeyan Ramasubramanian }
944c4f52879SKarthikeyan Ramasubramanian 
9452aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
9462aaa43c7SBartosz Golaszewski {
9472aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
9482aaa43c7SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
9492aaa43c7SBartosz Golaszewski 
9502aaa43c7SBartosz Golaszewski 	uart_xmit_advance(uport, port->tx_remaining);
9512aaa43c7SBartosz Golaszewski 	geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
9522aaa43c7SBartosz Golaszewski 	port->tx_dma_addr = 0;
9532aaa43c7SBartosz Golaszewski 	port->tx_remaining = 0;
9542aaa43c7SBartosz Golaszewski 
9552aaa43c7SBartosz Golaszewski 	if (!uart_circ_empty(xmit))
9562aaa43c7SBartosz Golaszewski 		qcom_geni_serial_start_tx_dma(uport);
9572aaa43c7SBartosz Golaszewski 
9582aaa43c7SBartosz Golaszewski 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
9592aaa43c7SBartosz Golaszewski 		uart_write_wakeup(uport);
9602aaa43c7SBartosz Golaszewski }
9612aaa43c7SBartosz Golaszewski 
962c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
963c4f52879SKarthikeyan Ramasubramanian {
964385298abSRyan Case 	u32 m_irq_en;
965385298abSRyan Case 	u32 m_irq_status;
966385298abSRyan Case 	u32 s_irq_status;
967385298abSRyan Case 	u32 geni_status;
9682aaa43c7SBartosz Golaszewski 	u32 dma;
9692aaa43c7SBartosz Golaszewski 	u32 dma_tx_status;
9702aaa43c7SBartosz Golaszewski 	u32 dma_rx_status;
971c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
972c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
973c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
97400ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
975c4f52879SKarthikeyan Ramasubramanian 
976c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
977ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
978c4f52879SKarthikeyan Ramasubramanian 
97975f4e830SJohan Hovold 	spin_lock(&uport->lock);
98075f4e830SJohan Hovold 
9819e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
9829e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
9832aaa43c7SBartosz Golaszewski 	dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
9842aaa43c7SBartosz Golaszewski 	dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
9859e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
9862aaa43c7SBartosz Golaszewski 	dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
9879e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
9889e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
9899e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
9902aaa43c7SBartosz Golaszewski 	writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
9912aaa43c7SBartosz Golaszewski 	writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
992c4f52879SKarthikeyan Ramasubramanian 
993c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
994c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
995c4f52879SKarthikeyan Ramasubramanian 
996c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
997c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
998c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
999c4f52879SKarthikeyan Ramasubramanian 	}
1000c4f52879SKarthikeyan Ramasubramanian 
1001fe6a00e8SBartosz Golaszewski 	if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
1002c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
1003c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
1004c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
1005fe6a00e8SBartosz Golaszewski 	} else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
1006c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
1007c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
1008c4f52879SKarthikeyan Ramasubramanian 	}
1009c4f52879SKarthikeyan Ramasubramanian 
10102aaa43c7SBartosz Golaszewski 	if (dma) {
10112aaa43c7SBartosz Golaszewski 		if (dma_tx_status & TX_DMA_DONE)
10122aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_tx_dma(uport);
10132aaa43c7SBartosz Golaszewski 
10142aaa43c7SBartosz Golaszewski 		if (dma_rx_status) {
10152aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_RESET_DONE)
10162aaa43c7SBartosz Golaszewski 				goto out_unlock;
10172aaa43c7SBartosz Golaszewski 
10182aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_DMA_PARITY_ERR) {
10192aaa43c7SBartosz Golaszewski 				uport->icount.parity++;
10202aaa43c7SBartosz Golaszewski 				drop_rx = true;
10212aaa43c7SBartosz Golaszewski 			}
10222aaa43c7SBartosz Golaszewski 
10232aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_DMA_BREAK)
10242aaa43c7SBartosz Golaszewski 				uport->icount.brk++;
10252aaa43c7SBartosz Golaszewski 
10262aaa43c7SBartosz Golaszewski 			if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
10272aaa43c7SBartosz Golaszewski 				qcom_geni_serial_handle_rx_dma(uport, drop_rx);
10282aaa43c7SBartosz Golaszewski 		}
10292aaa43c7SBartosz Golaszewski 	} else {
10302aaa43c7SBartosz Golaszewski 		if (m_irq_status & m_irq_en &
10312aaa43c7SBartosz Golaszewski 		    (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
10322aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_tx_fifo(uport,
10332aaa43c7SBartosz Golaszewski 					m_irq_status & M_CMD_DONE_EN,
10342aaa43c7SBartosz Golaszewski 					geni_status & M_GENI_CMD_ACTIVE);
10352aaa43c7SBartosz Golaszewski 
1036fe6a00e8SBartosz Golaszewski 		if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
10372aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
10382aaa43c7SBartosz Golaszewski 	}
1039c4f52879SKarthikeyan Ramasubramanian 
1040c4f52879SKarthikeyan Ramasubramanian out_unlock:
104175f4e830SJohan Hovold 	uart_unlock_and_check_sysrq(uport);
1042336447b3SDouglas Anderson 
1043c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
1044c4f52879SKarthikeyan Ramasubramanian }
1045c4f52879SKarthikeyan Ramasubramanian 
1046b8caf69aSKrzysztof Kozlowski static int setup_fifos(struct qcom_geni_serial_port *port)
1047c4f52879SKarthikeyan Ramasubramanian {
1048c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1049b8caf69aSKrzysztof Kozlowski 	u32 old_rx_fifo_depth = port->rx_fifo_depth;
1050c4f52879SKarthikeyan Ramasubramanian 
1051c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1052c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1053c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1054c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1055c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
1056c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1057b8caf69aSKrzysztof Kozlowski 
1058a3cf6b94SIlpo Järvinen 	if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
1059a3cf6b94SIlpo Järvinen 		port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
1060b8caf69aSKrzysztof Kozlowski 					     port->rx_fifo_depth * sizeof(u32),
1061b8caf69aSKrzysztof Kozlowski 					     GFP_KERNEL);
1062a3cf6b94SIlpo Järvinen 		if (!port->rx_buf)
1063b8caf69aSKrzysztof Kozlowski 			return -ENOMEM;
1064b8caf69aSKrzysztof Kozlowski 	}
1065b8caf69aSKrzysztof Kozlowski 
1066b8caf69aSKrzysztof Kozlowski 	return 0;
1067c4f52879SKarthikeyan Ramasubramanian }
1068c4f52879SKarthikeyan Ramasubramanian 
1069c4f52879SKarthikeyan Ramasubramanian 
1070c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
1071c4f52879SKarthikeyan Ramasubramanian {
10723e4aaea7SAkash Asthana 	disable_irq(uport->irq);
1073d8aca2f9SBartosz Golaszewski 	qcom_geni_serial_stop_tx(uport);
1074d8aca2f9SBartosz Golaszewski 	qcom_geni_serial_stop_rx(uport);
1075c4f52879SKarthikeyan Ramasubramanian }
1076c4f52879SKarthikeyan Ramasubramanian 
1077c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
1078c4f52879SKarthikeyan Ramasubramanian {
107900ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1080385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1081c362272bSDouglas Anderson 	u32 proto;
10829fa3c4b1SRoja Rani Yarubandi 	u32 pin_swap;
1083b8caf69aSKrzysztof Kozlowski 	int ret;
1084c362272bSDouglas Anderson 
1085c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
1086c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
1087c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1088c362272bSDouglas Anderson 		return -ENXIO;
1089c362272bSDouglas Anderson 	}
1090c362272bSDouglas Anderson 
1091c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
1092c362272bSDouglas Anderson 
1093b8caf69aSKrzysztof Kozlowski 	ret = setup_fifos(port);
1094b8caf69aSKrzysztof Kozlowski 	if (ret)
1095b8caf69aSKrzysztof Kozlowski 		return ret;
1096c4f52879SKarthikeyan Ramasubramanian 
10979e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
10989fa3c4b1SRoja Rani Yarubandi 
10999fa3c4b1SRoja Rani Yarubandi 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
11009fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap) {
11019fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
11029fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
11039fa3c4b1SRoja Rani Yarubandi 	}
11049fa3c4b1SRoja Rani Yarubandi 	if (port->cts_rts_swap) {
11059fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
11069fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO0_SEL;
11079fa3c4b1SRoja Rani Yarubandi 	}
11089fa3c4b1SRoja Rani Yarubandi 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
11099fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap || port->cts_rts_swap)
11109fa3c4b1SRoja Rani Yarubandi 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
11119fa3c4b1SRoja Rani Yarubandi 
1112c4f52879SKarthikeyan Ramasubramanian 	/*
1113c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
1114c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
1115c4f52879SKarthikeyan Ramasubramanian 	 */
11168a8a66a1SGirish Mahadevan 	if (uart_console(uport))
1117c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
1118650c8bd3SDouglas Anderson 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1119650c8bd3SDouglas Anderson 			       false, true, true);
1120a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
11212aaa43c7SBartosz Golaszewski 	geni_se_select_mode(&port->se, port->dev_data->mode);
112235781d83SAniket Randive 	qcom_geni_serial_start_rx(uport);
1123c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
1124c362272bSDouglas Anderson 
1125c4f52879SKarthikeyan Ramasubramanian 	return 0;
1126c4f52879SKarthikeyan Ramasubramanian }
1127c4f52879SKarthikeyan Ramasubramanian 
1128c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
1129c4f52879SKarthikeyan Ramasubramanian {
1130c4f52879SKarthikeyan Ramasubramanian 	int ret;
113100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1132c4f52879SKarthikeyan Ramasubramanian 
1133c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1134c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
1135c4f52879SKarthikeyan Ramasubramanian 		if (ret)
1136c4f52879SKarthikeyan Ramasubramanian 			return ret;
1137c4f52879SKarthikeyan Ramasubramanian 	}
11383e4aaea7SAkash Asthana 	enable_irq(uport->irq);
1139c4f52879SKarthikeyan Ramasubramanian 
11403e4aaea7SAkash Asthana 	return 0;
1141c4f52879SKarthikeyan Ramasubramanian }
1142c4f52879SKarthikeyan Ramasubramanian 
1143c474c775SVijaya Krishna Nivarthi static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
1144c474c775SVijaya Krishna Nivarthi 			unsigned int *clk_div, unsigned int percent_tol)
1145c474c775SVijaya Krishna Nivarthi {
1146c474c775SVijaya Krishna Nivarthi 	unsigned long freq;
1147c474c775SVijaya Krishna Nivarthi 	unsigned long div, maxdiv;
1148c474c775SVijaya Krishna Nivarthi 	u64 mult;
1149c474c775SVijaya Krishna Nivarthi 	unsigned long offset, abs_tol, achieved;
1150c474c775SVijaya Krishna Nivarthi 
1151c474c775SVijaya Krishna Nivarthi 	abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
1152c474c775SVijaya Krishna Nivarthi 	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
1153c474c775SVijaya Krishna Nivarthi 	div = 1;
1154c474c775SVijaya Krishna Nivarthi 	while (div <= maxdiv) {
1155c474c775SVijaya Krishna Nivarthi 		mult = (u64)div * desired_clk;
1156c474c775SVijaya Krishna Nivarthi 		if (mult != (unsigned long)mult)
1157c474c775SVijaya Krishna Nivarthi 			break;
1158c474c775SVijaya Krishna Nivarthi 
1159c474c775SVijaya Krishna Nivarthi 		offset = div * abs_tol;
1160c474c775SVijaya Krishna Nivarthi 		freq = clk_round_rate(clk, mult - offset);
1161c474c775SVijaya Krishna Nivarthi 
1162c474c775SVijaya Krishna Nivarthi 		/* Can only get lower if we're done */
1163c474c775SVijaya Krishna Nivarthi 		if (freq < mult - offset)
1164c474c775SVijaya Krishna Nivarthi 			break;
1165c474c775SVijaya Krishna Nivarthi 
1166c474c775SVijaya Krishna Nivarthi 		/*
1167c474c775SVijaya Krishna Nivarthi 		 * Re-calculate div in case rounding skipped rates but we
1168c474c775SVijaya Krishna Nivarthi 		 * ended up at a good one, then check for a match.
1169c474c775SVijaya Krishna Nivarthi 		 */
1170c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_CLOSEST(freq, desired_clk);
1171c474c775SVijaya Krishna Nivarthi 		achieved = DIV_ROUND_CLOSEST(freq, div);
1172c474c775SVijaya Krishna Nivarthi 		if (achieved <= desired_clk + abs_tol &&
1173c474c775SVijaya Krishna Nivarthi 		    achieved >= desired_clk - abs_tol) {
1174c474c775SVijaya Krishna Nivarthi 			*clk_div = div;
1175c474c775SVijaya Krishna Nivarthi 			return freq;
1176c474c775SVijaya Krishna Nivarthi 		}
1177c474c775SVijaya Krishna Nivarthi 
1178c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_UP(freq, desired_clk);
1179c474c775SVijaya Krishna Nivarthi 	}
1180c474c775SVijaya Krishna Nivarthi 
1181c474c775SVijaya Krishna Nivarthi 	return 0;
1182c474c775SVijaya Krishna Nivarthi }
1183c474c775SVijaya Krishna Nivarthi 
1184c2194bc9SVijaya Krishna Nivarthi static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1185ce734600SVivek Gautam 			unsigned int sampling_rate, unsigned int *clk_div)
1186c4f52879SKarthikeyan Ramasubramanian {
1187c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
1188c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
1189c4f52879SKarthikeyan Ramasubramanian 
1190ce734600SVivek Gautam 	desired_clk = baud * sampling_rate;
1191c474c775SVijaya Krishna Nivarthi 	if (!desired_clk)
1192c2194bc9SVijaya Krishna Nivarthi 		return 0;
1193c2194bc9SVijaya Krishna Nivarthi 
1194c474c775SVijaya Krishna Nivarthi 	/*
1195c474c775SVijaya Krishna Nivarthi 	 * try to find a clock rate within 2% tolerance, then within 5%
1196c474c775SVijaya Krishna Nivarthi 	 */
1197c474c775SVijaya Krishna Nivarthi 	ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1198c474c775SVijaya Krishna Nivarthi 	if (!ser_clk)
1199c474c775SVijaya Krishna Nivarthi 		ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1200c2194bc9SVijaya Krishna Nivarthi 
1201c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
1202c4f52879SKarthikeyan Ramasubramanian }
1203c4f52879SKarthikeyan Ramasubramanian 
1204c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
1205bec5b814SIlpo Järvinen 					 struct ktermios *termios,
1206bec5b814SIlpo Järvinen 					 const struct ktermios *old)
1207c4f52879SKarthikeyan Ramasubramanian {
1208c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
1209385298abSRyan Case 	u32 bits_per_char;
1210385298abSRyan Case 	u32 tx_trans_cfg;
1211385298abSRyan Case 	u32 tx_parity_cfg;
1212385298abSRyan Case 	u32 rx_trans_cfg;
1213385298abSRyan Case 	u32 rx_parity_cfg;
1214385298abSRyan Case 	u32 stop_bit_len;
1215c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
1216385298abSRyan Case 	u32 ser_clk_cfg;
121700ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1218c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
1219ce734600SVivek Gautam 	u32 ver, sampling_rate;
12207cf563b2SAkash Asthana 	unsigned int avg_bw_core;
1221c4f52879SKarthikeyan Ramasubramanian 
1222c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
1223c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
1224c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1225c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
1226ce734600SVivek Gautam 
1227ce734600SVivek Gautam 	sampling_rate = UART_OVERSAMPLING;
1228ce734600SVivek Gautam 	/* Sampling rate is halved for IP versions >= 2.5 */
1229ce734600SVivek Gautam 	ver = geni_se_get_qup_hw_version(&port->se);
1230c9ca43d4SParas Sharma 	if (ver >= QUP_SE_VERSION_2_5)
1231ce734600SVivek Gautam 		sampling_rate /= 2;
1232ce734600SVivek Gautam 
1233c2194bc9SVijaya Krishna Nivarthi 	clk_rate = get_clk_div_rate(port->se.clk, baud,
1234c2194bc9SVijaya Krishna Nivarthi 		sampling_rate, &clk_div);
1235c474c775SVijaya Krishna Nivarthi 	if (!clk_rate) {
1236c474c775SVijaya Krishna Nivarthi 		dev_err(port->se.dev,
12370fec5180SDouglas Anderson 			"Couldn't find suitable clock rate for %u\n",
1238c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate);
1239c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
1240c474c775SVijaya Krishna Nivarthi 	}
1241c474c775SVijaya Krishna Nivarthi 
12420fec5180SDouglas Anderson 	dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
1243c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate, clk_rate, clk_div);
1244c4f52879SKarthikeyan Ramasubramanian 
1245c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
1246a5819b54SRajendra Nayak 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1247c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
1248c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1249c4f52879SKarthikeyan Ramasubramanian 
12507cf563b2SAkash Asthana 	/*
12517cf563b2SAkash Asthana 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
12527cf563b2SAkash Asthana 	 * only.
12537cf563b2SAkash Asthana 	 */
12547cf563b2SAkash Asthana 	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
12557cf563b2SAkash Asthana 						: GENI_DEFAULT_BW;
12567cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
12577cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
12587cf563b2SAkash Asthana 	geni_icc_set_bw(&port->se);
12597cf563b2SAkash Asthana 
1260c4f52879SKarthikeyan Ramasubramanian 	/* parity */
12619e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
12629e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
12639e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
12649e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1265c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
1266c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
1267c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
1268c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
1269c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
1270c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
1271c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
1272c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
1273c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
1274c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
1275c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
1276c4f52879SKarthikeyan Ramasubramanian 		} else {
1277c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
1278c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
1279c4f52879SKarthikeyan Ramasubramanian 		}
1280c4f52879SKarthikeyan Ramasubramanian 	} else {
1281c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1282c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1283c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1284c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1285c4f52879SKarthikeyan Ramasubramanian 	}
1286c4f52879SKarthikeyan Ramasubramanian 
1287c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
12883ec2ff37SJiri Slaby 	bits_per_char = tty_get_char_size(termios->c_cflag);
1289c4f52879SKarthikeyan Ramasubramanian 
1290c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1291c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1292c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1293c4f52879SKarthikeyan Ramasubramanian 	else
1294c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1295c4f52879SKarthikeyan Ramasubramanian 
1296c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1297c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1298c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1299c4f52879SKarthikeyan Ramasubramanian 	else
1300c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1301c4f52879SKarthikeyan Ramasubramanian 
1302c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1303c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1304c4f52879SKarthikeyan Ramasubramanian 
13058a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
13069e06d55fSRyan Case 		writel(port->loopback,
13078a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
13089e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
13099e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
13109e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
13119e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
13129e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
13139e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
13149e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
13159e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
13169e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1317c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1318c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1319c4f52879SKarthikeyan Ramasubramanian }
1320c4f52879SKarthikeyan Ramasubramanian 
1321c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1322975efc66SJohn Stultz static int qcom_geni_console_setup(struct console *co, char *options)
1323c4f52879SKarthikeyan Ramasubramanian {
1324c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1325c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
13262ec812a0SDouglas Anderson 	int baud = 115200;
1327c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1328c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1329c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1330c362272bSDouglas Anderson 	int ret;
1331c4f52879SKarthikeyan Ramasubramanian 
1332c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1333c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1334c4f52879SKarthikeyan Ramasubramanian 
13358a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1336c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
13376a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1338c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1339c4f52879SKarthikeyan Ramasubramanian 	}
1340c4f52879SKarthikeyan Ramasubramanian 
1341c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1342c4f52879SKarthikeyan Ramasubramanian 
1343c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1344c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1345c4f52879SKarthikeyan Ramasubramanian 
1346c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1347c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1348c362272bSDouglas Anderson 		if (ret)
1349c362272bSDouglas Anderson 			return ret;
1350c4f52879SKarthikeyan Ramasubramanian 	}
1351c4f52879SKarthikeyan Ramasubramanian 
1352c4f52879SKarthikeyan Ramasubramanian 	if (options)
1353c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1354c4f52879SKarthikeyan Ramasubramanian 
1355c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1356c4f52879SKarthikeyan Ramasubramanian }
1357c4f52879SKarthikeyan Ramasubramanian 
135843f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
135943f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
136043f1831bSKarthikeyan Ramasubramanian {
136143f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
136243f1831bSKarthikeyan Ramasubramanian 
136343f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
136443f1831bSKarthikeyan Ramasubramanian }
136543f1831bSKarthikeyan Ramasubramanian 
1366205b5bddSDouglas Anderson #ifdef CONFIG_CONSOLE_POLL
1367205b5bddSDouglas Anderson static int qcom_geni_serial_earlycon_read(struct console *con,
1368205b5bddSDouglas Anderson 					  char *s, unsigned int n)
1369205b5bddSDouglas Anderson {
1370205b5bddSDouglas Anderson 	struct earlycon_device *dev = con->data;
1371205b5bddSDouglas Anderson 	struct uart_port *uport = &dev->port;
1372205b5bddSDouglas Anderson 	int num_read = 0;
1373205b5bddSDouglas Anderson 	int ch;
1374205b5bddSDouglas Anderson 
1375205b5bddSDouglas Anderson 	while (num_read < n) {
1376205b5bddSDouglas Anderson 		ch = qcom_geni_serial_get_char(uport);
1377205b5bddSDouglas Anderson 		if (ch == NO_POLL_CHAR)
1378205b5bddSDouglas Anderson 			break;
1379205b5bddSDouglas Anderson 		s[num_read++] = ch;
1380205b5bddSDouglas Anderson 	}
1381205b5bddSDouglas Anderson 
1382205b5bddSDouglas Anderson 	return num_read;
1383205b5bddSDouglas Anderson }
1384205b5bddSDouglas Anderson 
1385205b5bddSDouglas Anderson static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1386205b5bddSDouglas Anderson 						      struct console *con)
1387205b5bddSDouglas Anderson {
1388205b5bddSDouglas Anderson 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1389205b5bddSDouglas Anderson 	con->read = qcom_geni_serial_earlycon_read;
1390205b5bddSDouglas Anderson }
1391205b5bddSDouglas Anderson #else
1392205b5bddSDouglas Anderson static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1393205b5bddSDouglas Anderson 						      struct console *con) { }
1394205b5bddSDouglas Anderson #endif
1395205b5bddSDouglas Anderson 
1396e42d6c3eSDouglas Anderson static struct qcom_geni_private_data earlycon_private_data;
1397e42d6c3eSDouglas Anderson 
139843f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
139943f1831bSKarthikeyan Ramasubramanian 								const char *opt)
140043f1831bSKarthikeyan Ramasubramanian {
140143f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
140243f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
140343f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
140443f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
140543f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
140643f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
140743f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
140843f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
140943f1831bSKarthikeyan Ramasubramanian 
141043f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
141143f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
141243f1831bSKarthikeyan Ramasubramanian 
1413e42d6c3eSDouglas Anderson 	uport->private_data = &earlycon_private_data;
1414e42d6c3eSDouglas Anderson 
141543f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
141643f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
141743f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
141843f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
141943f1831bSKarthikeyan Ramasubramanian 	/*
142043f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
142143f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
142243f1831bSKarthikeyan Ramasubramanian 	 */
142343f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
142443f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
142543f1831bSKarthikeyan Ramasubramanian 
142643f1831bSKarthikeyan Ramasubramanian 	/*
142743f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
142843f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
142943f1831bSKarthikeyan Ramasubramanian 	 */
143043f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
143143f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
1432650c8bd3SDouglas Anderson 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1433650c8bd3SDouglas Anderson 			       false, true, true);
143443f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
143543f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
143643f1831bSKarthikeyan Ramasubramanian 
14379e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
14389e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
14399e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
14409e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
14419e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
14429e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
14439e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
144443f1831bSKarthikeyan Ramasubramanian 
144543f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
144643f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
1447205b5bddSDouglas Anderson 	qcom_geni_serial_enable_early_read(&se, dev->con);
1448205b5bddSDouglas Anderson 
144943f1831bSKarthikeyan Ramasubramanian 	return 0;
145043f1831bSKarthikeyan Ramasubramanian }
145143f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
145243f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
145343f1831bSKarthikeyan Ramasubramanian 
1454c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1455c4f52879SKarthikeyan Ramasubramanian {
1456c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1457c4f52879SKarthikeyan Ramasubramanian }
1458c4f52879SKarthikeyan Ramasubramanian 
1459c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1460c4f52879SKarthikeyan Ramasubramanian {
1461c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1462c4f52879SKarthikeyan Ramasubramanian }
1463c4f52879SKarthikeyan Ramasubramanian 
1464c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1465c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1466c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1467c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1468c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1469c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1470c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1471c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1472c4f52879SKarthikeyan Ramasubramanian };
1473c4f52879SKarthikeyan Ramasubramanian 
1474c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1475c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1476c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1477c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1478c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1479c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1480c4f52879SKarthikeyan Ramasubramanian };
1481c4f52879SKarthikeyan Ramasubramanian #else
1482c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1483c4f52879SKarthikeyan Ramasubramanian {
1484c4f52879SKarthikeyan Ramasubramanian 	return 0;
1485c4f52879SKarthikeyan Ramasubramanian }
1486c4f52879SKarthikeyan Ramasubramanian 
1487c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1488c4f52879SKarthikeyan Ramasubramanian {
1489c4f52879SKarthikeyan Ramasubramanian }
1490c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1491c4f52879SKarthikeyan Ramasubramanian 
14928a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
14938a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
14948a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
14958a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
14968a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
14978a8a66a1SGirish Mahadevan };
14988a8a66a1SGirish Mahadevan 
14998a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1500c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1501c4f52879SKarthikeyan Ramasubramanian {
150200ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1503c4f52879SKarthikeyan Ramasubramanian 
1504c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1505c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1506c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1507c362272bSDouglas Anderson 
15087cf563b2SAkash Asthana 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
15097cf563b2SAkash Asthana 		geni_icc_enable(&port->se);
1510c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
15117cf563b2SAkash Asthana 	} else if (new_state == UART_PM_STATE_OFF &&
15127cf563b2SAkash Asthana 			old_state == UART_PM_STATE_ON) {
1513c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
15147cf563b2SAkash Asthana 		geni_icc_disable(&port->se);
15157cf563b2SAkash Asthana 	}
1516c4f52879SKarthikeyan Ramasubramanian }
1517c4f52879SKarthikeyan Ramasubramanian 
1518c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1519c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
15202aaa43c7SBartosz Golaszewski 	.stop_tx = qcom_geni_serial_stop_tx_fifo,
15212aaa43c7SBartosz Golaszewski 	.start_tx = qcom_geni_serial_start_tx_fifo,
15222aaa43c7SBartosz Golaszewski 	.stop_rx = qcom_geni_serial_stop_rx_fifo,
15232aaa43c7SBartosz Golaszewski 	.start_rx = qcom_geni_serial_start_rx_fifo,
1524c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1525c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1526c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1527c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1528c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1529c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
15308a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
15318a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1532c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1533c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1534c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1535c4f52879SKarthikeyan Ramasubramanian #endif
15368a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
15378a8a66a1SGirish Mahadevan };
15388a8a66a1SGirish Mahadevan 
15398a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
15408a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
15412aaa43c7SBartosz Golaszewski 	.stop_tx = qcom_geni_serial_stop_tx_dma,
15422aaa43c7SBartosz Golaszewski 	.start_tx = qcom_geni_serial_start_tx_dma,
15432aaa43c7SBartosz Golaszewski 	.start_rx = qcom_geni_serial_start_rx_dma,
15442aaa43c7SBartosz Golaszewski 	.stop_rx = qcom_geni_serial_stop_rx_dma,
15458a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
15468a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
15478a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
15488a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
15498a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
15508a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
15518a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
15528a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
15538a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1554c4f52879SKarthikeyan Ramasubramanian };
1555c4f52879SKarthikeyan Ramasubramanian 
1556c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1557c4f52879SKarthikeyan Ramasubramanian {
1558c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
155971581242SColin Ian King 	int line;
1560c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1561c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1562c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1563066cd1c4SKarthikeyan Ramasubramanian 	int irq;
15648a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
156540ec6d41SBartosz Golaszewski 	const struct qcom_geni_device_data *data;
1566c4f52879SKarthikeyan Ramasubramanian 
156740ec6d41SBartosz Golaszewski 	data = of_device_get_match_data(&pdev->dev);
156840ec6d41SBartosz Golaszewski 	if (!data)
156940ec6d41SBartosz Golaszewski 		return -EINVAL;
15708a8a66a1SGirish Mahadevan 
157140ec6d41SBartosz Golaszewski 	if (data->console) {
15728a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1573c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
15748a8a66a1SGirish Mahadevan 	} else {
15758a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
157608b0adb1SDmitry Baryshkov 		line = of_alias_get_id(pdev->dev.of_node, "serial");
157708b0adb1SDmitry Baryshkov 		if (line == -ENODEV) /* compat with non-standard aliases */
15788a8a66a1SGirish Mahadevan 			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
15798a8a66a1SGirish Mahadevan 	}
1580c4f52879SKarthikeyan Ramasubramanian 
158140ec6d41SBartosz Golaszewski 	port = get_port_from_line(line, data->console);
1582c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
15836a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
15846a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1585c4f52879SKarthikeyan Ramasubramanian 	}
1586c4f52879SKarthikeyan Ramasubramanian 
1587c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1588c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1589c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1590c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1591c4f52879SKarthikeyan Ramasubramanian 
1592c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
159340ec6d41SBartosz Golaszewski 	port->dev_data = data;
1594c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1595c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1596c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1597c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1598c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1599c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1600c4f52879SKarthikeyan Ramasubramanian 		return ret;
1601c4f52879SKarthikeyan Ramasubramanian 	}
1602c4f52879SKarthikeyan Ramasubramanian 
1603c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16047693b331SWei Yongjun 	if (!res)
16057693b331SWei Yongjun 		return -EINVAL;
1606c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1607c4f52879SKarthikeyan Ramasubramanian 
1608c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1609c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1610c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1611c4f52879SKarthikeyan Ramasubramanian 
161240ec6d41SBartosz Golaszewski 	if (!data->console) {
16132aaa43c7SBartosz Golaszewski 		port->rx_buf = devm_kzalloc(uport->dev,
16142aaa43c7SBartosz Golaszewski 					    DMA_RX_BUF_SIZE, GFP_KERNEL);
16152aaa43c7SBartosz Golaszewski 		if (!port->rx_buf)
1616f9d690b6Ssatya priya 			return -ENOMEM;
1617f9d690b6Ssatya priya 	}
1618f9d690b6Ssatya priya 
16197cf563b2SAkash Asthana 	ret = geni_icc_get(&port->se, NULL);
16207cf563b2SAkash Asthana 	if (ret)
16217cf563b2SAkash Asthana 		return ret;
16227cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
16237cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
16247cf563b2SAkash Asthana 
16257cf563b2SAkash Asthana 	/* Set BW for register access */
16267cf563b2SAkash Asthana 	ret = geni_icc_set_bw(&port->se);
16277cf563b2SAkash Asthana 	if (ret)
16287cf563b2SAkash Asthana 		return ret;
16297cf563b2SAkash Asthana 
1630f3974413SAkash Asthana 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1631f3974413SAkash Asthana 			"qcom_geni_serial_%s%d",
1632f3974413SAkash Asthana 			uart_console(uport) ? "console" : "uart", uport->line);
1633f3974413SAkash Asthana 	if (!port->name)
1634f3974413SAkash Asthana 		return -ENOMEM;
1635f3974413SAkash Asthana 
1636066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
16371df21786SStephen Boyd 	if (irq < 0)
1638066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1639066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
16408f122698SDmitry Safonov 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1641c4f52879SKarthikeyan Ramasubramanian 
164240ec6d41SBartosz Golaszewski 	if (!data->console)
1643f3974413SAkash Asthana 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
16443e4aaea7SAkash Asthana 
16459fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
16469fa3c4b1SRoja Rani Yarubandi 		port->rx_tx_swap = true;
16479fa3c4b1SRoja Rani Yarubandi 
16489fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
16499fa3c4b1SRoja Rani Yarubandi 		port->cts_rts_swap = true;
16509fa3c4b1SRoja Rani Yarubandi 
1651300894a6SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1652300894a6SYangtao Li 	if (ret)
1653300894a6SYangtao Li 		return ret;
1654a5819b54SRajendra Nayak 	/* OPP table is optional */
1655300894a6SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
1656c7ac46daSViresh Kumar 	if (ret && ret != -ENODEV) {
1657a5819b54SRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1658300894a6SYangtao Li 		return ret;
1659a5819b54SRajendra Nayak 	}
1660a5819b54SRajendra Nayak 
1661e42d6c3eSDouglas Anderson 	port->private_data.drv = drv;
1662e42d6c3eSDouglas Anderson 	uport->private_data = &port->private_data;
1663c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
1664f3974413SAkash Asthana 
1665f3974413SAkash Asthana 	ret = uart_add_one_port(drv, uport);
1666f3974413SAkash Asthana 	if (ret)
1667300894a6SYangtao Li 		return ret;
1668f3974413SAkash Asthana 
1669f3974413SAkash Asthana 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1670f3974413SAkash Asthana 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1671f3974413SAkash Asthana 			IRQF_TRIGGER_HIGH, port->name, uport);
1672f3974413SAkash Asthana 	if (ret) {
1673f3974413SAkash Asthana 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1674f3974413SAkash Asthana 		uart_remove_one_port(drv, uport);
1675300894a6SYangtao Li 		return ret;
1676f3974413SAkash Asthana 	}
1677f3974413SAkash Asthana 
1678f3974413SAkash Asthana 	/*
1679f3974413SAkash Asthana 	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1680f3974413SAkash Asthana 	 * enabled/disabled from dev_pm_arm_wake_irq during system
1681f3974413SAkash Asthana 	 * suspend/resume respectively.
1682f3974413SAkash Asthana 	 */
1683f3974413SAkash Asthana 	pm_runtime_set_active(&pdev->dev);
1684f3974413SAkash Asthana 
1685f3974413SAkash Asthana 	if (port->wakeup_irq > 0) {
1686f3974413SAkash Asthana 		device_init_wakeup(&pdev->dev, true);
1687f3974413SAkash Asthana 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1688f3974413SAkash Asthana 						port->wakeup_irq);
1689f3974413SAkash Asthana 		if (ret) {
1690f3974413SAkash Asthana 			device_init_wakeup(&pdev->dev, false);
1691f3974413SAkash Asthana 			uart_remove_one_port(drv, uport);
1692300894a6SYangtao Li 			return ret;
1693f3974413SAkash Asthana 		}
1694f3974413SAkash Asthana 	}
1695f3974413SAkash Asthana 
1696f3974413SAkash Asthana 	return 0;
1697c4f52879SKarthikeyan Ramasubramanian }
1698c4f52879SKarthikeyan Ramasubramanian 
1699c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1700c4f52879SKarthikeyan Ramasubramanian {
1701c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1702e42d6c3eSDouglas Anderson 	struct uart_driver *drv = port->private_data.drv;
1703c4f52879SKarthikeyan Ramasubramanian 
1704f3974413SAkash Asthana 	dev_pm_clear_wake_irq(&pdev->dev);
1705f3974413SAkash Asthana 	device_init_wakeup(&pdev->dev, false);
1706c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1707f3974413SAkash Asthana 
1708c4f52879SKarthikeyan Ramasubramanian 	return 0;
1709c4f52879SKarthikeyan Ramasubramanian }
1710c4f52879SKarthikeyan Ramasubramanian 
17115342ab0aSArnd Bergmann static int qcom_geni_serial_sys_suspend(struct device *dev)
1712c4f52879SKarthikeyan Ramasubramanian {
1713a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1714c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1715e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1716c4f52879SKarthikeyan Ramasubramanian 
17174a3107f6SRajendra Nayak 	/*
17184a3107f6SRajendra Nayak 	 * This is done so we can hit the lowest possible state in suspend
17194a3107f6SRajendra Nayak 	 * even with no_console_suspend
17204a3107f6SRajendra Nayak 	 */
17214a3107f6SRajendra Nayak 	if (uart_console(uport)) {
1722408e532eSVijaya Krishna Nivarthi 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
17234a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
17244a3107f6SRajendra Nayak 	}
1725e42d6c3eSDouglas Anderson 	return uart_suspend_port(private_data->drv, uport);
17268a8a66a1SGirish Mahadevan }
17278a8a66a1SGirish Mahadevan 
17285342ab0aSArnd Bergmann static int qcom_geni_serial_sys_resume(struct device *dev)
1729c4f52879SKarthikeyan Ramasubramanian {
17304a3107f6SRajendra Nayak 	int ret;
1731a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1732c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1733e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1734c4f52879SKarthikeyan Ramasubramanian 
17354a3107f6SRajendra Nayak 	ret = uart_resume_port(private_data->drv, uport);
17364a3107f6SRajendra Nayak 	if (uart_console(uport)) {
1737408e532eSVijaya Krishna Nivarthi 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
17384a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
17394a3107f6SRajendra Nayak 	}
17404a3107f6SRajendra Nayak 	return ret;
1741c4f52879SKarthikeyan Ramasubramanian }
1742c4f52879SKarthikeyan Ramasubramanian 
174335781d83SAniket Randive static int qcom_geni_serial_sys_hib_resume(struct device *dev)
174435781d83SAniket Randive {
174535781d83SAniket Randive 	int ret = 0;
174635781d83SAniket Randive 	struct uart_port *uport;
174735781d83SAniket Randive 	struct qcom_geni_private_data *private_data;
174835781d83SAniket Randive 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
174935781d83SAniket Randive 
175035781d83SAniket Randive 	uport = &port->uport;
175135781d83SAniket Randive 	private_data = uport->private_data;
175235781d83SAniket Randive 
175335781d83SAniket Randive 	if (uart_console(uport)) {
175435781d83SAniket Randive 		geni_icc_set_tag(&port->se, 0x7);
175535781d83SAniket Randive 		geni_icc_set_bw(&port->se);
175635781d83SAniket Randive 		ret = uart_resume_port(private_data->drv, uport);
175735781d83SAniket Randive 		/*
175835781d83SAniket Randive 		 * For hibernation usecase clients for
175935781d83SAniket Randive 		 * console UART won't call port setup during restore,
176035781d83SAniket Randive 		 * hence call port setup for console uart.
176135781d83SAniket Randive 		 */
176235781d83SAniket Randive 		qcom_geni_serial_port_setup(uport);
176335781d83SAniket Randive 	} else {
176435781d83SAniket Randive 		/*
176535781d83SAniket Randive 		 * Peripheral register settings are lost during hibernation.
176635781d83SAniket Randive 		 * Update setup flag such that port setup happens again
176735781d83SAniket Randive 		 * during next session. Clients of HS-UART will close and
176835781d83SAniket Randive 		 * open the port during hibernation.
176935781d83SAniket Randive 		 */
177035781d83SAniket Randive 		port->setup = false;
177135781d83SAniket Randive 	}
177235781d83SAniket Randive 	return ret;
177335781d83SAniket Randive }
177435781d83SAniket Randive 
177540ec6d41SBartosz Golaszewski static const struct qcom_geni_device_data qcom_geni_console_data = {
177640ec6d41SBartosz Golaszewski 	.console = true,
17772aaa43c7SBartosz Golaszewski 	.mode = GENI_SE_FIFO,
177840ec6d41SBartosz Golaszewski };
177940ec6d41SBartosz Golaszewski 
178040ec6d41SBartosz Golaszewski static const struct qcom_geni_device_data qcom_geni_uart_data = {
178140ec6d41SBartosz Golaszewski 	.console = false,
17822aaa43c7SBartosz Golaszewski 	.mode = GENI_SE_DMA,
178340ec6d41SBartosz Golaszewski };
178440ec6d41SBartosz Golaszewski 
1785c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
17865342ab0aSArnd Bergmann 	.suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17875342ab0aSArnd Bergmann 	.resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
17885342ab0aSArnd Bergmann 	.freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17895342ab0aSArnd Bergmann 	.poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17905342ab0aSArnd Bergmann 	.restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
17915342ab0aSArnd Bergmann 	.thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1792c4f52879SKarthikeyan Ramasubramanian };
1793c4f52879SKarthikeyan Ramasubramanian 
1794c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
179540ec6d41SBartosz Golaszewski 	{
179640ec6d41SBartosz Golaszewski 		.compatible = "qcom,geni-debug-uart",
179740ec6d41SBartosz Golaszewski 		.data = &qcom_geni_console_data,
179840ec6d41SBartosz Golaszewski 	},
179940ec6d41SBartosz Golaszewski 	{
180040ec6d41SBartosz Golaszewski 		.compatible = "qcom,geni-uart",
180140ec6d41SBartosz Golaszewski 		.data = &qcom_geni_uart_data,
180240ec6d41SBartosz Golaszewski 	},
1803c4f52879SKarthikeyan Ramasubramanian 	{}
1804c4f52879SKarthikeyan Ramasubramanian };
1805c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1806c4f52879SKarthikeyan Ramasubramanian 
1807c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1808c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1809c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1810c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1811c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1812c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1813c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1814c4f52879SKarthikeyan Ramasubramanian 	},
1815c4f52879SKarthikeyan Ramasubramanian };
1816c4f52879SKarthikeyan Ramasubramanian 
1817c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1818c4f52879SKarthikeyan Ramasubramanian {
1819c4f52879SKarthikeyan Ramasubramanian 	int ret;
1820c4f52879SKarthikeyan Ramasubramanian 
1821c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1822c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1823c4f52879SKarthikeyan Ramasubramanian 		return ret;
1824c4f52879SKarthikeyan Ramasubramanian 
18258a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
18268a8a66a1SGirish Mahadevan 	if (ret) {
1827c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1828c4f52879SKarthikeyan Ramasubramanian 		return ret;
1829c4f52879SKarthikeyan Ramasubramanian 	}
18308a8a66a1SGirish Mahadevan 
18318a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
18328a8a66a1SGirish Mahadevan 	if (ret) {
18338a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
18348a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
18358a8a66a1SGirish Mahadevan 	}
18368a8a66a1SGirish Mahadevan 	return ret;
18378a8a66a1SGirish Mahadevan }
1838c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1839c4f52879SKarthikeyan Ramasubramanian 
1840c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1841c4f52879SKarthikeyan Ramasubramanian {
1842c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1843c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
18448a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1845c4f52879SKarthikeyan Ramasubramanian }
1846c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1847c4f52879SKarthikeyan Ramasubramanian 
1848c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1849c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1850