xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision b1f84dd321667d07f08fab99d042f1e39e9ee5bb)
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 {
1658a8a66a1SGirish Mahadevan 	struct platform_device *pdev = to_platform_device(dev);
1668a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1678a8a66a1SGirish Mahadevan 
1688a8a66a1SGirish Mahadevan 	return snprintf(buf, sizeof(u32), "%d\n", port->loopback);
1698a8a66a1SGirish Mahadevan }
1708a8a66a1SGirish Mahadevan 
1718a8a66a1SGirish Mahadevan static ssize_t loopback_store(struct device *dev,
1728a8a66a1SGirish Mahadevan 				struct device_attribute *attr, const char *buf,
1738a8a66a1SGirish Mahadevan 				size_t size)
1748a8a66a1SGirish Mahadevan {
1758a8a66a1SGirish Mahadevan 	struct platform_device *pdev = to_platform_device(dev);
1768a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1778a8a66a1SGirish Mahadevan 	u32 loopback;
1788a8a66a1SGirish Mahadevan 
1798a8a66a1SGirish Mahadevan 	if (kstrtoint(buf, 0, &loopback) || loopback > MAX_LOOPBACK_CFG) {
1808a8a66a1SGirish Mahadevan 		dev_err(dev, "Invalid input\n");
1818a8a66a1SGirish Mahadevan 		return -EINVAL;
1828a8a66a1SGirish Mahadevan 	}
1838a8a66a1SGirish Mahadevan 	port->loopback = loopback;
1848a8a66a1SGirish Mahadevan 	return size;
1858a8a66a1SGirish Mahadevan }
1868a8a66a1SGirish Mahadevan static DEVICE_ATTR_RW(loopback);
1878a8a66a1SGirish Mahadevan 
188f7371750SKarthikeyan Ramasubramanian static struct qcom_geni_serial_port qcom_geni_console_port = {
189f7371750SKarthikeyan Ramasubramanian 	.uport = {
190f7371750SKarthikeyan Ramasubramanian 		.iotype = UPIO_MEM,
191f7371750SKarthikeyan Ramasubramanian 		.ops = &qcom_geni_console_pops,
192f7371750SKarthikeyan Ramasubramanian 		.flags = UPF_BOOT_AUTOCONF,
193f7371750SKarthikeyan Ramasubramanian 		.line = 0,
194f7371750SKarthikeyan Ramasubramanian 	},
195f7371750SKarthikeyan Ramasubramanian };
196c4f52879SKarthikeyan Ramasubramanian 
197c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_request_port(struct uart_port *uport)
198c4f52879SKarthikeyan Ramasubramanian {
199c4f52879SKarthikeyan Ramasubramanian 	struct platform_device *pdev = to_platform_device(uport->dev);
200c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
201c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
202c4f52879SKarthikeyan Ramasubramanian 
203c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
204c4f52879SKarthikeyan Ramasubramanian 	uport->membase = devm_ioremap_resource(&pdev->dev, res);
205c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(uport->membase))
206c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(uport->membase);
207c4f52879SKarthikeyan Ramasubramanian 	port->se.base = uport->membase;
208c4f52879SKarthikeyan Ramasubramanian 	return 0;
209c4f52879SKarthikeyan Ramasubramanian }
210c4f52879SKarthikeyan Ramasubramanian 
211c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
212c4f52879SKarthikeyan Ramasubramanian {
213c4f52879SKarthikeyan Ramasubramanian 	if (cfg_flags & UART_CONFIG_TYPE) {
214c4f52879SKarthikeyan Ramasubramanian 		uport->type = PORT_MSM;
215c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_request_port(uport);
216c4f52879SKarthikeyan Ramasubramanian 	}
217c4f52879SKarthikeyan Ramasubramanian }
218c4f52879SKarthikeyan Ramasubramanian 
2198a8a66a1SGirish Mahadevan static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
220c4f52879SKarthikeyan Ramasubramanian {
2218a8a66a1SGirish Mahadevan 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
2228a8a66a1SGirish Mahadevan 	u32 geni_ios;
2238a8a66a1SGirish Mahadevan 
2248a8a66a1SGirish Mahadevan 	if (uart_console(uport) || !uart_cts_enabled(uport)) {
2258a8a66a1SGirish Mahadevan 		mctrl |= TIOCM_CTS;
2268a8a66a1SGirish Mahadevan 	} else {
2278a8a66a1SGirish Mahadevan 		geni_ios = readl_relaxed(uport->membase + SE_GENI_IOS);
2288a8a66a1SGirish Mahadevan 		if (!(geni_ios & IO2_DATA_IN))
2298a8a66a1SGirish Mahadevan 			mctrl |= TIOCM_CTS;
230c4f52879SKarthikeyan Ramasubramanian 	}
231c4f52879SKarthikeyan Ramasubramanian 
2328a8a66a1SGirish Mahadevan 	return mctrl;
2338a8a66a1SGirish Mahadevan }
2348a8a66a1SGirish Mahadevan 
2358a8a66a1SGirish Mahadevan static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
236c4f52879SKarthikeyan Ramasubramanian 							unsigned int mctrl)
237c4f52879SKarthikeyan Ramasubramanian {
2388a8a66a1SGirish Mahadevan 	u32 uart_manual_rfr = 0;
2398a8a66a1SGirish Mahadevan 
2408a8a66a1SGirish Mahadevan 	if (uart_console(uport) || !uart_cts_enabled(uport))
2418a8a66a1SGirish Mahadevan 		return;
2428a8a66a1SGirish Mahadevan 
2438a8a66a1SGirish Mahadevan 	if (!(mctrl & TIOCM_RTS))
2448a8a66a1SGirish Mahadevan 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
2458a8a66a1SGirish Mahadevan 	writel_relaxed(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
246c4f52879SKarthikeyan Ramasubramanian }
247c4f52879SKarthikeyan Ramasubramanian 
248c4f52879SKarthikeyan Ramasubramanian static const char *qcom_geni_serial_get_type(struct uart_port *uport)
249c4f52879SKarthikeyan Ramasubramanian {
250c4f52879SKarthikeyan Ramasubramanian 	return "MSM";
251c4f52879SKarthikeyan Ramasubramanian }
252c4f52879SKarthikeyan Ramasubramanian 
2538a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
254c4f52879SKarthikeyan Ramasubramanian {
2558a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port;
2568a8a66a1SGirish Mahadevan 	int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
2578a8a66a1SGirish Mahadevan 
2588a8a66a1SGirish Mahadevan 	if (line < 0 || line >= nr_ports)
259c4f52879SKarthikeyan Ramasubramanian 		return ERR_PTR(-ENXIO);
2608a8a66a1SGirish Mahadevan 
2618a8a66a1SGirish Mahadevan 	port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
2628a8a66a1SGirish Mahadevan 	return port;
263c4f52879SKarthikeyan Ramasubramanian }
264c4f52879SKarthikeyan Ramasubramanian 
265c4f52879SKarthikeyan Ramasubramanian static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
266c4f52879SKarthikeyan Ramasubramanian 				int offset, int field, bool set)
267c4f52879SKarthikeyan Ramasubramanian {
268c4f52879SKarthikeyan Ramasubramanian 	u32 reg;
269c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
270c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
271c4f52879SKarthikeyan Ramasubramanian 	unsigned int fifo_bits;
272c4f52879SKarthikeyan Ramasubramanian 	unsigned long timeout_us = 20000;
273c4f52879SKarthikeyan Ramasubramanian 
274c4f52879SKarthikeyan Ramasubramanian 	/* Ensure polling is not re-ordered before the prior writes/reads */
275c4f52879SKarthikeyan Ramasubramanian 	mb();
276c4f52879SKarthikeyan Ramasubramanian 
277c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data) {
278c4f52879SKarthikeyan Ramasubramanian 		port = to_dev_port(uport, uport);
279c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
280c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
281c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
282c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
283c4f52879SKarthikeyan Ramasubramanian 		/*
284c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
285c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
286c4f52879SKarthikeyan Ramasubramanian 		 */
287c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
288c4f52879SKarthikeyan Ramasubramanian 	}
289c4f52879SKarthikeyan Ramasubramanian 
29043f1831bSKarthikeyan Ramasubramanian 	/*
29143f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
29243f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29343f1831bSKarthikeyan Ramasubramanian 	 */
29443f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29543f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
29643f1831bSKarthikeyan Ramasubramanian 		reg = readl_relaxed(uport->membase + offset);
29743f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29843f1831bSKarthikeyan Ramasubramanian 			return true;
29943f1831bSKarthikeyan Ramasubramanian 		udelay(10);
30043f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
30143f1831bSKarthikeyan Ramasubramanian 	}
30243f1831bSKarthikeyan Ramasubramanian 	return false;
303c4f52879SKarthikeyan Ramasubramanian }
304c4f52879SKarthikeyan Ramasubramanian 
305c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
306c4f52879SKarthikeyan Ramasubramanian {
307c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
308c4f52879SKarthikeyan Ramasubramanian 
309c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
310c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
311c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
312c4f52879SKarthikeyan Ramasubramanian }
313c4f52879SKarthikeyan Ramasubramanian 
314c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
315c4f52879SKarthikeyan Ramasubramanian {
316c4f52879SKarthikeyan Ramasubramanian 	int done;
317c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
318c4f52879SKarthikeyan Ramasubramanian 
319c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
320c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
321c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
322c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_GENI_CMD_ABORT, uport->membase +
323c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
324c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
325c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
326c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
327c4f52879SKarthikeyan Ramasubramanian 	}
328c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
329c4f52879SKarthikeyan Ramasubramanian }
330c4f52879SKarthikeyan Ramasubramanian 
331c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
332c4f52879SKarthikeyan Ramasubramanian {
333c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
334c4f52879SKarthikeyan Ramasubramanian 
335c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
336c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
337c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
338c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
339c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
340c4f52879SKarthikeyan Ramasubramanian }
341c4f52879SKarthikeyan Ramasubramanian 
342c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
343c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
344c4f52879SKarthikeyan Ramasubramanian {
345c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo;
346c4f52879SKarthikeyan Ramasubramanian 	u32 status;
347c4f52879SKarthikeyan Ramasubramanian 
348c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS);
349c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
350c4f52879SKarthikeyan Ramasubramanian 
351c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS);
352c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
353c4f52879SKarthikeyan Ramasubramanian 
354c4f52879SKarthikeyan Ramasubramanian 	/*
355c4f52879SKarthikeyan Ramasubramanian 	 * Ensure the writes to clear interrupts is not re-ordered after
356c4f52879SKarthikeyan Ramasubramanian 	 * reading the data.
357c4f52879SKarthikeyan Ramasubramanian 	 */
358c4f52879SKarthikeyan Ramasubramanian 	mb();
359c4f52879SKarthikeyan Ramasubramanian 
360c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_RX_FIFO_STATUS);
361c4f52879SKarthikeyan Ramasubramanian 	if (!(status & RX_FIFO_WC_MSK))
362c4f52879SKarthikeyan Ramasubramanian 		return NO_POLL_CHAR;
363c4f52879SKarthikeyan Ramasubramanian 
364c4f52879SKarthikeyan Ramasubramanian 	rx_fifo = readl(uport->membase + SE_GENI_RX_FIFOn);
365c4f52879SKarthikeyan Ramasubramanian 	return rx_fifo & 0xff;
366c4f52879SKarthikeyan Ramasubramanian }
367c4f52879SKarthikeyan Ramasubramanian 
368c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
369c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
370c4f52879SKarthikeyan Ramasubramanian {
371c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
372c4f52879SKarthikeyan Ramasubramanian 
373c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(port->tx_wm, uport->membase + SE_GENI_TX_WATERMARK_REG);
374c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
375c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
376c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
377c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(c, uport->membase + SE_GENI_TX_FIFOn);
378c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase +
379c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
380c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
381c4f52879SKarthikeyan Ramasubramanian }
382c4f52879SKarthikeyan Ramasubramanian #endif
383c4f52879SKarthikeyan Ramasubramanian 
384c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
385c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch)
386c4f52879SKarthikeyan Ramasubramanian {
387c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(ch, uport->membase + SE_GENI_TX_FIFOn);
388c4f52879SKarthikeyan Ramasubramanian }
389c4f52879SKarthikeyan Ramasubramanian 
390c4f52879SKarthikeyan Ramasubramanian static void
391c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
392c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
393c4f52879SKarthikeyan Ramasubramanian {
394c4f52879SKarthikeyan Ramasubramanian 	int i;
395c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
396c4f52879SKarthikeyan Ramasubramanian 
397c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
398f0262568SKarthikeyan Ramasubramanian 		/*
399f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
400f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
401f0262568SKarthikeyan Ramasubramanian 		 */
402c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
403c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
404c4f52879SKarthikeyan Ramasubramanian 	}
405c4f52879SKarthikeyan Ramasubramanian 
406c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
407c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
408c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
409c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
410c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
411c4f52879SKarthikeyan Ramasubramanian 
412c4f52879SKarthikeyan Ramasubramanian 		/*
413c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
414c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
415c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
416c4f52879SKarthikeyan Ramasubramanian 		 * lost.
417c4f52879SKarthikeyan Ramasubramanian 		 */
418c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
419c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
420c4f52879SKarthikeyan Ramasubramanian 			break;
4216a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
422c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
423c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
424c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase +
425c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
426c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
427c4f52879SKarthikeyan Ramasubramanian 	}
428c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
429c4f52879SKarthikeyan Ramasubramanian }
430c4f52879SKarthikeyan Ramasubramanian 
431c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
432c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
433c4f52879SKarthikeyan Ramasubramanian {
434c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
435c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
436c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
437c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
438c4f52879SKarthikeyan Ramasubramanian 
439c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
440c4f52879SKarthikeyan Ramasubramanian 
4418a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
442c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
443c4f52879SKarthikeyan Ramasubramanian 		return;
444c4f52879SKarthikeyan Ramasubramanian 
445c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
446c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
447c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
448c4f52879SKarthikeyan Ramasubramanian 	else
449c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
450c4f52879SKarthikeyan Ramasubramanian 
451c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
452c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
453c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
454c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
455c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
456c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
457c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
458c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
459c4f52879SKarthikeyan Ramasubramanian 			writel_relaxed(M_CMD_ABORT_EN, uport->membase +
460c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
461c4f52879SKarthikeyan Ramasubramanian 		}
462c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_CMD_CANCEL_EN, uport->membase +
463c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
464c4f52879SKarthikeyan Ramasubramanian 	}
465c4f52879SKarthikeyan Ramasubramanian 
466c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
467c4f52879SKarthikeyan Ramasubramanian 	if (locked)
468c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
469c4f52879SKarthikeyan Ramasubramanian }
470c4f52879SKarthikeyan Ramasubramanian 
471c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
472c4f52879SKarthikeyan Ramasubramanian {
473c4f52879SKarthikeyan Ramasubramanian 	u32 i;
474c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
475c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
476c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
477c4f52879SKarthikeyan Ramasubramanian 
478c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
479c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
480c4f52879SKarthikeyan Ramasubramanian 		int c;
481c4f52879SKarthikeyan Ramasubramanian 		int chunk = min_t(int, bytes - i, port->rx_bytes_pw);
482c4f52879SKarthikeyan Ramasubramanian 
483c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
484c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
485c4f52879SKarthikeyan Ramasubramanian 		if (drop)
486c4f52879SKarthikeyan Ramasubramanian 			continue;
487c4f52879SKarthikeyan Ramasubramanian 
488c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
489c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
490c4f52879SKarthikeyan Ramasubramanian 
491c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
492c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
493c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
494c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
495c4f52879SKarthikeyan Ramasubramanian 					continue;
496c4f52879SKarthikeyan Ramasubramanian 			}
497c4f52879SKarthikeyan Ramasubramanian 
498c4f52879SKarthikeyan Ramasubramanian 			sysrq = uart_handle_sysrq_char(uport, buf[c]);
499c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
500c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
501c4f52879SKarthikeyan Ramasubramanian 		}
502c4f52879SKarthikeyan Ramasubramanian 	}
503c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
504c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
505c4f52879SKarthikeyan Ramasubramanian 	return 0;
506c4f52879SKarthikeyan Ramasubramanian }
507c4f52879SKarthikeyan Ramasubramanian #else
508c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
509c4f52879SKarthikeyan Ramasubramanian {
510c4f52879SKarthikeyan Ramasubramanian 	return -EPERM;
511c4f52879SKarthikeyan Ramasubramanian }
512c4f52879SKarthikeyan Ramasubramanian 
513c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
514c4f52879SKarthikeyan Ramasubramanian 
5158a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5168a8a66a1SGirish Mahadevan {
5178a8a66a1SGirish Mahadevan 	unsigned char *buf;
5188a8a66a1SGirish Mahadevan 	struct tty_port *tport;
5198a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
5208a8a66a1SGirish Mahadevan 	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
5218a8a66a1SGirish Mahadevan 	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
5228a8a66a1SGirish Mahadevan 	int ret;
5238a8a66a1SGirish Mahadevan 
5248a8a66a1SGirish Mahadevan 	tport = &uport->state->port;
5258a8a66a1SGirish Mahadevan 	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
5268a8a66a1SGirish Mahadevan 	if (drop)
5278a8a66a1SGirish Mahadevan 		return 0;
5288a8a66a1SGirish Mahadevan 
5298a8a66a1SGirish Mahadevan 	buf = (unsigned char *)port->rx_fifo;
5308a8a66a1SGirish Mahadevan 	ret = tty_insert_flip_string(tport, buf, bytes);
5318a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5328a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5338a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5348a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5358a8a66a1SGirish Mahadevan 	}
5368a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5378a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5388a8a66a1SGirish Mahadevan 	return ret;
5398a8a66a1SGirish Mahadevan }
5408a8a66a1SGirish Mahadevan 
541c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_tx(struct uart_port *uport)
542c4f52879SKarthikeyan Ramasubramanian {
543c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
544c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
545c4f52879SKarthikeyan Ramasubramanian 	u32 status;
546c4f52879SKarthikeyan Ramasubramanian 
547c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
5487fb5b880SKarthikeyan Ramasubramanian 		/*
5497fb5b880SKarthikeyan Ramasubramanian 		 * readl ensures reading & writing of IRQ_EN register
5507fb5b880SKarthikeyan Ramasubramanian 		 * is not re-ordered before checking the status of the
5517fb5b880SKarthikeyan Ramasubramanian 		 * Serial Engine.
5527fb5b880SKarthikeyan Ramasubramanian 		 */
5537fb5b880SKarthikeyan Ramasubramanian 		status = readl(uport->membase + SE_GENI_STATUS);
554c4f52879SKarthikeyan Ramasubramanian 		if (status & M_GENI_CMD_ACTIVE)
555c4f52879SKarthikeyan Ramasubramanian 			return;
556c4f52879SKarthikeyan Ramasubramanian 
557c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_tx_empty(uport))
558c4f52879SKarthikeyan Ramasubramanian 			return;
559c4f52879SKarthikeyan Ramasubramanian 
560c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase +	SE_GENI_M_IRQ_EN);
561c4f52879SKarthikeyan Ramasubramanian 		irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
562c4f52879SKarthikeyan Ramasubramanian 
563c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(port->tx_wm, uport->membase +
564c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_TX_WATERMARK_REG);
565c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
566c4f52879SKarthikeyan Ramasubramanian 	}
567c4f52879SKarthikeyan Ramasubramanian }
568c4f52879SKarthikeyan Ramasubramanian 
569c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_tx(struct uart_port *uport)
570c4f52879SKarthikeyan Ramasubramanian {
571c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
572c4f52879SKarthikeyan Ramasubramanian 	u32 status;
573c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
574c4f52879SKarthikeyan Ramasubramanian 
575c4f52879SKarthikeyan Ramasubramanian 	irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
576c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~M_CMD_DONE_EN;
577c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
578c4f52879SKarthikeyan Ramasubramanian 		irq_en &= ~M_TX_FIFO_WATERMARK_EN;
579c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(0, uport->membase +
580c4f52879SKarthikeyan Ramasubramanian 				     SE_GENI_TX_WATERMARK_REG);
581c4f52879SKarthikeyan Ramasubramanian 	}
582c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
583c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
584c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
585c4f52879SKarthikeyan Ramasubramanian 	if (!(status & M_GENI_CMD_ACTIVE))
586c4f52879SKarthikeyan Ramasubramanian 		return;
587c4f52879SKarthikeyan Ramasubramanian 
588c4f52879SKarthikeyan Ramasubramanian 	/*
589c4f52879SKarthikeyan Ramasubramanian 	 * Ensure cancel command write is not re-ordered before checking
590c4f52879SKarthikeyan Ramasubramanian 	 * the status of the Primary Sequencer.
591c4f52879SKarthikeyan Ramasubramanian 	 */
592c4f52879SKarthikeyan Ramasubramanian 	mb();
593c4f52879SKarthikeyan Ramasubramanian 
594c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
595c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
596c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
597c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
598c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
599c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
600c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(M_CMD_ABORT_EN, uport->membase +
601c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
602c4f52879SKarthikeyan Ramasubramanian 	}
603c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
604c4f52879SKarthikeyan Ramasubramanian }
605c4f52879SKarthikeyan Ramasubramanian 
606c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_rx(struct uart_port *uport)
607c4f52879SKarthikeyan Ramasubramanian {
608c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
609c4f52879SKarthikeyan Ramasubramanian 	u32 status;
610c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
611c4f52879SKarthikeyan Ramasubramanian 
612c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
613c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
614c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_rx(uport);
615c4f52879SKarthikeyan Ramasubramanian 
616c4f52879SKarthikeyan Ramasubramanian 	/*
617c4f52879SKarthikeyan Ramasubramanian 	 * Ensure setup command write is not re-ordered before checking
618c4f52879SKarthikeyan Ramasubramanian 	 * the status of the Secondary Sequencer.
619c4f52879SKarthikeyan Ramasubramanian 	 */
620c4f52879SKarthikeyan Ramasubramanian 	mb();
621c4f52879SKarthikeyan Ramasubramanian 
622c4f52879SKarthikeyan Ramasubramanian 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
623c4f52879SKarthikeyan Ramasubramanian 
624c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
625c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN);
626c4f52879SKarthikeyan Ramasubramanian 		irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
627c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
628c4f52879SKarthikeyan Ramasubramanian 
629c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
630c4f52879SKarthikeyan Ramasubramanian 		irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
631c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
632c4f52879SKarthikeyan Ramasubramanian 	}
633c4f52879SKarthikeyan Ramasubramanian }
634c4f52879SKarthikeyan Ramasubramanian 
635c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport)
636c4f52879SKarthikeyan Ramasubramanian {
637c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
638c4f52879SKarthikeyan Ramasubramanian 	u32 status;
639c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
640c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN;
641c4f52879SKarthikeyan Ramasubramanian 
642c4f52879SKarthikeyan Ramasubramanian 	if (port->xfer_mode == GENI_SE_FIFO) {
643c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN);
644c4f52879SKarthikeyan Ramasubramanian 		irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
645c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
646c4f52879SKarthikeyan Ramasubramanian 
647c4f52879SKarthikeyan Ramasubramanian 		irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
648c4f52879SKarthikeyan Ramasubramanian 		irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
649c4f52879SKarthikeyan Ramasubramanian 		writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
650c4f52879SKarthikeyan Ramasubramanian 	}
651c4f52879SKarthikeyan Ramasubramanian 
652c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
653c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop rx is called multiple times. */
654c4f52879SKarthikeyan Ramasubramanian 	if (!(status & S_GENI_CMD_ACTIVE))
655c4f52879SKarthikeyan Ramasubramanian 		return;
656c4f52879SKarthikeyan Ramasubramanian 
657c4f52879SKarthikeyan Ramasubramanian 	/*
658c4f52879SKarthikeyan Ramasubramanian 	 * Ensure cancel command write is not re-ordered before checking
659c4f52879SKarthikeyan Ramasubramanian 	 * the status of the Secondary Sequencer.
660c4f52879SKarthikeyan Ramasubramanian 	 */
661c4f52879SKarthikeyan Ramasubramanian 	mb();
662c4f52879SKarthikeyan Ramasubramanian 
663c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
664c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
665c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_CANCEL, false);
666c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_STATUS);
667c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
668c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
669c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
670c4f52879SKarthikeyan Ramasubramanian }
671c4f52879SKarthikeyan Ramasubramanian 
672c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
673c4f52879SKarthikeyan Ramasubramanian {
674c4f52879SKarthikeyan Ramasubramanian 	u32 status;
675c4f52879SKarthikeyan Ramasubramanian 	u32 word_cnt;
676c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_byte_cnt;
677c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_partial;
678c4f52879SKarthikeyan Ramasubramanian 	u32 total_bytes;
679c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
680c4f52879SKarthikeyan Ramasubramanian 
681c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase +	SE_GENI_RX_FIFO_STATUS);
682c4f52879SKarthikeyan Ramasubramanian 	word_cnt = status & RX_FIFO_WC_MSK;
683c4f52879SKarthikeyan Ramasubramanian 	last_word_partial = status & RX_LAST;
684c4f52879SKarthikeyan Ramasubramanian 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
685c4f52879SKarthikeyan Ramasubramanian 						RX_LAST_BYTE_VALID_SHFT;
686c4f52879SKarthikeyan Ramasubramanian 
687c4f52879SKarthikeyan Ramasubramanian 	if (!word_cnt)
688c4f52879SKarthikeyan Ramasubramanian 		return;
689c4f52879SKarthikeyan Ramasubramanian 	total_bytes = port->rx_bytes_pw * (word_cnt - 1);
690c4f52879SKarthikeyan Ramasubramanian 	if (last_word_partial && last_word_byte_cnt)
691c4f52879SKarthikeyan Ramasubramanian 		total_bytes += last_word_byte_cnt;
692c4f52879SKarthikeyan Ramasubramanian 	else
693c4f52879SKarthikeyan Ramasubramanian 		total_bytes += port->rx_bytes_pw;
694c4f52879SKarthikeyan Ramasubramanian 	port->handle_rx(uport, total_bytes, drop);
695c4f52879SKarthikeyan Ramasubramanian }
696c4f52879SKarthikeyan Ramasubramanian 
697c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_tx(struct uart_port *uport)
698c4f52879SKarthikeyan Ramasubramanian {
699c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
700c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
701c4f52879SKarthikeyan Ramasubramanian 	size_t avail;
702c4f52879SKarthikeyan Ramasubramanian 	size_t remaining;
703c4f52879SKarthikeyan Ramasubramanian 	int i;
704c4f52879SKarthikeyan Ramasubramanian 	u32 status;
705c4f52879SKarthikeyan Ramasubramanian 	unsigned int chunk;
706c4f52879SKarthikeyan Ramasubramanian 	int tail;
7078a8a66a1SGirish Mahadevan 	u32 irq_en;
708c4f52879SKarthikeyan Ramasubramanian 
709c4f52879SKarthikeyan Ramasubramanian 	chunk = uart_circ_chars_pending(xmit);
710c4f52879SKarthikeyan Ramasubramanian 	status = readl_relaxed(uport->membase + SE_GENI_TX_FIFO_STATUS);
711c4f52879SKarthikeyan Ramasubramanian 	/* Both FIFO and framework buffer are drained */
712638a6f4eSEvan Green 	if (!chunk && !status) {
713c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_tx(uport);
714c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
715c4f52879SKarthikeyan Ramasubramanian 	}
716c4f52879SKarthikeyan Ramasubramanian 
7178a8a66a1SGirish Mahadevan 	if (!uart_console(uport)) {
7188a8a66a1SGirish Mahadevan 		irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
7198a8a66a1SGirish Mahadevan 		irq_en &= ~(M_TX_FIFO_WATERMARK_EN);
7208a8a66a1SGirish Mahadevan 		writel_relaxed(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
7218a8a66a1SGirish Mahadevan 		writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
7228a8a66a1SGirish Mahadevan 	}
7238a8a66a1SGirish Mahadevan 
724c4f52879SKarthikeyan Ramasubramanian 	avail = (port->tx_fifo_depth - port->tx_wm) * port->tx_bytes_pw;
725638a6f4eSEvan Green 	tail = xmit->tail;
7268e70c47cSKarthikeyan Ramasubramanian 	chunk = min3((size_t)chunk, (size_t)(UART_XMIT_SIZE - tail), avail);
727c4f52879SKarthikeyan Ramasubramanian 	if (!chunk)
728c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
729c4f52879SKarthikeyan Ramasubramanian 
730c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, chunk);
731c4f52879SKarthikeyan Ramasubramanian 
732c4f52879SKarthikeyan Ramasubramanian 	remaining = chunk;
733c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < chunk; ) {
734c4f52879SKarthikeyan Ramasubramanian 		unsigned int tx_bytes;
73569736b57SKarthikeyan Ramasubramanian 		u8 buf[sizeof(u32)];
736c4f52879SKarthikeyan Ramasubramanian 		int c;
737c4f52879SKarthikeyan Ramasubramanian 
73869736b57SKarthikeyan Ramasubramanian 		memset(buf, 0, ARRAY_SIZE(buf));
7396a10635eSKarthikeyan Ramasubramanian 		tx_bytes = min_t(size_t, remaining, port->tx_bytes_pw);
740c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < tx_bytes ; c++)
74169736b57SKarthikeyan Ramasubramanian 			buf[c] = xmit->buf[tail + c];
742c4f52879SKarthikeyan Ramasubramanian 
74369736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
744c4f52879SKarthikeyan Ramasubramanian 
745c4f52879SKarthikeyan Ramasubramanian 		i += tx_bytes;
746638a6f4eSEvan Green 		tail += tx_bytes;
747c4f52879SKarthikeyan Ramasubramanian 		uport->icount.tx += tx_bytes;
748c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
749c4f52879SKarthikeyan Ramasubramanian 	}
750638a6f4eSEvan Green 
751638a6f4eSEvan Green 	xmit->tail = tail & (UART_XMIT_SIZE - 1);
7528a8a66a1SGirish Mahadevan 	if (uart_console(uport))
753c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
754c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
755638a6f4eSEvan Green 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
756c4f52879SKarthikeyan Ramasubramanian 		uart_write_wakeup(uport);
757c4f52879SKarthikeyan Ramasubramanian }
758c4f52879SKarthikeyan Ramasubramanian 
759c4f52879SKarthikeyan Ramasubramanian static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
760c4f52879SKarthikeyan Ramasubramanian {
761c4f52879SKarthikeyan Ramasubramanian 	unsigned int m_irq_status;
762c4f52879SKarthikeyan Ramasubramanian 	unsigned int s_irq_status;
763c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
764c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
765c4f52879SKarthikeyan Ramasubramanian 	unsigned int m_irq_en;
766c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
767c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
768c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
769c4f52879SKarthikeyan Ramasubramanian 
770c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
771ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
772c4f52879SKarthikeyan Ramasubramanian 
773c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
774c4f52879SKarthikeyan Ramasubramanian 	m_irq_status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS);
775c4f52879SKarthikeyan Ramasubramanian 	s_irq_status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS);
776c4f52879SKarthikeyan Ramasubramanian 	m_irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN);
777c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
778c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
779c4f52879SKarthikeyan Ramasubramanian 
780c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
781c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
782c4f52879SKarthikeyan Ramasubramanian 
783c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
784c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
785c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
786c4f52879SKarthikeyan Ramasubramanian 	}
787c4f52879SKarthikeyan Ramasubramanian 
788c4f52879SKarthikeyan Ramasubramanian 	if (m_irq_status & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN) &&
789c4f52879SKarthikeyan Ramasubramanian 	    m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
790c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_tx(uport);
791c4f52879SKarthikeyan Ramasubramanian 
792c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
793c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
794c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
795c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
796c4f52879SKarthikeyan Ramasubramanian 	} else if (s_irq_status & S_GP_IRQ_2_EN ||
797c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_GP_IRQ_3_EN) {
798c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
799c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
800c4f52879SKarthikeyan Ramasubramanian 	}
801c4f52879SKarthikeyan Ramasubramanian 
802c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
803c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_RX_FIFO_LAST_EN)
804c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_rx(uport, drop_rx);
805c4f52879SKarthikeyan Ramasubramanian 
806c4f52879SKarthikeyan Ramasubramanian out_unlock:
807c4f52879SKarthikeyan Ramasubramanian 	spin_unlock_irqrestore(&uport->lock, flags);
808c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
809c4f52879SKarthikeyan Ramasubramanian }
810c4f52879SKarthikeyan Ramasubramanian 
8116a10635eSKarthikeyan Ramasubramanian static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
812c4f52879SKarthikeyan Ramasubramanian {
813c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
814c4f52879SKarthikeyan Ramasubramanian 
815c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
816c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
817c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
818c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
819c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
820c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
821c4f52879SKarthikeyan Ramasubramanian }
822c4f52879SKarthikeyan Ramasubramanian 
823c4f52879SKarthikeyan Ramasubramanian static void set_rfr_wm(struct qcom_geni_serial_port *port)
824c4f52879SKarthikeyan Ramasubramanian {
825c4f52879SKarthikeyan Ramasubramanian 	/*
826c4f52879SKarthikeyan Ramasubramanian 	 * Set RFR (Flow off) to FIFO_DEPTH - 2.
827c4f52879SKarthikeyan Ramasubramanian 	 * RX WM level at 10% RX_FIFO_DEPTH.
828c4f52879SKarthikeyan Ramasubramanian 	 * TX WM level at 10% TX_FIFO_DEPTH.
829c4f52879SKarthikeyan Ramasubramanian 	 */
830c4f52879SKarthikeyan Ramasubramanian 	port->rx_rfr = port->rx_fifo_depth - 2;
831c4f52879SKarthikeyan Ramasubramanian 	port->rx_wm = UART_CONSOLE_RX_WM;
832c4f52879SKarthikeyan Ramasubramanian 	port->tx_wm = DEF_TX_WM;
833c4f52879SKarthikeyan Ramasubramanian }
834c4f52879SKarthikeyan Ramasubramanian 
835c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
836c4f52879SKarthikeyan Ramasubramanian {
837c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
838c4f52879SKarthikeyan Ramasubramanian 
839c4f52879SKarthikeyan Ramasubramanian 	/* Stop the console before stopping the current tx */
8408a8a66a1SGirish Mahadevan 	if (uart_console(uport))
841c4f52879SKarthikeyan Ramasubramanian 		console_stop(uport->cons);
842c4f52879SKarthikeyan Ramasubramanian 
843c4f52879SKarthikeyan Ramasubramanian 	free_irq(uport->irq, uport);
844c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
845c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_tx(uport);
846c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
847c4f52879SKarthikeyan Ramasubramanian 	spin_unlock_irqrestore(&uport->lock, flags);
848c4f52879SKarthikeyan Ramasubramanian }
849c4f52879SKarthikeyan Ramasubramanian 
850c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
851c4f52879SKarthikeyan Ramasubramanian {
852c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
853c4f52879SKarthikeyan Ramasubramanian 	unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
854c362272bSDouglas Anderson 	u32 proto;
855c362272bSDouglas Anderson 
8569f641df4SDouglas Anderson 	if (uart_console(uport)) {
857c362272bSDouglas Anderson 		port->tx_bytes_pw = 1;
8589f641df4SDouglas Anderson 		port->rx_bytes_pw = CONSOLE_RX_BYTES_PW;
8599f641df4SDouglas Anderson 	} else {
860c362272bSDouglas Anderson 		port->tx_bytes_pw = 4;
8619f641df4SDouglas Anderson 		port->rx_bytes_pw = 4;
8629f641df4SDouglas Anderson 	}
863c362272bSDouglas Anderson 
864c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
865c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
866c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
867c362272bSDouglas Anderson 		return -ENXIO;
868c362272bSDouglas Anderson 	}
869c362272bSDouglas Anderson 
870c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
871c362272bSDouglas Anderson 
872c362272bSDouglas Anderson 	get_tx_fifo_size(port);
873c4f52879SKarthikeyan Ramasubramanian 
874c4f52879SKarthikeyan Ramasubramanian 	set_rfr_wm(port);
875c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
876c4f52879SKarthikeyan Ramasubramanian 	/*
877c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
878c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
879c4f52879SKarthikeyan Ramasubramanian 	 */
880c4f52879SKarthikeyan Ramasubramanian 	port->xfer_mode = GENI_SE_FIFO;
8818a8a66a1SGirish Mahadevan 	if (uart_console(uport))
882c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
883c4f52879SKarthikeyan Ramasubramanian 	geni_se_config_packing(&port->se, BITS_PER_BYTE, port->tx_bytes_pw,
884c4f52879SKarthikeyan Ramasubramanian 						false, true, false);
885c4f52879SKarthikeyan Ramasubramanian 	geni_se_config_packing(&port->se, BITS_PER_BYTE, port->rx_bytes_pw,
886c4f52879SKarthikeyan Ramasubramanian 						false, false, true);
887c4f52879SKarthikeyan Ramasubramanian 	geni_se_init(&port->se, port->rx_wm, port->rx_rfr);
888c4f52879SKarthikeyan Ramasubramanian 	geni_se_select_mode(&port->se, port->xfer_mode);
8898a8a66a1SGirish Mahadevan 	if (!uart_console(uport)) {
890329e0989SKees Cook 		port->rx_fifo = devm_kcalloc(uport->dev,
891329e0989SKees Cook 			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
8928a8a66a1SGirish Mahadevan 		if (!port->rx_fifo)
8938a8a66a1SGirish Mahadevan 			return -ENOMEM;
8948a8a66a1SGirish Mahadevan 	}
895c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
896c362272bSDouglas Anderson 
897c4f52879SKarthikeyan Ramasubramanian 	return 0;
898c4f52879SKarthikeyan Ramasubramanian }
899c4f52879SKarthikeyan Ramasubramanian 
900c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
901c4f52879SKarthikeyan Ramasubramanian {
902c4f52879SKarthikeyan Ramasubramanian 	int ret;
903c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
904c4f52879SKarthikeyan Ramasubramanian 
905c4f52879SKarthikeyan Ramasubramanian 	scnprintf(port->name, sizeof(port->name),
9068a8a66a1SGirish Mahadevan 		  "qcom_serial_%s%d",
9078a8a66a1SGirish Mahadevan 		(uart_console(uport) ? "console" : "uart"), uport->line);
908c4f52879SKarthikeyan Ramasubramanian 
909c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
910c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
911c4f52879SKarthikeyan Ramasubramanian 		if (ret)
912c4f52879SKarthikeyan Ramasubramanian 			return ret;
913c4f52879SKarthikeyan Ramasubramanian 	}
914c4f52879SKarthikeyan Ramasubramanian 
915c4f52879SKarthikeyan Ramasubramanian 	ret = request_irq(uport->irq, qcom_geni_serial_isr, IRQF_TRIGGER_HIGH,
916c4f52879SKarthikeyan Ramasubramanian 							port->name, uport);
917c4f52879SKarthikeyan Ramasubramanian 	if (ret)
918c4f52879SKarthikeyan Ramasubramanian 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
919c4f52879SKarthikeyan Ramasubramanian 	return ret;
920c4f52879SKarthikeyan Ramasubramanian }
921c4f52879SKarthikeyan Ramasubramanian 
922c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_cfg(unsigned long clk_freq)
923c4f52879SKarthikeyan Ramasubramanian {
924c4f52879SKarthikeyan Ramasubramanian 	int i;
925c4f52879SKarthikeyan Ramasubramanian 
926c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
927c4f52879SKarthikeyan Ramasubramanian 		if (!(root_freq[i] % clk_freq))
928c4f52879SKarthikeyan Ramasubramanian 			return root_freq[i];
929c4f52879SKarthikeyan Ramasubramanian 	}
930c4f52879SKarthikeyan Ramasubramanian 	return 0;
931c4f52879SKarthikeyan Ramasubramanian }
932c4f52879SKarthikeyan Ramasubramanian 
933c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_div_rate(unsigned int baud, unsigned int *clk_div)
934c4f52879SKarthikeyan Ramasubramanian {
935c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
936c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
937c4f52879SKarthikeyan Ramasubramanian 
938c4f52879SKarthikeyan Ramasubramanian 	desired_clk = baud * UART_OVERSAMPLING;
939c4f52879SKarthikeyan Ramasubramanian 	ser_clk = get_clk_cfg(desired_clk);
940c4f52879SKarthikeyan Ramasubramanian 	if (!ser_clk) {
941c4f52879SKarthikeyan Ramasubramanian 		pr_err("%s: Can't find matching DFS entry for baud %d\n",
942c4f52879SKarthikeyan Ramasubramanian 								__func__, baud);
943c4f52879SKarthikeyan Ramasubramanian 		return ser_clk;
944c4f52879SKarthikeyan Ramasubramanian 	}
945c4f52879SKarthikeyan Ramasubramanian 
946c4f52879SKarthikeyan Ramasubramanian 	*clk_div = ser_clk / desired_clk;
947c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
948c4f52879SKarthikeyan Ramasubramanian }
949c4f52879SKarthikeyan Ramasubramanian 
950c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
951c4f52879SKarthikeyan Ramasubramanian 				struct ktermios *termios, struct ktermios *old)
952c4f52879SKarthikeyan Ramasubramanian {
953c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
954c4f52879SKarthikeyan Ramasubramanian 	unsigned int bits_per_char;
955c4f52879SKarthikeyan Ramasubramanian 	unsigned int tx_trans_cfg;
956c4f52879SKarthikeyan Ramasubramanian 	unsigned int tx_parity_cfg;
957c4f52879SKarthikeyan Ramasubramanian 	unsigned int rx_trans_cfg;
958c4f52879SKarthikeyan Ramasubramanian 	unsigned int rx_parity_cfg;
959c4f52879SKarthikeyan Ramasubramanian 	unsigned int stop_bit_len;
960c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
961c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk_cfg;
962c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
963c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
964c4f52879SKarthikeyan Ramasubramanian 
965c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
966c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
967c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
968c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
969c4f52879SKarthikeyan Ramasubramanian 	clk_rate = get_clk_div_rate(baud, &clk_div);
970c4f52879SKarthikeyan Ramasubramanian 	if (!clk_rate)
971c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
972c4f52879SKarthikeyan Ramasubramanian 
973c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
974c4f52879SKarthikeyan Ramasubramanian 	clk_set_rate(port->se.clk, clk_rate);
975c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
976c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
977c4f52879SKarthikeyan Ramasubramanian 
978c4f52879SKarthikeyan Ramasubramanian 	/* parity */
979c4f52879SKarthikeyan Ramasubramanian 	tx_trans_cfg = readl_relaxed(uport->membase + SE_UART_TX_TRANS_CFG);
980c4f52879SKarthikeyan Ramasubramanian 	tx_parity_cfg = readl_relaxed(uport->membase + SE_UART_TX_PARITY_CFG);
981c4f52879SKarthikeyan Ramasubramanian 	rx_trans_cfg = readl_relaxed(uport->membase + SE_UART_RX_TRANS_CFG);
982c4f52879SKarthikeyan Ramasubramanian 	rx_parity_cfg = readl_relaxed(uport->membase + SE_UART_RX_PARITY_CFG);
983c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
984c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
985c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
986c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
987c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
988c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
989c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
990c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
991c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
992c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
993c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
994c4f52879SKarthikeyan Ramasubramanian 		} else {
995c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
996c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
997c4f52879SKarthikeyan Ramasubramanian 		}
998c4f52879SKarthikeyan Ramasubramanian 	} else {
999c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1000c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1001c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
1002c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
1003c4f52879SKarthikeyan Ramasubramanian 	}
1004c4f52879SKarthikeyan Ramasubramanian 
1005c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
1006c4f52879SKarthikeyan Ramasubramanian 	switch (termios->c_cflag & CSIZE) {
1007c4f52879SKarthikeyan Ramasubramanian 	case CS5:
1008c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 5;
1009c4f52879SKarthikeyan Ramasubramanian 		break;
1010c4f52879SKarthikeyan Ramasubramanian 	case CS6:
1011c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 6;
1012c4f52879SKarthikeyan Ramasubramanian 		break;
1013c4f52879SKarthikeyan Ramasubramanian 	case CS7:
1014c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 7;
1015c4f52879SKarthikeyan Ramasubramanian 		break;
1016c4f52879SKarthikeyan Ramasubramanian 	case CS8:
1017c4f52879SKarthikeyan Ramasubramanian 	default:
1018c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 8;
1019c4f52879SKarthikeyan Ramasubramanian 		break;
1020c4f52879SKarthikeyan Ramasubramanian 	}
1021c4f52879SKarthikeyan Ramasubramanian 
1022c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1023c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1024c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1025c4f52879SKarthikeyan Ramasubramanian 	else
1026c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1027c4f52879SKarthikeyan Ramasubramanian 
1028c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1029c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1030c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1031c4f52879SKarthikeyan Ramasubramanian 	else
1032c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1033c4f52879SKarthikeyan Ramasubramanian 
1034c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1035c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1036c4f52879SKarthikeyan Ramasubramanian 
10378a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
10388a8a66a1SGirish Mahadevan 		writel_relaxed(port->loopback,
10398a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
1040c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1041c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1042c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1043c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1044c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1045c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1046c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1047c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1048c4f52879SKarthikeyan Ramasubramanian 	writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1049c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1050c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1051c4f52879SKarthikeyan Ramasubramanian }
1052c4f52879SKarthikeyan Ramasubramanian 
1053c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1054c4f52879SKarthikeyan Ramasubramanian {
10557fb5b880SKarthikeyan Ramasubramanian 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1056c4f52879SKarthikeyan Ramasubramanian }
1057c4f52879SKarthikeyan Ramasubramanian 
1058c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1059c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_console_setup(struct console *co, char *options)
1060c4f52879SKarthikeyan Ramasubramanian {
1061c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1062c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1063c4f52879SKarthikeyan Ramasubramanian 	int baud;
1064c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1065c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1066c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1067c362272bSDouglas Anderson 	int ret;
1068c4f52879SKarthikeyan Ramasubramanian 
1069c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1070c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1071c4f52879SKarthikeyan Ramasubramanian 
10728a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1073c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
10746a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1075c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1076c4f52879SKarthikeyan Ramasubramanian 	}
1077c4f52879SKarthikeyan Ramasubramanian 
1078c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1079c4f52879SKarthikeyan Ramasubramanian 
1080c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1081c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1082c4f52879SKarthikeyan Ramasubramanian 
1083c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1084c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1085c362272bSDouglas Anderson 		if (ret)
1086c362272bSDouglas Anderson 			return ret;
1087c4f52879SKarthikeyan Ramasubramanian 	}
1088c4f52879SKarthikeyan Ramasubramanian 
1089c4f52879SKarthikeyan Ramasubramanian 	if (options)
1090c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1091c4f52879SKarthikeyan Ramasubramanian 
1092c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1093c4f52879SKarthikeyan Ramasubramanian }
1094c4f52879SKarthikeyan Ramasubramanian 
109543f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
109643f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
109743f1831bSKarthikeyan Ramasubramanian {
109843f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
109943f1831bSKarthikeyan Ramasubramanian 
110043f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
110143f1831bSKarthikeyan Ramasubramanian }
110243f1831bSKarthikeyan Ramasubramanian 
110343f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
110443f1831bSKarthikeyan Ramasubramanian 								const char *opt)
110543f1831bSKarthikeyan Ramasubramanian {
110643f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
110743f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
110843f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
110943f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
111043f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
111143f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
111243f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
111343f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
111443f1831bSKarthikeyan Ramasubramanian 
111543f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
111643f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
111743f1831bSKarthikeyan Ramasubramanian 
111843f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
111943f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
112043f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
112143f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
112243f1831bSKarthikeyan Ramasubramanian 	/*
112343f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
112443f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
112543f1831bSKarthikeyan Ramasubramanian 	 */
112643f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
112743f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
112843f1831bSKarthikeyan Ramasubramanian 
112943f1831bSKarthikeyan Ramasubramanian 	/*
113043f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
113143f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
113243f1831bSKarthikeyan Ramasubramanian 	 */
113343f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
113443f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
113543f1831bSKarthikeyan Ramasubramanian 	geni_se_config_packing(&se, BITS_PER_BYTE, 1, false, true, false);
113643f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
113743f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
113843f1831bSKarthikeyan Ramasubramanian 
113943f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
114043f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
114143f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
114243f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
114343f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
114443f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
114543f1831bSKarthikeyan Ramasubramanian 	writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
114643f1831bSKarthikeyan Ramasubramanian 
114743f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
114843f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
114943f1831bSKarthikeyan Ramasubramanian 	return 0;
115043f1831bSKarthikeyan Ramasubramanian }
115143f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
115243f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
115343f1831bSKarthikeyan Ramasubramanian 
1154c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1155c4f52879SKarthikeyan Ramasubramanian {
1156c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1157c4f52879SKarthikeyan Ramasubramanian }
1158c4f52879SKarthikeyan Ramasubramanian 
1159c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1160c4f52879SKarthikeyan Ramasubramanian {
1161c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1162c4f52879SKarthikeyan Ramasubramanian }
1163c4f52879SKarthikeyan Ramasubramanian 
1164c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1165c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1166c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1167c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1168c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1169c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1170c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1171c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1172c4f52879SKarthikeyan Ramasubramanian };
1173c4f52879SKarthikeyan Ramasubramanian 
1174c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1175c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1176c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1177c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1178c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1179c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1180c4f52879SKarthikeyan Ramasubramanian };
1181c4f52879SKarthikeyan Ramasubramanian #else
1182c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1183c4f52879SKarthikeyan Ramasubramanian {
1184c4f52879SKarthikeyan Ramasubramanian 	return 0;
1185c4f52879SKarthikeyan Ramasubramanian }
1186c4f52879SKarthikeyan Ramasubramanian 
1187c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1188c4f52879SKarthikeyan Ramasubramanian {
1189c4f52879SKarthikeyan Ramasubramanian }
1190c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1191c4f52879SKarthikeyan Ramasubramanian 
11928a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
11938a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
11948a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
11958a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
11968a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
11978a8a66a1SGirish Mahadevan };
11988a8a66a1SGirish Mahadevan 
11998a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1200c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1201c4f52879SKarthikeyan Ramasubramanian {
1202c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1203c4f52879SKarthikeyan Ramasubramanian 
1204c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1205c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1206c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1207c362272bSDouglas Anderson 
1208c4f52879SKarthikeyan Ramasubramanian 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
1209c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
1210c4f52879SKarthikeyan Ramasubramanian 	else if (new_state == UART_PM_STATE_OFF &&
1211c4f52879SKarthikeyan Ramasubramanian 			old_state == UART_PM_STATE_ON)
1212c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
1213c4f52879SKarthikeyan Ramasubramanian }
1214c4f52879SKarthikeyan Ramasubramanian 
1215c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1216c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
1217c4f52879SKarthikeyan Ramasubramanian 	.stop_tx = qcom_geni_serial_stop_tx,
1218c4f52879SKarthikeyan Ramasubramanian 	.start_tx = qcom_geni_serial_start_tx,
1219c4f52879SKarthikeyan Ramasubramanian 	.stop_rx = qcom_geni_serial_stop_rx,
1220c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1221c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1222c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1223c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1224c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1225c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
12268a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
12278a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1228c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1229c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1230c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1231c4f52879SKarthikeyan Ramasubramanian #endif
12328a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
12338a8a66a1SGirish Mahadevan };
12348a8a66a1SGirish Mahadevan 
12358a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
12368a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
12378a8a66a1SGirish Mahadevan 	.stop_tx = qcom_geni_serial_stop_tx,
12388a8a66a1SGirish Mahadevan 	.start_tx = qcom_geni_serial_start_tx,
12398a8a66a1SGirish Mahadevan 	.stop_rx = qcom_geni_serial_stop_rx,
12408a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
12418a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
12428a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
12438a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
12448a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
12458a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
12468a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
12478a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
12488a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1249c4f52879SKarthikeyan Ramasubramanian };
1250c4f52879SKarthikeyan Ramasubramanian 
1251c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1252c4f52879SKarthikeyan Ramasubramanian {
1253c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
1254c4f52879SKarthikeyan Ramasubramanian 	int line = -1;
1255c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1256c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1257c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1258066cd1c4SKarthikeyan Ramasubramanian 	int irq;
12598a8a66a1SGirish Mahadevan 	bool console = false;
12608a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
1261c4f52879SKarthikeyan Ramasubramanian 
12628a8a66a1SGirish Mahadevan 	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
12638a8a66a1SGirish Mahadevan 		console = true;
12648a8a66a1SGirish Mahadevan 
12658a8a66a1SGirish Mahadevan 	if (console) {
12668a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1267c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
12688a8a66a1SGirish Mahadevan 	} else {
12698a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
12708a8a66a1SGirish Mahadevan 		line = of_alias_get_id(pdev->dev.of_node, "hsuart");
12718a8a66a1SGirish Mahadevan 	}
1272c4f52879SKarthikeyan Ramasubramanian 
12738a8a66a1SGirish Mahadevan 	port = get_port_from_line(line, console);
1274c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
12756a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
12766a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1277c4f52879SKarthikeyan Ramasubramanian 	}
1278c4f52879SKarthikeyan Ramasubramanian 
1279c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1280c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1281c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1282c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1283c4f52879SKarthikeyan Ramasubramanian 
1284c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
1285c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1286c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1287c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1288c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1289c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1290c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1291c4f52879SKarthikeyan Ramasubramanian 		return ret;
1292c4f52879SKarthikeyan Ramasubramanian 	}
1293c4f52879SKarthikeyan Ramasubramanian 
1294c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12957693b331SWei Yongjun 	if (!res)
12967693b331SWei Yongjun 		return -EINVAL;
1297c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1298c4f52879SKarthikeyan Ramasubramanian 
1299c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1300c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1301c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1302c4f52879SKarthikeyan Ramasubramanian 
1303066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
1304066cd1c4SKarthikeyan Ramasubramanian 	if (irq < 0) {
1305066cd1c4SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Failed to get IRQ %d\n", irq);
1306066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1307c4f52879SKarthikeyan Ramasubramanian 	}
1308066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
1309c4f52879SKarthikeyan Ramasubramanian 
13108a8a66a1SGirish Mahadevan 	uport->private_data = drv;
1311c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
13128a8a66a1SGirish Mahadevan 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
13138a8a66a1SGirish Mahadevan 	if (!console)
13148a8a66a1SGirish Mahadevan 		device_create_file(uport->dev, &dev_attr_loopback);
13158a8a66a1SGirish Mahadevan 	return uart_add_one_port(drv, uport);
1316c4f52879SKarthikeyan Ramasubramanian }
1317c4f52879SKarthikeyan Ramasubramanian 
1318c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1319c4f52879SKarthikeyan Ramasubramanian {
1320c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1321c4f52879SKarthikeyan Ramasubramanian 	struct uart_driver *drv = port->uport.private_data;
1322c4f52879SKarthikeyan Ramasubramanian 
1323c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1324c4f52879SKarthikeyan Ramasubramanian 	return 0;
1325c4f52879SKarthikeyan Ramasubramanian }
1326c4f52879SKarthikeyan Ramasubramanian 
1327*b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1328c4f52879SKarthikeyan Ramasubramanian {
1329a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1330c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1331c4f52879SKarthikeyan Ramasubramanian 
1332*b1f84dd3SMukesh Kumar Savaliya 	return uart_suspend_port(uport->private_data, uport);
13338a8a66a1SGirish Mahadevan }
13348a8a66a1SGirish Mahadevan 
1335*b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
1336c4f52879SKarthikeyan Ramasubramanian {
1337a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1338c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1339c4f52879SKarthikeyan Ramasubramanian 
1340*b1f84dd3SMukesh Kumar Savaliya 	return uart_resume_port(uport->private_data, uport);
1341c4f52879SKarthikeyan Ramasubramanian }
1342c4f52879SKarthikeyan Ramasubramanian 
1343c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1344*b1f84dd3SMukesh Kumar Savaliya 	SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1345*b1f84dd3SMukesh Kumar Savaliya 					qcom_geni_serial_sys_resume)
1346c4f52879SKarthikeyan Ramasubramanian };
1347c4f52879SKarthikeyan Ramasubramanian 
1348c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
1349c4f52879SKarthikeyan Ramasubramanian 	{ .compatible = "qcom,geni-debug-uart", },
13508a8a66a1SGirish Mahadevan 	{ .compatible = "qcom,geni-uart", },
1351c4f52879SKarthikeyan Ramasubramanian 	{}
1352c4f52879SKarthikeyan Ramasubramanian };
1353c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1354c4f52879SKarthikeyan Ramasubramanian 
1355c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1356c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1357c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1358c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1359c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1360c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1361c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1362c4f52879SKarthikeyan Ramasubramanian 	},
1363c4f52879SKarthikeyan Ramasubramanian };
1364c4f52879SKarthikeyan Ramasubramanian 
1365c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1366c4f52879SKarthikeyan Ramasubramanian {
1367c4f52879SKarthikeyan Ramasubramanian 	int ret;
1368c4f52879SKarthikeyan Ramasubramanian 
1369c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1370c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1371c4f52879SKarthikeyan Ramasubramanian 		return ret;
1372c4f52879SKarthikeyan Ramasubramanian 
13738a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
13748a8a66a1SGirish Mahadevan 	if (ret) {
1375c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1376c4f52879SKarthikeyan Ramasubramanian 		return ret;
1377c4f52879SKarthikeyan Ramasubramanian 	}
13788a8a66a1SGirish Mahadevan 
13798a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
13808a8a66a1SGirish Mahadevan 	if (ret) {
13818a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
13828a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
13838a8a66a1SGirish Mahadevan 	}
13848a8a66a1SGirish Mahadevan 	return ret;
13858a8a66a1SGirish Mahadevan }
1386c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1387c4f52879SKarthikeyan Ramasubramanian 
1388c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1389c4f52879SKarthikeyan Ramasubramanian {
1390c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1391c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
13928a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1393c4f52879SKarthikeyan Ramasubramanian }
1394c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1395c4f52879SKarthikeyan Ramasubramanian 
1396c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1397c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1398