xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision d8851a96ba25d5fa2a3d89550f8333db5b694919)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
460457d5eSSai Prakash Ranjan /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
560457d5eSSai Prakash Ranjan #define __DISABLE_TRACE_MMIO__
660457d5eSSai Prakash Ranjan 
7c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
8c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
113e4aaea7SAkash Asthana #include <linux/irq.h>
12c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
14c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
15a5819b54SRajendra Nayak #include <linux/pm_opp.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
17f3974413SAkash Asthana #include <linux/pm_runtime.h>
188b7103f3SAkash Asthana #include <linux/pm_wakeirq.h>
19491581f4SElliot Berman #include <linux/soc/qcom/geni-se.h>
20c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
21c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
22c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
23c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
24c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
25408e532eSVijaya Krishna Nivarthi #include <dt-bindings/interconnect/qcom,icc.h>
26c4f52879SKarthikeyan Ramasubramanian 
27c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
288a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
299fa3c4b1SRoja Rani Yarubandi #define SE_UART_IO_MACRO_CTRL		0x240
30c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
31c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
32c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
33c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
34c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
35c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
36c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
37c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
38c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
398a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
40c4f52879SKarthikeyan Ramasubramanian 
41c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
42c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN			BIT(0)
43c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK			BIT(1)
44c4f52879SKarthikeyan Ramasubramanian 
45c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
46c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1		0
47c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2		2
48c4f52879SKarthikeyan Ramasubramanian 
49c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
50c4f52879SKarthikeyan Ramasubramanian #define UART_RX_PAR_EN			BIT(3)
51c4f52879SKarthikeyan Ramasubramanian 
52c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_WORD_LEN */
53c4f52879SKarthikeyan Ramasubramanian #define RX_WORD_LEN_MASK		GENMASK(9, 0)
54c4f52879SKarthikeyan Ramasubramanian 
55c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_STALE_CNT */
56c4f52879SKarthikeyan Ramasubramanian #define RX_STALE_CNT			GENMASK(23, 0)
57c4f52879SKarthikeyan Ramasubramanian 
58c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
59c4f52879SKarthikeyan Ramasubramanian #define PAR_CALC_EN			BIT(0)
60c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN			0x00
61c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD				0x01
62c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE			0x10
63c4f52879SKarthikeyan Ramasubramanian 
648a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
658a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN		BIT(31)
668a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY		BIT(1)
678a8a66a1SGirish Mahadevan #define UART_RFR_READY			BIT(0)
688a8a66a1SGirish Mahadevan 
69c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
70c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX			0x1
71c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
72c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ			0x1
732aaa43c7SBartosz Golaszewski #define UART_PARAM			0x1
742aaa43c7SBartosz Golaszewski #define UART_PARAM_RFR_OPEN		BIT(7)
75c4f52879SKarthikeyan Ramasubramanian 
76c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING		32
77c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT			16
78c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR		10
79c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS		1
808a8a66a1SGirish Mahadevan #define GENI_UART_PORTS			3
81c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS		16
82c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM			2
83c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS		32
84a85fb9ceSRyan Case #define UART_RX_WM			2
8569bd1a4fSAkash Asthana 
8669bd1a4fSAkash Asthana /* SE_UART_LOOPBACK_CFG */
8769bd1a4fSAkash Asthana #define RX_TX_SORTED			BIT(0)
8869bd1a4fSAkash Asthana #define CTS_RTS_SORTED			BIT(1)
8969bd1a4fSAkash Asthana #define RX_TX_CTS_RTS_SORTED		(RX_TX_SORTED | CTS_RTS_SORTED)
90c4f52879SKarthikeyan Ramasubramanian 
919fa3c4b1SRoja Rani Yarubandi /* UART pin swap value */
929fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO0_IO1_MASK	GENMASK(3, 0)
939fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO0_SEL		0x3
949fa3c4b1SRoja Rani Yarubandi #define DEFAULT_IO_MACRO_IO2_IO3_MASK	GENMASK(15, 4)
959fa3c4b1SRoja Rani Yarubandi #define IO_MACRO_IO2_IO3_SWAP		0x4640
969fa3c4b1SRoja Rani Yarubandi 
97650c8bd3SDouglas Anderson /* We always configure 4 bytes per FIFO word */
98bd795584SBartosz Golaszewski #define BYTES_PER_FIFO_WORD		4U
99650c8bd3SDouglas Anderson 
1002aaa43c7SBartosz Golaszewski #define DMA_RX_BUF_SIZE		2048
1012aaa43c7SBartosz Golaszewski 
10240ec6d41SBartosz Golaszewski struct qcom_geni_device_data {
10340ec6d41SBartosz Golaszewski 	bool console;
1042aaa43c7SBartosz Golaszewski 	enum geni_se_xfer_mode mode;
10540ec6d41SBartosz Golaszewski };
10640ec6d41SBartosz Golaszewski 
107e42d6c3eSDouglas Anderson struct qcom_geni_private_data {
108e42d6c3eSDouglas Anderson 	/* NOTE: earlycon port will have NULL here */
109e42d6c3eSDouglas Anderson 	struct uart_driver *drv;
110e42d6c3eSDouglas Anderson 
111e42d6c3eSDouglas Anderson 	u32 poll_cached_bytes;
112e42d6c3eSDouglas Anderson 	unsigned int poll_cached_bytes_cnt;
113650c8bd3SDouglas Anderson 
114650c8bd3SDouglas Anderson 	u32 write_cached_bytes;
115650c8bd3SDouglas Anderson 	unsigned int write_cached_bytes_cnt;
116e42d6c3eSDouglas Anderson };
117c4f52879SKarthikeyan Ramasubramanian 
118c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
119c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
120c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
121f3974413SAkash Asthana 	const char *name;
122c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
123c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
124c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
1252aaa43c7SBartosz Golaszewski 	dma_addr_t tx_dma_addr;
1262aaa43c7SBartosz Golaszewski 	dma_addr_t rx_dma_addr;
127c4f52879SKarthikeyan Ramasubramanian 	bool setup;
128c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
1292aaa43c7SBartosz Golaszewski 	void *rx_buf;
1308a8a66a1SGirish Mahadevan 	u32 loopback;
131c4f52879SKarthikeyan Ramasubramanian 	bool brk;
132a1fee899SRyan Case 
133a1fee899SRyan Case 	unsigned int tx_remaining;
1348b7103f3SAkash Asthana 	int wakeup_irq;
1359fa3c4b1SRoja Rani Yarubandi 	bool rx_tx_swap;
1369fa3c4b1SRoja Rani Yarubandi 	bool cts_rts_swap;
137e42d6c3eSDouglas Anderson 
138e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data private_data;
13940ec6d41SBartosz Golaszewski 	const struct qcom_geni_device_data *dev_data;
140c4f52879SKarthikeyan Ramasubramanian };
141c4f52879SKarthikeyan Ramasubramanian 
142f7371750SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops;
1438a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops;
144c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver;
1458a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver;
146c4f52879SKarthikeyan Ramasubramanian 
14700ce7c6eSBartosz Golaszewski static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
14800ce7c6eSBartosz Golaszewski {
14900ce7c6eSBartosz Golaszewski 	return container_of(uport, struct qcom_geni_serial_port, uport);
15000ce7c6eSBartosz Golaszewski }
151c4f52879SKarthikeyan Ramasubramanian 
1528a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1538a8a66a1SGirish Mahadevan 	[0] = {
1548a8a66a1SGirish Mahadevan 		.uport = {
1558a8a66a1SGirish Mahadevan 			.iotype = UPIO_MEM,
1568a8a66a1SGirish Mahadevan 			.ops = &qcom_geni_uart_pops,
1578a8a66a1SGirish Mahadevan 			.flags = UPF_BOOT_AUTOCONF,
1588a8a66a1SGirish Mahadevan 			.line = 0,
1598a8a66a1SGirish Mahadevan 		},
1608a8a66a1SGirish Mahadevan 	},
1618a8a66a1SGirish Mahadevan 	[1] = {
1628a8a66a1SGirish Mahadevan 		.uport = {
1638a8a66a1SGirish Mahadevan 			.iotype = UPIO_MEM,
1648a8a66a1SGirish Mahadevan 			.ops = &qcom_geni_uart_pops,
1658a8a66a1SGirish Mahadevan 			.flags = UPF_BOOT_AUTOCONF,
1668a8a66a1SGirish Mahadevan 			.line = 1,
1678a8a66a1SGirish Mahadevan 		},
1688a8a66a1SGirish Mahadevan 	},
1698a8a66a1SGirish Mahadevan 	[2] = {
1708a8a66a1SGirish Mahadevan 		.uport = {
1718a8a66a1SGirish Mahadevan 			.iotype = UPIO_MEM,
1728a8a66a1SGirish Mahadevan 			.ops = &qcom_geni_uart_pops,
1738a8a66a1SGirish Mahadevan 			.flags = UPF_BOOT_AUTOCONF,
1748a8a66a1SGirish Mahadevan 			.line = 2,
1758a8a66a1SGirish Mahadevan 		},
1768a8a66a1SGirish Mahadevan 	},
1778a8a66a1SGirish Mahadevan };
1788a8a66a1SGirish Mahadevan 
179f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
180f7371750SKarthikeyan Ramasubramanian 	.uport = {
181f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
182f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
183f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
184f7371750SKarthikeyan Ramasubramanian 		.line = 0,
185f7371750SKarthikeyan Ramasubramanian 	},
186f7371750SKarthikeyan Ramasubramanian };
187c4f52879SKarthikeyan Ramasubramanian 
188c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
189c4f52879SKarthikeyan Ramasubramanian {
190c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
19100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
192c4f52879SKarthikeyan Ramasubramanian 
19344e60d52SYueHaibing 	uport->membase = devm_platform_ioremap_resource(pdev, 0);
194c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
195c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
196c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
197c4f52879SKarthikeyan Ramasubramanian 	return 0;
198c4f52879SKarthikeyan Ramasubramanian }
199c4f52879SKarthikeyan Ramasubramanian 
200c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
201c4f52879SKarthikeyan Ramasubramanian {
202c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
203c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
204c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
205c4f52879SKarthikeyan Ramasubramanian 	}
206c4f52879SKarthikeyan Ramasubramanian }
207c4f52879SKarthikeyan Ramasubramanian 
2088a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
209c4f52879SKarthikeyan Ramasubramanian {
2108a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2118a8a66a1SGirish Mahadevan 	u32 geni_ios;
2128a8a66a1SGirish Mahadevan 
213e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport)) {
2148a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2158a8a66a1SGirish Mahadevan 	} else {
2169e06d55fSRyan Case 		geni_ios = readl(uport->membase + SE_GENI_IOS);
2178a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2188a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
219c4f52879SKarthikeyan Ramasubramanian 	}
220c4f52879SKarthikeyan Ramasubramanian 
2218a8a66a1SGirish Mahadevan 	return mctrl;
2228a8a66a1SGirish Mahadevan }
2238a8a66a1SGirish Mahadevan 
2248a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
225c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
226c4f52879SKarthikeyan Ramasubramanian {
2278a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
22800ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
2298a8a66a1SGirish Mahadevan 
230e8a6ca80SMatthias Kaehlcke 	if (uart_console(uport))
2318a8a66a1SGirish Mahadevan 		return;
2328a8a66a1SGirish Mahadevan 
23369bd1a4fSAkash Asthana 	if (mctrl & TIOCM_LOOP)
23469bd1a4fSAkash Asthana 		port->loopback = RX_TX_CTS_RTS_SORTED;
23569bd1a4fSAkash Asthana 
236a4ced376Ssatya priya 	if (!(mctrl & TIOCM_RTS) && !uport->suspended)
2378a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2389e06d55fSRyan Case 	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
239c4f52879SKarthikeyan Ramasubramanian }
240c4f52879SKarthikeyan Ramasubramanian 
241c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
242c4f52879SKarthikeyan Ramasubramanian {
243c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
244c4f52879SKarthikeyan Ramasubramanian }
245c4f52879SKarthikeyan Ramasubramanian 
2468a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
247c4f52879SKarthikeyan Ramasubramanian {
2488a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2498a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2508a8a66a1SGirish Mahadevan 
2518a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
252c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2538a8a66a1SGirish Mahadevan 
2548a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2558a8a66a1SGirish Mahadevan 	return port;
256c4f52879SKarthikeyan Ramasubramanian }
257c4f52879SKarthikeyan Ramasubramanian 
2582aaa43c7SBartosz Golaszewski static bool qcom_geni_serial_main_active(struct uart_port *uport)
2592aaa43c7SBartosz Golaszewski {
2602aaa43c7SBartosz Golaszewski 	return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
2612aaa43c7SBartosz Golaszewski }
2622aaa43c7SBartosz Golaszewski 
2632aaa43c7SBartosz Golaszewski static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
2642aaa43c7SBartosz Golaszewski {
2652aaa43c7SBartosz Golaszewski 	return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
2662aaa43c7SBartosz Golaszewski }
2672aaa43c7SBartosz Golaszewski 
268c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
269c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
270c4f52879SKarthikeyan Ramasubramanian {
271c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
272c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
273c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
274c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
275c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
276e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
277c4f52879SKarthikeyan Ramasubramanian 
278e42d6c3eSDouglas Anderson 	if (private_data->drv) {
27900ce7c6eSBartosz Golaszewski 		port = to_dev_port(uport);
280c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
281c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
282c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
283c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
284c4f52879SKarthikeyan Ramasubramanian 		/*
285c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
286c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
287c4f52879SKarthikeyan Ramasubramanian 		 */
288c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
289c4f52879SKarthikeyan Ramasubramanian 	}
290c4f52879SKarthikeyan Ramasubramanian 
29143f1831bSKarthikeyan Ramasubramanian 	/*
29243f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
29343f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29443f1831bSKarthikeyan Ramasubramanian 	 */
29543f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29643f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
2979e06d55fSRyan Case 		reg = readl(uport->membase + offset);
29843f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29943f1831bSKarthikeyan Ramasubramanian 			return true;
30043f1831bSKarthikeyan Ramasubramanian 		udelay(10);
30143f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
30243f1831bSKarthikeyan Ramasubramanian 	}
30343f1831bSKarthikeyan Ramasubramanian 	return false;
304c4f52879SKarthikeyan Ramasubramanian }
305c4f52879SKarthikeyan Ramasubramanian 
306c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
307c4f52879SKarthikeyan Ramasubramanian {
308c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
309c4f52879SKarthikeyan Ramasubramanian 
3109e06d55fSRyan Case 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
311c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
312c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
313c4f52879SKarthikeyan Ramasubramanian }
314c4f52879SKarthikeyan Ramasubramanian 
315c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
316c4f52879SKarthikeyan Ramasubramanian {
317c4f52879SKarthikeyan Ramasubramanian 	int done;
318c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
319c4f52879SKarthikeyan Ramasubramanian 
320c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
321c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
322c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
3239e06d55fSRyan Case 		writel(M_GENI_CMD_ABORT, uport->membase +
324c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
325c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
326c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
327c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
328c4f52879SKarthikeyan Ramasubramanian 	}
3299e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
330c4f52879SKarthikeyan Ramasubramanian }
331c4f52879SKarthikeyan Ramasubramanian 
332c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
333c4f52879SKarthikeyan Ramasubramanian {
334c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
335c4f52879SKarthikeyan Ramasubramanian 
336c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
337c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
338c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
3399e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
3409e06d55fSRyan Case 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
341c4f52879SKarthikeyan Ramasubramanian }
342c4f52879SKarthikeyan Ramasubramanian 
343c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
344c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
345c4f52879SKarthikeyan Ramasubramanian {
346e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
347c4f52879SKarthikeyan Ramasubramanian 	u32 status;
348e42d6c3eSDouglas Anderson 	u32 word_cnt;
349e42d6c3eSDouglas Anderson 	int ret;
350c4f52879SKarthikeyan Ramasubramanian 
351e42d6c3eSDouglas Anderson 	if (!private_data->poll_cached_bytes_cnt) {
3529e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
3539e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
354c4f52879SKarthikeyan Ramasubramanian 
3559e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
3569e06d55fSRyan Case 		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
357c4f52879SKarthikeyan Ramasubramanian 
3589e06d55fSRyan Case 		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
359e42d6c3eSDouglas Anderson 		word_cnt = status & RX_FIFO_WC_MSK;
360e42d6c3eSDouglas Anderson 		if (!word_cnt)
361c4f52879SKarthikeyan Ramasubramanian 			return NO_POLL_CHAR;
362c4f52879SKarthikeyan Ramasubramanian 
363e42d6c3eSDouglas Anderson 		if (word_cnt == 1 && (status & RX_LAST))
364d681a6e4SDouglas Anderson 			/*
365d681a6e4SDouglas Anderson 			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
366d681a6e4SDouglas Anderson 			 * treated as if it was BYTES_PER_FIFO_WORD.
367d681a6e4SDouglas Anderson 			 */
368e42d6c3eSDouglas Anderson 			private_data->poll_cached_bytes_cnt =
369e42d6c3eSDouglas Anderson 				(status & RX_LAST_BYTE_VALID_MSK) >>
370e42d6c3eSDouglas Anderson 				RX_LAST_BYTE_VALID_SHFT;
371d681a6e4SDouglas Anderson 
372d681a6e4SDouglas Anderson 		if (private_data->poll_cached_bytes_cnt == 0)
373d681a6e4SDouglas Anderson 			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
374e42d6c3eSDouglas Anderson 
375e42d6c3eSDouglas Anderson 		private_data->poll_cached_bytes =
376e42d6c3eSDouglas Anderson 			readl(uport->membase + SE_GENI_RX_FIFOn);
377e42d6c3eSDouglas Anderson 	}
378e42d6c3eSDouglas Anderson 
379e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes_cnt--;
380e42d6c3eSDouglas Anderson 	ret = private_data->poll_cached_bytes & 0xff;
381e42d6c3eSDouglas Anderson 	private_data->poll_cached_bytes >>= 8;
382e42d6c3eSDouglas Anderson 
383e42d6c3eSDouglas Anderson 	return ret;
384c4f52879SKarthikeyan Ramasubramanian }
385c4f52879SKarthikeyan Ramasubramanian 
386c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
387c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
388c4f52879SKarthikeyan Ramasubramanian {
389a85fb9ceSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
390c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
391c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
392c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
3939e06d55fSRyan Case 	writel(c, uport->membase + SE_GENI_TX_FIFOn);
3949e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
395c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
396c4f52879SKarthikeyan Ramasubramanian }
397c4f52879SKarthikeyan Ramasubramanian #endif
398c4f52879SKarthikeyan Ramasubramanian 
399c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
4003f8bab17SJiri Slaby static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
401c4f52879SKarthikeyan Ramasubramanian {
402650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
403650c8bd3SDouglas Anderson 
404650c8bd3SDouglas Anderson 	private_data->write_cached_bytes =
405650c8bd3SDouglas Anderson 		(private_data->write_cached_bytes >> 8) | (ch << 24);
406650c8bd3SDouglas Anderson 	private_data->write_cached_bytes_cnt++;
407650c8bd3SDouglas Anderson 
408650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
409650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
410650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
411650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
412650c8bd3SDouglas Anderson 	}
413c4f52879SKarthikeyan Ramasubramanian }
414c4f52879SKarthikeyan Ramasubramanian 
415c4f52879SKarthikeyan Ramasubramanian static void
416c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
417c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
418c4f52879SKarthikeyan Ramasubramanian {
419650c8bd3SDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
420650c8bd3SDouglas Anderson 
421c4f52879SKarthikeyan Ramasubramanian 	int i;
422c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
423c4f52879SKarthikeyan Ramasubramanian 
424c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
425f0262568SKarthikeyan Ramasubramanian 		/*
426f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
427f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
428f0262568SKarthikeyan Ramasubramanian 		 */
429c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
430c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
431c4f52879SKarthikeyan Ramasubramanian 	}
432c4f52879SKarthikeyan Ramasubramanian 
4339e06d55fSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
434c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
435c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
436c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
437c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
438c4f52879SKarthikeyan Ramasubramanian 
439c4f52879SKarthikeyan Ramasubramanian 		/*
440c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
441c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
442c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
443c4f52879SKarthikeyan Ramasubramanian 		 * lost.
444c4f52879SKarthikeyan Ramasubramanian 		 */
445c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
446c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
447c4f52879SKarthikeyan Ramasubramanian 			break;
4486a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
449c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
450c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
4519e06d55fSRyan Case 		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
452c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
453c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
454c4f52879SKarthikeyan Ramasubramanian 	}
455650c8bd3SDouglas Anderson 
456650c8bd3SDouglas Anderson 	if (private_data->write_cached_bytes_cnt) {
457650c8bd3SDouglas Anderson 		private_data->write_cached_bytes >>= BITS_PER_BYTE *
458650c8bd3SDouglas Anderson 			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
459650c8bd3SDouglas Anderson 		writel(private_data->write_cached_bytes,
460650c8bd3SDouglas Anderson 		       uport->membase + SE_GENI_TX_FIFOn);
461650c8bd3SDouglas Anderson 		private_data->write_cached_bytes_cnt = 0;
462650c8bd3SDouglas Anderson 	}
463650c8bd3SDouglas Anderson 
464c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
465c4f52879SKarthikeyan Ramasubramanian }
466c4f52879SKarthikeyan Ramasubramanian 
467c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
468c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
469c4f52879SKarthikeyan Ramasubramanian {
470c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
471c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
472c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
473c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
474a1fee899SRyan Case 	u32 geni_status;
475663abb1aSRyan Case 	u32 irq_en;
476c4f52879SKarthikeyan Ramasubramanian 
477c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
478c4f52879SKarthikeyan Ramasubramanian 
4798a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
480c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
481c4f52879SKarthikeyan Ramasubramanian 		return;
482c4f52879SKarthikeyan Ramasubramanian 
483c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
484c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
485c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
486c4f52879SKarthikeyan Ramasubramanian 	else
487c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
488c4f52879SKarthikeyan Ramasubramanian 
4899e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
490a1fee899SRyan Case 
491c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
492c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
493c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
494c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
495c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
496c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
497c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
498c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
4999e06d55fSRyan Case 			writel(M_CMD_ABORT_EN, uport->membase +
500c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
501c4f52879SKarthikeyan Ramasubramanian 		}
5029e06d55fSRyan Case 		writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
503a1fee899SRyan Case 	} else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
504a1fee899SRyan Case 		/*
505a1fee899SRyan Case 		 * It seems we can't interrupt existing transfers if all data
506a1fee899SRyan Case 		 * has been sent, in which case we need to look for done first.
507a1fee899SRyan Case 		 */
508a1fee899SRyan Case 		qcom_geni_serial_poll_tx_done(uport);
509663abb1aSRyan Case 
510d2b574c0SJiri Slaby 		if (!uart_circ_empty(&uport->state->xmit)) {
5119e06d55fSRyan Case 			irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
5129e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
513663abb1aSRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
514663abb1aSRyan Case 		}
515c4f52879SKarthikeyan Ramasubramanian 	}
516c4f52879SKarthikeyan Ramasubramanian 
517c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
518a1fee899SRyan Case 
519a1fee899SRyan Case 	if (port->tx_remaining)
520a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, port->tx_remaining);
521a1fee899SRyan Case 
522c4f52879SKarthikeyan Ramasubramanian 	if (locked)
523c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
524c4f52879SKarthikeyan Ramasubramanian }
525c4f52879SKarthikeyan Ramasubramanian 
5260626afe5SBartosz Golaszewski static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
527c4f52879SKarthikeyan Ramasubramanian {
528c4f52879SKarthikeyan Ramasubramanian 	u32 i;
529c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
530c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
53100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
532c4f52879SKarthikeyan Ramasubramanian 
533c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
534c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
535c4f52879SKarthikeyan Ramasubramanian 		int c;
536650c8bd3SDouglas Anderson 		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
537c4f52879SKarthikeyan Ramasubramanian 
538c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
539c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
540c4f52879SKarthikeyan Ramasubramanian 		if (drop)
541c4f52879SKarthikeyan Ramasubramanian 			continue;
542c4f52879SKarthikeyan Ramasubramanian 
543c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
544c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
545c4f52879SKarthikeyan Ramasubramanian 
546c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
547c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
548c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
549c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
550c4f52879SKarthikeyan Ramasubramanian 					continue;
551c4f52879SKarthikeyan Ramasubramanian 			}
552c4f52879SKarthikeyan Ramasubramanian 
553336447b3SDouglas Anderson 			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
554babeca85SDouglas Anderson 
555c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
556c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
557c4f52879SKarthikeyan Ramasubramanian 		}
558c4f52879SKarthikeyan Ramasubramanian 	}
559c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
560c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
561c4f52879SKarthikeyan Ramasubramanian }
562c4f52879SKarthikeyan Ramasubramanian #else
5630626afe5SBartosz Golaszewski static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
564c4f52879SKarthikeyan Ramasubramanian {
5650626afe5SBartosz Golaszewski 
566c4f52879SKarthikeyan Ramasubramanian }
567c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
568c4f52879SKarthikeyan Ramasubramanian 
5690626afe5SBartosz Golaszewski static void handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5708a8a66a1SGirish Mahadevan {
57100ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
5722aaa43c7SBartosz Golaszewski 	struct tty_port *tport = &uport->state->port;
5738a8a66a1SGirish Mahadevan 	int ret;
5748a8a66a1SGirish Mahadevan 
5752aaa43c7SBartosz Golaszewski 	ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
5768a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5778a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5788a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5798a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5808a8a66a1SGirish Mahadevan 	}
5818a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5828a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5838a8a66a1SGirish Mahadevan }
5848a8a66a1SGirish Mahadevan 
585d0fabb0dSBartosz Golaszewski static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
586d0fabb0dSBartosz Golaszewski {
587d0fabb0dSBartosz Golaszewski 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
588d0fabb0dSBartosz Golaszewski }
589d0fabb0dSBartosz Golaszewski 
5902aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
591c4f52879SKarthikeyan Ramasubramanian {
5922aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
5932aaa43c7SBartosz Golaszewski 	bool done;
5942aaa43c7SBartosz Golaszewski 	u32 m_irq_en;
595c4f52879SKarthikeyan Ramasubramanian 
5962aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_main_active(uport))
597c4f52879SKarthikeyan Ramasubramanian 		return;
598c4f52879SKarthikeyan Ramasubramanian 
59995fcfc08SJohan Hovold 	if (port->tx_dma_addr) {
6002aaa43c7SBartosz Golaszewski 		geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
6012aaa43c7SBartosz Golaszewski 				      port->tx_remaining);
6022aaa43c7SBartosz Golaszewski 		port->tx_dma_addr = 0;
6032aaa43c7SBartosz Golaszewski 		port->tx_remaining = 0;
6042aaa43c7SBartosz Golaszewski 	}
6052aaa43c7SBartosz Golaszewski 
6062aaa43c7SBartosz Golaszewski 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
6072aaa43c7SBartosz Golaszewski 	writel(m_irq_en, uport->membase + SE_GENI_M_IRQ_EN);
6082aaa43c7SBartosz Golaszewski 	geni_se_cancel_m_cmd(&port->se);
6092aaa43c7SBartosz Golaszewski 
6102aaa43c7SBartosz Golaszewski 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
6112aaa43c7SBartosz Golaszewski 					 S_CMD_CANCEL_EN, true);
6122aaa43c7SBartosz Golaszewski 	if (!done) {
6132aaa43c7SBartosz Golaszewski 		geni_se_abort_m_cmd(&port->se);
6142aaa43c7SBartosz Golaszewski 		done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
6152aaa43c7SBartosz Golaszewski 						 M_CMD_ABORT_EN, true);
6162aaa43c7SBartosz Golaszewski 		if (!done)
6172aaa43c7SBartosz Golaszewski 			dev_err_ratelimited(uport->dev, "M_CMD_ABORT_EN not set");
6182aaa43c7SBartosz Golaszewski 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
6192aaa43c7SBartosz Golaszewski 	}
6202aaa43c7SBartosz Golaszewski 
6212aaa43c7SBartosz Golaszewski 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
6222aaa43c7SBartosz Golaszewski }
6232aaa43c7SBartosz Golaszewski 
6242aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
6252aaa43c7SBartosz Golaszewski {
6262aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
6272aaa43c7SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
6282aaa43c7SBartosz Golaszewski 	unsigned int xmit_size;
6292aaa43c7SBartosz Golaszewski 	int ret;
6302aaa43c7SBartosz Golaszewski 
6312aaa43c7SBartosz Golaszewski 	if (port->tx_dma_addr)
6322aaa43c7SBartosz Golaszewski 		return;
6332aaa43c7SBartosz Golaszewski 
63497820780SJohan Hovold 	if (uart_circ_empty(xmit))
63597820780SJohan Hovold 		return;
63697820780SJohan Hovold 
6372aaa43c7SBartosz Golaszewski 	xmit_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
6382aaa43c7SBartosz Golaszewski 
6392aaa43c7SBartosz Golaszewski 	qcom_geni_serial_setup_tx(uport, xmit_size);
6402aaa43c7SBartosz Golaszewski 
6412aaa43c7SBartosz Golaszewski 	ret = geni_se_tx_dma_prep(&port->se, &xmit->buf[xmit->tail],
6422aaa43c7SBartosz Golaszewski 				  xmit_size, &port->tx_dma_addr);
6432aaa43c7SBartosz Golaszewski 	if (ret) {
6442aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
6452aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_tx_dma(uport);
6462aaa43c7SBartosz Golaszewski 		return;
6472aaa43c7SBartosz Golaszewski 	}
6482aaa43c7SBartosz Golaszewski 
6492aaa43c7SBartosz Golaszewski 	port->tx_remaining = xmit_size;
6502aaa43c7SBartosz Golaszewski }
6512aaa43c7SBartosz Golaszewski 
6522aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
6532aaa43c7SBartosz Golaszewski {
6542aaa43c7SBartosz Golaszewski 	u32 irq_en;
6552aaa43c7SBartosz Golaszewski 
6562aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_main_active(uport) ||
6572aaa43c7SBartosz Golaszewski 	    !qcom_geni_serial_tx_empty(uport))
658c4f52879SKarthikeyan Ramasubramanian 		return;
659c4f52879SKarthikeyan Ramasubramanian 
6609e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
661c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
662c4f52879SKarthikeyan Ramasubramanian 
663bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
6649e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
665c4f52879SKarthikeyan Ramasubramanian }
666c4f52879SKarthikeyan Ramasubramanian 
6672aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
668c4f52879SKarthikeyan Ramasubramanian {
669c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
67000ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
671c4f52879SKarthikeyan Ramasubramanian 
6729e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
673bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
674bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
6759e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
676c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
6772aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_main_active(uport))
678c4f52879SKarthikeyan Ramasubramanian 		return;
679c4f52879SKarthikeyan Ramasubramanian 
680c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
681c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
682c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
683c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
684c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
685c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
6869e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
687c4f52879SKarthikeyan Ramasubramanian 	}
6889e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
689c4f52879SKarthikeyan Ramasubramanian }
690c4f52879SKarthikeyan Ramasubramanian 
6912aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
692c4f52879SKarthikeyan Ramasubramanian {
693c4f52879SKarthikeyan Ramasubramanian 	u32 status;
694d0fabb0dSBartosz Golaszewski 	u32 word_cnt;
695d0fabb0dSBartosz Golaszewski 	u32 last_word_byte_cnt;
696d0fabb0dSBartosz Golaszewski 	u32 last_word_partial;
697d0fabb0dSBartosz Golaszewski 	u32 total_bytes;
698c4f52879SKarthikeyan Ramasubramanian 
699d0fabb0dSBartosz Golaszewski 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
700d0fabb0dSBartosz Golaszewski 	word_cnt = status & RX_FIFO_WC_MSK;
701d0fabb0dSBartosz Golaszewski 	last_word_partial = status & RX_LAST;
702d0fabb0dSBartosz Golaszewski 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
703d0fabb0dSBartosz Golaszewski 						RX_LAST_BYTE_VALID_SHFT;
704c4f52879SKarthikeyan Ramasubramanian 
705d0fabb0dSBartosz Golaszewski 	if (!word_cnt)
706d0fabb0dSBartosz Golaszewski 		return;
707d0fabb0dSBartosz Golaszewski 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
708d0fabb0dSBartosz Golaszewski 	if (last_word_partial && last_word_byte_cnt)
709d0fabb0dSBartosz Golaszewski 		total_bytes += last_word_byte_cnt;
710d0fabb0dSBartosz Golaszewski 	else
711d0fabb0dSBartosz Golaszewski 		total_bytes += BYTES_PER_FIFO_WORD;
7122aaa43c7SBartosz Golaszewski 	handle_rx_console(uport, total_bytes, drop);
713c4f52879SKarthikeyan Ramasubramanian }
714c4f52879SKarthikeyan Ramasubramanian 
7152aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
716c4f52879SKarthikeyan Ramasubramanian {
717c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
71800ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
719679aac5eSsatya priya 	u32 s_irq_status;
720c4f52879SKarthikeyan Ramasubramanian 
7219e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
722c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
7239e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
724c4f52879SKarthikeyan Ramasubramanian 
7259e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
726c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
7279e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
728c4f52879SKarthikeyan Ramasubramanian 
7292aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
730c4f52879SKarthikeyan Ramasubramanian 		return;
731c4f52879SKarthikeyan Ramasubramanian 
732c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
733679aac5eSsatya priya 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
734679aac5eSsatya priya 					S_CMD_CANCEL_EN, true);
735679aac5eSsatya priya 	/*
736679aac5eSsatya priya 	 * If timeout occurs secondary engine remains active
737679aac5eSsatya priya 	 * and Abort sequence is executed.
738679aac5eSsatya priya 	 */
739679aac5eSsatya priya 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
740679aac5eSsatya priya 	/* Flush the Rx buffer */
741679aac5eSsatya priya 	if (s_irq_status & S_RX_FIFO_LAST_EN)
7422aaa43c7SBartosz Golaszewski 		qcom_geni_serial_handle_rx_fifo(uport, true);
743679aac5eSsatya priya 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
744679aac5eSsatya priya 
7452aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
746c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
747c4f52879SKarthikeyan Ramasubramanian }
748c4f52879SKarthikeyan Ramasubramanian 
7492aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
750c4f52879SKarthikeyan Ramasubramanian {
751d0fabb0dSBartosz Golaszewski 	u32 irq_en;
75200ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
753c4f52879SKarthikeyan Ramasubramanian 
7542aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7552aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_fifo(uport);
756c4f52879SKarthikeyan Ramasubramanian 
757d0fabb0dSBartosz Golaszewski 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
758d0fabb0dSBartosz Golaszewski 
759d0fabb0dSBartosz Golaszewski 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
760d0fabb0dSBartosz Golaszewski 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
761d0fabb0dSBartosz Golaszewski 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
762d0fabb0dSBartosz Golaszewski 
763d0fabb0dSBartosz Golaszewski 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
764d0fabb0dSBartosz Golaszewski 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
765d0fabb0dSBartosz Golaszewski 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
766c4f52879SKarthikeyan Ramasubramanian }
767c4f52879SKarthikeyan Ramasubramanian 
7682aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
7692aaa43c7SBartosz Golaszewski {
7702aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
7712aaa43c7SBartosz Golaszewski 
7722aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
7732aaa43c7SBartosz Golaszewski 		return;
7742aaa43c7SBartosz Golaszewski 
7752aaa43c7SBartosz Golaszewski 	geni_se_cancel_s_cmd(&port->se);
7762aaa43c7SBartosz Golaszewski 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
7772aaa43c7SBartosz Golaszewski 				  S_CMD_CANCEL_EN, true);
7782aaa43c7SBartosz Golaszewski 
7792aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7802aaa43c7SBartosz Golaszewski 		qcom_geni_serial_abort_rx(uport);
7812aaa43c7SBartosz Golaszewski 
7822aaa43c7SBartosz Golaszewski 	if (port->rx_dma_addr) {
7832aaa43c7SBartosz Golaszewski 		geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
7842aaa43c7SBartosz Golaszewski 				      DMA_RX_BUF_SIZE);
7852aaa43c7SBartosz Golaszewski 		port->rx_dma_addr = 0;
7862aaa43c7SBartosz Golaszewski 	}
7872aaa43c7SBartosz Golaszewski }
7882aaa43c7SBartosz Golaszewski 
7892aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
7902aaa43c7SBartosz Golaszewski {
7912aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
7922aaa43c7SBartosz Golaszewski 	int ret;
7932aaa43c7SBartosz Golaszewski 
7942aaa43c7SBartosz Golaszewski 	if (qcom_geni_serial_secondary_active(uport))
7952aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
7962aaa43c7SBartosz Golaszewski 
7972aaa43c7SBartosz Golaszewski 	geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
7982aaa43c7SBartosz Golaszewski 
7992aaa43c7SBartosz Golaszewski 	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
8002aaa43c7SBartosz Golaszewski 				  DMA_RX_BUF_SIZE,
8012aaa43c7SBartosz Golaszewski 				  &port->rx_dma_addr);
8022aaa43c7SBartosz Golaszewski 	if (ret) {
8032aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
8042aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8052aaa43c7SBartosz Golaszewski 	}
8062aaa43c7SBartosz Golaszewski }
8072aaa43c7SBartosz Golaszewski 
8082aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
8092aaa43c7SBartosz Golaszewski {
8102aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
8112aaa43c7SBartosz Golaszewski 	u32 rx_in;
8122aaa43c7SBartosz Golaszewski 	int ret;
8132aaa43c7SBartosz Golaszewski 
8142aaa43c7SBartosz Golaszewski 	if (!qcom_geni_serial_secondary_active(uport))
8152aaa43c7SBartosz Golaszewski 		return;
8162aaa43c7SBartosz Golaszewski 
8172aaa43c7SBartosz Golaszewski 	if (!port->rx_dma_addr)
8182aaa43c7SBartosz Golaszewski 		return;
8192aaa43c7SBartosz Golaszewski 
8202aaa43c7SBartosz Golaszewski 	geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
8212aaa43c7SBartosz Golaszewski 	port->rx_dma_addr = 0;
8222aaa43c7SBartosz Golaszewski 
8232aaa43c7SBartosz Golaszewski 	rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
8242aaa43c7SBartosz Golaszewski 	if (!rx_in) {
8252aaa43c7SBartosz Golaszewski 		dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
8262aaa43c7SBartosz Golaszewski 		return;
8272aaa43c7SBartosz Golaszewski 	}
8282aaa43c7SBartosz Golaszewski 
8292aaa43c7SBartosz Golaszewski 	if (!drop)
8302aaa43c7SBartosz Golaszewski 		handle_rx_uart(uport, rx_in, drop);
8312aaa43c7SBartosz Golaszewski 
8322aaa43c7SBartosz Golaszewski 	ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
8332aaa43c7SBartosz Golaszewski 				  DMA_RX_BUF_SIZE,
8342aaa43c7SBartosz Golaszewski 				  &port->rx_dma_addr);
8352aaa43c7SBartosz Golaszewski 	if (ret) {
8362aaa43c7SBartosz Golaszewski 		dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
8372aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_rx_dma(uport);
8382aaa43c7SBartosz Golaszewski 	}
8392aaa43c7SBartosz Golaszewski }
8402aaa43c7SBartosz Golaszewski 
8412aaa43c7SBartosz Golaszewski static void qcom_geni_serial_start_rx(struct uart_port *uport)
8422aaa43c7SBartosz Golaszewski {
8432aaa43c7SBartosz Golaszewski 	uport->ops->start_rx(uport);
8442aaa43c7SBartosz Golaszewski }
8452aaa43c7SBartosz Golaszewski 
8462aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_rx(struct uart_port *uport)
8472aaa43c7SBartosz Golaszewski {
8482aaa43c7SBartosz Golaszewski 	uport->ops->stop_rx(uport);
8492aaa43c7SBartosz Golaszewski }
8502aaa43c7SBartosz Golaszewski 
8512aaa43c7SBartosz Golaszewski static void qcom_geni_serial_stop_tx(struct uart_port *uport)
8522aaa43c7SBartosz Golaszewski {
8532aaa43c7SBartosz Golaszewski 	uport->ops->stop_tx(uport);
8542aaa43c7SBartosz Golaszewski }
8552aaa43c7SBartosz Golaszewski 
856bd795584SBartosz Golaszewski static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
857d420fb49SBartosz Golaszewski 					     unsigned int chunk)
858c4f52879SKarthikeyan Ramasubramanian {
85900ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
860c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
861bd795584SBartosz Golaszewski 	unsigned int tx_bytes, c, remaining = chunk;
862bd795584SBartosz Golaszewski 	u8 buf[BYTES_PER_FIFO_WORD];
863c4f52879SKarthikeyan Ramasubramanian 
864bd795584SBartosz Golaszewski 	while (remaining) {
8653550f897SDan Carpenter 		memset(buf, 0, sizeof(buf));
866bd795584SBartosz Golaszewski 		tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
8673c66eb4bSMatthias Kaehlcke 
8683c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
869bd795584SBartosz Golaszewski 			buf[c] = xmit->buf[xmit->tail];
870bd795584SBartosz Golaszewski 			uart_xmit_advance(uport, 1);
8713c66eb4bSMatthias Kaehlcke 		}
872c4f52879SKarthikeyan Ramasubramanian 
87369736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
874c4f52879SKarthikeyan Ramasubramanian 
875c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
876a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
877c4f52879SKarthikeyan Ramasubramanian 	}
878d420fb49SBartosz Golaszewski }
879d420fb49SBartosz Golaszewski 
8802aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
8812aaa43c7SBartosz Golaszewski 					    bool done, bool active)
882d420fb49SBartosz Golaszewski {
883d420fb49SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
884d420fb49SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
885d420fb49SBartosz Golaszewski 	size_t avail;
886d420fb49SBartosz Golaszewski 	size_t pending;
887d420fb49SBartosz Golaszewski 	u32 status;
888d420fb49SBartosz Golaszewski 	u32 irq_en;
889d420fb49SBartosz Golaszewski 	unsigned int chunk;
890d420fb49SBartosz Golaszewski 
891d420fb49SBartosz Golaszewski 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
892d420fb49SBartosz Golaszewski 
893d420fb49SBartosz Golaszewski 	/* Complete the current tx command before taking newly added data */
894d420fb49SBartosz Golaszewski 	if (active)
895d420fb49SBartosz Golaszewski 		pending = port->tx_remaining;
896d420fb49SBartosz Golaszewski 	else
897d420fb49SBartosz Golaszewski 		pending = uart_circ_chars_pending(xmit);
898d420fb49SBartosz Golaszewski 
899d420fb49SBartosz Golaszewski 	/* All data has been transmitted and acknowledged as received */
900d420fb49SBartosz Golaszewski 	if (!pending && !status && done) {
9012aaa43c7SBartosz Golaszewski 		qcom_geni_serial_stop_tx_fifo(uport);
902d420fb49SBartosz Golaszewski 		goto out_write_wakeup;
903d420fb49SBartosz Golaszewski 	}
904d420fb49SBartosz Golaszewski 
905d420fb49SBartosz Golaszewski 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
906d420fb49SBartosz Golaszewski 	avail *= BYTES_PER_FIFO_WORD;
907d420fb49SBartosz Golaszewski 
908d420fb49SBartosz Golaszewski 	chunk = min(avail, pending);
909d420fb49SBartosz Golaszewski 	if (!chunk)
910d420fb49SBartosz Golaszewski 		goto out_write_wakeup;
911d420fb49SBartosz Golaszewski 
912d420fb49SBartosz Golaszewski 	if (!port->tx_remaining) {
913d420fb49SBartosz Golaszewski 		qcom_geni_serial_setup_tx(uport, pending);
914d420fb49SBartosz Golaszewski 		port->tx_remaining = pending;
915d420fb49SBartosz Golaszewski 
916d420fb49SBartosz Golaszewski 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
917d420fb49SBartosz Golaszewski 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
918d420fb49SBartosz Golaszewski 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
919d420fb49SBartosz Golaszewski 					uport->membase + SE_GENI_M_IRQ_EN);
920d420fb49SBartosz Golaszewski 	}
921d420fb49SBartosz Golaszewski 
922bd795584SBartosz Golaszewski 	qcom_geni_serial_send_chunk_fifo(uport, chunk);
92364a42807SRyan Case 
92464a42807SRyan Case 	/*
92564a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
92664a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
92764a42807SRyan Case 	 * so we must clear it again here after our writes.
92864a42807SRyan Case 	 */
9299e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
93064a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
93164a42807SRyan Case 
932c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
93364a42807SRyan Case 	if (!port->tx_remaining) {
9349e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
93564a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
9369e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
93764a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
93864a42807SRyan Case 	}
93964a42807SRyan Case 
940638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
941c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
942c4f52879SKarthikeyan Ramasubramanian }
943c4f52879SKarthikeyan Ramasubramanian 
9442aaa43c7SBartosz Golaszewski static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
9452aaa43c7SBartosz Golaszewski {
9462aaa43c7SBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
9472aaa43c7SBartosz Golaszewski 	struct circ_buf *xmit = &uport->state->xmit;
9482aaa43c7SBartosz Golaszewski 
9492aaa43c7SBartosz Golaszewski 	uart_xmit_advance(uport, port->tx_remaining);
9502aaa43c7SBartosz Golaszewski 	geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
9512aaa43c7SBartosz Golaszewski 	port->tx_dma_addr = 0;
9522aaa43c7SBartosz Golaszewski 	port->tx_remaining = 0;
9532aaa43c7SBartosz Golaszewski 
9542aaa43c7SBartosz Golaszewski 	if (!uart_circ_empty(xmit))
9552aaa43c7SBartosz Golaszewski 		qcom_geni_serial_start_tx_dma(uport);
9562aaa43c7SBartosz Golaszewski 
9572aaa43c7SBartosz Golaszewski 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
9582aaa43c7SBartosz Golaszewski 		uart_write_wakeup(uport);
9592aaa43c7SBartosz Golaszewski }
9602aaa43c7SBartosz Golaszewski 
961c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
962c4f52879SKarthikeyan Ramasubramanian {
963385298abSRyan Case 	u32 m_irq_en;
964385298abSRyan Case 	u32 m_irq_status;
965385298abSRyan Case 	u32 s_irq_status;
966385298abSRyan Case 	u32 geni_status;
9672aaa43c7SBartosz Golaszewski 	u32 dma;
9682aaa43c7SBartosz Golaszewski 	u32 dma_tx_status;
9692aaa43c7SBartosz Golaszewski 	u32 dma_rx_status;
970c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
971c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
972c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
97300ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
974c4f52879SKarthikeyan Ramasubramanian 
975c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
976ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
977c4f52879SKarthikeyan Ramasubramanian 
97875f4e830SJohan Hovold 	spin_lock(&uport->lock);
97975f4e830SJohan Hovold 
9809e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
9819e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
9822aaa43c7SBartosz Golaszewski 	dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
9832aaa43c7SBartosz Golaszewski 	dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
9849e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
9852aaa43c7SBartosz Golaszewski 	dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
9869e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
9879e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
9889e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
9892aaa43c7SBartosz Golaszewski 	writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
9902aaa43c7SBartosz Golaszewski 	writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
991c4f52879SKarthikeyan Ramasubramanian 
992c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
993c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
994c4f52879SKarthikeyan Ramasubramanian 
995c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
996c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
997c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
998c4f52879SKarthikeyan Ramasubramanian 	}
999c4f52879SKarthikeyan Ramasubramanian 
1000fe6a00e8SBartosz Golaszewski 	if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
1001c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
1002c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
1003c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
1004fe6a00e8SBartosz Golaszewski 	} else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
1005c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
1006c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
1007c4f52879SKarthikeyan Ramasubramanian 	}
1008c4f52879SKarthikeyan Ramasubramanian 
10092aaa43c7SBartosz Golaszewski 	if (dma) {
10102aaa43c7SBartosz Golaszewski 		if (dma_tx_status & TX_DMA_DONE)
10112aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_tx_dma(uport);
10122aaa43c7SBartosz Golaszewski 
10132aaa43c7SBartosz Golaszewski 		if (dma_rx_status) {
10142aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_RESET_DONE)
10152aaa43c7SBartosz Golaszewski 				goto out_unlock;
10162aaa43c7SBartosz Golaszewski 
10172aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_DMA_PARITY_ERR) {
10182aaa43c7SBartosz Golaszewski 				uport->icount.parity++;
10192aaa43c7SBartosz Golaszewski 				drop_rx = true;
10202aaa43c7SBartosz Golaszewski 			}
10212aaa43c7SBartosz Golaszewski 
10222aaa43c7SBartosz Golaszewski 			if (dma_rx_status & RX_DMA_BREAK)
10232aaa43c7SBartosz Golaszewski 				uport->icount.brk++;
10242aaa43c7SBartosz Golaszewski 
10252aaa43c7SBartosz Golaszewski 			if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
10262aaa43c7SBartosz Golaszewski 				qcom_geni_serial_handle_rx_dma(uport, drop_rx);
10272aaa43c7SBartosz Golaszewski 		}
10282aaa43c7SBartosz Golaszewski 	} else {
10292aaa43c7SBartosz Golaszewski 		if (m_irq_status & m_irq_en &
10302aaa43c7SBartosz Golaszewski 		    (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
10312aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_tx_fifo(uport,
10322aaa43c7SBartosz Golaszewski 					m_irq_status & M_CMD_DONE_EN,
10332aaa43c7SBartosz Golaszewski 					geni_status & M_GENI_CMD_ACTIVE);
10342aaa43c7SBartosz Golaszewski 
1035fe6a00e8SBartosz Golaszewski 		if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
10362aaa43c7SBartosz Golaszewski 			qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
10372aaa43c7SBartosz Golaszewski 	}
1038c4f52879SKarthikeyan Ramasubramanian 
1039c4f52879SKarthikeyan Ramasubramanian out_unlock:
104075f4e830SJohan Hovold 	uart_unlock_and_check_sysrq(uport);
1041336447b3SDouglas Anderson 
1042c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
1043c4f52879SKarthikeyan Ramasubramanian }
1044c4f52879SKarthikeyan Ramasubramanian 
1045b8caf69aSKrzysztof Kozlowski static int setup_fifos(struct qcom_geni_serial_port *port)
1046c4f52879SKarthikeyan Ramasubramanian {
1047c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1048b8caf69aSKrzysztof Kozlowski 	u32 old_rx_fifo_depth = port->rx_fifo_depth;
1049c4f52879SKarthikeyan Ramasubramanian 
1050c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1051c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1052c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1053c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1054c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
1055c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1056b8caf69aSKrzysztof Kozlowski 
1057a3cf6b94SIlpo Järvinen 	if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
1058a3cf6b94SIlpo Järvinen 		port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
1059b8caf69aSKrzysztof Kozlowski 					     port->rx_fifo_depth * sizeof(u32),
1060b8caf69aSKrzysztof Kozlowski 					     GFP_KERNEL);
1061a3cf6b94SIlpo Järvinen 		if (!port->rx_buf)
1062b8caf69aSKrzysztof Kozlowski 			return -ENOMEM;
1063b8caf69aSKrzysztof Kozlowski 	}
1064b8caf69aSKrzysztof Kozlowski 
1065b8caf69aSKrzysztof Kozlowski 	return 0;
1066c4f52879SKarthikeyan Ramasubramanian }
1067c4f52879SKarthikeyan Ramasubramanian 
1068c4f52879SKarthikeyan Ramasubramanian 
1069c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
1070c4f52879SKarthikeyan Ramasubramanian {
10713e4aaea7SAkash Asthana 	disable_irq(uport->irq);
10729aff74ccSJohan Hovold 
10739aff74ccSJohan Hovold 	if (uart_console(uport))
10749aff74ccSJohan Hovold 		return;
10759aff74ccSJohan Hovold 
1076d8aca2f9SBartosz Golaszewski 	qcom_geni_serial_stop_tx(uport);
1077d8aca2f9SBartosz Golaszewski 	qcom_geni_serial_stop_rx(uport);
1078c4f52879SKarthikeyan Ramasubramanian }
1079c4f52879SKarthikeyan Ramasubramanian 
1080c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
1081c4f52879SKarthikeyan Ramasubramanian {
108200ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1083385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1084c362272bSDouglas Anderson 	u32 proto;
10859fa3c4b1SRoja Rani Yarubandi 	u32 pin_swap;
1086b8caf69aSKrzysztof Kozlowski 	int ret;
1087c362272bSDouglas Anderson 
1088c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
1089c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
1090c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1091c362272bSDouglas Anderson 		return -ENXIO;
1092c362272bSDouglas Anderson 	}
1093c362272bSDouglas Anderson 
1094c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
1095c362272bSDouglas Anderson 
1096b8caf69aSKrzysztof Kozlowski 	ret = setup_fifos(port);
1097b8caf69aSKrzysztof Kozlowski 	if (ret)
1098b8caf69aSKrzysztof Kozlowski 		return ret;
1099c4f52879SKarthikeyan Ramasubramanian 
11009e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
11019fa3c4b1SRoja Rani Yarubandi 
11029fa3c4b1SRoja Rani Yarubandi 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
11039fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap) {
11049fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
11059fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
11069fa3c4b1SRoja Rani Yarubandi 	}
11079fa3c4b1SRoja Rani Yarubandi 	if (port->cts_rts_swap) {
11089fa3c4b1SRoja Rani Yarubandi 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
11099fa3c4b1SRoja Rani Yarubandi 		pin_swap |= IO_MACRO_IO0_SEL;
11109fa3c4b1SRoja Rani Yarubandi 	}
11119fa3c4b1SRoja Rani Yarubandi 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
11129fa3c4b1SRoja Rani Yarubandi 	if (port->rx_tx_swap || port->cts_rts_swap)
11139fa3c4b1SRoja Rani Yarubandi 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
11149fa3c4b1SRoja Rani Yarubandi 
1115c4f52879SKarthikeyan Ramasubramanian 	/*
1116c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
1117c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
1118c4f52879SKarthikeyan Ramasubramanian 	 */
11198a8a66a1SGirish Mahadevan 	if (uart_console(uport))
1120c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
1121650c8bd3SDouglas Anderson 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1122650c8bd3SDouglas Anderson 			       false, true, true);
1123a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
11242aaa43c7SBartosz Golaszewski 	geni_se_select_mode(&port->se, port->dev_data->mode);
112535781d83SAniket Randive 	qcom_geni_serial_start_rx(uport);
1126c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
1127c362272bSDouglas Anderson 
1128c4f52879SKarthikeyan Ramasubramanian 	return 0;
1129c4f52879SKarthikeyan Ramasubramanian }
1130c4f52879SKarthikeyan Ramasubramanian 
1131c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
1132c4f52879SKarthikeyan Ramasubramanian {
1133c4f52879SKarthikeyan Ramasubramanian 	int ret;
113400ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1135c4f52879SKarthikeyan Ramasubramanian 
1136c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1137c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
1138c4f52879SKarthikeyan Ramasubramanian 		if (ret)
1139c4f52879SKarthikeyan Ramasubramanian 			return ret;
1140c4f52879SKarthikeyan Ramasubramanian 	}
11413e4aaea7SAkash Asthana 	enable_irq(uport->irq);
1142c4f52879SKarthikeyan Ramasubramanian 
11433e4aaea7SAkash Asthana 	return 0;
1144c4f52879SKarthikeyan Ramasubramanian }
1145c4f52879SKarthikeyan Ramasubramanian 
1146c474c775SVijaya Krishna Nivarthi static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
1147c474c775SVijaya Krishna Nivarthi 			unsigned int *clk_div, unsigned int percent_tol)
1148c474c775SVijaya Krishna Nivarthi {
1149c474c775SVijaya Krishna Nivarthi 	unsigned long freq;
1150c474c775SVijaya Krishna Nivarthi 	unsigned long div, maxdiv;
1151c474c775SVijaya Krishna Nivarthi 	u64 mult;
1152c474c775SVijaya Krishna Nivarthi 	unsigned long offset, abs_tol, achieved;
1153c474c775SVijaya Krishna Nivarthi 
1154c474c775SVijaya Krishna Nivarthi 	abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
1155c474c775SVijaya Krishna Nivarthi 	maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
1156c474c775SVijaya Krishna Nivarthi 	div = 1;
1157c474c775SVijaya Krishna Nivarthi 	while (div <= maxdiv) {
1158c474c775SVijaya Krishna Nivarthi 		mult = (u64)div * desired_clk;
1159c474c775SVijaya Krishna Nivarthi 		if (mult != (unsigned long)mult)
1160c474c775SVijaya Krishna Nivarthi 			break;
1161c474c775SVijaya Krishna Nivarthi 
1162c474c775SVijaya Krishna Nivarthi 		offset = div * abs_tol;
1163c474c775SVijaya Krishna Nivarthi 		freq = clk_round_rate(clk, mult - offset);
1164c474c775SVijaya Krishna Nivarthi 
1165c474c775SVijaya Krishna Nivarthi 		/* Can only get lower if we're done */
1166c474c775SVijaya Krishna Nivarthi 		if (freq < mult - offset)
1167c474c775SVijaya Krishna Nivarthi 			break;
1168c474c775SVijaya Krishna Nivarthi 
1169c474c775SVijaya Krishna Nivarthi 		/*
1170c474c775SVijaya Krishna Nivarthi 		 * Re-calculate div in case rounding skipped rates but we
1171c474c775SVijaya Krishna Nivarthi 		 * ended up at a good one, then check for a match.
1172c474c775SVijaya Krishna Nivarthi 		 */
1173c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_CLOSEST(freq, desired_clk);
1174c474c775SVijaya Krishna Nivarthi 		achieved = DIV_ROUND_CLOSEST(freq, div);
1175c474c775SVijaya Krishna Nivarthi 		if (achieved <= desired_clk + abs_tol &&
1176c474c775SVijaya Krishna Nivarthi 		    achieved >= desired_clk - abs_tol) {
1177c474c775SVijaya Krishna Nivarthi 			*clk_div = div;
1178c474c775SVijaya Krishna Nivarthi 			return freq;
1179c474c775SVijaya Krishna Nivarthi 		}
1180c474c775SVijaya Krishna Nivarthi 
1181c474c775SVijaya Krishna Nivarthi 		div = DIV_ROUND_UP(freq, desired_clk);
1182c474c775SVijaya Krishna Nivarthi 	}
1183c474c775SVijaya Krishna Nivarthi 
1184c474c775SVijaya Krishna Nivarthi 	return 0;
1185c474c775SVijaya Krishna Nivarthi }
1186c474c775SVijaya Krishna Nivarthi 
1187c2194bc9SVijaya Krishna Nivarthi static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1188ce734600SVivek Gautam 			unsigned int sampling_rate, unsigned int *clk_div)
1189c4f52879SKarthikeyan Ramasubramanian {
1190c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
1191c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
1192c4f52879SKarthikeyan Ramasubramanian 
1193ce734600SVivek Gautam 	desired_clk = baud * sampling_rate;
1194c474c775SVijaya Krishna Nivarthi 	if (!desired_clk)
1195c2194bc9SVijaya Krishna Nivarthi 		return 0;
1196c2194bc9SVijaya Krishna Nivarthi 
1197c474c775SVijaya Krishna Nivarthi 	/*
1198c474c775SVijaya Krishna Nivarthi 	 * try to find a clock rate within 2% tolerance, then within 5%
1199c474c775SVijaya Krishna Nivarthi 	 */
1200c474c775SVijaya Krishna Nivarthi 	ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1201c474c775SVijaya Krishna Nivarthi 	if (!ser_clk)
1202c474c775SVijaya Krishna Nivarthi 		ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1203c2194bc9SVijaya Krishna Nivarthi 
1204c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
1205c4f52879SKarthikeyan Ramasubramanian }
1206c4f52879SKarthikeyan Ramasubramanian 
1207c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
1208bec5b814SIlpo Järvinen 					 struct ktermios *termios,
1209bec5b814SIlpo Järvinen 					 const struct ktermios *old)
1210c4f52879SKarthikeyan Ramasubramanian {
1211c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
1212385298abSRyan Case 	u32 bits_per_char;
1213385298abSRyan Case 	u32 tx_trans_cfg;
1214385298abSRyan Case 	u32 tx_parity_cfg;
1215385298abSRyan Case 	u32 rx_trans_cfg;
1216385298abSRyan Case 	u32 rx_parity_cfg;
1217385298abSRyan Case 	u32 stop_bit_len;
1218c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
1219385298abSRyan Case 	u32 ser_clk_cfg;
122000ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1221c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
1222ce734600SVivek Gautam 	u32 ver, sampling_rate;
12237cf563b2SAkash Asthana 	unsigned int avg_bw_core;
1224c4f52879SKarthikeyan Ramasubramanian 
1225c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
1226c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
1227c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1228c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
1229ce734600SVivek Gautam 
1230ce734600SVivek Gautam 	sampling_rate = UART_OVERSAMPLING;
1231ce734600SVivek Gautam 	/* Sampling rate is halved for IP versions >= 2.5 */
1232ce734600SVivek Gautam 	ver = geni_se_get_qup_hw_version(&port->se);
1233c9ca43d4SParas Sharma 	if (ver >= QUP_SE_VERSION_2_5)
1234ce734600SVivek Gautam 		sampling_rate /= 2;
1235ce734600SVivek Gautam 
1236c2194bc9SVijaya Krishna Nivarthi 	clk_rate = get_clk_div_rate(port->se.clk, baud,
1237c2194bc9SVijaya Krishna Nivarthi 		sampling_rate, &clk_div);
1238c474c775SVijaya Krishna Nivarthi 	if (!clk_rate) {
1239c474c775SVijaya Krishna Nivarthi 		dev_err(port->se.dev,
12400fec5180SDouglas Anderson 			"Couldn't find suitable clock rate for %u\n",
1241c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate);
1242c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
1243c474c775SVijaya Krishna Nivarthi 	}
1244c474c775SVijaya Krishna Nivarthi 
12450fec5180SDouglas Anderson 	dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
1246c474c775SVijaya Krishna Nivarthi 			baud * sampling_rate, clk_rate, clk_div);
1247c4f52879SKarthikeyan Ramasubramanian 
1248c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
1249a5819b54SRajendra Nayak 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1250c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
1251c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1252c4f52879SKarthikeyan Ramasubramanian 
12537cf563b2SAkash Asthana 	/*
12547cf563b2SAkash Asthana 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
12557cf563b2SAkash Asthana 	 * only.
12567cf563b2SAkash Asthana 	 */
12577cf563b2SAkash Asthana 	avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
12587cf563b2SAkash Asthana 						: GENI_DEFAULT_BW;
12597cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
12607cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
12617cf563b2SAkash Asthana 	geni_icc_set_bw(&port->se);
12627cf563b2SAkash Asthana 
1263c4f52879SKarthikeyan Ramasubramanian 	/* parity */
12649e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
12659e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
12669e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
12679e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1268c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
1269c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
1270c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
1271c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
1272c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
1273c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
1274c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
1275c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
1276c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
1277c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
1278c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
1279c4f52879SKarthikeyan Ramasubramanian 		} else {
1280c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
1281c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
1282c4f52879SKarthikeyan Ramasubramanian 		}
1283c4f52879SKarthikeyan Ramasubramanian 	} else {
1284c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1285c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1286c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1287c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1288c4f52879SKarthikeyan Ramasubramanian 	}
1289c4f52879SKarthikeyan Ramasubramanian 
1290c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
12913ec2ff37SJiri Slaby 	bits_per_char = tty_get_char_size(termios->c_cflag);
1292c4f52879SKarthikeyan Ramasubramanian 
1293c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1294c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1295c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1296c4f52879SKarthikeyan Ramasubramanian 	else
1297c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1298c4f52879SKarthikeyan Ramasubramanian 
1299c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1300c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1301c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1302c4f52879SKarthikeyan Ramasubramanian 	else
1303c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1304c4f52879SKarthikeyan Ramasubramanian 
1305c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1306c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1307c4f52879SKarthikeyan Ramasubramanian 
13088a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
13099e06d55fSRyan Case 		writel(port->loopback,
13108a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
13119e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
13129e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
13139e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
13149e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
13159e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
13169e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
13179e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
13189e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
13199e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1320c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1321c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1322c4f52879SKarthikeyan Ramasubramanian }
1323c4f52879SKarthikeyan Ramasubramanian 
1324c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1325975efc66SJohn Stultz static int qcom_geni_console_setup(struct console *co, char *options)
1326c4f52879SKarthikeyan Ramasubramanian {
1327c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1328c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
13292ec812a0SDouglas Anderson 	int baud = 115200;
1330c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1331c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1332c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1333c362272bSDouglas Anderson 	int ret;
1334c4f52879SKarthikeyan Ramasubramanian 
1335c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1336c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1337c4f52879SKarthikeyan Ramasubramanian 
13388a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1339c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
13406a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1341c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1342c4f52879SKarthikeyan Ramasubramanian 	}
1343c4f52879SKarthikeyan Ramasubramanian 
1344c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1345c4f52879SKarthikeyan Ramasubramanian 
1346c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1347c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1348c4f52879SKarthikeyan Ramasubramanian 
1349c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1350c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1351c362272bSDouglas Anderson 		if (ret)
1352c362272bSDouglas Anderson 			return ret;
1353c4f52879SKarthikeyan Ramasubramanian 	}
1354c4f52879SKarthikeyan Ramasubramanian 
1355c4f52879SKarthikeyan Ramasubramanian 	if (options)
1356c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1357c4f52879SKarthikeyan Ramasubramanian 
1358c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1359c4f52879SKarthikeyan Ramasubramanian }
1360c4f52879SKarthikeyan Ramasubramanian 
136143f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
136243f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
136343f1831bSKarthikeyan Ramasubramanian {
136443f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
136543f1831bSKarthikeyan Ramasubramanian 
136643f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
136743f1831bSKarthikeyan Ramasubramanian }
136843f1831bSKarthikeyan Ramasubramanian 
1369205b5bddSDouglas Anderson #ifdef CONFIG_CONSOLE_POLL
1370205b5bddSDouglas Anderson static int qcom_geni_serial_earlycon_read(struct console *con,
1371205b5bddSDouglas Anderson 					  char *s, unsigned int n)
1372205b5bddSDouglas Anderson {
1373205b5bddSDouglas Anderson 	struct earlycon_device *dev = con->data;
1374205b5bddSDouglas Anderson 	struct uart_port *uport = &dev->port;
1375205b5bddSDouglas Anderson 	int num_read = 0;
1376205b5bddSDouglas Anderson 	int ch;
1377205b5bddSDouglas Anderson 
1378205b5bddSDouglas Anderson 	while (num_read < n) {
1379205b5bddSDouglas Anderson 		ch = qcom_geni_serial_get_char(uport);
1380205b5bddSDouglas Anderson 		if (ch == NO_POLL_CHAR)
1381205b5bddSDouglas Anderson 			break;
1382205b5bddSDouglas Anderson 		s[num_read++] = ch;
1383205b5bddSDouglas Anderson 	}
1384205b5bddSDouglas Anderson 
1385205b5bddSDouglas Anderson 	return num_read;
1386205b5bddSDouglas Anderson }
1387205b5bddSDouglas Anderson 
1388205b5bddSDouglas Anderson static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1389205b5bddSDouglas Anderson 						      struct console *con)
1390205b5bddSDouglas Anderson {
1391205b5bddSDouglas Anderson 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1392205b5bddSDouglas Anderson 	con->read = qcom_geni_serial_earlycon_read;
1393205b5bddSDouglas Anderson }
1394205b5bddSDouglas Anderson #else
1395205b5bddSDouglas Anderson static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1396205b5bddSDouglas Anderson 						      struct console *con) { }
1397205b5bddSDouglas Anderson #endif
1398205b5bddSDouglas Anderson 
1399e42d6c3eSDouglas Anderson static struct qcom_geni_private_data earlycon_private_data;
1400e42d6c3eSDouglas Anderson 
140143f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
140243f1831bSKarthikeyan Ramasubramanian 								const char *opt)
140343f1831bSKarthikeyan Ramasubramanian {
140443f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
140543f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
140643f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
140743f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
140843f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
140943f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
141043f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
141143f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
141243f1831bSKarthikeyan Ramasubramanian 
141343f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
141443f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
141543f1831bSKarthikeyan Ramasubramanian 
1416e42d6c3eSDouglas Anderson 	uport->private_data = &earlycon_private_data;
1417e42d6c3eSDouglas Anderson 
141843f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
141943f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
142043f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
142143f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
142243f1831bSKarthikeyan Ramasubramanian 	/*
142343f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
142443f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
142543f1831bSKarthikeyan Ramasubramanian 	 */
142643f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
142743f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
142843f1831bSKarthikeyan Ramasubramanian 
142943f1831bSKarthikeyan Ramasubramanian 	/*
143043f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
143143f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
143243f1831bSKarthikeyan Ramasubramanian 	 */
143343f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
143443f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
1435650c8bd3SDouglas Anderson 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1436650c8bd3SDouglas Anderson 			       false, true, true);
143743f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
143843f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
143943f1831bSKarthikeyan Ramasubramanian 
14409e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
14419e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
14429e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
14439e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
14449e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
14459e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
14469e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
144743f1831bSKarthikeyan Ramasubramanian 
144843f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
144943f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
1450205b5bddSDouglas Anderson 	qcom_geni_serial_enable_early_read(&se, dev->con);
1451205b5bddSDouglas Anderson 
145243f1831bSKarthikeyan Ramasubramanian 	return 0;
145343f1831bSKarthikeyan Ramasubramanian }
145443f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
145543f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
145643f1831bSKarthikeyan Ramasubramanian 
1457c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1458c4f52879SKarthikeyan Ramasubramanian {
1459c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1460c4f52879SKarthikeyan Ramasubramanian }
1461c4f52879SKarthikeyan Ramasubramanian 
1462c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1463c4f52879SKarthikeyan Ramasubramanian {
1464c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1465c4f52879SKarthikeyan Ramasubramanian }
1466c4f52879SKarthikeyan Ramasubramanian 
1467c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1468c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1469c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1470c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1471c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1472c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1473c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1474c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1475c4f52879SKarthikeyan Ramasubramanian };
1476c4f52879SKarthikeyan Ramasubramanian 
1477c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1478c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1479c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1480c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1481c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1482c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1483c4f52879SKarthikeyan Ramasubramanian };
1484c4f52879SKarthikeyan Ramasubramanian #else
1485c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1486c4f52879SKarthikeyan Ramasubramanian {
1487c4f52879SKarthikeyan Ramasubramanian 	return 0;
1488c4f52879SKarthikeyan Ramasubramanian }
1489c4f52879SKarthikeyan Ramasubramanian 
1490c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1491c4f52879SKarthikeyan Ramasubramanian {
1492c4f52879SKarthikeyan Ramasubramanian }
1493c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1494c4f52879SKarthikeyan Ramasubramanian 
14958a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
14968a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
14978a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
14988a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
14998a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
15008a8a66a1SGirish Mahadevan };
15018a8a66a1SGirish Mahadevan 
15028a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1503c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1504c4f52879SKarthikeyan Ramasubramanian {
150500ce7c6eSBartosz Golaszewski 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1506c4f52879SKarthikeyan Ramasubramanian 
1507c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1508c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1509c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1510c362272bSDouglas Anderson 
15117cf563b2SAkash Asthana 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
15127cf563b2SAkash Asthana 		geni_icc_enable(&port->se);
1513c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
15147cf563b2SAkash Asthana 	} else if (new_state == UART_PM_STATE_OFF &&
15157cf563b2SAkash Asthana 			old_state == UART_PM_STATE_ON) {
1516c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
15177cf563b2SAkash Asthana 		geni_icc_disable(&port->se);
15187cf563b2SAkash Asthana 	}
1519c4f52879SKarthikeyan Ramasubramanian }
1520c4f52879SKarthikeyan Ramasubramanian 
1521c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1522c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
15232aaa43c7SBartosz Golaszewski 	.stop_tx = qcom_geni_serial_stop_tx_fifo,
15242aaa43c7SBartosz Golaszewski 	.start_tx = qcom_geni_serial_start_tx_fifo,
15252aaa43c7SBartosz Golaszewski 	.stop_rx = qcom_geni_serial_stop_rx_fifo,
15262aaa43c7SBartosz Golaszewski 	.start_rx = qcom_geni_serial_start_rx_fifo,
1527c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1528c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1529c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1530c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1531c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1532c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
15338a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
15348a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1535c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1536c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1537c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1538*d8851a96SDouglas Anderson 	.poll_init = qcom_geni_serial_port_setup,
1539c4f52879SKarthikeyan Ramasubramanian #endif
15408a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
15418a8a66a1SGirish Mahadevan };
15428a8a66a1SGirish Mahadevan 
15438a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
15448a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
15452aaa43c7SBartosz Golaszewski 	.stop_tx = qcom_geni_serial_stop_tx_dma,
15462aaa43c7SBartosz Golaszewski 	.start_tx = qcom_geni_serial_start_tx_dma,
15472aaa43c7SBartosz Golaszewski 	.start_rx = qcom_geni_serial_start_rx_dma,
15482aaa43c7SBartosz Golaszewski 	.stop_rx = qcom_geni_serial_stop_rx_dma,
15498a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
15508a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
15518a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
15528a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
15538a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
15548a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
15558a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
15568a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
15578a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1558c4f52879SKarthikeyan Ramasubramanian };
1559c4f52879SKarthikeyan Ramasubramanian 
1560c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1561c4f52879SKarthikeyan Ramasubramanian {
1562c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
156371581242SColin Ian King 	int line;
1564c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1565c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1566c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1567066cd1c4SKarthikeyan Ramasubramanian 	int irq;
15688a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
156940ec6d41SBartosz Golaszewski 	const struct qcom_geni_device_data *data;
1570c4f52879SKarthikeyan Ramasubramanian 
157140ec6d41SBartosz Golaszewski 	data = of_device_get_match_data(&pdev->dev);
157240ec6d41SBartosz Golaszewski 	if (!data)
157340ec6d41SBartosz Golaszewski 		return -EINVAL;
15748a8a66a1SGirish Mahadevan 
157540ec6d41SBartosz Golaszewski 	if (data->console) {
15768a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1577c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
15788a8a66a1SGirish Mahadevan 	} else {
15798a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
158008b0adb1SDmitry Baryshkov 		line = of_alias_get_id(pdev->dev.of_node, "serial");
158108b0adb1SDmitry Baryshkov 		if (line == -ENODEV) /* compat with non-standard aliases */
15828a8a66a1SGirish Mahadevan 			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
15838a8a66a1SGirish Mahadevan 	}
1584c4f52879SKarthikeyan Ramasubramanian 
158540ec6d41SBartosz Golaszewski 	port = get_port_from_line(line, data->console);
1586c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
15876a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
15886a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1589c4f52879SKarthikeyan Ramasubramanian 	}
1590c4f52879SKarthikeyan Ramasubramanian 
1591c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1592c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1593c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1594c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1595c4f52879SKarthikeyan Ramasubramanian 
1596c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
159740ec6d41SBartosz Golaszewski 	port->dev_data = data;
1598c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1599c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1600c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1601c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1602c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1603c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1604c4f52879SKarthikeyan Ramasubramanian 		return ret;
1605c4f52879SKarthikeyan Ramasubramanian 	}
1606c4f52879SKarthikeyan Ramasubramanian 
1607c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16087693b331SWei Yongjun 	if (!res)
16097693b331SWei Yongjun 		return -EINVAL;
1610c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1611c4f52879SKarthikeyan Ramasubramanian 
1612c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1613c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1614c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1615c4f52879SKarthikeyan Ramasubramanian 
161640ec6d41SBartosz Golaszewski 	if (!data->console) {
16172aaa43c7SBartosz Golaszewski 		port->rx_buf = devm_kzalloc(uport->dev,
16182aaa43c7SBartosz Golaszewski 					    DMA_RX_BUF_SIZE, GFP_KERNEL);
16192aaa43c7SBartosz Golaszewski 		if (!port->rx_buf)
1620f9d690b6Ssatya priya 			return -ENOMEM;
1621f9d690b6Ssatya priya 	}
1622f9d690b6Ssatya priya 
16237cf563b2SAkash Asthana 	ret = geni_icc_get(&port->se, NULL);
16247cf563b2SAkash Asthana 	if (ret)
16257cf563b2SAkash Asthana 		return ret;
16267cf563b2SAkash Asthana 	port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
16277cf563b2SAkash Asthana 	port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
16287cf563b2SAkash Asthana 
16297cf563b2SAkash Asthana 	/* Set BW for register access */
16307cf563b2SAkash Asthana 	ret = geni_icc_set_bw(&port->se);
16317cf563b2SAkash Asthana 	if (ret)
16327cf563b2SAkash Asthana 		return ret;
16337cf563b2SAkash Asthana 
1634f3974413SAkash Asthana 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1635f3974413SAkash Asthana 			"qcom_geni_serial_%s%d",
1636f3974413SAkash Asthana 			uart_console(uport) ? "console" : "uart", uport->line);
1637f3974413SAkash Asthana 	if (!port->name)
1638f3974413SAkash Asthana 		return -ENOMEM;
1639f3974413SAkash Asthana 
1640066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
16411df21786SStephen Boyd 	if (irq < 0)
1642066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1643066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
16448f122698SDmitry Safonov 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1645c4f52879SKarthikeyan Ramasubramanian 
164640ec6d41SBartosz Golaszewski 	if (!data->console)
1647f3974413SAkash Asthana 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
16483e4aaea7SAkash Asthana 
16499fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
16509fa3c4b1SRoja Rani Yarubandi 		port->rx_tx_swap = true;
16519fa3c4b1SRoja Rani Yarubandi 
16529fa3c4b1SRoja Rani Yarubandi 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
16539fa3c4b1SRoja Rani Yarubandi 		port->cts_rts_swap = true;
16549fa3c4b1SRoja Rani Yarubandi 
1655300894a6SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1656300894a6SYangtao Li 	if (ret)
1657300894a6SYangtao Li 		return ret;
1658a5819b54SRajendra Nayak 	/* OPP table is optional */
1659300894a6SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
1660c7ac46daSViresh Kumar 	if (ret && ret != -ENODEV) {
1661a5819b54SRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1662300894a6SYangtao Li 		return ret;
1663a5819b54SRajendra Nayak 	}
1664a5819b54SRajendra Nayak 
1665e42d6c3eSDouglas Anderson 	port->private_data.drv = drv;
1666e42d6c3eSDouglas Anderson 	uport->private_data = &port->private_data;
1667c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
1668f3974413SAkash Asthana 
1669f3974413SAkash Asthana 	ret = uart_add_one_port(drv, uport);
1670f3974413SAkash Asthana 	if (ret)
1671300894a6SYangtao Li 		return ret;
1672f3974413SAkash Asthana 
1673f3974413SAkash Asthana 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1674f3974413SAkash Asthana 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1675f3974413SAkash Asthana 			IRQF_TRIGGER_HIGH, port->name, uport);
1676f3974413SAkash Asthana 	if (ret) {
1677f3974413SAkash Asthana 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1678f3974413SAkash Asthana 		uart_remove_one_port(drv, uport);
1679300894a6SYangtao Li 		return ret;
1680f3974413SAkash Asthana 	}
1681f3974413SAkash Asthana 
1682f3974413SAkash Asthana 	/*
1683f3974413SAkash Asthana 	 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1684f3974413SAkash Asthana 	 * enabled/disabled from dev_pm_arm_wake_irq during system
1685f3974413SAkash Asthana 	 * suspend/resume respectively.
1686f3974413SAkash Asthana 	 */
1687f3974413SAkash Asthana 	pm_runtime_set_active(&pdev->dev);
1688f3974413SAkash Asthana 
1689f3974413SAkash Asthana 	if (port->wakeup_irq > 0) {
1690f3974413SAkash Asthana 		device_init_wakeup(&pdev->dev, true);
1691f3974413SAkash Asthana 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1692f3974413SAkash Asthana 						port->wakeup_irq);
1693f3974413SAkash Asthana 		if (ret) {
1694f3974413SAkash Asthana 			device_init_wakeup(&pdev->dev, false);
1695f3974413SAkash Asthana 			uart_remove_one_port(drv, uport);
1696300894a6SYangtao Li 			return ret;
1697f3974413SAkash Asthana 		}
1698f3974413SAkash Asthana 	}
1699f3974413SAkash Asthana 
1700f3974413SAkash Asthana 	return 0;
1701c4f52879SKarthikeyan Ramasubramanian }
1702c4f52879SKarthikeyan Ramasubramanian 
1703c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1704c4f52879SKarthikeyan Ramasubramanian {
1705c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1706e42d6c3eSDouglas Anderson 	struct uart_driver *drv = port->private_data.drv;
1707c4f52879SKarthikeyan Ramasubramanian 
1708f3974413SAkash Asthana 	dev_pm_clear_wake_irq(&pdev->dev);
1709f3974413SAkash Asthana 	device_init_wakeup(&pdev->dev, false);
1710c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1711f3974413SAkash Asthana 
1712c4f52879SKarthikeyan Ramasubramanian 	return 0;
1713c4f52879SKarthikeyan Ramasubramanian }
1714c4f52879SKarthikeyan Ramasubramanian 
17155342ab0aSArnd Bergmann static int qcom_geni_serial_sys_suspend(struct device *dev)
1716c4f52879SKarthikeyan Ramasubramanian {
1717a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1718c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1719e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1720c4f52879SKarthikeyan Ramasubramanian 
17214a3107f6SRajendra Nayak 	/*
17224a3107f6SRajendra Nayak 	 * This is done so we can hit the lowest possible state in suspend
17234a3107f6SRajendra Nayak 	 * even with no_console_suspend
17244a3107f6SRajendra Nayak 	 */
17254a3107f6SRajendra Nayak 	if (uart_console(uport)) {
1726408e532eSVijaya Krishna Nivarthi 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
17274a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
17284a3107f6SRajendra Nayak 	}
1729e42d6c3eSDouglas Anderson 	return uart_suspend_port(private_data->drv, uport);
17308a8a66a1SGirish Mahadevan }
17318a8a66a1SGirish Mahadevan 
17325342ab0aSArnd Bergmann static int qcom_geni_serial_sys_resume(struct device *dev)
1733c4f52879SKarthikeyan Ramasubramanian {
17344a3107f6SRajendra Nayak 	int ret;
1735a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1736c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1737e42d6c3eSDouglas Anderson 	struct qcom_geni_private_data *private_data = uport->private_data;
1738c4f52879SKarthikeyan Ramasubramanian 
17394a3107f6SRajendra Nayak 	ret = uart_resume_port(private_data->drv, uport);
17404a3107f6SRajendra Nayak 	if (uart_console(uport)) {
1741408e532eSVijaya Krishna Nivarthi 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
17424a3107f6SRajendra Nayak 		geni_icc_set_bw(&port->se);
17434a3107f6SRajendra Nayak 	}
17444a3107f6SRajendra Nayak 	return ret;
1745c4f52879SKarthikeyan Ramasubramanian }
1746c4f52879SKarthikeyan Ramasubramanian 
174735781d83SAniket Randive static int qcom_geni_serial_sys_hib_resume(struct device *dev)
174835781d83SAniket Randive {
174935781d83SAniket Randive 	int ret = 0;
175035781d83SAniket Randive 	struct uart_port *uport;
175135781d83SAniket Randive 	struct qcom_geni_private_data *private_data;
175235781d83SAniket Randive 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
175335781d83SAniket Randive 
175435781d83SAniket Randive 	uport = &port->uport;
175535781d83SAniket Randive 	private_data = uport->private_data;
175635781d83SAniket Randive 
175735781d83SAniket Randive 	if (uart_console(uport)) {
175835781d83SAniket Randive 		geni_icc_set_tag(&port->se, 0x7);
175935781d83SAniket Randive 		geni_icc_set_bw(&port->se);
176035781d83SAniket Randive 		ret = uart_resume_port(private_data->drv, uport);
176135781d83SAniket Randive 		/*
176235781d83SAniket Randive 		 * For hibernation usecase clients for
176335781d83SAniket Randive 		 * console UART won't call port setup during restore,
176435781d83SAniket Randive 		 * hence call port setup for console uart.
176535781d83SAniket Randive 		 */
176635781d83SAniket Randive 		qcom_geni_serial_port_setup(uport);
176735781d83SAniket Randive 	} else {
176835781d83SAniket Randive 		/*
176935781d83SAniket Randive 		 * Peripheral register settings are lost during hibernation.
177035781d83SAniket Randive 		 * Update setup flag such that port setup happens again
177135781d83SAniket Randive 		 * during next session. Clients of HS-UART will close and
177235781d83SAniket Randive 		 * open the port during hibernation.
177335781d83SAniket Randive 		 */
177435781d83SAniket Randive 		port->setup = false;
177535781d83SAniket Randive 	}
177635781d83SAniket Randive 	return ret;
177735781d83SAniket Randive }
177835781d83SAniket Randive 
177940ec6d41SBartosz Golaszewski static const struct qcom_geni_device_data qcom_geni_console_data = {
178040ec6d41SBartosz Golaszewski 	.console = true,
17812aaa43c7SBartosz Golaszewski 	.mode = GENI_SE_FIFO,
178240ec6d41SBartosz Golaszewski };
178340ec6d41SBartosz Golaszewski 
178440ec6d41SBartosz Golaszewski static const struct qcom_geni_device_data qcom_geni_uart_data = {
178540ec6d41SBartosz Golaszewski 	.console = false,
17862aaa43c7SBartosz Golaszewski 	.mode = GENI_SE_DMA,
178740ec6d41SBartosz Golaszewski };
178840ec6d41SBartosz Golaszewski 
1789c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
17905342ab0aSArnd Bergmann 	.suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17915342ab0aSArnd Bergmann 	.resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
17925342ab0aSArnd Bergmann 	.freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17935342ab0aSArnd Bergmann 	.poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
17945342ab0aSArnd Bergmann 	.restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
17955342ab0aSArnd Bergmann 	.thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1796c4f52879SKarthikeyan Ramasubramanian };
1797c4f52879SKarthikeyan Ramasubramanian 
1798c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
179940ec6d41SBartosz Golaszewski 	{
180040ec6d41SBartosz Golaszewski 		.compatible = "qcom,geni-debug-uart",
180140ec6d41SBartosz Golaszewski 		.data = &qcom_geni_console_data,
180240ec6d41SBartosz Golaszewski 	},
180340ec6d41SBartosz Golaszewski 	{
180440ec6d41SBartosz Golaszewski 		.compatible = "qcom,geni-uart",
180540ec6d41SBartosz Golaszewski 		.data = &qcom_geni_uart_data,
180640ec6d41SBartosz Golaszewski 	},
1807c4f52879SKarthikeyan Ramasubramanian 	{}
1808c4f52879SKarthikeyan Ramasubramanian };
1809c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1810c4f52879SKarthikeyan Ramasubramanian 
1811c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1812c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1813c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1814c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1815c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1816c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1817c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1818c4f52879SKarthikeyan Ramasubramanian 	},
1819c4f52879SKarthikeyan Ramasubramanian };
1820c4f52879SKarthikeyan Ramasubramanian 
1821c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1822c4f52879SKarthikeyan Ramasubramanian {
1823c4f52879SKarthikeyan Ramasubramanian 	int ret;
1824c4f52879SKarthikeyan Ramasubramanian 
1825c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1826c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1827c4f52879SKarthikeyan Ramasubramanian 		return ret;
1828c4f52879SKarthikeyan Ramasubramanian 
18298a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
18308a8a66a1SGirish Mahadevan 	if (ret) {
1831c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1832c4f52879SKarthikeyan Ramasubramanian 		return ret;
1833c4f52879SKarthikeyan Ramasubramanian 	}
18348a8a66a1SGirish Mahadevan 
18358a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
18368a8a66a1SGirish Mahadevan 	if (ret) {
18378a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
18388a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
18398a8a66a1SGirish Mahadevan 	}
18408a8a66a1SGirish Mahadevan 	return ret;
18418a8a66a1SGirish Mahadevan }
1842c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1843c4f52879SKarthikeyan Ramasubramanian 
1844c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1845c4f52879SKarthikeyan Ramasubramanian {
1846c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1847c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
18488a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1849c4f52879SKarthikeyan Ramasubramanian }
1850c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1851c4f52879SKarthikeyan Ramasubramanian 
1852c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1853c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1854