xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision 7034ef87fa880defb4cfc2ca552641d26f425f21)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
4c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
5c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
6c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
7c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
8c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
11c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
12c4f52879SKarthikeyan Ramasubramanian #include <linux/qcom-geni-se.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
14c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
15c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
17c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
18c4f52879SKarthikeyan Ramasubramanian 
19c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
208a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
21c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
22c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
23c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
24c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
25c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
26c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
27c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
28c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
29c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
308a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
31c4f52879SKarthikeyan Ramasubramanian 
32c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
33c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN		BIT(0)
34c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK		BIT(1)
35c4f52879SKarthikeyan Ramasubramanian 
36c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_WORD_LEN */
37c4f52879SKarthikeyan Ramasubramanian #define TX_WORD_LEN_MSK		GENMASK(9, 0)
38c4f52879SKarthikeyan Ramasubramanian 
39c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
40c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_MSK	GENMASK(23, 0)
41c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1	0
42c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1_5	1
43c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2	2
44c4f52879SKarthikeyan Ramasubramanian 
45c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_TRANS_LEN */
46c4f52879SKarthikeyan Ramasubramanian #define TX_TRANS_LEN_MSK	GENMASK(23, 0)
47c4f52879SKarthikeyan Ramasubramanian 
48c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
49c4f52879SKarthikeyan Ramasubramanian #define UART_RX_INS_STATUS_BIT	BIT(2)
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_MODE_MSK		GENMASK(2, 1)
61c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_SHFT		1
62c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN		0x00
63c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD			0x01
64c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE		0x10
65c4f52879SKarthikeyan Ramasubramanian #define PAR_MARK		0x11
66c4f52879SKarthikeyan Ramasubramanian 
678a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
688a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN	BIT(31)
698a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY	BIT(1)
708a8a66a1SGirish Mahadevan #define UART_RFR_READY		BIT(0)
718a8a66a1SGirish Mahadevan 
72c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
73c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX		0x1
74c4f52879SKarthikeyan Ramasubramanian #define UART_START_BREAK	0x4
75c4f52879SKarthikeyan Ramasubramanian #define UART_STOP_BREAK		0x5
76c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
77c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ		0x1
78c4f52879SKarthikeyan Ramasubramanian #define UART_PARAM		0x1
79c4f52879SKarthikeyan Ramasubramanian 
80c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING	32
81c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT		16
82c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR	10
83c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS	1
848a8a66a1SGirish Mahadevan #define GENI_UART_PORTS		3
85c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS	16
86c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM		2
87c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS	32
88c4f52879SKarthikeyan Ramasubramanian #define UART_CONSOLE_RX_WM	2
898a8a66a1SGirish Mahadevan #define MAX_LOOPBACK_CFG	3
90c4f52879SKarthikeyan Ramasubramanian 
91c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
929f641df4SDouglas Anderson #define CONSOLE_RX_BYTES_PW 1
93c4f52879SKarthikeyan Ramasubramanian #else
949f641df4SDouglas Anderson #define CONSOLE_RX_BYTES_PW 4
95c4f52879SKarthikeyan Ramasubramanian #endif
96c4f52879SKarthikeyan Ramasubramanian 
97c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
98c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
99c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
100c4f52879SKarthikeyan Ramasubramanian 	char name[20];
101c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
102c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
103c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
104c4f52879SKarthikeyan Ramasubramanian 	u32 tx_wm;
105c4f52879SKarthikeyan Ramasubramanian 	u32 rx_wm;
106c4f52879SKarthikeyan Ramasubramanian 	u32 rx_rfr;
107c4f52879SKarthikeyan Ramasubramanian 	enum geni_se_xfer_mode xfer_mode;
108c4f52879SKarthikeyan Ramasubramanian 	bool setup;
109c4f52879SKarthikeyan Ramasubramanian 	int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
110c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
111c4f52879SKarthikeyan Ramasubramanian 	unsigned int tx_bytes_pw;
112c4f52879SKarthikeyan Ramasubramanian 	unsigned int rx_bytes_pw;
1138a8a66a1SGirish Mahadevan 	u32 *rx_fifo;
1148a8a66a1SGirish Mahadevan 	u32 loopback;
115c4f52879SKarthikeyan Ramasubramanian 	bool brk;
116c4f52879SKarthikeyan Ramasubramanian };
117c4f52879SKarthikeyan Ramasubramanian 
118f7371750SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops;
1198a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops;
120c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver;
1218a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver;
122c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
1238a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
124c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
125c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport);
126c4f52879SKarthikeyan Ramasubramanian 
127c4f52879SKarthikeyan Ramasubramanian static const unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
128c4f52879SKarthikeyan Ramasubramanian 					32000000, 48000000, 64000000, 80000000,
1298a8a66a1SGirish Mahadevan 					96000000, 100000000, 102400000,
1308a8a66a1SGirish Mahadevan 					112000000, 120000000, 128000000};
131c4f52879SKarthikeyan Ramasubramanian 
132c4f52879SKarthikeyan Ramasubramanian #define to_dev_port(ptr, member) \
133c4f52879SKarthikeyan Ramasubramanian 		container_of(ptr, struct qcom_geni_serial_port, member)
134c4f52879SKarthikeyan Ramasubramanian 
1358a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1368a8a66a1SGirish Mahadevan 	[0] = {
1378a8a66a1SGirish Mahadevan 		.uport = {
1388a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1398a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1408a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1418a8a66a1SGirish Mahadevan 				.line = 0,
1428a8a66a1SGirish Mahadevan 		},
1438a8a66a1SGirish Mahadevan 	},
1448a8a66a1SGirish Mahadevan 	[1] = {
1458a8a66a1SGirish Mahadevan 		.uport = {
1468a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1478a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1488a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1498a8a66a1SGirish Mahadevan 				.line = 1,
1508a8a66a1SGirish Mahadevan 		},
1518a8a66a1SGirish Mahadevan 	},
1528a8a66a1SGirish Mahadevan 	[2] = {
1538a8a66a1SGirish Mahadevan 		.uport = {
1548a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1558a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1568a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1578a8a66a1SGirish Mahadevan 				.line = 2,
1588a8a66a1SGirish Mahadevan 		},
1598a8a66a1SGirish Mahadevan 	},
1608a8a66a1SGirish Mahadevan };
1618a8a66a1SGirish Mahadevan 
1628a8a66a1SGirish Mahadevan static ssize_t loopback_show(struct device *dev,
1638a8a66a1SGirish Mahadevan 				struct device_attribute *attr, char *buf)
1648a8a66a1SGirish Mahadevan {
165*7034ef87SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1668a8a66a1SGirish Mahadevan 
1678a8a66a1SGirish Mahadevan 	return snprintf(buf, sizeof(u32), "%d\n", port->loopback);
1688a8a66a1SGirish Mahadevan }
1698a8a66a1SGirish Mahadevan 
1708a8a66a1SGirish Mahadevan static ssize_t loopback_store(struct device *dev,
1718a8a66a1SGirish Mahadevan 				struct device_attribute *attr, const char *buf,
1728a8a66a1SGirish Mahadevan 				size_t size)
1738a8a66a1SGirish Mahadevan {
174*7034ef87SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1758a8a66a1SGirish Mahadevan 	u32 loopback;
1768a8a66a1SGirish Mahadevan 
1778a8a66a1SGirish Mahadevan 	if (kstrtoint(buf, 0, &loopback) || loopback > MAX_LOOPBACK_CFG) {
1788a8a66a1SGirish Mahadevan 		dev_err(dev, "Invalid input\n");
1798a8a66a1SGirish Mahadevan 		return -EINVAL;
1808a8a66a1SGirish Mahadevan 	}
1818a8a66a1SGirish Mahadevan 	port->loopback = loopback;
1828a8a66a1SGirish Mahadevan 	return size;
1838a8a66a1SGirish Mahadevan }
1848a8a66a1SGirish Mahadevan static DEVICE_ATTR_RW(loopback);
1858a8a66a1SGirish Mahadevan 
186f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
187f7371750SKarthikeyan Ramasubramanian 	.uport = {
188f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
189f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
190f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
191f7371750SKarthikeyan Ramasubramanian 		.line = 0,
192f7371750SKarthikeyan Ramasubramanian 	},
193f7371750SKarthikeyan Ramasubramanian };
194c4f52879SKarthikeyan Ramasubramanian 
195c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
196c4f52879SKarthikeyan Ramasubramanian {
197c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
198c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
199c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
200c4f52879SKarthikeyan Ramasubramanian 
201c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
202c4f52879SKarthikeyan Ramasubramanian 	uport->membase = devm_ioremap_resource(&pdev->dev, res);
203c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
204c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
205c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
206c4f52879SKarthikeyan Ramasubramanian 	return 0;
207c4f52879SKarthikeyan Ramasubramanian }
208c4f52879SKarthikeyan Ramasubramanian 
209c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
210c4f52879SKarthikeyan Ramasubramanian {
211c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
212c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
213c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
214c4f52879SKarthikeyan Ramasubramanian 	}
215c4f52879SKarthikeyan Ramasubramanian }
216c4f52879SKarthikeyan Ramasubramanian 
2178a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
218c4f52879SKarthikeyan Ramasubramanian {
2198a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2208a8a66a1SGirish Mahadevan 	u32 geni_ios;
2218a8a66a1SGirish Mahadevan 
2228a8a66a1SGirish Mahadevan 	if (uart_console(uport) || !uart_cts_enabled(uport)) {
2238a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2248a8a66a1SGirish Mahadevan 	} else {
2258a8a66a1SGirish Mahadevan 		geni_ios = readl_relaxed(uport->membase + SE_GENI_IOS);
2268a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2278a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
228c4f52879SKarthikeyan Ramasubramanian 	}
229c4f52879SKarthikeyan Ramasubramanian 
2308a8a66a1SGirish Mahadevan 	return mctrl;
2318a8a66a1SGirish Mahadevan }
2328a8a66a1SGirish Mahadevan 
2338a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
234c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
235c4f52879SKarthikeyan Ramasubramanian {
2368a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
2378a8a66a1SGirish Mahadevan 
2388a8a66a1SGirish Mahadevan 	if (uart_console(uport) || !uart_cts_enabled(uport))
2398a8a66a1SGirish Mahadevan 		return;
2408a8a66a1SGirish Mahadevan 
2418a8a66a1SGirish Mahadevan 	if (!(mctrl & TIOCM_RTS))
2428a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2438a8a66a1SGirish Mahadevan 	writel_relaxed(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
244c4f52879SKarthikeyan Ramasubramanian }
245c4f52879SKarthikeyan Ramasubramanian 
246c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
247c4f52879SKarthikeyan Ramasubramanian {
248c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
249c4f52879SKarthikeyan Ramasubramanian }
250c4f52879SKarthikeyan Ramasubramanian 
2518a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
252c4f52879SKarthikeyan Ramasubramanian {
2538a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2548a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2558a8a66a1SGirish Mahadevan 
2568a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
257c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2588a8a66a1SGirish Mahadevan 
2598a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2608a8a66a1SGirish Mahadevan 	return port;
261c4f52879SKarthikeyan Ramasubramanian }
262c4f52879SKarthikeyan Ramasubramanian 
263c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
264c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
265c4f52879SKarthikeyan Ramasubramanian {
266c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
267c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
268c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
269c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
270c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
271c4f52879SKarthikeyan Ramasubramanian 
272c4f52879SKarthikeyan Ramasubramanian 	/* Ensure polling is not re-ordered before the prior writes/reads */
273c4f52879SKarthikeyan Ramasubramanian 	mb();
274c4f52879SKarthikeyan Ramasubramanian 
275c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data) {
276c4f52879SKarthikeyan Ramasubramanian 		port = to_dev_port(uport, uport);
277c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
278c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
279c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
280c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
281c4f52879SKarthikeyan Ramasubramanian 		/*
282c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
283c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
284c4f52879SKarthikeyan Ramasubramanian 		 */
285c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
286c4f52879SKarthikeyan Ramasubramanian 	}
287c4f52879SKarthikeyan Ramasubramanian 
28843f1831bSKarthikeyan Ramasubramanian 	/*
28943f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
29043f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29143f1831bSKarthikeyan Ramasubramanian 	 */
29243f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29343f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
29443f1831bSKarthikeyan Ramasubramanian 		reg = readl_relaxed(uport->membase + offset);
29543f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29643f1831bSKarthikeyan Ramasubramanian 			return true;
29743f1831bSKarthikeyan Ramasubramanian 		udelay(10);
29843f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
29943f1831bSKarthikeyan Ramasubramanian 	}
30043f1831bSKarthikeyan Ramasubramanian 	return false;
301c4f52879SKarthikeyan Ramasubramanian }
302c4f52879SKarthikeyan Ramasubramanian 
303c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
304c4f52879SKarthikeyan Ramasubramanian {
305c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
306c4f52879SKarthikeyan Ramasubramanian 
307c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
308c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
309c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
310c4f52879SKarthikeyan Ramasubramanian }
311c4f52879SKarthikeyan Ramasubramanian 
312c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
313c4f52879SKarthikeyan Ramasubramanian {
314c4f52879SKarthikeyan Ramasubramanian 	int done;
315c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
316c4f52879SKarthikeyan Ramasubramanian 
317c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
318c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
319c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
320c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_GENI_CMD_ABORT, uport->membase +
321c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
322c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
323c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
324c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
325c4f52879SKarthikeyan Ramasubramanian 	}
326c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
327c4f52879SKarthikeyan Ramasubramanian }
328c4f52879SKarthikeyan Ramasubramanian 
329c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
330c4f52879SKarthikeyan Ramasubramanian {
331c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
332c4f52879SKarthikeyan Ramasubramanian 
333c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
334c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
335c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
336c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
337c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
338c4f52879SKarthikeyan Ramasubramanian }
339c4f52879SKarthikeyan Ramasubramanian 
340c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
341c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
342c4f52879SKarthikeyan Ramasubramanian {
343c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo;
344c4f52879SKarthikeyan Ramasubramanian 	u32 status;
345c4f52879SKarthikeyan Ramasubramanian 
346c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS);
347c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
348c4f52879SKarthikeyan Ramasubramanian 
349c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS);
350c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
351c4f52879SKarthikeyan Ramasubramanian 
352c4f52879SKarthikeyan Ramasubramanian 	/*
353c4f52879SKarthikeyan Ramasubramanian 	 * Ensure the writes to clear interrupts is not re-ordered after
354c4f52879SKarthikeyan Ramasubramanian 	 * reading the data.
355c4f52879SKarthikeyan Ramasubramanian 	 */
356c4f52879SKarthikeyan Ramasubramanian 	mb();
357c4f52879SKarthikeyan Ramasubramanian 
358c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_RX_FIFO_STATUS);
359c4f52879SKarthikeyan Ramasubramanian 	if (!(status & RX_FIFO_WC_MSK))
360c4f52879SKarthikeyan Ramasubramanian 		return NO_POLL_CHAR;
361c4f52879SKarthikeyan Ramasubramanian 
362c4f52879SKarthikeyan Ramasubramanian 	rx_fifo = readl(uport->membase + SE_GENI_RX_FIFOn);
363c4f52879SKarthikeyan Ramasubramanian 	return rx_fifo & 0xff;
364c4f52879SKarthikeyan Ramasubramanian }
365c4f52879SKarthikeyan Ramasubramanian 
366c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
367c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
368c4f52879SKarthikeyan Ramasubramanian {
369c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
370c4f52879SKarthikeyan Ramasubramanian 
371c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(port->tx_wm, uport->membase + SE_GENI_TX_WATERMARK_REG);
372c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
373c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
374c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
375c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(c, uport->membase + SE_GENI_TX_FIFOn);
376c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase +
377c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
378c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
379c4f52879SKarthikeyan Ramasubramanian }
380c4f52879SKarthikeyan Ramasubramanian #endif
381c4f52879SKarthikeyan Ramasubramanian 
382c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
383c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch)
384c4f52879SKarthikeyan Ramasubramanian {
385c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(ch, uport->membase + SE_GENI_TX_FIFOn);
386c4f52879SKarthikeyan Ramasubramanian }
387c4f52879SKarthikeyan Ramasubramanian 
388c4f52879SKarthikeyan Ramasubramanian static void
389c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
390c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
391c4f52879SKarthikeyan Ramasubramanian {
392c4f52879SKarthikeyan Ramasubramanian 	int i;
393c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
394c4f52879SKarthikeyan Ramasubramanian 
395c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
396f0262568SKarthikeyan Ramasubramanian 		/*
397f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
398f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
399f0262568SKarthikeyan Ramasubramanian 		 */
400c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
401c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
402c4f52879SKarthikeyan Ramasubramanian 	}
403c4f52879SKarthikeyan Ramasubramanian 
404c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
405c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
406c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
407c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
408c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
409c4f52879SKarthikeyan Ramasubramanian 
410c4f52879SKarthikeyan Ramasubramanian 		/*
411c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
412c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
413c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
414c4f52879SKarthikeyan Ramasubramanian 		 * lost.
415c4f52879SKarthikeyan Ramasubramanian 		 */
416c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
417c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
418c4f52879SKarthikeyan Ramasubramanian 			break;
4196a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
420c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
421c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
422c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase +
423c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
424c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
425c4f52879SKarthikeyan Ramasubramanian 	}
426c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
427c4f52879SKarthikeyan Ramasubramanian }
428c4f52879SKarthikeyan Ramasubramanian 
429c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
430c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
431c4f52879SKarthikeyan Ramasubramanian {
432c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
433c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
434c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
435c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
436c4f52879SKarthikeyan Ramasubramanian 
437c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
438c4f52879SKarthikeyan Ramasubramanian 
4398a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
440c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
441c4f52879SKarthikeyan Ramasubramanian 		return;
442c4f52879SKarthikeyan Ramasubramanian 
443c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
444c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
445c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
446c4f52879SKarthikeyan Ramasubramanian 	else
447c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
448c4f52879SKarthikeyan Ramasubramanian 
449c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
450c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
451c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
452c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
453c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
454c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
455c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
456c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
457c4f52879SKarthikeyan Ramasubramanian 			writel_relaxed(M_CMD_ABORT_EN, uport->membase +
458c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
459c4f52879SKarthikeyan Ramasubramanian 		}
460c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_CMD_CANCEL_EN, uport->membase +
461c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
462c4f52879SKarthikeyan Ramasubramanian 	}
463c4f52879SKarthikeyan Ramasubramanian 
464c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
465c4f52879SKarthikeyan Ramasubramanian 	if (locked)
466c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
467c4f52879SKarthikeyan Ramasubramanian }
468c4f52879SKarthikeyan Ramasubramanian 
469c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
470c4f52879SKarthikeyan Ramasubramanian {
471c4f52879SKarthikeyan Ramasubramanian 	u32 i;
472c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
473c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
474c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
475c4f52879SKarthikeyan Ramasubramanian 
476c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
477c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
478c4f52879SKarthikeyan Ramasubramanian 		int c;
479c4f52879SKarthikeyan Ramasubramanian 		int chunk = min_t(int, bytes - i, port->rx_bytes_pw);
480c4f52879SKarthikeyan Ramasubramanian 
481c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
482c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
483c4f52879SKarthikeyan Ramasubramanian 		if (drop)
484c4f52879SKarthikeyan Ramasubramanian 			continue;
485c4f52879SKarthikeyan Ramasubramanian 
486c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
487c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
488c4f52879SKarthikeyan Ramasubramanian 
489c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
490c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
491c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
492c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
493c4f52879SKarthikeyan Ramasubramanian 					continue;
494c4f52879SKarthikeyan Ramasubramanian 			}
495c4f52879SKarthikeyan Ramasubramanian 
496c4f52879SKarthikeyan Ramasubramanian 			sysrq = uart_handle_sysrq_char(uport, buf[c]);
497c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
498c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
499c4f52879SKarthikeyan Ramasubramanian 		}
500c4f52879SKarthikeyan Ramasubramanian 	}
501c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
502c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
503c4f52879SKarthikeyan Ramasubramanian 	return 0;
504c4f52879SKarthikeyan Ramasubramanian }
505c4f52879SKarthikeyan Ramasubramanian #else
506c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
507c4f52879SKarthikeyan Ramasubramanian {
508c4f52879SKarthikeyan Ramasubramanian 	return -EPERM;
509c4f52879SKarthikeyan Ramasubramanian }
510c4f52879SKarthikeyan Ramasubramanian 
511c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
512c4f52879SKarthikeyan Ramasubramanian 
5138a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5148a8a66a1SGirish Mahadevan {
5158a8a66a1SGirish Mahadevan 	unsigned char *buf;
5168a8a66a1SGirish Mahadevan 	struct tty_port *tport;
5178a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
5188a8a66a1SGirish Mahadevan 	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
5198a8a66a1SGirish Mahadevan 	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
5208a8a66a1SGirish Mahadevan 	int ret;
5218a8a66a1SGirish Mahadevan 
5228a8a66a1SGirish Mahadevan 	tport = &uport->state->port;
5238a8a66a1SGirish Mahadevan 	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
5248a8a66a1SGirish Mahadevan 	if (drop)
5258a8a66a1SGirish Mahadevan 		return 0;
5268a8a66a1SGirish Mahadevan 
5278a8a66a1SGirish Mahadevan 	buf = (unsigned char *)port->rx_fifo;
5288a8a66a1SGirish Mahadevan 	ret = tty_insert_flip_string(tport, buf, bytes);
5298a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5308a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5318a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5328a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5338a8a66a1SGirish Mahadevan 	}
5348a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5358a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5368a8a66a1SGirish Mahadevan 	return ret;
5378a8a66a1SGirish Mahadevan }
5388a8a66a1SGirish Mahadevan 
539c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_tx(struct uart_port *uport)
540c4f52879SKarthikeyan Ramasubramanian {
541c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
542c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
543c4f52879SKarthikeyan Ramasubramanian 	u32 status;
544c4f52879SKarthikeyan Ramasubramanian 
545c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
5467fb5b880SKarthikeyan Ramasubramanian 		/*
5477fb5b880SKarthikeyan Ramasubramanian 		 * readl ensures reading & writing of IRQ_EN register
5487fb5b880SKarthikeyan Ramasubramanian 		 * is not re-ordered before checking the status of the
5497fb5b880SKarthikeyan Ramasubramanian 		 * Serial Engine.
5507fb5b880SKarthikeyan Ramasubramanian 		 */
5517fb5b880SKarthikeyan Ramasubramanian 		status = readl(uport->membase + SE_GENI_STATUS);
552c4f52879SKarthikeyan Ramasubramanian 		if (status & M_GENI_CMD_ACTIVE)
553c4f52879SKarthikeyan Ramasubramanian 			return;
554c4f52879SKarthikeyan Ramasubramanian 
555c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_tx_empty(uport))
556c4f52879SKarthikeyan Ramasubramanian 			return;
557c4f52879SKarthikeyan Ramasubramanian 
558c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase +	SE_GENI_M_IRQ_EN);
559c4f52879SKarthikeyan Ramasubramanian 		irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
560c4f52879SKarthikeyan Ramasubramanian 
561c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(port->tx_wm, uport->membase +
562c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_TX_WATERMARK_REG);
563c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
564c4f52879SKarthikeyan Ramasubramanian 	}
565c4f52879SKarthikeyan Ramasubramanian }
566c4f52879SKarthikeyan Ramasubramanian 
567c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_tx(struct uart_port *uport)
568c4f52879SKarthikeyan Ramasubramanian {
569c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
570c4f52879SKarthikeyan Ramasubramanian 	u32 status;
571c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
572c4f52879SKarthikeyan Ramasubramanian 
573c4f52879SKarthikeyan Ramasubramanian 	irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
574c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~M_CMD_DONE_EN;
575c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
576c4f52879SKarthikeyan Ramasubramanian 		irq_en &= ~M_TX_FIFO_WATERMARK_EN;
577c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(0, uport->membase +
578c4f52879SKarthikeyan Ramasubramanian 				     SE_GENI_TX_WATERMARK_REG);
579c4f52879SKarthikeyan Ramasubramanian 	}
580c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
581c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
582c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
583c4f52879SKarthikeyan Ramasubramanian 	if (!(status & M_GENI_CMD_ACTIVE))
584c4f52879SKarthikeyan Ramasubramanian 		return;
585c4f52879SKarthikeyan Ramasubramanian 
586c4f52879SKarthikeyan Ramasubramanian 	/*
587c4f52879SKarthikeyan Ramasubramanian 	 * Ensure cancel command write is not re-ordered before checking
588c4f52879SKarthikeyan Ramasubramanian 	 * the status of the Primary Sequencer.
589c4f52879SKarthikeyan Ramasubramanian 	 */
590c4f52879SKarthikeyan Ramasubramanian 	mb();
591c4f52879SKarthikeyan Ramasubramanian 
592c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
593c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
594c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
595c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
596c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
597c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
598c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_CMD_ABORT_EN, uport->membase +
599c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
600c4f52879SKarthikeyan Ramasubramanian 	}
601c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
602c4f52879SKarthikeyan Ramasubramanian }
603c4f52879SKarthikeyan Ramasubramanian 
604c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_rx(struct uart_port *uport)
605c4f52879SKarthikeyan Ramasubramanian {
606c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
607c4f52879SKarthikeyan Ramasubramanian 	u32 status;
608c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
609c4f52879SKarthikeyan Ramasubramanian 
610c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
611c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
612c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_rx(uport);
613c4f52879SKarthikeyan Ramasubramanian 
614c4f52879SKarthikeyan Ramasubramanian 	/*
615c4f52879SKarthikeyan Ramasubramanian 	 * Ensure setup command write is not re-ordered before checking
616c4f52879SKarthikeyan Ramasubramanian 	 * the status of the Secondary Sequencer.
617c4f52879SKarthikeyan Ramasubramanian 	 */
618c4f52879SKarthikeyan Ramasubramanian 	mb();
619c4f52879SKarthikeyan Ramasubramanian 
620c4f52879SKarthikeyan Ramasubramanian 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
621c4f52879SKarthikeyan Ramasubramanian 
622c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
623c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN);
624c4f52879SKarthikeyan Ramasubramanian 		irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
625c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
626c4f52879SKarthikeyan Ramasubramanian 
627c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
628c4f52879SKarthikeyan Ramasubramanian 		irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
629c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
630c4f52879SKarthikeyan Ramasubramanian 	}
631c4f52879SKarthikeyan Ramasubramanian }
632c4f52879SKarthikeyan Ramasubramanian 
633c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport)
634c4f52879SKarthikeyan Ramasubramanian {
635c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
636c4f52879SKarthikeyan Ramasubramanian 	u32 status;
637c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
638c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN;
639c4f52879SKarthikeyan Ramasubramanian 
640c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
641c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN);
642c4f52879SKarthikeyan Ramasubramanian 		irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
643c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
644c4f52879SKarthikeyan Ramasubramanian 
645c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
646c4f52879SKarthikeyan Ramasubramanian 		irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
647c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
648c4f52879SKarthikeyan Ramasubramanian 	}
649c4f52879SKarthikeyan Ramasubramanian 
650c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
651c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop rx is called multiple times. */
652c4f52879SKarthikeyan Ramasubramanian 	if (!(status & S_GENI_CMD_ACTIVE))
653c4f52879SKarthikeyan Ramasubramanian 		return;
654c4f52879SKarthikeyan Ramasubramanian 
655c4f52879SKarthikeyan Ramasubramanian 	/*
656c4f52879SKarthikeyan Ramasubramanian 	 * Ensure cancel command write is not re-ordered before checking
657c4f52879SKarthikeyan Ramasubramanian 	 * the status of the Secondary Sequencer.
658c4f52879SKarthikeyan Ramasubramanian 	 */
659c4f52879SKarthikeyan Ramasubramanian 	mb();
660c4f52879SKarthikeyan Ramasubramanian 
661c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
662c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
663c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_CANCEL, false);
664c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
665c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
666c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
667c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
668c4f52879SKarthikeyan Ramasubramanian }
669c4f52879SKarthikeyan Ramasubramanian 
670c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
671c4f52879SKarthikeyan Ramasubramanian {
672c4f52879SKarthikeyan Ramasubramanian 	u32 status;
673c4f52879SKarthikeyan Ramasubramanian 	u32 word_cnt;
674c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_byte_cnt;
675c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_partial;
676c4f52879SKarthikeyan Ramasubramanian 	u32 total_bytes;
677c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
678c4f52879SKarthikeyan Ramasubramanian 
679c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase +	SE_GENI_RX_FIFO_STATUS);
680c4f52879SKarthikeyan Ramasubramanian 	word_cnt = status & RX_FIFO_WC_MSK;
681c4f52879SKarthikeyan Ramasubramanian 	last_word_partial = status & RX_LAST;
682c4f52879SKarthikeyan Ramasubramanian 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
683c4f52879SKarthikeyan Ramasubramanian 						RX_LAST_BYTE_VALID_SHFT;
684c4f52879SKarthikeyan Ramasubramanian 
685c4f52879SKarthikeyan Ramasubramanian 	if (!word_cnt)
686c4f52879SKarthikeyan Ramasubramanian 		return;
687c4f52879SKarthikeyan Ramasubramanian 	total_bytes = port->rx_bytes_pw * (word_cnt - 1);
688c4f52879SKarthikeyan Ramasubramanian 	if (last_word_partial && last_word_byte_cnt)
689c4f52879SKarthikeyan Ramasubramanian 		total_bytes += last_word_byte_cnt;
690c4f52879SKarthikeyan Ramasubramanian 	else
691c4f52879SKarthikeyan Ramasubramanian 		total_bytes += port->rx_bytes_pw;
692c4f52879SKarthikeyan Ramasubramanian 	port->handle_rx(uport, total_bytes, drop);
693c4f52879SKarthikeyan Ramasubramanian }
694c4f52879SKarthikeyan Ramasubramanian 
695c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_tx(struct uart_port *uport)
696c4f52879SKarthikeyan Ramasubramanian {
697c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
698c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
699c4f52879SKarthikeyan Ramasubramanian 	size_t avail;
700c4f52879SKarthikeyan Ramasubramanian 	size_t remaining;
701c4f52879SKarthikeyan Ramasubramanian 	int i;
702c4f52879SKarthikeyan Ramasubramanian 	u32 status;
703c4f52879SKarthikeyan Ramasubramanian 	unsigned int chunk;
704c4f52879SKarthikeyan Ramasubramanian 	int tail;
7058a8a66a1SGirish Mahadevan 	u32 irq_en;
706c4f52879SKarthikeyan Ramasubramanian 
707c4f52879SKarthikeyan Ramasubramanian 	chunk = uart_circ_chars_pending(xmit);
708c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_TX_FIFO_STATUS);
709c4f52879SKarthikeyan Ramasubramanian 	/* Both FIFO and framework buffer are drained */
710638a6f4eSEvan Green 	if (!chunk && !status) {
711c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_tx(uport);
712c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
713c4f52879SKarthikeyan Ramasubramanian 	}
714c4f52879SKarthikeyan Ramasubramanian 
7158a8a66a1SGirish Mahadevan 	if (!uart_console(uport)) {
7168a8a66a1SGirish Mahadevan 		irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
7178a8a66a1SGirish Mahadevan 		irq_en &= ~(M_TX_FIFO_WATERMARK_EN);
7188a8a66a1SGirish Mahadevan 		writel_relaxed(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
7198a8a66a1SGirish Mahadevan 		writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
7208a8a66a1SGirish Mahadevan 	}
7218a8a66a1SGirish Mahadevan 
722c4f52879SKarthikeyan Ramasubramanian 	avail = (port->tx_fifo_depth - port->tx_wm) * port->tx_bytes_pw;
723638a6f4eSEvan Green 	tail = xmit->tail;
7248e70c47cSKarthikeyan Ramasubramanian 	chunk = min3((size_t)chunk, (size_t)(UART_XMIT_SIZE - tail), avail);
725c4f52879SKarthikeyan Ramasubramanian 	if (!chunk)
726c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
727c4f52879SKarthikeyan Ramasubramanian 
728c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, chunk);
729c4f52879SKarthikeyan Ramasubramanian 
730c4f52879SKarthikeyan Ramasubramanian 	remaining = chunk;
731c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < chunk; ) {
732c4f52879SKarthikeyan Ramasubramanian 		unsigned int tx_bytes;
73369736b57SKarthikeyan Ramasubramanian 		u8 buf[sizeof(u32)];
734c4f52879SKarthikeyan Ramasubramanian 		int c;
735c4f52879SKarthikeyan Ramasubramanian 
73669736b57SKarthikeyan Ramasubramanian 		memset(buf, 0, ARRAY_SIZE(buf));
7376a10635eSKarthikeyan Ramasubramanian 		tx_bytes = min_t(size_t, remaining, port->tx_bytes_pw);
738c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < tx_bytes ; c++)
73969736b57SKarthikeyan Ramasubramanian 			buf[c] = xmit->buf[tail + c];
740c4f52879SKarthikeyan Ramasubramanian 
74169736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
742c4f52879SKarthikeyan Ramasubramanian 
743c4f52879SKarthikeyan Ramasubramanian 		i += tx_bytes;
744638a6f4eSEvan Green 		tail += tx_bytes;
745c4f52879SKarthikeyan Ramasubramanian 		uport->icount.tx += tx_bytes;
746c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
747c4f52879SKarthikeyan Ramasubramanian 	}
748638a6f4eSEvan Green 
749638a6f4eSEvan Green 	xmit->tail = tail & (UART_XMIT_SIZE - 1);
7508a8a66a1SGirish Mahadevan 	if (uart_console(uport))
751c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
752c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
753638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
754c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
755c4f52879SKarthikeyan Ramasubramanian }
756c4f52879SKarthikeyan Ramasubramanian 
757c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
758c4f52879SKarthikeyan Ramasubramanian {
759c4f52879SKarthikeyan Ramasubramanian 	unsigned int m_irq_status;
760c4f52879SKarthikeyan Ramasubramanian 	unsigned int s_irq_status;
761c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
762c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
763c4f52879SKarthikeyan Ramasubramanian 	unsigned int m_irq_en;
764c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
765c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
766c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
767c4f52879SKarthikeyan Ramasubramanian 
768c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
769ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
770c4f52879SKarthikeyan Ramasubramanian 
771c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
772c4f52879SKarthikeyan Ramasubramanian 	m_irq_status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS);
773c4f52879SKarthikeyan Ramasubramanian 	s_irq_status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS);
774c4f52879SKarthikeyan Ramasubramanian 	m_irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
775c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
776c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
777c4f52879SKarthikeyan Ramasubramanian 
778c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
779c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
780c4f52879SKarthikeyan Ramasubramanian 
781c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
782c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
783c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
784c4f52879SKarthikeyan Ramasubramanian 	}
785c4f52879SKarthikeyan Ramasubramanian 
786c4f52879SKarthikeyan Ramasubramanian 	if (m_irq_status & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN) &&
787c4f52879SKarthikeyan Ramasubramanian 	    m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
788c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_tx(uport);
789c4f52879SKarthikeyan Ramasubramanian 
790c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
791c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
792c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
793c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
794c4f52879SKarthikeyan Ramasubramanian 	} else if (s_irq_status & S_GP_IRQ_2_EN ||
795c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_GP_IRQ_3_EN) {
796c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
797c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
798c4f52879SKarthikeyan Ramasubramanian 	}
799c4f52879SKarthikeyan Ramasubramanian 
800c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
801c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_RX_FIFO_LAST_EN)
802c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_rx(uport, drop_rx);
803c4f52879SKarthikeyan Ramasubramanian 
804c4f52879SKarthikeyan Ramasubramanian out_unlock:
805c4f52879SKarthikeyan Ramasubramanian 	spin_unlock_irqrestore(&uport->lock, flags);
806c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
807c4f52879SKarthikeyan Ramasubramanian }
808c4f52879SKarthikeyan Ramasubramanian 
8096a10635eSKarthikeyan Ramasubramanian static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
810c4f52879SKarthikeyan Ramasubramanian {
811c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
812c4f52879SKarthikeyan Ramasubramanian 
813c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
814c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
815c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
816c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
817c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
818c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
819c4f52879SKarthikeyan Ramasubramanian }
820c4f52879SKarthikeyan Ramasubramanian 
821c4f52879SKarthikeyan Ramasubramanian static void set_rfr_wm(struct qcom_geni_serial_port *port)
822c4f52879SKarthikeyan Ramasubramanian {
823c4f52879SKarthikeyan Ramasubramanian 	/*
824c4f52879SKarthikeyan Ramasubramanian 	 * Set RFR (Flow off) to FIFO_DEPTH - 2.
825c4f52879SKarthikeyan Ramasubramanian 	 * RX WM level at 10% RX_FIFO_DEPTH.
826c4f52879SKarthikeyan Ramasubramanian 	 * TX WM level at 10% TX_FIFO_DEPTH.
827c4f52879SKarthikeyan Ramasubramanian 	 */
828c4f52879SKarthikeyan Ramasubramanian 	port->rx_rfr = port->rx_fifo_depth - 2;
829c4f52879SKarthikeyan Ramasubramanian 	port->rx_wm = UART_CONSOLE_RX_WM;
830c4f52879SKarthikeyan Ramasubramanian 	port->tx_wm = DEF_TX_WM;
831c4f52879SKarthikeyan Ramasubramanian }
832c4f52879SKarthikeyan Ramasubramanian 
833c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
834c4f52879SKarthikeyan Ramasubramanian {
835c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
836c4f52879SKarthikeyan Ramasubramanian 
837c4f52879SKarthikeyan Ramasubramanian 	/* Stop the console before stopping the current tx */
8388a8a66a1SGirish Mahadevan 	if (uart_console(uport))
839c4f52879SKarthikeyan Ramasubramanian 		console_stop(uport->cons);
840c4f52879SKarthikeyan Ramasubramanian 
841c4f52879SKarthikeyan Ramasubramanian 	free_irq(uport->irq, uport);
842c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
843c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_tx(uport);
844c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
845c4f52879SKarthikeyan Ramasubramanian 	spin_unlock_irqrestore(&uport->lock, flags);
846c4f52879SKarthikeyan Ramasubramanian }
847c4f52879SKarthikeyan Ramasubramanian 
848c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
849c4f52879SKarthikeyan Ramasubramanian {
850c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
851c4f52879SKarthikeyan Ramasubramanian 	unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
852c362272bSDouglas Anderson 	u32 proto;
853c362272bSDouglas Anderson 
8549f641df4SDouglas Anderson 	if (uart_console(uport)) {
855c362272bSDouglas Anderson 		port->tx_bytes_pw = 1;
8569f641df4SDouglas Anderson 		port->rx_bytes_pw = CONSOLE_RX_BYTES_PW;
8579f641df4SDouglas Anderson 	} else {
858c362272bSDouglas Anderson 		port->tx_bytes_pw = 4;
8599f641df4SDouglas Anderson 		port->rx_bytes_pw = 4;
8609f641df4SDouglas Anderson 	}
861c362272bSDouglas Anderson 
862c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
863c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
864c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
865c362272bSDouglas Anderson 		return -ENXIO;
866c362272bSDouglas Anderson 	}
867c362272bSDouglas Anderson 
868c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
869c362272bSDouglas Anderson 
870c362272bSDouglas Anderson 	get_tx_fifo_size(port);
871c4f52879SKarthikeyan Ramasubramanian 
872c4f52879SKarthikeyan Ramasubramanian 	set_rfr_wm(port);
873c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
874c4f52879SKarthikeyan Ramasubramanian 	/*
875c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
876c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
877c4f52879SKarthikeyan Ramasubramanian 	 */
878c4f52879SKarthikeyan Ramasubramanian 	port->xfer_mode = GENI_SE_FIFO;
8798a8a66a1SGirish Mahadevan 	if (uart_console(uport))
880c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
881c4f52879SKarthikeyan Ramasubramanian 	geni_se_config_packing(&port->se, BITS_PER_BYTE, port->tx_bytes_pw,
882c4f52879SKarthikeyan Ramasubramanian 						false, true, false);
883c4f52879SKarthikeyan Ramasubramanian 	geni_se_config_packing(&port->se, BITS_PER_BYTE, port->rx_bytes_pw,
884c4f52879SKarthikeyan Ramasubramanian 						false, false, true);
885c4f52879SKarthikeyan Ramasubramanian 	geni_se_init(&port->se, port->rx_wm, port->rx_rfr);
886c4f52879SKarthikeyan Ramasubramanian 	geni_se_select_mode(&port->se, port->xfer_mode);
8878a8a66a1SGirish Mahadevan 	if (!uart_console(uport)) {
888329e0989SKees Cook 		port->rx_fifo = devm_kcalloc(uport->dev,
889329e0989SKees Cook 			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
8908a8a66a1SGirish Mahadevan 		if (!port->rx_fifo)
8918a8a66a1SGirish Mahadevan 			return -ENOMEM;
8928a8a66a1SGirish Mahadevan 	}
893c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
894c362272bSDouglas Anderson 
895c4f52879SKarthikeyan Ramasubramanian 	return 0;
896c4f52879SKarthikeyan Ramasubramanian }
897c4f52879SKarthikeyan Ramasubramanian 
898c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
899c4f52879SKarthikeyan Ramasubramanian {
900c4f52879SKarthikeyan Ramasubramanian 	int ret;
901c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
902c4f52879SKarthikeyan Ramasubramanian 
903c4f52879SKarthikeyan Ramasubramanian 	scnprintf(port->name, sizeof(port->name),
9048a8a66a1SGirish Mahadevan 		  "qcom_serial_%s%d",
9058a8a66a1SGirish Mahadevan 		(uart_console(uport) ? "console" : "uart"), uport->line);
906c4f52879SKarthikeyan Ramasubramanian 
907c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
908c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
909c4f52879SKarthikeyan Ramasubramanian 		if (ret)
910c4f52879SKarthikeyan Ramasubramanian 			return ret;
911c4f52879SKarthikeyan Ramasubramanian 	}
912c4f52879SKarthikeyan Ramasubramanian 
913c4f52879SKarthikeyan Ramasubramanian 	ret = request_irq(uport->irq, qcom_geni_serial_isr, IRQF_TRIGGER_HIGH,
914c4f52879SKarthikeyan Ramasubramanian 							port->name, uport);
915c4f52879SKarthikeyan Ramasubramanian 	if (ret)
916c4f52879SKarthikeyan Ramasubramanian 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
917c4f52879SKarthikeyan Ramasubramanian 	return ret;
918c4f52879SKarthikeyan Ramasubramanian }
919c4f52879SKarthikeyan Ramasubramanian 
920c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_cfg(unsigned long clk_freq)
921c4f52879SKarthikeyan Ramasubramanian {
922c4f52879SKarthikeyan Ramasubramanian 	int i;
923c4f52879SKarthikeyan Ramasubramanian 
924c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
925c4f52879SKarthikeyan Ramasubramanian 		if (!(root_freq[i] % clk_freq))
926c4f52879SKarthikeyan Ramasubramanian 			return root_freq[i];
927c4f52879SKarthikeyan Ramasubramanian 	}
928c4f52879SKarthikeyan Ramasubramanian 	return 0;
929c4f52879SKarthikeyan Ramasubramanian }
930c4f52879SKarthikeyan Ramasubramanian 
931c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_div_rate(unsigned int baud, unsigned int *clk_div)
932c4f52879SKarthikeyan Ramasubramanian {
933c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
934c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
935c4f52879SKarthikeyan Ramasubramanian 
936c4f52879SKarthikeyan Ramasubramanian 	desired_clk = baud * UART_OVERSAMPLING;
937c4f52879SKarthikeyan Ramasubramanian 	ser_clk = get_clk_cfg(desired_clk);
938c4f52879SKarthikeyan Ramasubramanian 	if (!ser_clk) {
939c4f52879SKarthikeyan Ramasubramanian 		pr_err("%s: Can't find matching DFS entry for baud %d\n",
940c4f52879SKarthikeyan Ramasubramanian 								__func__, baud);
941c4f52879SKarthikeyan Ramasubramanian 		return ser_clk;
942c4f52879SKarthikeyan Ramasubramanian 	}
943c4f52879SKarthikeyan Ramasubramanian 
944c4f52879SKarthikeyan Ramasubramanian 	*clk_div = ser_clk / desired_clk;
945c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
946c4f52879SKarthikeyan Ramasubramanian }
947c4f52879SKarthikeyan Ramasubramanian 
948c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
949c4f52879SKarthikeyan Ramasubramanian 				struct ktermios *termios, struct ktermios *old)
950c4f52879SKarthikeyan Ramasubramanian {
951c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
952c4f52879SKarthikeyan Ramasubramanian 	unsigned int bits_per_char;
953c4f52879SKarthikeyan Ramasubramanian 	unsigned int tx_trans_cfg;
954c4f52879SKarthikeyan Ramasubramanian 	unsigned int tx_parity_cfg;
955c4f52879SKarthikeyan Ramasubramanian 	unsigned int rx_trans_cfg;
956c4f52879SKarthikeyan Ramasubramanian 	unsigned int rx_parity_cfg;
957c4f52879SKarthikeyan Ramasubramanian 	unsigned int stop_bit_len;
958c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
959c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk_cfg;
960c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
961c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
962c4f52879SKarthikeyan Ramasubramanian 
963c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
964c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
965c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
966c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
967c4f52879SKarthikeyan Ramasubramanian 	clk_rate = get_clk_div_rate(baud, &clk_div);
968c4f52879SKarthikeyan Ramasubramanian 	if (!clk_rate)
969c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
970c4f52879SKarthikeyan Ramasubramanian 
971c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
972c4f52879SKarthikeyan Ramasubramanian 	clk_set_rate(port->se.clk, clk_rate);
973c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
974c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
975c4f52879SKarthikeyan Ramasubramanian 
976c4f52879SKarthikeyan Ramasubramanian 	/* parity */
977c4f52879SKarthikeyan Ramasubramanian 	tx_trans_cfg = readl_relaxed(uport->membase + SE_UART_TX_TRANS_CFG);
978c4f52879SKarthikeyan Ramasubramanian 	tx_parity_cfg = readl_relaxed(uport->membase + SE_UART_TX_PARITY_CFG);
979c4f52879SKarthikeyan Ramasubramanian 	rx_trans_cfg = readl_relaxed(uport->membase + SE_UART_RX_TRANS_CFG);
980c4f52879SKarthikeyan Ramasubramanian 	rx_parity_cfg = readl_relaxed(uport->membase + SE_UART_RX_PARITY_CFG);
981c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
982c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
983c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
984c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
985c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
986c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
987c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
988c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
989c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
990c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
991c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
992c4f52879SKarthikeyan Ramasubramanian 		} else {
993c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
994c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
995c4f52879SKarthikeyan Ramasubramanian 		}
996c4f52879SKarthikeyan Ramasubramanian 	} else {
997c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
998c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
999c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1000c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1001c4f52879SKarthikeyan Ramasubramanian 	}
1002c4f52879SKarthikeyan Ramasubramanian 
1003c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
1004c4f52879SKarthikeyan Ramasubramanian 	switch (termios->c_cflag & CSIZE) {
1005c4f52879SKarthikeyan Ramasubramanian 	case CS5:
1006c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 5;
1007c4f52879SKarthikeyan Ramasubramanian 		break;
1008c4f52879SKarthikeyan Ramasubramanian 	case CS6:
1009c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 6;
1010c4f52879SKarthikeyan Ramasubramanian 		break;
1011c4f52879SKarthikeyan Ramasubramanian 	case CS7:
1012c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 7;
1013c4f52879SKarthikeyan Ramasubramanian 		break;
1014c4f52879SKarthikeyan Ramasubramanian 	case CS8:
1015c4f52879SKarthikeyan Ramasubramanian 	default:
1016c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 8;
1017c4f52879SKarthikeyan Ramasubramanian 		break;
1018c4f52879SKarthikeyan Ramasubramanian 	}
1019c4f52879SKarthikeyan Ramasubramanian 
1020c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1021c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1022c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1023c4f52879SKarthikeyan Ramasubramanian 	else
1024c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1025c4f52879SKarthikeyan Ramasubramanian 
1026c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1027c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1028c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1029c4f52879SKarthikeyan Ramasubramanian 	else
1030c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1031c4f52879SKarthikeyan Ramasubramanian 
1032c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1033c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1034c4f52879SKarthikeyan Ramasubramanian 
10358a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
10368a8a66a1SGirish Mahadevan 		writel_relaxed(port->loopback,
10378a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
1038c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1039c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1040c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1041c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1042c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1043c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1044c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1045c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1046c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1047c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1048c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1049c4f52879SKarthikeyan Ramasubramanian }
1050c4f52879SKarthikeyan Ramasubramanian 
1051c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1052c4f52879SKarthikeyan Ramasubramanian {
10537fb5b880SKarthikeyan Ramasubramanian 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1054c4f52879SKarthikeyan Ramasubramanian }
1055c4f52879SKarthikeyan Ramasubramanian 
1056c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1057c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_console_setup(struct console *co, char *options)
1058c4f52879SKarthikeyan Ramasubramanian {
1059c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1060c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1061c4f52879SKarthikeyan Ramasubramanian 	int baud;
1062c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1063c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1064c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1065c362272bSDouglas Anderson 	int ret;
1066c4f52879SKarthikeyan Ramasubramanian 
1067c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1068c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1069c4f52879SKarthikeyan Ramasubramanian 
10708a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1071c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
10726a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1073c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1074c4f52879SKarthikeyan Ramasubramanian 	}
1075c4f52879SKarthikeyan Ramasubramanian 
1076c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1077c4f52879SKarthikeyan Ramasubramanian 
1078c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1079c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1080c4f52879SKarthikeyan Ramasubramanian 
1081c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1082c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1083c362272bSDouglas Anderson 		if (ret)
1084c362272bSDouglas Anderson 			return ret;
1085c4f52879SKarthikeyan Ramasubramanian 	}
1086c4f52879SKarthikeyan Ramasubramanian 
1087c4f52879SKarthikeyan Ramasubramanian 	if (options)
1088c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1089c4f52879SKarthikeyan Ramasubramanian 
1090c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1091c4f52879SKarthikeyan Ramasubramanian }
1092c4f52879SKarthikeyan Ramasubramanian 
109343f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
109443f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
109543f1831bSKarthikeyan Ramasubramanian {
109643f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
109743f1831bSKarthikeyan Ramasubramanian 
109843f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
109943f1831bSKarthikeyan Ramasubramanian }
110043f1831bSKarthikeyan Ramasubramanian 
110143f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
110243f1831bSKarthikeyan Ramasubramanian 								const char *opt)
110343f1831bSKarthikeyan Ramasubramanian {
110443f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
110543f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
110643f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
110743f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
110843f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
110943f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
111043f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
111143f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
111243f1831bSKarthikeyan Ramasubramanian 
111343f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
111443f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
111543f1831bSKarthikeyan Ramasubramanian 
111643f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
111743f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
111843f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
111943f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
112043f1831bSKarthikeyan Ramasubramanian 	/*
112143f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
112243f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
112343f1831bSKarthikeyan Ramasubramanian 	 */
112443f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
112543f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
112643f1831bSKarthikeyan Ramasubramanian 
112743f1831bSKarthikeyan Ramasubramanian 	/*
112843f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
112943f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
113043f1831bSKarthikeyan Ramasubramanian 	 */
113143f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
113243f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
113343f1831bSKarthikeyan Ramasubramanian 	geni_se_config_packing(&se, BITS_PER_BYTE, 1, false, true, false);
113443f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
113543f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
113643f1831bSKarthikeyan Ramasubramanian 
113743f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
113843f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
113943f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
114043f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
114143f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
114243f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
114343f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
114443f1831bSKarthikeyan Ramasubramanian 
114543f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
114643f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
114743f1831bSKarthikeyan Ramasubramanian 	return 0;
114843f1831bSKarthikeyan Ramasubramanian }
114943f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
115043f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
115143f1831bSKarthikeyan Ramasubramanian 
1152c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1153c4f52879SKarthikeyan Ramasubramanian {
1154c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1155c4f52879SKarthikeyan Ramasubramanian }
1156c4f52879SKarthikeyan Ramasubramanian 
1157c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1158c4f52879SKarthikeyan Ramasubramanian {
1159c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1160c4f52879SKarthikeyan Ramasubramanian }
1161c4f52879SKarthikeyan Ramasubramanian 
1162c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1163c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1164c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1165c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1166c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1167c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1168c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1169c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1170c4f52879SKarthikeyan Ramasubramanian };
1171c4f52879SKarthikeyan Ramasubramanian 
1172c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1173c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1174c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1175c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1176c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1177c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1178c4f52879SKarthikeyan Ramasubramanian };
1179c4f52879SKarthikeyan Ramasubramanian #else
1180c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1181c4f52879SKarthikeyan Ramasubramanian {
1182c4f52879SKarthikeyan Ramasubramanian 	return 0;
1183c4f52879SKarthikeyan Ramasubramanian }
1184c4f52879SKarthikeyan Ramasubramanian 
1185c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1186c4f52879SKarthikeyan Ramasubramanian {
1187c4f52879SKarthikeyan Ramasubramanian }
1188c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1189c4f52879SKarthikeyan Ramasubramanian 
11908a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
11918a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
11928a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
11938a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
11948a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
11958a8a66a1SGirish Mahadevan };
11968a8a66a1SGirish Mahadevan 
11978a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1198c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1199c4f52879SKarthikeyan Ramasubramanian {
1200c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1201c4f52879SKarthikeyan Ramasubramanian 
1202c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1203c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1204c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1205c362272bSDouglas Anderson 
1206c4f52879SKarthikeyan Ramasubramanian 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
1207c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
1208c4f52879SKarthikeyan Ramasubramanian 	else if (new_state == UART_PM_STATE_OFF &&
1209c4f52879SKarthikeyan Ramasubramanian 			old_state == UART_PM_STATE_ON)
1210c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
1211c4f52879SKarthikeyan Ramasubramanian }
1212c4f52879SKarthikeyan Ramasubramanian 
1213c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1214c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
1215c4f52879SKarthikeyan Ramasubramanian 	.stop_tx = qcom_geni_serial_stop_tx,
1216c4f52879SKarthikeyan Ramasubramanian 	.start_tx = qcom_geni_serial_start_tx,
1217c4f52879SKarthikeyan Ramasubramanian 	.stop_rx = qcom_geni_serial_stop_rx,
1218c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1219c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1220c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1221c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1222c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1223c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
12248a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
12258a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1226c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1227c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1228c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1229c4f52879SKarthikeyan Ramasubramanian #endif
12308a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
12318a8a66a1SGirish Mahadevan };
12328a8a66a1SGirish Mahadevan 
12338a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
12348a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
12358a8a66a1SGirish Mahadevan 	.stop_tx = qcom_geni_serial_stop_tx,
12368a8a66a1SGirish Mahadevan 	.start_tx = qcom_geni_serial_start_tx,
12378a8a66a1SGirish Mahadevan 	.stop_rx = qcom_geni_serial_stop_rx,
12388a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
12398a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
12408a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
12418a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
12428a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
12438a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
12448a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
12458a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
12468a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1247c4f52879SKarthikeyan Ramasubramanian };
1248c4f52879SKarthikeyan Ramasubramanian 
1249c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1250c4f52879SKarthikeyan Ramasubramanian {
1251c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
1252c4f52879SKarthikeyan Ramasubramanian 	int line = -1;
1253c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1254c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1255c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1256066cd1c4SKarthikeyan Ramasubramanian 	int irq;
12578a8a66a1SGirish Mahadevan 	bool console = false;
12588a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
1259c4f52879SKarthikeyan Ramasubramanian 
12608a8a66a1SGirish Mahadevan 	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
12618a8a66a1SGirish Mahadevan 		console = true;
12628a8a66a1SGirish Mahadevan 
12638a8a66a1SGirish Mahadevan 	if (console) {
12648a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1265c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
12668a8a66a1SGirish Mahadevan 	} else {
12678a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
12688a8a66a1SGirish Mahadevan 		line = of_alias_get_id(pdev->dev.of_node, "hsuart");
12698a8a66a1SGirish Mahadevan 	}
1270c4f52879SKarthikeyan Ramasubramanian 
12718a8a66a1SGirish Mahadevan 	port = get_port_from_line(line, console);
1272c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
12736a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
12746a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1275c4f52879SKarthikeyan Ramasubramanian 	}
1276c4f52879SKarthikeyan Ramasubramanian 
1277c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1278c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1279c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1280c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1281c4f52879SKarthikeyan Ramasubramanian 
1282c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
1283c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1284c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1285c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1286c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1287c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1288c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1289c4f52879SKarthikeyan Ramasubramanian 		return ret;
1290c4f52879SKarthikeyan Ramasubramanian 	}
1291c4f52879SKarthikeyan Ramasubramanian 
1292c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12937693b331SWei Yongjun 	if (!res)
12947693b331SWei Yongjun 		return -EINVAL;
1295c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1296c4f52879SKarthikeyan Ramasubramanian 
1297c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1298c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1299c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1300c4f52879SKarthikeyan Ramasubramanian 
1301066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
1302066cd1c4SKarthikeyan Ramasubramanian 	if (irq < 0) {
1303066cd1c4SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Failed to get IRQ %d\n", irq);
1304066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1305c4f52879SKarthikeyan Ramasubramanian 	}
1306066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
1307c4f52879SKarthikeyan Ramasubramanian 
13088a8a66a1SGirish Mahadevan 	uport->private_data = drv;
1309c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
13108a8a66a1SGirish Mahadevan 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
13118a8a66a1SGirish Mahadevan 	if (!console)
13128a8a66a1SGirish Mahadevan 		device_create_file(uport->dev, &dev_attr_loopback);
13138a8a66a1SGirish Mahadevan 	return uart_add_one_port(drv, uport);
1314c4f52879SKarthikeyan Ramasubramanian }
1315c4f52879SKarthikeyan Ramasubramanian 
1316c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1317c4f52879SKarthikeyan Ramasubramanian {
1318c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1319c4f52879SKarthikeyan Ramasubramanian 	struct uart_driver *drv = port->uport.private_data;
1320c4f52879SKarthikeyan Ramasubramanian 
1321c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1322c4f52879SKarthikeyan Ramasubramanian 	return 0;
1323c4f52879SKarthikeyan Ramasubramanian }
1324c4f52879SKarthikeyan Ramasubramanian 
1325b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1326c4f52879SKarthikeyan Ramasubramanian {
1327a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1328c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1329c4f52879SKarthikeyan Ramasubramanian 
1330b1f84dd3SMukesh Kumar Savaliya 	return uart_suspend_port(uport->private_data, uport);
13318a8a66a1SGirish Mahadevan }
13328a8a66a1SGirish Mahadevan 
1333b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
1334c4f52879SKarthikeyan Ramasubramanian {
1335a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1336c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1337c4f52879SKarthikeyan Ramasubramanian 
1338b1f84dd3SMukesh Kumar Savaliya 	return uart_resume_port(uport->private_data, uport);
1339c4f52879SKarthikeyan Ramasubramanian }
1340c4f52879SKarthikeyan Ramasubramanian 
1341c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1342b1f84dd3SMukesh Kumar Savaliya 	SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1343b1f84dd3SMukesh Kumar Savaliya 					qcom_geni_serial_sys_resume)
1344c4f52879SKarthikeyan Ramasubramanian };
1345c4f52879SKarthikeyan Ramasubramanian 
1346c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
1347c4f52879SKarthikeyan Ramasubramanian 	{ .compatible = "qcom,geni-debug-uart", },
13488a8a66a1SGirish Mahadevan 	{ .compatible = "qcom,geni-uart", },
1349c4f52879SKarthikeyan Ramasubramanian 	{}
1350c4f52879SKarthikeyan Ramasubramanian };
1351c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1352c4f52879SKarthikeyan Ramasubramanian 
1353c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1354c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1355c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1356c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1357c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1358c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1359c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1360c4f52879SKarthikeyan Ramasubramanian 	},
1361c4f52879SKarthikeyan Ramasubramanian };
1362c4f52879SKarthikeyan Ramasubramanian 
1363c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1364c4f52879SKarthikeyan Ramasubramanian {
1365c4f52879SKarthikeyan Ramasubramanian 	int ret;
1366c4f52879SKarthikeyan Ramasubramanian 
1367c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1368c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1369c4f52879SKarthikeyan Ramasubramanian 		return ret;
1370c4f52879SKarthikeyan Ramasubramanian 
13718a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
13728a8a66a1SGirish Mahadevan 	if (ret) {
1373c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1374c4f52879SKarthikeyan Ramasubramanian 		return ret;
1375c4f52879SKarthikeyan Ramasubramanian 	}
13768a8a66a1SGirish Mahadevan 
13778a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
13788a8a66a1SGirish Mahadevan 	if (ret) {
13798a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
13808a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
13818a8a66a1SGirish Mahadevan 	}
13828a8a66a1SGirish Mahadevan 	return ret;
13838a8a66a1SGirish Mahadevan }
1384c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1385c4f52879SKarthikeyan Ramasubramanian 
1386c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1387c4f52879SKarthikeyan Ramasubramanian {
1388c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1389c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
13908a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1391c4f52879SKarthikeyan Ramasubramanian }
1392c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1393c4f52879SKarthikeyan Ramasubramanian 
1394c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1395c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1396