xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision 385298abbe968e9eb30b8f84e22e5165ebe2dd9f)
1c4f52879SKarthikeyan Ramasubramanian // SPDX-License-Identifier: GPL-2.0
2c4f52879SKarthikeyan Ramasubramanian // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3c4f52879SKarthikeyan Ramasubramanian 
4babeca85SDouglas Anderson #if defined(CONFIG_SERIAL_QCOM_GENI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
5babeca85SDouglas Anderson # define SUPPORT_SYSRQ
6babeca85SDouglas Anderson #endif
7babeca85SDouglas Anderson 
8c4f52879SKarthikeyan Ramasubramanian #include <linux/clk.h>
9c4f52879SKarthikeyan Ramasubramanian #include <linux/console.h>
10c4f52879SKarthikeyan Ramasubramanian #include <linux/io.h>
11c4f52879SKarthikeyan Ramasubramanian #include <linux/iopoll.h>
12c4f52879SKarthikeyan Ramasubramanian #include <linux/module.h>
13c4f52879SKarthikeyan Ramasubramanian #include <linux/of.h>
14c4f52879SKarthikeyan Ramasubramanian #include <linux/of_device.h>
15c4f52879SKarthikeyan Ramasubramanian #include <linux/platform_device.h>
16c4f52879SKarthikeyan Ramasubramanian #include <linux/qcom-geni-se.h>
17c4f52879SKarthikeyan Ramasubramanian #include <linux/serial.h>
18c4f52879SKarthikeyan Ramasubramanian #include <linux/serial_core.h>
19c4f52879SKarthikeyan Ramasubramanian #include <linux/slab.h>
20c4f52879SKarthikeyan Ramasubramanian #include <linux/tty.h>
21c4f52879SKarthikeyan Ramasubramanian #include <linux/tty_flip.h>
22c4f52879SKarthikeyan Ramasubramanian 
23c4f52879SKarthikeyan Ramasubramanian /* UART specific GENI registers */
248a8a66a1SGirish Mahadevan #define SE_UART_LOOPBACK_CFG		0x22c
25c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_CFG		0x25c
26c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_WORD_LEN		0x268
27c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_STOP_BIT_LEN		0x26c
28c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_TRANS_LEN		0x270
29c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_TRANS_CFG		0x280
30c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_WORD_LEN		0x28c
31c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_STALE_CNT		0x294
32c4f52879SKarthikeyan Ramasubramanian #define SE_UART_TX_PARITY_CFG		0x2a4
33c4f52879SKarthikeyan Ramasubramanian #define SE_UART_RX_PARITY_CFG		0x2a8
348a8a66a1SGirish Mahadevan #define SE_UART_MANUAL_RFR		0x2ac
35c4f52879SKarthikeyan Ramasubramanian 
36c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TRANS_CFG */
37c4f52879SKarthikeyan Ramasubramanian #define UART_TX_PAR_EN		BIT(0)
38c4f52879SKarthikeyan Ramasubramanian #define UART_CTS_MASK		BIT(1)
39c4f52879SKarthikeyan Ramasubramanian 
40c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_WORD_LEN */
41c4f52879SKarthikeyan Ramasubramanian #define TX_WORD_LEN_MSK		GENMASK(9, 0)
42c4f52879SKarthikeyan Ramasubramanian 
43c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_STOP_BIT_LEN */
44c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_MSK	GENMASK(23, 0)
45c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1	0
46c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_1_5	1
47c4f52879SKarthikeyan Ramasubramanian #define TX_STOP_BIT_LEN_2	2
48c4f52879SKarthikeyan Ramasubramanian 
49c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_TRANS_LEN */
50c4f52879SKarthikeyan Ramasubramanian #define TX_TRANS_LEN_MSK	GENMASK(23, 0)
51c4f52879SKarthikeyan Ramasubramanian 
52c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_TRANS_CFG */
53c4f52879SKarthikeyan Ramasubramanian #define UART_RX_INS_STATUS_BIT	BIT(2)
54c4f52879SKarthikeyan Ramasubramanian #define UART_RX_PAR_EN		BIT(3)
55c4f52879SKarthikeyan Ramasubramanian 
56c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_WORD_LEN */
57c4f52879SKarthikeyan Ramasubramanian #define RX_WORD_LEN_MASK	GENMASK(9, 0)
58c4f52879SKarthikeyan Ramasubramanian 
59c4f52879SKarthikeyan Ramasubramanian /* SE_UART_RX_STALE_CNT */
60c4f52879SKarthikeyan Ramasubramanian #define RX_STALE_CNT		GENMASK(23, 0)
61c4f52879SKarthikeyan Ramasubramanian 
62c4f52879SKarthikeyan Ramasubramanian /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
63c4f52879SKarthikeyan Ramasubramanian #define PAR_CALC_EN		BIT(0)
64c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_MSK		GENMASK(2, 1)
65c4f52879SKarthikeyan Ramasubramanian #define PAR_MODE_SHFT		1
66c4f52879SKarthikeyan Ramasubramanian #define PAR_EVEN		0x00
67c4f52879SKarthikeyan Ramasubramanian #define PAR_ODD			0x01
68c4f52879SKarthikeyan Ramasubramanian #define PAR_SPACE		0x10
69c4f52879SKarthikeyan Ramasubramanian #define PAR_MARK		0x11
70c4f52879SKarthikeyan Ramasubramanian 
718a8a66a1SGirish Mahadevan /* SE_UART_MANUAL_RFR register fields */
728a8a66a1SGirish Mahadevan #define UART_MANUAL_RFR_EN	BIT(31)
738a8a66a1SGirish Mahadevan #define UART_RFR_NOT_READY	BIT(1)
748a8a66a1SGirish Mahadevan #define UART_RFR_READY		BIT(0)
758a8a66a1SGirish Mahadevan 
76c4f52879SKarthikeyan Ramasubramanian /* UART M_CMD OP codes */
77c4f52879SKarthikeyan Ramasubramanian #define UART_START_TX		0x1
78c4f52879SKarthikeyan Ramasubramanian #define UART_START_BREAK	0x4
79c4f52879SKarthikeyan Ramasubramanian #define UART_STOP_BREAK		0x5
80c4f52879SKarthikeyan Ramasubramanian /* UART S_CMD OP codes */
81c4f52879SKarthikeyan Ramasubramanian #define UART_START_READ		0x1
82c4f52879SKarthikeyan Ramasubramanian #define UART_PARAM		0x1
83c4f52879SKarthikeyan Ramasubramanian 
84c4f52879SKarthikeyan Ramasubramanian #define UART_OVERSAMPLING	32
85c4f52879SKarthikeyan Ramasubramanian #define STALE_TIMEOUT		16
86c4f52879SKarthikeyan Ramasubramanian #define DEFAULT_BITS_PER_CHAR	10
87c4f52879SKarthikeyan Ramasubramanian #define GENI_UART_CONS_PORTS	1
888a8a66a1SGirish Mahadevan #define GENI_UART_PORTS		3
89c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_DEPTH_WORDS	16
90c4f52879SKarthikeyan Ramasubramanian #define DEF_TX_WM		2
91c4f52879SKarthikeyan Ramasubramanian #define DEF_FIFO_WIDTH_BITS	32
92a85fb9ceSRyan Case #define UART_RX_WM		2
938a8a66a1SGirish Mahadevan #define MAX_LOOPBACK_CFG	3
94c4f52879SKarthikeyan Ramasubramanian 
95c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
969f641df4SDouglas Anderson #define CONSOLE_RX_BYTES_PW 1
97c4f52879SKarthikeyan Ramasubramanian #else
989f641df4SDouglas Anderson #define CONSOLE_RX_BYTES_PW 4
99c4f52879SKarthikeyan Ramasubramanian #endif
100c4f52879SKarthikeyan Ramasubramanian 
101c4f52879SKarthikeyan Ramasubramanian struct qcom_geni_serial_port {
102c4f52879SKarthikeyan Ramasubramanian 	struct uart_port uport;
103c4f52879SKarthikeyan Ramasubramanian 	struct geni_se se;
104c4f52879SKarthikeyan Ramasubramanian 	char name[20];
105c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_depth;
106c4f52879SKarthikeyan Ramasubramanian 	u32 tx_fifo_width;
107c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo_depth;
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;
116a1fee899SRyan Case 
117a1fee899SRyan Case 	unsigned int tx_remaining;
118c4f52879SKarthikeyan Ramasubramanian };
119c4f52879SKarthikeyan Ramasubramanian 
120f7371750SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops;
1218a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops;
122c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver;
1238a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver;
124c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
1258a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
126c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
127c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport);
128c4f52879SKarthikeyan Ramasubramanian 
129c4f52879SKarthikeyan Ramasubramanian static const unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
130c4f52879SKarthikeyan Ramasubramanian 					32000000, 48000000, 64000000, 80000000,
1318a8a66a1SGirish Mahadevan 					96000000, 100000000, 102400000,
1328a8a66a1SGirish Mahadevan 					112000000, 120000000, 128000000};
133c4f52879SKarthikeyan Ramasubramanian 
134c4f52879SKarthikeyan Ramasubramanian #define to_dev_port(ptr, member) \
135c4f52879SKarthikeyan Ramasubramanian 		container_of(ptr, struct qcom_geni_serial_port, member)
136c4f52879SKarthikeyan Ramasubramanian 
1378a8a66a1SGirish Mahadevan static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
1388a8a66a1SGirish Mahadevan 	[0] = {
1398a8a66a1SGirish Mahadevan 		.uport = {
1408a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1418a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1428a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1438a8a66a1SGirish Mahadevan 				.line = 0,
1448a8a66a1SGirish Mahadevan 		},
1458a8a66a1SGirish Mahadevan 	},
1468a8a66a1SGirish Mahadevan 	[1] = {
1478a8a66a1SGirish Mahadevan 		.uport = {
1488a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1498a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1508a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1518a8a66a1SGirish Mahadevan 				.line = 1,
1528a8a66a1SGirish Mahadevan 		},
1538a8a66a1SGirish Mahadevan 	},
1548a8a66a1SGirish Mahadevan 	[2] = {
1558a8a66a1SGirish Mahadevan 		.uport = {
1568a8a66a1SGirish Mahadevan 				.iotype = UPIO_MEM,
1578a8a66a1SGirish Mahadevan 				.ops = &qcom_geni_uart_pops,
1588a8a66a1SGirish Mahadevan 				.flags = UPF_BOOT_AUTOCONF,
1598a8a66a1SGirish Mahadevan 				.line = 2,
1608a8a66a1SGirish Mahadevan 		},
1618a8a66a1SGirish Mahadevan 	},
1628a8a66a1SGirish Mahadevan };
1638a8a66a1SGirish Mahadevan 
1648a8a66a1SGirish Mahadevan static ssize_t loopback_show(struct device *dev,
1658a8a66a1SGirish Mahadevan 				struct device_attribute *attr, char *buf)
1668a8a66a1SGirish Mahadevan {
1677034ef87SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1688a8a66a1SGirish Mahadevan 
1698a8a66a1SGirish Mahadevan 	return snprintf(buf, sizeof(u32), "%d\n", port->loopback);
1708a8a66a1SGirish Mahadevan }
1718a8a66a1SGirish Mahadevan 
1728a8a66a1SGirish Mahadevan static ssize_t loopback_store(struct device *dev,
1738a8a66a1SGirish Mahadevan 				struct device_attribute *attr, const char *buf,
1748a8a66a1SGirish Mahadevan 				size_t size)
1758a8a66a1SGirish Mahadevan {
1767034ef87SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
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 {
2279e06d55fSRyan Case 		geni_ios = readl(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;
2459e06d55fSRyan Case 	writel(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 	if (uport->private_data) {
275c4f52879SKarthikeyan Ramasubramanian 		port = to_dev_port(uport, uport);
276c4f52879SKarthikeyan Ramasubramanian 		baud = port->baud;
277c4f52879SKarthikeyan Ramasubramanian 		if (!baud)
278c4f52879SKarthikeyan Ramasubramanian 			baud = 115200;
279c4f52879SKarthikeyan Ramasubramanian 		fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
280c4f52879SKarthikeyan Ramasubramanian 		/*
281c4f52879SKarthikeyan Ramasubramanian 		 * Total polling iterations based on FIFO worth of bytes to be
282c4f52879SKarthikeyan Ramasubramanian 		 * sent at current baud. Add a little fluff to the wait.
283c4f52879SKarthikeyan Ramasubramanian 		 */
284c4f52879SKarthikeyan Ramasubramanian 		timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
285c4f52879SKarthikeyan Ramasubramanian 	}
286c4f52879SKarthikeyan Ramasubramanian 
28743f1831bSKarthikeyan Ramasubramanian 	/*
28843f1831bSKarthikeyan Ramasubramanian 	 * Use custom implementation instead of readl_poll_atomic since ktimer
28943f1831bSKarthikeyan Ramasubramanian 	 * is not ready at the time of early console.
29043f1831bSKarthikeyan Ramasubramanian 	 */
29143f1831bSKarthikeyan Ramasubramanian 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
29243f1831bSKarthikeyan Ramasubramanian 	while (timeout_us) {
2939e06d55fSRyan Case 		reg = readl(uport->membase + offset);
29443f1831bSKarthikeyan Ramasubramanian 		if ((bool)(reg & field) == set)
29543f1831bSKarthikeyan Ramasubramanian 			return true;
29643f1831bSKarthikeyan Ramasubramanian 		udelay(10);
29743f1831bSKarthikeyan Ramasubramanian 		timeout_us -= 10;
29843f1831bSKarthikeyan Ramasubramanian 	}
29943f1831bSKarthikeyan Ramasubramanian 	return false;
300c4f52879SKarthikeyan Ramasubramanian }
301c4f52879SKarthikeyan Ramasubramanian 
302c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
303c4f52879SKarthikeyan Ramasubramanian {
304c4f52879SKarthikeyan Ramasubramanian 	u32 m_cmd;
305c4f52879SKarthikeyan Ramasubramanian 
3069e06d55fSRyan Case 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
307c4f52879SKarthikeyan Ramasubramanian 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
308c4f52879SKarthikeyan Ramasubramanian 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
309c4f52879SKarthikeyan Ramasubramanian }
310c4f52879SKarthikeyan Ramasubramanian 
311c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
312c4f52879SKarthikeyan Ramasubramanian {
313c4f52879SKarthikeyan Ramasubramanian 	int done;
314c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = M_CMD_DONE_EN;
315c4f52879SKarthikeyan Ramasubramanian 
316c4f52879SKarthikeyan Ramasubramanian 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
317c4f52879SKarthikeyan Ramasubramanian 						M_CMD_DONE_EN, true);
318c4f52879SKarthikeyan Ramasubramanian 	if (!done) {
3199e06d55fSRyan Case 		writel(M_GENI_CMD_ABORT, uport->membase +
320c4f52879SKarthikeyan Ramasubramanian 						SE_GENI_M_CMD_CTRL_REG);
321c4f52879SKarthikeyan Ramasubramanian 		irq_clear |= M_CMD_ABORT_EN;
322c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
323c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
324c4f52879SKarthikeyan Ramasubramanian 	}
3259e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
326c4f52879SKarthikeyan Ramasubramanian }
327c4f52879SKarthikeyan Ramasubramanian 
328c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_abort_rx(struct uart_port *uport)
329c4f52879SKarthikeyan Ramasubramanian {
330c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
331c4f52879SKarthikeyan Ramasubramanian 
332c4f52879SKarthikeyan Ramasubramanian 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
333c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
334c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_ABORT, false);
3359e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
3369e06d55fSRyan Case 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
337c4f52879SKarthikeyan Ramasubramanian }
338c4f52879SKarthikeyan Ramasubramanian 
339c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
340c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_get_char(struct uart_port *uport)
341c4f52879SKarthikeyan Ramasubramanian {
342c4f52879SKarthikeyan Ramasubramanian 	u32 rx_fifo;
343c4f52879SKarthikeyan Ramasubramanian 	u32 status;
344c4f52879SKarthikeyan Ramasubramanian 
3459e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
3469e06d55fSRyan Case 	writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
347c4f52879SKarthikeyan Ramasubramanian 
3489e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
3499e06d55fSRyan Case 	writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
350c4f52879SKarthikeyan Ramasubramanian 
3519e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
352c4f52879SKarthikeyan Ramasubramanian 	if (!(status & RX_FIFO_WC_MSK))
353c4f52879SKarthikeyan Ramasubramanian 		return NO_POLL_CHAR;
354c4f52879SKarthikeyan Ramasubramanian 
355c4f52879SKarthikeyan Ramasubramanian 	rx_fifo = readl(uport->membase + SE_GENI_RX_FIFOn);
356c4f52879SKarthikeyan Ramasubramanian 	return rx_fifo & 0xff;
357c4f52879SKarthikeyan Ramasubramanian }
358c4f52879SKarthikeyan Ramasubramanian 
359c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
360c4f52879SKarthikeyan Ramasubramanian 							unsigned char c)
361c4f52879SKarthikeyan Ramasubramanian {
362a85fb9ceSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
363c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, 1);
364c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
365c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true));
3669e06d55fSRyan Case 	writel(c, uport->membase + SE_GENI_TX_FIFOn);
3679e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
368c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
369c4f52879SKarthikeyan Ramasubramanian }
370c4f52879SKarthikeyan Ramasubramanian #endif
371c4f52879SKarthikeyan Ramasubramanian 
372c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
373c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch)
374c4f52879SKarthikeyan Ramasubramanian {
3759e06d55fSRyan Case 	writel(ch, uport->membase + SE_GENI_TX_FIFOn);
376c4f52879SKarthikeyan Ramasubramanian }
377c4f52879SKarthikeyan Ramasubramanian 
378c4f52879SKarthikeyan Ramasubramanian static void
379c4f52879SKarthikeyan Ramasubramanian __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
380c4f52879SKarthikeyan Ramasubramanian 				 unsigned int count)
381c4f52879SKarthikeyan Ramasubramanian {
382c4f52879SKarthikeyan Ramasubramanian 	int i;
383c4f52879SKarthikeyan Ramasubramanian 	u32 bytes_to_send = count;
384c4f52879SKarthikeyan Ramasubramanian 
385c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; i++) {
386f0262568SKarthikeyan Ramasubramanian 		/*
387f0262568SKarthikeyan Ramasubramanian 		 * uart_console_write() adds a carriage return for each newline.
388f0262568SKarthikeyan Ramasubramanian 		 * Account for additional bytes to be written.
389f0262568SKarthikeyan Ramasubramanian 		 */
390c4f52879SKarthikeyan Ramasubramanian 		if (s[i] == '\n')
391c4f52879SKarthikeyan Ramasubramanian 			bytes_to_send++;
392c4f52879SKarthikeyan Ramasubramanian 	}
393c4f52879SKarthikeyan Ramasubramanian 
3949e06d55fSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
395c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
396c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < count; ) {
397c4f52879SKarthikeyan Ramasubramanian 		size_t chars_to_write = 0;
398c4f52879SKarthikeyan Ramasubramanian 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
399c4f52879SKarthikeyan Ramasubramanian 
400c4f52879SKarthikeyan Ramasubramanian 		/*
401c4f52879SKarthikeyan Ramasubramanian 		 * If the WM bit never set, then the Tx state machine is not
402c4f52879SKarthikeyan Ramasubramanian 		 * in a valid state, so break, cancel/abort any existing
403c4f52879SKarthikeyan Ramasubramanian 		 * command. Unfortunately the current data being written is
404c4f52879SKarthikeyan Ramasubramanian 		 * lost.
405c4f52879SKarthikeyan Ramasubramanian 		 */
406c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
407c4f52879SKarthikeyan Ramasubramanian 						M_TX_FIFO_WATERMARK_EN, true))
408c4f52879SKarthikeyan Ramasubramanian 			break;
4096a10635eSKarthikeyan Ramasubramanian 		chars_to_write = min_t(size_t, count - i, avail / 2);
410c4f52879SKarthikeyan Ramasubramanian 		uart_console_write(uport, s + i, chars_to_write,
411c4f52879SKarthikeyan Ramasubramanian 						qcom_geni_serial_wr_char);
4129e06d55fSRyan Case 		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
413c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
414c4f52879SKarthikeyan Ramasubramanian 		i += chars_to_write;
415c4f52879SKarthikeyan Ramasubramanian 	}
416c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
417c4f52879SKarthikeyan Ramasubramanian }
418c4f52879SKarthikeyan Ramasubramanian 
419c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_console_write(struct console *co, const char *s,
420c4f52879SKarthikeyan Ramasubramanian 			      unsigned int count)
421c4f52879SKarthikeyan Ramasubramanian {
422c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
423c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
424c4f52879SKarthikeyan Ramasubramanian 	bool locked = true;
425c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
426a1fee899SRyan Case 	u32 geni_status;
427663abb1aSRyan Case 	u32 irq_en;
428c4f52879SKarthikeyan Ramasubramanian 
429c4f52879SKarthikeyan Ramasubramanian 	WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
430c4f52879SKarthikeyan Ramasubramanian 
4318a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
432c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port))
433c4f52879SKarthikeyan Ramasubramanian 		return;
434c4f52879SKarthikeyan Ramasubramanian 
435c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
436c4f52879SKarthikeyan Ramasubramanian 	if (oops_in_progress)
437c4f52879SKarthikeyan Ramasubramanian 		locked = spin_trylock_irqsave(&uport->lock, flags);
438c4f52879SKarthikeyan Ramasubramanian 	else
439c4f52879SKarthikeyan Ramasubramanian 		spin_lock_irqsave(&uport->lock, flags);
440c4f52879SKarthikeyan Ramasubramanian 
4419e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
442a1fee899SRyan Case 
443c4f52879SKarthikeyan Ramasubramanian 	/* Cancel the current write to log the fault */
444c4f52879SKarthikeyan Ramasubramanian 	if (!locked) {
445c4f52879SKarthikeyan Ramasubramanian 		geni_se_cancel_m_cmd(&port->se);
446c4f52879SKarthikeyan Ramasubramanian 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
447c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
448c4f52879SKarthikeyan Ramasubramanian 			geni_se_abort_m_cmd(&port->se);
449c4f52879SKarthikeyan Ramasubramanian 			qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
450c4f52879SKarthikeyan Ramasubramanian 							M_CMD_ABORT_EN, true);
4519e06d55fSRyan Case 			writel(M_CMD_ABORT_EN, uport->membase +
452c4f52879SKarthikeyan Ramasubramanian 							SE_GENI_M_IRQ_CLEAR);
453c4f52879SKarthikeyan Ramasubramanian 		}
4549e06d55fSRyan Case 		writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
455a1fee899SRyan Case 	} else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
456a1fee899SRyan Case 		/*
457a1fee899SRyan Case 		 * It seems we can't interrupt existing transfers if all data
458a1fee899SRyan Case 		 * has been sent, in which case we need to look for done first.
459a1fee899SRyan Case 		 */
460a1fee899SRyan Case 		qcom_geni_serial_poll_tx_done(uport);
461663abb1aSRyan Case 
462663abb1aSRyan Case 		if (uart_circ_chars_pending(&uport->state->xmit)) {
4639e06d55fSRyan Case 			irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
4649e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
465663abb1aSRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
466663abb1aSRyan Case 		}
467c4f52879SKarthikeyan Ramasubramanian 	}
468c4f52879SKarthikeyan Ramasubramanian 
469c4f52879SKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(uport, s, count);
470a1fee899SRyan Case 
471a1fee899SRyan Case 	if (port->tx_remaining)
472a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, port->tx_remaining);
473a1fee899SRyan Case 
474c4f52879SKarthikeyan Ramasubramanian 	if (locked)
475c4f52879SKarthikeyan Ramasubramanian 		spin_unlock_irqrestore(&uport->lock, flags);
476c4f52879SKarthikeyan Ramasubramanian }
477c4f52879SKarthikeyan Ramasubramanian 
478c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
479c4f52879SKarthikeyan Ramasubramanian {
480c4f52879SKarthikeyan Ramasubramanian 	u32 i;
481c4f52879SKarthikeyan Ramasubramanian 	unsigned char buf[sizeof(u32)];
482c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport;
483c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
484c4f52879SKarthikeyan Ramasubramanian 
485c4f52879SKarthikeyan Ramasubramanian 	tport = &uport->state->port;
486c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < bytes; ) {
487c4f52879SKarthikeyan Ramasubramanian 		int c;
488c4f52879SKarthikeyan Ramasubramanian 		int chunk = min_t(int, bytes - i, port->rx_bytes_pw);
489c4f52879SKarthikeyan Ramasubramanian 
490c4f52879SKarthikeyan Ramasubramanian 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
491c4f52879SKarthikeyan Ramasubramanian 		i += chunk;
492c4f52879SKarthikeyan Ramasubramanian 		if (drop)
493c4f52879SKarthikeyan Ramasubramanian 			continue;
494c4f52879SKarthikeyan Ramasubramanian 
495c4f52879SKarthikeyan Ramasubramanian 		for (c = 0; c < chunk; c++) {
496c4f52879SKarthikeyan Ramasubramanian 			int sysrq;
497c4f52879SKarthikeyan Ramasubramanian 
498c4f52879SKarthikeyan Ramasubramanian 			uport->icount.rx++;
499c4f52879SKarthikeyan Ramasubramanian 			if (port->brk && buf[c] == 0) {
500c4f52879SKarthikeyan Ramasubramanian 				port->brk = false;
501c4f52879SKarthikeyan Ramasubramanian 				if (uart_handle_break(uport))
502c4f52879SKarthikeyan Ramasubramanian 					continue;
503c4f52879SKarthikeyan Ramasubramanian 			}
504c4f52879SKarthikeyan Ramasubramanian 
505336447b3SDouglas Anderson 			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
506babeca85SDouglas Anderson 
507c4f52879SKarthikeyan Ramasubramanian 			if (!sysrq)
508c4f52879SKarthikeyan Ramasubramanian 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
509c4f52879SKarthikeyan Ramasubramanian 		}
510c4f52879SKarthikeyan Ramasubramanian 	}
511c4f52879SKarthikeyan Ramasubramanian 	if (!drop)
512c4f52879SKarthikeyan Ramasubramanian 		tty_flip_buffer_push(tport);
513c4f52879SKarthikeyan Ramasubramanian 	return 0;
514c4f52879SKarthikeyan Ramasubramanian }
515c4f52879SKarthikeyan Ramasubramanian #else
516c4f52879SKarthikeyan Ramasubramanian static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
517c4f52879SKarthikeyan Ramasubramanian {
518c4f52879SKarthikeyan Ramasubramanian 	return -EPERM;
519c4f52879SKarthikeyan Ramasubramanian }
520c4f52879SKarthikeyan Ramasubramanian 
521c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
522c4f52879SKarthikeyan Ramasubramanian 
5238a8a66a1SGirish Mahadevan static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
5248a8a66a1SGirish Mahadevan {
5258a8a66a1SGirish Mahadevan 	unsigned char *buf;
5268a8a66a1SGirish Mahadevan 	struct tty_port *tport;
5278a8a66a1SGirish Mahadevan 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
5288a8a66a1SGirish Mahadevan 	u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
5298a8a66a1SGirish Mahadevan 	u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
5308a8a66a1SGirish Mahadevan 	int ret;
5318a8a66a1SGirish Mahadevan 
5328a8a66a1SGirish Mahadevan 	tport = &uport->state->port;
5338a8a66a1SGirish Mahadevan 	ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
5348a8a66a1SGirish Mahadevan 	if (drop)
5358a8a66a1SGirish Mahadevan 		return 0;
5368a8a66a1SGirish Mahadevan 
5378a8a66a1SGirish Mahadevan 	buf = (unsigned char *)port->rx_fifo;
5388a8a66a1SGirish Mahadevan 	ret = tty_insert_flip_string(tport, buf, bytes);
5398a8a66a1SGirish Mahadevan 	if (ret != bytes) {
5408a8a66a1SGirish Mahadevan 		dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
5418a8a66a1SGirish Mahadevan 				__func__, ret, bytes);
5428a8a66a1SGirish Mahadevan 		WARN_ON_ONCE(1);
5438a8a66a1SGirish Mahadevan 	}
5448a8a66a1SGirish Mahadevan 	uport->icount.rx += ret;
5458a8a66a1SGirish Mahadevan 	tty_flip_buffer_push(tport);
5468a8a66a1SGirish Mahadevan 	return ret;
5478a8a66a1SGirish Mahadevan }
5488a8a66a1SGirish Mahadevan 
549c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_tx(struct uart_port *uport)
550c4f52879SKarthikeyan Ramasubramanian {
551c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
552c4f52879SKarthikeyan Ramasubramanian 	u32 status;
553c4f52879SKarthikeyan Ramasubramanian 
5547fb5b880SKarthikeyan Ramasubramanian 	status = readl(uport->membase + SE_GENI_STATUS);
555c4f52879SKarthikeyan Ramasubramanian 	if (status & M_GENI_CMD_ACTIVE)
556c4f52879SKarthikeyan Ramasubramanian 		return;
557c4f52879SKarthikeyan Ramasubramanian 
558c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_tx_empty(uport))
559c4f52879SKarthikeyan Ramasubramanian 		return;
560c4f52879SKarthikeyan Ramasubramanian 
5619e06d55fSRyan Case 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
562c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
563c4f52879SKarthikeyan Ramasubramanian 
564bdc05a8aSRyan Case 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
5659e06d55fSRyan Case 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
566c4f52879SKarthikeyan Ramasubramanian }
567c4f52879SKarthikeyan Ramasubramanian 
568c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_tx(struct uart_port *uport)
569c4f52879SKarthikeyan Ramasubramanian {
570c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
571c4f52879SKarthikeyan Ramasubramanian 	u32 status;
572c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
573c4f52879SKarthikeyan Ramasubramanian 
5749e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
575bdc05a8aSRyan Case 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
576bdc05a8aSRyan Case 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
5779e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
5789e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
579c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop tx is called multiple times. */
580c4f52879SKarthikeyan Ramasubramanian 	if (!(status & M_GENI_CMD_ACTIVE))
581c4f52879SKarthikeyan Ramasubramanian 		return;
582c4f52879SKarthikeyan Ramasubramanian 
583c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_m_cmd(&port->se);
584c4f52879SKarthikeyan Ramasubramanian 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
585c4f52879SKarthikeyan Ramasubramanian 						M_CMD_CANCEL_EN, true)) {
586c4f52879SKarthikeyan Ramasubramanian 		geni_se_abort_m_cmd(&port->se);
587c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
588c4f52879SKarthikeyan Ramasubramanian 						M_CMD_ABORT_EN, true);
5899e06d55fSRyan Case 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
590c4f52879SKarthikeyan Ramasubramanian 	}
5919e06d55fSRyan Case 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
592c4f52879SKarthikeyan Ramasubramanian }
593c4f52879SKarthikeyan Ramasubramanian 
594c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_start_rx(struct uart_port *uport)
595c4f52879SKarthikeyan Ramasubramanian {
596c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
597c4f52879SKarthikeyan Ramasubramanian 	u32 status;
598c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
599c4f52879SKarthikeyan Ramasubramanian 
6009e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
601c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
602c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_rx(uport);
603c4f52879SKarthikeyan Ramasubramanian 
604c4f52879SKarthikeyan Ramasubramanian 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
605c4f52879SKarthikeyan Ramasubramanian 
6069e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
607c4f52879SKarthikeyan Ramasubramanian 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
6089e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
609c4f52879SKarthikeyan Ramasubramanian 
6109e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
611c4f52879SKarthikeyan Ramasubramanian 	irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
6129e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
613c4f52879SKarthikeyan Ramasubramanian }
614c4f52879SKarthikeyan Ramasubramanian 
615c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_stop_rx(struct uart_port *uport)
616c4f52879SKarthikeyan Ramasubramanian {
617c4f52879SKarthikeyan Ramasubramanian 	u32 irq_en;
618c4f52879SKarthikeyan Ramasubramanian 	u32 status;
619c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
620c4f52879SKarthikeyan Ramasubramanian 	u32 irq_clear = S_CMD_DONE_EN;
621c4f52879SKarthikeyan Ramasubramanian 
6229e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
623c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
6249e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
625c4f52879SKarthikeyan Ramasubramanian 
6269e06d55fSRyan Case 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
627c4f52879SKarthikeyan Ramasubramanian 	irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
6289e06d55fSRyan Case 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
629c4f52879SKarthikeyan Ramasubramanian 
6309e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
631c4f52879SKarthikeyan Ramasubramanian 	/* Possible stop rx is called multiple times. */
632c4f52879SKarthikeyan Ramasubramanian 	if (!(status & S_GENI_CMD_ACTIVE))
633c4f52879SKarthikeyan Ramasubramanian 		return;
634c4f52879SKarthikeyan Ramasubramanian 
635c4f52879SKarthikeyan Ramasubramanian 	geni_se_cancel_s_cmd(&port->se);
636c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
637c4f52879SKarthikeyan Ramasubramanian 					S_GENI_CMD_CANCEL, false);
6389e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_STATUS);
6399e06d55fSRyan Case 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
640c4f52879SKarthikeyan Ramasubramanian 	if (status & S_GENI_CMD_ACTIVE)
641c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_abort_rx(uport);
642c4f52879SKarthikeyan Ramasubramanian }
643c4f52879SKarthikeyan Ramasubramanian 
644c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
645c4f52879SKarthikeyan Ramasubramanian {
646c4f52879SKarthikeyan Ramasubramanian 	u32 status;
647c4f52879SKarthikeyan Ramasubramanian 	u32 word_cnt;
648c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_byte_cnt;
649c4f52879SKarthikeyan Ramasubramanian 	u32 last_word_partial;
650c4f52879SKarthikeyan Ramasubramanian 	u32 total_bytes;
651c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
652c4f52879SKarthikeyan Ramasubramanian 
6539e06d55fSRyan Case 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
654c4f52879SKarthikeyan Ramasubramanian 	word_cnt = status & RX_FIFO_WC_MSK;
655c4f52879SKarthikeyan Ramasubramanian 	last_word_partial = status & RX_LAST;
656c4f52879SKarthikeyan Ramasubramanian 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
657c4f52879SKarthikeyan Ramasubramanian 						RX_LAST_BYTE_VALID_SHFT;
658c4f52879SKarthikeyan Ramasubramanian 
659c4f52879SKarthikeyan Ramasubramanian 	if (!word_cnt)
660c4f52879SKarthikeyan Ramasubramanian 		return;
661c4f52879SKarthikeyan Ramasubramanian 	total_bytes = port->rx_bytes_pw * (word_cnt - 1);
662c4f52879SKarthikeyan Ramasubramanian 	if (last_word_partial && last_word_byte_cnt)
663c4f52879SKarthikeyan Ramasubramanian 		total_bytes += last_word_byte_cnt;
664c4f52879SKarthikeyan Ramasubramanian 	else
665c4f52879SKarthikeyan Ramasubramanian 		total_bytes += port->rx_bytes_pw;
666c4f52879SKarthikeyan Ramasubramanian 	port->handle_rx(uport, total_bytes, drop);
667c4f52879SKarthikeyan Ramasubramanian }
668c4f52879SKarthikeyan Ramasubramanian 
669a1fee899SRyan Case static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
670a1fee899SRyan Case 		bool active)
671c4f52879SKarthikeyan Ramasubramanian {
672c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
673c4f52879SKarthikeyan Ramasubramanian 	struct circ_buf *xmit = &uport->state->xmit;
674c4f52879SKarthikeyan Ramasubramanian 	size_t avail;
675c4f52879SKarthikeyan Ramasubramanian 	size_t remaining;
676a1fee899SRyan Case 	size_t pending;
677c4f52879SKarthikeyan Ramasubramanian 	int i;
678c4f52879SKarthikeyan Ramasubramanian 	u32 status;
67964a42807SRyan Case 	u32 irq_en;
680c4f52879SKarthikeyan Ramasubramanian 	unsigned int chunk;
681c4f52879SKarthikeyan Ramasubramanian 	int tail;
682c4f52879SKarthikeyan Ramasubramanian 
6839e06d55fSRyan Case 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
684a1fee899SRyan Case 
685a1fee899SRyan Case 	/* Complete the current tx command before taking newly added data */
686a1fee899SRyan Case 	if (active)
687a1fee899SRyan Case 		pending = port->tx_remaining;
688a1fee899SRyan Case 	else
689a1fee899SRyan Case 		pending = uart_circ_chars_pending(xmit);
690a1fee899SRyan Case 
691a1fee899SRyan Case 	/* All data has been transmitted and acknowledged as received */
692a1fee899SRyan Case 	if (!pending && !status && done) {
693c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_stop_tx(uport);
694c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
695c4f52879SKarthikeyan Ramasubramanian 	}
696c4f52879SKarthikeyan Ramasubramanian 
697a1fee899SRyan Case 	avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
698a1fee899SRyan Case 	avail *= port->tx_bytes_pw;
6998a8a66a1SGirish Mahadevan 
700638a6f4eSEvan Green 	tail = xmit->tail;
7013c66eb4bSMatthias Kaehlcke 	chunk = min(avail, pending);
702c4f52879SKarthikeyan Ramasubramanian 	if (!chunk)
703c4f52879SKarthikeyan Ramasubramanian 		goto out_write_wakeup;
704c4f52879SKarthikeyan Ramasubramanian 
705a1fee899SRyan Case 	if (!port->tx_remaining) {
706a1fee899SRyan Case 		qcom_geni_serial_setup_tx(uport, pending);
707a1fee899SRyan Case 		port->tx_remaining = pending;
70864a42807SRyan Case 
7099e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
71064a42807SRyan Case 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
7119e06d55fSRyan Case 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
71264a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
713a1fee899SRyan Case 	}
714c4f52879SKarthikeyan Ramasubramanian 
715c4f52879SKarthikeyan Ramasubramanian 	remaining = chunk;
716c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < chunk; ) {
717c4f52879SKarthikeyan Ramasubramanian 		unsigned int tx_bytes;
71869736b57SKarthikeyan Ramasubramanian 		u8 buf[sizeof(u32)];
719c4f52879SKarthikeyan Ramasubramanian 		int c;
720c4f52879SKarthikeyan Ramasubramanian 
72169736b57SKarthikeyan Ramasubramanian 		memset(buf, 0, ARRAY_SIZE(buf));
7226a10635eSKarthikeyan Ramasubramanian 		tx_bytes = min_t(size_t, remaining, port->tx_bytes_pw);
7233c66eb4bSMatthias Kaehlcke 
7243c66eb4bSMatthias Kaehlcke 		for (c = 0; c < tx_bytes ; c++) {
7253c66eb4bSMatthias Kaehlcke 			buf[c] = xmit->buf[tail++];
7263c66eb4bSMatthias Kaehlcke 			tail &= UART_XMIT_SIZE - 1;
7273c66eb4bSMatthias Kaehlcke 		}
728c4f52879SKarthikeyan Ramasubramanian 
72969736b57SKarthikeyan Ramasubramanian 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
730c4f52879SKarthikeyan Ramasubramanian 
731c4f52879SKarthikeyan Ramasubramanian 		i += tx_bytes;
732c4f52879SKarthikeyan Ramasubramanian 		uport->icount.tx += tx_bytes;
733c4f52879SKarthikeyan Ramasubramanian 		remaining -= tx_bytes;
734a1fee899SRyan Case 		port->tx_remaining -= tx_bytes;
735c4f52879SKarthikeyan Ramasubramanian 	}
736638a6f4eSEvan Green 
7373c66eb4bSMatthias Kaehlcke 	xmit->tail = tail;
73864a42807SRyan Case 
73964a42807SRyan Case 	/*
74064a42807SRyan Case 	 * The tx fifo watermark is level triggered and latched. Though we had
74164a42807SRyan Case 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
74264a42807SRyan Case 	 * so we must clear it again here after our writes.
74364a42807SRyan Case 	 */
7449e06d55fSRyan Case 	writel(M_TX_FIFO_WATERMARK_EN,
74564a42807SRyan Case 			uport->membase + SE_GENI_M_IRQ_CLEAR);
74664a42807SRyan Case 
747c4f52879SKarthikeyan Ramasubramanian out_write_wakeup:
74864a42807SRyan Case 	if (!port->tx_remaining) {
7499e06d55fSRyan Case 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
75064a42807SRyan Case 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
7519e06d55fSRyan Case 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
75264a42807SRyan Case 					uport->membase + SE_GENI_M_IRQ_EN);
75364a42807SRyan Case 	}
75464a42807SRyan Case 
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 {
761*385298abSRyan Case 	u32 m_irq_en;
762*385298abSRyan Case 	u32 m_irq_status;
763*385298abSRyan Case 	u32 s_irq_status;
764*385298abSRyan Case 	u32 geni_status;
765c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = dev;
766c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
767c4f52879SKarthikeyan Ramasubramanian 	bool drop_rx = false;
768c4f52879SKarthikeyan Ramasubramanian 	struct tty_port *tport = &uport->state->port;
769c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
770c4f52879SKarthikeyan Ramasubramanian 
771c4f52879SKarthikeyan Ramasubramanian 	if (uport->suspended)
772ec91df8dSKarthikeyan Ramasubramanian 		return IRQ_NONE;
773c4f52879SKarthikeyan Ramasubramanian 
774c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
7759e06d55fSRyan Case 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
7769e06d55fSRyan Case 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
7779e06d55fSRyan Case 	geni_status = readl(uport->membase + SE_GENI_STATUS);
7789e06d55fSRyan Case 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
7799e06d55fSRyan Case 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
7809e06d55fSRyan Case 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
781c4f52879SKarthikeyan Ramasubramanian 
782c4f52879SKarthikeyan Ramasubramanian 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
783c4f52879SKarthikeyan Ramasubramanian 		goto out_unlock;
784c4f52879SKarthikeyan Ramasubramanian 
785c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
786c4f52879SKarthikeyan Ramasubramanian 		uport->icount.overrun++;
787c4f52879SKarthikeyan Ramasubramanian 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
788c4f52879SKarthikeyan Ramasubramanian 	}
789c4f52879SKarthikeyan Ramasubramanian 
79064a42807SRyan Case 	if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
791a1fee899SRyan Case 		qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
792a1fee899SRyan Case 					geni_status & M_GENI_CMD_ACTIVE);
793c4f52879SKarthikeyan Ramasubramanian 
794c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
795c4f52879SKarthikeyan Ramasubramanian 		if (s_irq_status & S_GP_IRQ_0_EN)
796c4f52879SKarthikeyan Ramasubramanian 			uport->icount.parity++;
797c4f52879SKarthikeyan Ramasubramanian 		drop_rx = true;
798c4f52879SKarthikeyan Ramasubramanian 	} else if (s_irq_status & S_GP_IRQ_2_EN ||
799c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_GP_IRQ_3_EN) {
800c4f52879SKarthikeyan Ramasubramanian 		uport->icount.brk++;
801c4f52879SKarthikeyan Ramasubramanian 		port->brk = true;
802c4f52879SKarthikeyan Ramasubramanian 	}
803c4f52879SKarthikeyan Ramasubramanian 
804c4f52879SKarthikeyan Ramasubramanian 	if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
805c4f52879SKarthikeyan Ramasubramanian 					s_irq_status & S_RX_FIFO_LAST_EN)
806c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_handle_rx(uport, drop_rx);
807c4f52879SKarthikeyan Ramasubramanian 
808c4f52879SKarthikeyan Ramasubramanian out_unlock:
809336447b3SDouglas Anderson 	uart_unlock_and_check_sysrq(uport, flags);
810336447b3SDouglas Anderson 
811c4f52879SKarthikeyan Ramasubramanian 	return IRQ_HANDLED;
812c4f52879SKarthikeyan Ramasubramanian }
813c4f52879SKarthikeyan Ramasubramanian 
8146a10635eSKarthikeyan Ramasubramanian static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
815c4f52879SKarthikeyan Ramasubramanian {
816c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
817c4f52879SKarthikeyan Ramasubramanian 
818c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
819c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
820c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
821c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
822c4f52879SKarthikeyan Ramasubramanian 	uport->fifosize =
823c4f52879SKarthikeyan Ramasubramanian 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
824c4f52879SKarthikeyan Ramasubramanian }
825c4f52879SKarthikeyan Ramasubramanian 
826c4f52879SKarthikeyan Ramasubramanian 
827c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_shutdown(struct uart_port *uport)
828c4f52879SKarthikeyan Ramasubramanian {
829c4f52879SKarthikeyan Ramasubramanian 	unsigned long flags;
830c4f52879SKarthikeyan Ramasubramanian 
831c4f52879SKarthikeyan Ramasubramanian 	/* Stop the console before stopping the current tx */
8328a8a66a1SGirish Mahadevan 	if (uart_console(uport))
833c4f52879SKarthikeyan Ramasubramanian 		console_stop(uport->cons);
834c4f52879SKarthikeyan Ramasubramanian 
835c4f52879SKarthikeyan Ramasubramanian 	free_irq(uport->irq, uport);
836c4f52879SKarthikeyan Ramasubramanian 	spin_lock_irqsave(&uport->lock, flags);
837c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_tx(uport);
838c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
839c4f52879SKarthikeyan Ramasubramanian 	spin_unlock_irqrestore(&uport->lock, flags);
840c4f52879SKarthikeyan Ramasubramanian }
841c4f52879SKarthikeyan Ramasubramanian 
842c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_port_setup(struct uart_port *uport)
843c4f52879SKarthikeyan Ramasubramanian {
844c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
845*385298abSRyan Case 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
846c362272bSDouglas Anderson 	u32 proto;
847c362272bSDouglas Anderson 
8489f641df4SDouglas Anderson 	if (uart_console(uport)) {
849c362272bSDouglas Anderson 		port->tx_bytes_pw = 1;
8509f641df4SDouglas Anderson 		port->rx_bytes_pw = CONSOLE_RX_BYTES_PW;
8519f641df4SDouglas Anderson 	} else {
852c362272bSDouglas Anderson 		port->tx_bytes_pw = 4;
8539f641df4SDouglas Anderson 		port->rx_bytes_pw = 4;
8549f641df4SDouglas Anderson 	}
855c362272bSDouglas Anderson 
856c362272bSDouglas Anderson 	proto = geni_se_read_proto(&port->se);
857c362272bSDouglas Anderson 	if (proto != GENI_SE_UART) {
858c362272bSDouglas Anderson 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
859c362272bSDouglas Anderson 		return -ENXIO;
860c362272bSDouglas Anderson 	}
861c362272bSDouglas Anderson 
862c362272bSDouglas Anderson 	qcom_geni_serial_stop_rx(uport);
863c362272bSDouglas Anderson 
864c362272bSDouglas Anderson 	get_tx_fifo_size(port);
865c4f52879SKarthikeyan Ramasubramanian 
8669e06d55fSRyan Case 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
867c4f52879SKarthikeyan Ramasubramanian 	/*
868c4f52879SKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
869c4f52879SKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
870c4f52879SKarthikeyan Ramasubramanian 	 */
8718a8a66a1SGirish Mahadevan 	if (uart_console(uport))
872c4f52879SKarthikeyan Ramasubramanian 		qcom_geni_serial_poll_tx_done(uport);
873c4f52879SKarthikeyan Ramasubramanian 	geni_se_config_packing(&port->se, BITS_PER_BYTE, port->tx_bytes_pw,
874c4f52879SKarthikeyan Ramasubramanian 						false, true, false);
875c4f52879SKarthikeyan Ramasubramanian 	geni_se_config_packing(&port->se, BITS_PER_BYTE, port->rx_bytes_pw,
876c4f52879SKarthikeyan Ramasubramanian 						false, false, true);
877a85fb9ceSRyan Case 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
878bdc05a8aSRyan Case 	geni_se_select_mode(&port->se, GENI_SE_FIFO);
8798a8a66a1SGirish Mahadevan 	if (!uart_console(uport)) {
880329e0989SKees Cook 		port->rx_fifo = devm_kcalloc(uport->dev,
881329e0989SKees Cook 			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
8828a8a66a1SGirish Mahadevan 		if (!port->rx_fifo)
8838a8a66a1SGirish Mahadevan 			return -ENOMEM;
8848a8a66a1SGirish Mahadevan 	}
885c4f52879SKarthikeyan Ramasubramanian 	port->setup = true;
886c362272bSDouglas Anderson 
887c4f52879SKarthikeyan Ramasubramanian 	return 0;
888c4f52879SKarthikeyan Ramasubramanian }
889c4f52879SKarthikeyan Ramasubramanian 
890c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_startup(struct uart_port *uport)
891c4f52879SKarthikeyan Ramasubramanian {
892c4f52879SKarthikeyan Ramasubramanian 	int ret;
893c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
894c4f52879SKarthikeyan Ramasubramanian 
895c4f52879SKarthikeyan Ramasubramanian 	scnprintf(port->name, sizeof(port->name),
8968a8a66a1SGirish Mahadevan 		  "qcom_serial_%s%d",
8978a8a66a1SGirish Mahadevan 		(uart_console(uport) ? "console" : "uart"), uport->line);
898c4f52879SKarthikeyan Ramasubramanian 
899c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
900c4f52879SKarthikeyan Ramasubramanian 		ret = qcom_geni_serial_port_setup(uport);
901c4f52879SKarthikeyan Ramasubramanian 		if (ret)
902c4f52879SKarthikeyan Ramasubramanian 			return ret;
903c4f52879SKarthikeyan Ramasubramanian 	}
904c4f52879SKarthikeyan Ramasubramanian 
905c4f52879SKarthikeyan Ramasubramanian 	ret = request_irq(uport->irq, qcom_geni_serial_isr, IRQF_TRIGGER_HIGH,
906c4f52879SKarthikeyan Ramasubramanian 							port->name, uport);
907c4f52879SKarthikeyan Ramasubramanian 	if (ret)
908c4f52879SKarthikeyan Ramasubramanian 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
909c4f52879SKarthikeyan Ramasubramanian 	return ret;
910c4f52879SKarthikeyan Ramasubramanian }
911c4f52879SKarthikeyan Ramasubramanian 
912c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_cfg(unsigned long clk_freq)
913c4f52879SKarthikeyan Ramasubramanian {
914c4f52879SKarthikeyan Ramasubramanian 	int i;
915c4f52879SKarthikeyan Ramasubramanian 
916c4f52879SKarthikeyan Ramasubramanian 	for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
917c4f52879SKarthikeyan Ramasubramanian 		if (!(root_freq[i] % clk_freq))
918c4f52879SKarthikeyan Ramasubramanian 			return root_freq[i];
919c4f52879SKarthikeyan Ramasubramanian 	}
920c4f52879SKarthikeyan Ramasubramanian 	return 0;
921c4f52879SKarthikeyan Ramasubramanian }
922c4f52879SKarthikeyan Ramasubramanian 
923c4f52879SKarthikeyan Ramasubramanian static unsigned long get_clk_div_rate(unsigned int baud, unsigned int *clk_div)
924c4f52879SKarthikeyan Ramasubramanian {
925c4f52879SKarthikeyan Ramasubramanian 	unsigned long ser_clk;
926c4f52879SKarthikeyan Ramasubramanian 	unsigned long desired_clk;
927c4f52879SKarthikeyan Ramasubramanian 
928c4f52879SKarthikeyan Ramasubramanian 	desired_clk = baud * UART_OVERSAMPLING;
929c4f52879SKarthikeyan Ramasubramanian 	ser_clk = get_clk_cfg(desired_clk);
930c4f52879SKarthikeyan Ramasubramanian 	if (!ser_clk) {
931c4f52879SKarthikeyan Ramasubramanian 		pr_err("%s: Can't find matching DFS entry for baud %d\n",
932c4f52879SKarthikeyan Ramasubramanian 								__func__, baud);
933c4f52879SKarthikeyan Ramasubramanian 		return ser_clk;
934c4f52879SKarthikeyan Ramasubramanian 	}
935c4f52879SKarthikeyan Ramasubramanian 
936c4f52879SKarthikeyan Ramasubramanian 	*clk_div = ser_clk / desired_clk;
937c4f52879SKarthikeyan Ramasubramanian 	return ser_clk;
938c4f52879SKarthikeyan Ramasubramanian }
939c4f52879SKarthikeyan Ramasubramanian 
940c4f52879SKarthikeyan Ramasubramanian static void qcom_geni_serial_set_termios(struct uart_port *uport,
941c4f52879SKarthikeyan Ramasubramanian 				struct ktermios *termios, struct ktermios *old)
942c4f52879SKarthikeyan Ramasubramanian {
943c4f52879SKarthikeyan Ramasubramanian 	unsigned int baud;
944*385298abSRyan Case 	u32 bits_per_char;
945*385298abSRyan Case 	u32 tx_trans_cfg;
946*385298abSRyan Case 	u32 tx_parity_cfg;
947*385298abSRyan Case 	u32 rx_trans_cfg;
948*385298abSRyan Case 	u32 rx_parity_cfg;
949*385298abSRyan Case 	u32 stop_bit_len;
950c4f52879SKarthikeyan Ramasubramanian 	unsigned int clk_div;
951*385298abSRyan Case 	u32 ser_clk_cfg;
952c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
953c4f52879SKarthikeyan Ramasubramanian 	unsigned long clk_rate;
954c4f52879SKarthikeyan Ramasubramanian 
955c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_stop_rx(uport);
956c4f52879SKarthikeyan Ramasubramanian 	/* baud rate */
957c4f52879SKarthikeyan Ramasubramanian 	baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
958c4f52879SKarthikeyan Ramasubramanian 	port->baud = baud;
959c4f52879SKarthikeyan Ramasubramanian 	clk_rate = get_clk_div_rate(baud, &clk_div);
960c4f52879SKarthikeyan Ramasubramanian 	if (!clk_rate)
961c4f52879SKarthikeyan Ramasubramanian 		goto out_restart_rx;
962c4f52879SKarthikeyan Ramasubramanian 
963c4f52879SKarthikeyan Ramasubramanian 	uport->uartclk = clk_rate;
964c4f52879SKarthikeyan Ramasubramanian 	clk_set_rate(port->se.clk, clk_rate);
965c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg = SER_CLK_EN;
966c4f52879SKarthikeyan Ramasubramanian 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
967c4f52879SKarthikeyan Ramasubramanian 
968c4f52879SKarthikeyan Ramasubramanian 	/* parity */
9699e06d55fSRyan Case 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
9709e06d55fSRyan Case 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
9719e06d55fSRyan Case 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
9729e06d55fSRyan Case 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
973c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & PARENB) {
974c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_TX_PAR_EN;
975c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg |= UART_RX_PAR_EN;
976c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg |= PAR_CALC_EN;
977c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg |= PAR_CALC_EN;
978c4f52879SKarthikeyan Ramasubramanian 		if (termios->c_cflag & PARODD) {
979c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_ODD;
980c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_ODD;
981c4f52879SKarthikeyan Ramasubramanian 		} else if (termios->c_cflag & CMSPAR) {
982c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_SPACE;
983c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_SPACE;
984c4f52879SKarthikeyan Ramasubramanian 		} else {
985c4f52879SKarthikeyan Ramasubramanian 			tx_parity_cfg |= PAR_EVEN;
986c4f52879SKarthikeyan Ramasubramanian 			rx_parity_cfg |= PAR_EVEN;
987c4f52879SKarthikeyan Ramasubramanian 		}
988c4f52879SKarthikeyan Ramasubramanian 	} else {
989c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_TX_PAR_EN;
990c4f52879SKarthikeyan Ramasubramanian 		rx_trans_cfg &= ~UART_RX_PAR_EN;
991c4f52879SKarthikeyan Ramasubramanian 		tx_parity_cfg &= ~PAR_CALC_EN;
992c4f52879SKarthikeyan Ramasubramanian 		rx_parity_cfg &= ~PAR_CALC_EN;
993c4f52879SKarthikeyan Ramasubramanian 	}
994c4f52879SKarthikeyan Ramasubramanian 
995c4f52879SKarthikeyan Ramasubramanian 	/* bits per char */
996c4f52879SKarthikeyan Ramasubramanian 	switch (termios->c_cflag & CSIZE) {
997c4f52879SKarthikeyan Ramasubramanian 	case CS5:
998c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 5;
999c4f52879SKarthikeyan Ramasubramanian 		break;
1000c4f52879SKarthikeyan Ramasubramanian 	case CS6:
1001c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 6;
1002c4f52879SKarthikeyan Ramasubramanian 		break;
1003c4f52879SKarthikeyan Ramasubramanian 	case CS7:
1004c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 7;
1005c4f52879SKarthikeyan Ramasubramanian 		break;
1006c4f52879SKarthikeyan Ramasubramanian 	case CS8:
1007c4f52879SKarthikeyan Ramasubramanian 	default:
1008c4f52879SKarthikeyan Ramasubramanian 		bits_per_char = 8;
1009c4f52879SKarthikeyan Ramasubramanian 		break;
1010c4f52879SKarthikeyan Ramasubramanian 	}
1011c4f52879SKarthikeyan Ramasubramanian 
1012c4f52879SKarthikeyan Ramasubramanian 	/* stop bits */
1013c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CSTOPB)
1014c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_2;
1015c4f52879SKarthikeyan Ramasubramanian 	else
1016c4f52879SKarthikeyan Ramasubramanian 		stop_bit_len = TX_STOP_BIT_LEN_1;
1017c4f52879SKarthikeyan Ramasubramanian 
1018c4f52879SKarthikeyan Ramasubramanian 	/* flow control, clear the CTS_MASK bit if using flow control. */
1019c4f52879SKarthikeyan Ramasubramanian 	if (termios->c_cflag & CRTSCTS)
1020c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg &= ~UART_CTS_MASK;
1021c4f52879SKarthikeyan Ramasubramanian 	else
1022c4f52879SKarthikeyan Ramasubramanian 		tx_trans_cfg |= UART_CTS_MASK;
1023c4f52879SKarthikeyan Ramasubramanian 
1024c4f52879SKarthikeyan Ramasubramanian 	if (baud)
1025c4f52879SKarthikeyan Ramasubramanian 		uart_update_timeout(uport, termios->c_cflag, baud);
1026c4f52879SKarthikeyan Ramasubramanian 
10278a8a66a1SGirish Mahadevan 	if (!uart_console(uport))
10289e06d55fSRyan Case 		writel(port->loopback,
10298a8a66a1SGirish Mahadevan 				uport->membase + SE_UART_LOOPBACK_CFG);
10309e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
10319e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
10329e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
10339e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
10349e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
10359e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
10369e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
10379e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
10389e06d55fSRyan Case 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1039c4f52879SKarthikeyan Ramasubramanian out_restart_rx:
1040c4f52879SKarthikeyan Ramasubramanian 	qcom_geni_serial_start_rx(uport);
1041c4f52879SKarthikeyan Ramasubramanian }
1042c4f52879SKarthikeyan Ramasubramanian 
1043c4f52879SKarthikeyan Ramasubramanian static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1044c4f52879SKarthikeyan Ramasubramanian {
10457fb5b880SKarthikeyan Ramasubramanian 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1046c4f52879SKarthikeyan Ramasubramanian }
1047c4f52879SKarthikeyan Ramasubramanian 
1048c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1049c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_console_setup(struct console *co, char *options)
1050c4f52879SKarthikeyan Ramasubramanian {
1051c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1052c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1053c4f52879SKarthikeyan Ramasubramanian 	int baud;
1054c4f52879SKarthikeyan Ramasubramanian 	int bits = 8;
1055c4f52879SKarthikeyan Ramasubramanian 	int parity = 'n';
1056c4f52879SKarthikeyan Ramasubramanian 	int flow = 'n';
1057c362272bSDouglas Anderson 	int ret;
1058c4f52879SKarthikeyan Ramasubramanian 
1059c4f52879SKarthikeyan Ramasubramanian 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1060c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1061c4f52879SKarthikeyan Ramasubramanian 
10628a8a66a1SGirish Mahadevan 	port = get_port_from_line(co->index, true);
1063c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
10646a10635eSKarthikeyan Ramasubramanian 		pr_err("Invalid line %d\n", co->index);
1065c4f52879SKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1066c4f52879SKarthikeyan Ramasubramanian 	}
1067c4f52879SKarthikeyan Ramasubramanian 
1068c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1069c4f52879SKarthikeyan Ramasubramanian 
1070c4f52879SKarthikeyan Ramasubramanian 	if (unlikely(!uport->membase))
1071c4f52879SKarthikeyan Ramasubramanian 		return -ENXIO;
1072c4f52879SKarthikeyan Ramasubramanian 
1073c4f52879SKarthikeyan Ramasubramanian 	if (!port->setup) {
1074c362272bSDouglas Anderson 		ret = qcom_geni_serial_port_setup(uport);
1075c362272bSDouglas Anderson 		if (ret)
1076c362272bSDouglas Anderson 			return ret;
1077c4f52879SKarthikeyan Ramasubramanian 	}
1078c4f52879SKarthikeyan Ramasubramanian 
1079c4f52879SKarthikeyan Ramasubramanian 	if (options)
1080c4f52879SKarthikeyan Ramasubramanian 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1081c4f52879SKarthikeyan Ramasubramanian 
1082c4f52879SKarthikeyan Ramasubramanian 	return uart_set_options(uport, co, baud, parity, bits, flow);
1083c4f52879SKarthikeyan Ramasubramanian }
1084c4f52879SKarthikeyan Ramasubramanian 
108543f1831bSKarthikeyan Ramasubramanian static void qcom_geni_serial_earlycon_write(struct console *con,
108643f1831bSKarthikeyan Ramasubramanian 					const char *s, unsigned int n)
108743f1831bSKarthikeyan Ramasubramanian {
108843f1831bSKarthikeyan Ramasubramanian 	struct earlycon_device *dev = con->data;
108943f1831bSKarthikeyan Ramasubramanian 
109043f1831bSKarthikeyan Ramasubramanian 	__qcom_geni_serial_console_write(&dev->port, s, n);
109143f1831bSKarthikeyan Ramasubramanian }
109243f1831bSKarthikeyan Ramasubramanian 
109343f1831bSKarthikeyan Ramasubramanian static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
109443f1831bSKarthikeyan Ramasubramanian 								const char *opt)
109543f1831bSKarthikeyan Ramasubramanian {
109643f1831bSKarthikeyan Ramasubramanian 	struct uart_port *uport = &dev->port;
109743f1831bSKarthikeyan Ramasubramanian 	u32 tx_trans_cfg;
109843f1831bSKarthikeyan Ramasubramanian 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
109943f1831bSKarthikeyan Ramasubramanian 	u32 rx_trans_cfg = 0;
110043f1831bSKarthikeyan Ramasubramanian 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
110143f1831bSKarthikeyan Ramasubramanian 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
110243f1831bSKarthikeyan Ramasubramanian 	u32 bits_per_char;
110343f1831bSKarthikeyan Ramasubramanian 	struct geni_se se;
110443f1831bSKarthikeyan Ramasubramanian 
110543f1831bSKarthikeyan Ramasubramanian 	if (!uport->membase)
110643f1831bSKarthikeyan Ramasubramanian 		return -EINVAL;
110743f1831bSKarthikeyan Ramasubramanian 
110843f1831bSKarthikeyan Ramasubramanian 	memset(&se, 0, sizeof(se));
110943f1831bSKarthikeyan Ramasubramanian 	se.base = uport->membase;
111043f1831bSKarthikeyan Ramasubramanian 	if (geni_se_read_proto(&se) != GENI_SE_UART)
111143f1831bSKarthikeyan Ramasubramanian 		return -ENXIO;
111243f1831bSKarthikeyan Ramasubramanian 	/*
111343f1831bSKarthikeyan Ramasubramanian 	 * Ignore Flow control.
111443f1831bSKarthikeyan Ramasubramanian 	 * n = 8.
111543f1831bSKarthikeyan Ramasubramanian 	 */
111643f1831bSKarthikeyan Ramasubramanian 	tx_trans_cfg = UART_CTS_MASK;
111743f1831bSKarthikeyan Ramasubramanian 	bits_per_char = BITS_PER_BYTE;
111843f1831bSKarthikeyan Ramasubramanian 
111943f1831bSKarthikeyan Ramasubramanian 	/*
112043f1831bSKarthikeyan Ramasubramanian 	 * Make an unconditional cancel on the main sequencer to reset
112143f1831bSKarthikeyan Ramasubramanian 	 * it else we could end up in data loss scenarios.
112243f1831bSKarthikeyan Ramasubramanian 	 */
112343f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_poll_tx_done(uport);
112443f1831bSKarthikeyan Ramasubramanian 	qcom_geni_serial_abort_rx(uport);
112543f1831bSKarthikeyan Ramasubramanian 	geni_se_config_packing(&se, BITS_PER_BYTE, 1, false, true, false);
112643f1831bSKarthikeyan Ramasubramanian 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
112743f1831bSKarthikeyan Ramasubramanian 	geni_se_select_mode(&se, GENI_SE_FIFO);
112843f1831bSKarthikeyan Ramasubramanian 
11299e06d55fSRyan Case 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
11309e06d55fSRyan Case 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
11319e06d55fSRyan Case 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
11329e06d55fSRyan Case 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
11339e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
11349e06d55fSRyan Case 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
11359e06d55fSRyan Case 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
113643f1831bSKarthikeyan Ramasubramanian 
113743f1831bSKarthikeyan Ramasubramanian 	dev->con->write = qcom_geni_serial_earlycon_write;
113843f1831bSKarthikeyan Ramasubramanian 	dev->con->setup = NULL;
113943f1831bSKarthikeyan Ramasubramanian 	return 0;
114043f1831bSKarthikeyan Ramasubramanian }
114143f1831bSKarthikeyan Ramasubramanian OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
114243f1831bSKarthikeyan Ramasubramanian 				qcom_geni_serial_earlycon_setup);
114343f1831bSKarthikeyan Ramasubramanian 
1144c4f52879SKarthikeyan Ramasubramanian static int __init console_register(struct uart_driver *drv)
1145c4f52879SKarthikeyan Ramasubramanian {
1146c4f52879SKarthikeyan Ramasubramanian 	return uart_register_driver(drv);
1147c4f52879SKarthikeyan Ramasubramanian }
1148c4f52879SKarthikeyan Ramasubramanian 
1149c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1150c4f52879SKarthikeyan Ramasubramanian {
1151c4f52879SKarthikeyan Ramasubramanian 	uart_unregister_driver(drv);
1152c4f52879SKarthikeyan Ramasubramanian }
1153c4f52879SKarthikeyan Ramasubramanian 
1154c4f52879SKarthikeyan Ramasubramanian static struct console cons_ops = {
1155c4f52879SKarthikeyan Ramasubramanian 	.name = "ttyMSM",
1156c4f52879SKarthikeyan Ramasubramanian 	.write = qcom_geni_serial_console_write,
1157c4f52879SKarthikeyan Ramasubramanian 	.device = uart_console_device,
1158c4f52879SKarthikeyan Ramasubramanian 	.setup = qcom_geni_console_setup,
1159c4f52879SKarthikeyan Ramasubramanian 	.flags = CON_PRINTBUFFER,
1160c4f52879SKarthikeyan Ramasubramanian 	.index = -1,
1161c4f52879SKarthikeyan Ramasubramanian 	.data = &qcom_geni_console_driver,
1162c4f52879SKarthikeyan Ramasubramanian };
1163c4f52879SKarthikeyan Ramasubramanian 
1164c4f52879SKarthikeyan Ramasubramanian static struct uart_driver qcom_geni_console_driver = {
1165c4f52879SKarthikeyan Ramasubramanian 	.owner = THIS_MODULE,
1166c4f52879SKarthikeyan Ramasubramanian 	.driver_name = "qcom_geni_console",
1167c4f52879SKarthikeyan Ramasubramanian 	.dev_name = "ttyMSM",
1168c4f52879SKarthikeyan Ramasubramanian 	.nr =  GENI_UART_CONS_PORTS,
1169c4f52879SKarthikeyan Ramasubramanian 	.cons = &cons_ops,
1170c4f52879SKarthikeyan Ramasubramanian };
1171c4f52879SKarthikeyan Ramasubramanian #else
1172c4f52879SKarthikeyan Ramasubramanian static int console_register(struct uart_driver *drv)
1173c4f52879SKarthikeyan Ramasubramanian {
1174c4f52879SKarthikeyan Ramasubramanian 	return 0;
1175c4f52879SKarthikeyan Ramasubramanian }
1176c4f52879SKarthikeyan Ramasubramanian 
1177c4f52879SKarthikeyan Ramasubramanian static void console_unregister(struct uart_driver *drv)
1178c4f52879SKarthikeyan Ramasubramanian {
1179c4f52879SKarthikeyan Ramasubramanian }
1180c4f52879SKarthikeyan Ramasubramanian #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1181c4f52879SKarthikeyan Ramasubramanian 
11828a8a66a1SGirish Mahadevan static struct uart_driver qcom_geni_uart_driver = {
11838a8a66a1SGirish Mahadevan 	.owner = THIS_MODULE,
11848a8a66a1SGirish Mahadevan 	.driver_name = "qcom_geni_uart",
11858a8a66a1SGirish Mahadevan 	.dev_name = "ttyHS",
11868a8a66a1SGirish Mahadevan 	.nr =  GENI_UART_PORTS,
11878a8a66a1SGirish Mahadevan };
11888a8a66a1SGirish Mahadevan 
11898a8a66a1SGirish Mahadevan static void qcom_geni_serial_pm(struct uart_port *uport,
1190c4f52879SKarthikeyan Ramasubramanian 		unsigned int new_state, unsigned int old_state)
1191c4f52879SKarthikeyan Ramasubramanian {
1192c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1193c4f52879SKarthikeyan Ramasubramanian 
1194c362272bSDouglas Anderson 	/* If we've never been called, treat it as off */
1195c362272bSDouglas Anderson 	if (old_state == UART_PM_STATE_UNDEFINED)
1196c362272bSDouglas Anderson 		old_state = UART_PM_STATE_OFF;
1197c362272bSDouglas Anderson 
1198c4f52879SKarthikeyan Ramasubramanian 	if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
1199c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_on(&port->se);
1200c4f52879SKarthikeyan Ramasubramanian 	else if (new_state == UART_PM_STATE_OFF &&
1201c4f52879SKarthikeyan Ramasubramanian 			old_state == UART_PM_STATE_ON)
1202c4f52879SKarthikeyan Ramasubramanian 		geni_se_resources_off(&port->se);
1203c4f52879SKarthikeyan Ramasubramanian }
1204c4f52879SKarthikeyan Ramasubramanian 
1205c4f52879SKarthikeyan Ramasubramanian static const struct uart_ops qcom_geni_console_pops = {
1206c4f52879SKarthikeyan Ramasubramanian 	.tx_empty = qcom_geni_serial_tx_empty,
1207c4f52879SKarthikeyan Ramasubramanian 	.stop_tx = qcom_geni_serial_stop_tx,
1208c4f52879SKarthikeyan Ramasubramanian 	.start_tx = qcom_geni_serial_start_tx,
1209c4f52879SKarthikeyan Ramasubramanian 	.stop_rx = qcom_geni_serial_stop_rx,
1210c4f52879SKarthikeyan Ramasubramanian 	.set_termios = qcom_geni_serial_set_termios,
1211c4f52879SKarthikeyan Ramasubramanian 	.startup = qcom_geni_serial_startup,
1212c4f52879SKarthikeyan Ramasubramanian 	.request_port = qcom_geni_serial_request_port,
1213c4f52879SKarthikeyan Ramasubramanian 	.config_port = qcom_geni_serial_config_port,
1214c4f52879SKarthikeyan Ramasubramanian 	.shutdown = qcom_geni_serial_shutdown,
1215c4f52879SKarthikeyan Ramasubramanian 	.type = qcom_geni_serial_get_type,
12168a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
12178a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
1218c4f52879SKarthikeyan Ramasubramanian #ifdef CONFIG_CONSOLE_POLL
1219c4f52879SKarthikeyan Ramasubramanian 	.poll_get_char	= qcom_geni_serial_get_char,
1220c4f52879SKarthikeyan Ramasubramanian 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1221c4f52879SKarthikeyan Ramasubramanian #endif
12228a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
12238a8a66a1SGirish Mahadevan };
12248a8a66a1SGirish Mahadevan 
12258a8a66a1SGirish Mahadevan static const struct uart_ops qcom_geni_uart_pops = {
12268a8a66a1SGirish Mahadevan 	.tx_empty = qcom_geni_serial_tx_empty,
12278a8a66a1SGirish Mahadevan 	.stop_tx = qcom_geni_serial_stop_tx,
12288a8a66a1SGirish Mahadevan 	.start_tx = qcom_geni_serial_start_tx,
12298a8a66a1SGirish Mahadevan 	.stop_rx = qcom_geni_serial_stop_rx,
12308a8a66a1SGirish Mahadevan 	.set_termios = qcom_geni_serial_set_termios,
12318a8a66a1SGirish Mahadevan 	.startup = qcom_geni_serial_startup,
12328a8a66a1SGirish Mahadevan 	.request_port = qcom_geni_serial_request_port,
12338a8a66a1SGirish Mahadevan 	.config_port = qcom_geni_serial_config_port,
12348a8a66a1SGirish Mahadevan 	.shutdown = qcom_geni_serial_shutdown,
12358a8a66a1SGirish Mahadevan 	.type = qcom_geni_serial_get_type,
12368a8a66a1SGirish Mahadevan 	.set_mctrl = qcom_geni_serial_set_mctrl,
12378a8a66a1SGirish Mahadevan 	.get_mctrl = qcom_geni_serial_get_mctrl,
12388a8a66a1SGirish Mahadevan 	.pm = qcom_geni_serial_pm,
1239c4f52879SKarthikeyan Ramasubramanian };
1240c4f52879SKarthikeyan Ramasubramanian 
1241c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_probe(struct platform_device *pdev)
1242c4f52879SKarthikeyan Ramasubramanian {
1243c4f52879SKarthikeyan Ramasubramanian 	int ret = 0;
1244c4f52879SKarthikeyan Ramasubramanian 	int line = -1;
1245c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port;
1246c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport;
1247c4f52879SKarthikeyan Ramasubramanian 	struct resource *res;
1248066cd1c4SKarthikeyan Ramasubramanian 	int irq;
12498a8a66a1SGirish Mahadevan 	bool console = false;
12508a8a66a1SGirish Mahadevan 	struct uart_driver *drv;
1251c4f52879SKarthikeyan Ramasubramanian 
12528a8a66a1SGirish Mahadevan 	if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
12538a8a66a1SGirish Mahadevan 		console = true;
12548a8a66a1SGirish Mahadevan 
12558a8a66a1SGirish Mahadevan 	if (console) {
12568a8a66a1SGirish Mahadevan 		drv = &qcom_geni_console_driver;
1257c4f52879SKarthikeyan Ramasubramanian 		line = of_alias_get_id(pdev->dev.of_node, "serial");
12588a8a66a1SGirish Mahadevan 	} else {
12598a8a66a1SGirish Mahadevan 		drv = &qcom_geni_uart_driver;
12608a8a66a1SGirish Mahadevan 		line = of_alias_get_id(pdev->dev.of_node, "hsuart");
12618a8a66a1SGirish Mahadevan 	}
1262c4f52879SKarthikeyan Ramasubramanian 
12638a8a66a1SGirish Mahadevan 	port = get_port_from_line(line, console);
1264c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port)) {
12656a10635eSKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Invalid line %d\n", line);
12666a10635eSKarthikeyan Ramasubramanian 		return PTR_ERR(port);
1267c4f52879SKarthikeyan Ramasubramanian 	}
1268c4f52879SKarthikeyan Ramasubramanian 
1269c4f52879SKarthikeyan Ramasubramanian 	uport = &port->uport;
1270c4f52879SKarthikeyan Ramasubramanian 	/* Don't allow 2 drivers to access the same port */
1271c4f52879SKarthikeyan Ramasubramanian 	if (uport->private_data)
1272c4f52879SKarthikeyan Ramasubramanian 		return -ENODEV;
1273c4f52879SKarthikeyan Ramasubramanian 
1274c4f52879SKarthikeyan Ramasubramanian 	uport->dev = &pdev->dev;
1275c4f52879SKarthikeyan Ramasubramanian 	port->se.dev = &pdev->dev;
1276c4f52879SKarthikeyan Ramasubramanian 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1277c4f52879SKarthikeyan Ramasubramanian 	port->se.clk = devm_clk_get(&pdev->dev, "se");
1278c4f52879SKarthikeyan Ramasubramanian 	if (IS_ERR(port->se.clk)) {
1279c4f52879SKarthikeyan Ramasubramanian 		ret = PTR_ERR(port->se.clk);
1280c4f52879SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1281c4f52879SKarthikeyan Ramasubramanian 		return ret;
1282c4f52879SKarthikeyan Ramasubramanian 	}
1283c4f52879SKarthikeyan Ramasubramanian 
1284c4f52879SKarthikeyan Ramasubramanian 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
12857693b331SWei Yongjun 	if (!res)
12867693b331SWei Yongjun 		return -EINVAL;
1287c4f52879SKarthikeyan Ramasubramanian 	uport->mapbase = res->start;
1288c4f52879SKarthikeyan Ramasubramanian 
1289c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1290c4f52879SKarthikeyan Ramasubramanian 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1291c4f52879SKarthikeyan Ramasubramanian 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1292c4f52879SKarthikeyan Ramasubramanian 
1293066cd1c4SKarthikeyan Ramasubramanian 	irq = platform_get_irq(pdev, 0);
1294066cd1c4SKarthikeyan Ramasubramanian 	if (irq < 0) {
1295066cd1c4SKarthikeyan Ramasubramanian 		dev_err(&pdev->dev, "Failed to get IRQ %d\n", irq);
1296066cd1c4SKarthikeyan Ramasubramanian 		return irq;
1297c4f52879SKarthikeyan Ramasubramanian 	}
1298066cd1c4SKarthikeyan Ramasubramanian 	uport->irq = irq;
1299c4f52879SKarthikeyan Ramasubramanian 
13008a8a66a1SGirish Mahadevan 	uport->private_data = drv;
1301c4f52879SKarthikeyan Ramasubramanian 	platform_set_drvdata(pdev, port);
13028a8a66a1SGirish Mahadevan 	port->handle_rx = console ? handle_rx_console : handle_rx_uart;
13038a8a66a1SGirish Mahadevan 	if (!console)
13048a8a66a1SGirish Mahadevan 		device_create_file(uport->dev, &dev_attr_loopback);
13058a8a66a1SGirish Mahadevan 	return uart_add_one_port(drv, uport);
1306c4f52879SKarthikeyan Ramasubramanian }
1307c4f52879SKarthikeyan Ramasubramanian 
1308c4f52879SKarthikeyan Ramasubramanian static int qcom_geni_serial_remove(struct platform_device *pdev)
1309c4f52879SKarthikeyan Ramasubramanian {
1310c4f52879SKarthikeyan Ramasubramanian 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1311c4f52879SKarthikeyan Ramasubramanian 	struct uart_driver *drv = port->uport.private_data;
1312c4f52879SKarthikeyan Ramasubramanian 
1313c4f52879SKarthikeyan Ramasubramanian 	uart_remove_one_port(drv, &port->uport);
1314c4f52879SKarthikeyan Ramasubramanian 	return 0;
1315c4f52879SKarthikeyan Ramasubramanian }
1316c4f52879SKarthikeyan Ramasubramanian 
1317b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1318c4f52879SKarthikeyan Ramasubramanian {
1319a406c4b8SWolfram Sang 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1320c4f52879SKarthikeyan Ramasubramanian 	struct uart_port *uport = &port->uport;
1321c4f52879SKarthikeyan Ramasubramanian 
1322b1f84dd3SMukesh Kumar Savaliya 	return uart_suspend_port(uport->private_data, uport);
13238a8a66a1SGirish Mahadevan }
13248a8a66a1SGirish Mahadevan 
1325b1f84dd3SMukesh Kumar Savaliya static int __maybe_unused qcom_geni_serial_sys_resume(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_resume_port(uport->private_data, uport);
1331c4f52879SKarthikeyan Ramasubramanian }
1332c4f52879SKarthikeyan Ramasubramanian 
1333c4f52879SKarthikeyan Ramasubramanian static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1334b1f84dd3SMukesh Kumar Savaliya 	SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1335b1f84dd3SMukesh Kumar Savaliya 					qcom_geni_serial_sys_resume)
1336c4f52879SKarthikeyan Ramasubramanian };
1337c4f52879SKarthikeyan Ramasubramanian 
1338c4f52879SKarthikeyan Ramasubramanian static const struct of_device_id qcom_geni_serial_match_table[] = {
1339c4f52879SKarthikeyan Ramasubramanian 	{ .compatible = "qcom,geni-debug-uart", },
13408a8a66a1SGirish Mahadevan 	{ .compatible = "qcom,geni-uart", },
1341c4f52879SKarthikeyan Ramasubramanian 	{}
1342c4f52879SKarthikeyan Ramasubramanian };
1343c4f52879SKarthikeyan Ramasubramanian MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1344c4f52879SKarthikeyan Ramasubramanian 
1345c4f52879SKarthikeyan Ramasubramanian static struct platform_driver qcom_geni_serial_platform_driver = {
1346c4f52879SKarthikeyan Ramasubramanian 	.remove = qcom_geni_serial_remove,
1347c4f52879SKarthikeyan Ramasubramanian 	.probe = qcom_geni_serial_probe,
1348c4f52879SKarthikeyan Ramasubramanian 	.driver = {
1349c4f52879SKarthikeyan Ramasubramanian 		.name = "qcom_geni_serial",
1350c4f52879SKarthikeyan Ramasubramanian 		.of_match_table = qcom_geni_serial_match_table,
1351c4f52879SKarthikeyan Ramasubramanian 		.pm = &qcom_geni_serial_pm_ops,
1352c4f52879SKarthikeyan Ramasubramanian 	},
1353c4f52879SKarthikeyan Ramasubramanian };
1354c4f52879SKarthikeyan Ramasubramanian 
1355c4f52879SKarthikeyan Ramasubramanian static int __init qcom_geni_serial_init(void)
1356c4f52879SKarthikeyan Ramasubramanian {
1357c4f52879SKarthikeyan Ramasubramanian 	int ret;
1358c4f52879SKarthikeyan Ramasubramanian 
1359c4f52879SKarthikeyan Ramasubramanian 	ret = console_register(&qcom_geni_console_driver);
1360c4f52879SKarthikeyan Ramasubramanian 	if (ret)
1361c4f52879SKarthikeyan Ramasubramanian 		return ret;
1362c4f52879SKarthikeyan Ramasubramanian 
13638a8a66a1SGirish Mahadevan 	ret = uart_register_driver(&qcom_geni_uart_driver);
13648a8a66a1SGirish Mahadevan 	if (ret) {
1365c4f52879SKarthikeyan Ramasubramanian 		console_unregister(&qcom_geni_console_driver);
1366c4f52879SKarthikeyan Ramasubramanian 		return ret;
1367c4f52879SKarthikeyan Ramasubramanian 	}
13688a8a66a1SGirish Mahadevan 
13698a8a66a1SGirish Mahadevan 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
13708a8a66a1SGirish Mahadevan 	if (ret) {
13718a8a66a1SGirish Mahadevan 		console_unregister(&qcom_geni_console_driver);
13728a8a66a1SGirish Mahadevan 		uart_unregister_driver(&qcom_geni_uart_driver);
13738a8a66a1SGirish Mahadevan 	}
13748a8a66a1SGirish Mahadevan 	return ret;
13758a8a66a1SGirish Mahadevan }
1376c4f52879SKarthikeyan Ramasubramanian module_init(qcom_geni_serial_init);
1377c4f52879SKarthikeyan Ramasubramanian 
1378c4f52879SKarthikeyan Ramasubramanian static void __exit qcom_geni_serial_exit(void)
1379c4f52879SKarthikeyan Ramasubramanian {
1380c4f52879SKarthikeyan Ramasubramanian 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
1381c4f52879SKarthikeyan Ramasubramanian 	console_unregister(&qcom_geni_console_driver);
13828a8a66a1SGirish Mahadevan 	uart_unregister_driver(&qcom_geni_uart_driver);
1383c4f52879SKarthikeyan Ramasubramanian }
1384c4f52879SKarthikeyan Ramasubramanian module_exit(qcom_geni_serial_exit);
1385c4f52879SKarthikeyan Ramasubramanian 
1386c4f52879SKarthikeyan Ramasubramanian MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1387c4f52879SKarthikeyan Ramasubramanian MODULE_LICENSE("GPL v2");
1388