xref: /linux/drivers/tty/serial/qcom_geni_serial.c (revision e95442c31ae65b57ddffdfa609531c549055f308)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  */
6 
7 /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
8 #define __DISABLE_TRACE_MMIO__
9 
10 #define CREATE_TRACE_POINTS
11 #include <trace/events/qcom_geni_serial.h>
12 
13 #include <linux/clk.h>
14 #include <linux/console.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/io.h>
17 #include <linux/iopoll.h>
18 #include <linux/irq.h>
19 #include <linux/irq_work.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/panic_notifier.h>
23 #include <linux/pm_opp.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm_wakeirq.h>
27 #include <linux/soc/qcom/geni-se.h>
28 #include <linux/serial.h>
29 #include <linux/serial_core.h>
30 #include <linux/slab.h>
31 #include <linux/tty.h>
32 #include <linux/tty_flip.h>
33 #include <dt-bindings/interconnect/qcom,icc.h>
34 
35 /* UART specific GENI registers */
36 #define SE_UART_LOOPBACK_CFG		0x22c
37 #define SE_UART_IO_MACRO_CTRL		0x240
38 #define SE_UART_TX_TRANS_CFG		0x25c
39 #define SE_UART_TX_WORD_LEN		0x268
40 #define SE_UART_TX_STOP_BIT_LEN		0x26c
41 #define SE_UART_TX_TRANS_LEN		0x270
42 #define SE_UART_RX_TRANS_CFG		0x280
43 #define SE_UART_RX_WORD_LEN		0x28c
44 #define SE_UART_RX_STALE_CNT		0x294
45 #define SE_UART_TX_PARITY_CFG		0x2a4
46 #define SE_UART_RX_PARITY_CFG		0x2a8
47 #define SE_UART_MANUAL_RFR		0x2ac
48 
49 /* SE_UART_TRANS_CFG */
50 #define UART_TX_PAR_EN			BIT(0)
51 #define UART_CTS_MASK			BIT(1)
52 
53 /* SE_UART_TX_STOP_BIT_LEN */
54 #define TX_STOP_BIT_LEN_1		0
55 #define TX_STOP_BIT_LEN_2		2
56 
57 /* SE_UART_RX_TRANS_CFG */
58 #define UART_RX_PAR_EN			BIT(4)
59 
60 /* SE_UART_RX_WORD_LEN */
61 #define RX_WORD_LEN_MASK		GENMASK(9, 0)
62 
63 /* SE_UART_RX_STALE_CNT */
64 #define RX_STALE_CNT			GENMASK(23, 0)
65 
66 /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
67 #define PAR_CALC_EN			BIT(0)
68 #define PAR_EVEN			0x00
69 #define PAR_ODD				0x01
70 #define PAR_SPACE			0x10
71 
72 /* SE_UART_MANUAL_RFR register fields */
73 #define UART_MANUAL_RFR_EN		BIT(31)
74 #define UART_RFR_NOT_READY		BIT(1)
75 #define UART_RFR_READY			BIT(0)
76 
77 /* UART M_CMD OP codes */
78 #define UART_START_TX			0x1
79 /* UART S_CMD OP codes */
80 #define UART_START_READ			0x1
81 #define UART_PARAM			0x1
82 #define UART_PARAM_RFR_OPEN		BIT(7)
83 
84 #define UART_OVERSAMPLING		32
85 #define STALE_TIMEOUT			16
86 #define DEFAULT_BITS_PER_CHAR		10
87 #define GENI_UART_CONS_PORTS		1
88 #define DEF_FIFO_DEPTH_WORDS		16
89 #define DEF_TX_WM			2
90 #define DEF_FIFO_WIDTH_BITS		32
91 #define UART_RX_WM			2
92 #define CONSOLE_TX_CHUNK_SIZE		32
93 
94 /* SE_UART_LOOPBACK_CFG */
95 #define RX_TX_SORTED			BIT(0)
96 #define CTS_RTS_SORTED			BIT(1)
97 #define RX_TX_CTS_RTS_SORTED		(RX_TX_SORTED | CTS_RTS_SORTED)
98 
99 /* UART pin swap value */
100 #define DEFAULT_IO_MACRO_IO0_IO1_MASK	GENMASK(3, 0)
101 #define IO_MACRO_IO0_SEL		0x3
102 #define DEFAULT_IO_MACRO_IO2_IO3_MASK	GENMASK(15, 4)
103 #define IO_MACRO_IO2_IO3_SWAP		0x4640
104 
105 /* We always configure 4 bytes per FIFO word */
106 #define BYTES_PER_FIFO_WORD		4U
107 
108 #define DMA_RX_BUF_SIZE		2048
109 
110 static DEFINE_IDA(port_ida);
111 
112 struct qcom_geni_device_data {
113 	bool console;
114 	enum geni_se_xfer_mode mode;
115 	int (*resources_init)(struct geni_se *se);
116 	int (*set_rate)(struct geni_se *se, unsigned long baud);
117 	int (*power_on)(struct geni_se *se);
118 	int (*power_off)(struct geni_se *se);
119 };
120 
121 struct qcom_geni_private_data {
122 	/* NOTE: earlycon port will have NULL here */
123 	struct uart_driver *drv;
124 
125 	u32 poll_cached_bytes;
126 	unsigned int poll_cached_bytes_cnt;
127 
128 	u32 write_cached_bytes;
129 	unsigned int write_cached_bytes_cnt;
130 };
131 
132 struct qcom_geni_serial_port {
133 	struct uart_port uport;
134 	struct geni_se se;
135 	const char *name;
136 	u32 tx_fifo_depth;
137 	u32 tx_fifo_width;
138 	u32 rx_fifo_depth;
139 	dma_addr_t tx_dma_addr;
140 	dma_addr_t rx_dma_addr;
141 	bool setup;
142 	unsigned long poll_timeout_us;
143 	unsigned long clk_rate;
144 	void *rx_buf;
145 	u32 loopback;
146 	bool brk;
147 
148 	unsigned int tx_remaining;
149 	unsigned int tx_queued;
150 	bool tx_dma_stale;
151 	int wakeup_irq;
152 	bool rx_tx_swap;
153 	bool cts_rts_swap;
154 	bool manual_flow;
155 	bool tx_kick_enabled;
156 	struct irq_work tx_kick;
157 	struct qcom_geni_private_data private_data;
158 	const struct qcom_geni_device_data *dev_data;
159 	struct notifier_block panic_nb;
160 };
161 
162 static const struct uart_ops qcom_geni_console_pops;
163 static const struct uart_ops qcom_geni_uart_pops;
164 static struct uart_driver qcom_geni_console_driver;
165 static struct uart_driver qcom_geni_uart_driver;
166 
167 static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport);
168 static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
169 static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
170 static int qcom_geni_serial_port_setup(struct uart_port *uport);
171 static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport);
172 static void qcom_geni_serial_resume_tx(struct uart_port *uport);
173 
174 static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
175 {
176 	return container_of(uport, struct qcom_geni_serial_port, uport);
177 }
178 
179 static struct qcom_geni_serial_port qcom_geni_console_port = {
180 	.uport = {
181 		.iotype = UPIO_MEM,
182 		.ops = &qcom_geni_console_pops,
183 		.flags = UPF_BOOT_AUTOCONF,
184 		.line = 0,
185 	},
186 };
187 
188 static const struct serial_rs485 qcom_geni_rs485_supported = {
189 	.flags = SER_RS485_ENABLED | SER_RS485_RTS_AFTER_SEND | SER_RS485_RTS_ON_SEND,
190 };
191 
192 /**
193  * qcom_geni_set_rs485_mode - Set RTS pin state for RS485 mode
194  * @uport: UART port
195  * @flag: RS485 flag to determine RTS polarity
196  *
197  * Enables manual RTS control for RS485. Sets RTS to READY or NOT_READY
198  * based on the specified flag if RS485 mode is enabled.
199  */
200 static void qcom_geni_set_rs485_mode(struct uart_port *uport, u32 flag)
201 {
202 	if (!(uport->rs485.flags & SER_RS485_ENABLED))
203 		return;
204 
205 	u32 rfr = UART_MANUAL_RFR_EN;
206 
207 	if (uport->rs485.flags & flag)
208 		rfr |= UART_RFR_NOT_READY;
209 	else
210 		rfr |= UART_RFR_READY;
211 
212 	writel(rfr, uport->membase + SE_UART_MANUAL_RFR);
213 }
214 
215 static int qcom_geni_serial_request_port(struct uart_port *uport)
216 {
217 	struct platform_device *pdev = to_platform_device(uport->dev);
218 	struct qcom_geni_serial_port *port = to_dev_port(uport);
219 
220 	uport->membase = devm_platform_ioremap_resource(pdev, 0);
221 	if (IS_ERR(uport->membase))
222 		return PTR_ERR(uport->membase);
223 	port->se.base = uport->membase;
224 	return 0;
225 }
226 
227 static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
228 {
229 	if (cfg_flags & UART_CONFIG_TYPE) {
230 		uport->type = PORT_MSM;
231 		qcom_geni_serial_request_port(uport);
232 	}
233 }
234 
235 static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
236 {
237 	unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
238 	u32 geni_ios = 0;
239 
240 	if (uart_console(uport)) {
241 		mctrl |= TIOCM_CTS;
242 	} else {
243 		geni_ios = readl(uport->membase + SE_GENI_IOS);
244 		if (!(geni_ios & IO2_DATA_IN))
245 			mctrl |= TIOCM_CTS;
246 	}
247 
248 	trace_geni_serial_get_mctrl(uport->dev, mctrl, geni_ios);
249 
250 	return mctrl;
251 }
252 
253 static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
254 							unsigned int mctrl)
255 {
256 	u32 uart_manual_rfr = 0;
257 	struct qcom_geni_serial_port *port = to_dev_port(uport);
258 
259 	if (uart_console(uport))
260 		return;
261 
262 	if (mctrl & TIOCM_LOOP)
263 		port->loopback = RX_TX_CTS_RTS_SORTED;
264 
265 	if (port->manual_flow && !(mctrl & TIOCM_RTS) && !uport->suspended)
266 		uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
267 	writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
268 
269 	trace_geni_serial_set_mctrl(uport->dev, mctrl, uart_manual_rfr);
270 }
271 
272 static const char *qcom_geni_serial_get_type(struct uart_port *uport)
273 {
274 	return "MSM";
275 }
276 
277 static struct qcom_geni_serial_port *get_port_from_line(int line, bool console, struct device *dev)
278 {
279 	struct qcom_geni_serial_port *port;
280 	int nr_ports = console ? GENI_UART_CONS_PORTS : CONFIG_SERIAL_QCOM_GENI_UART_PORTS;
281 
282 	if (console) {
283 		if (line < 0 || line >= nr_ports)
284 			return ERR_PTR(-ENXIO);
285 
286 		port = &qcom_geni_console_port;
287 	} else {
288 		int max_alias_num = of_alias_get_highest_id("serial");
289 
290 		port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
291 		if (!port)
292 			return ERR_PTR(-ENOMEM);
293 
294 		if (line < 0 || line >= nr_ports)
295 			line = ida_alloc_range(&port_ida, max_alias_num + 1,
296 					       nr_ports - 1, GFP_KERNEL);
297 		else
298 			line = ida_alloc_range(&port_ida, line,
299 					       nr_ports - 1, GFP_KERNEL);
300 
301 		if (line < 0)
302 			return ERR_PTR(-ENXIO);
303 
304 		port->uport.iotype = UPIO_MEM;
305 		port->uport.ops = &qcom_geni_uart_pops;
306 		port->uport.flags = UPF_BOOT_AUTOCONF;
307 		port->uport.line = line;
308 	}
309 	return port;
310 }
311 
312 static bool qcom_geni_serial_main_active(struct uart_port *uport)
313 {
314 	return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
315 }
316 
317 static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
318 {
319 	return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
320 }
321 
322 static bool qcom_geni_serial_poll_bitfield(struct uart_port *uport,
323 					   unsigned int offset, u32 field, u32 val)
324 {
325 	u32 reg;
326 	struct qcom_geni_serial_port *port;
327 	unsigned long timeout_us = 20000;
328 	struct qcom_geni_private_data *private_data = uport->private_data;
329 
330 	if (private_data->drv) {
331 		port = to_dev_port(uport);
332 		if (port->poll_timeout_us)
333 			timeout_us = port->poll_timeout_us;
334 	}
335 
336 	/*
337 	 * Use custom implementation instead of readl_poll_atomic since ktimer
338 	 * is not ready at the time of early console.
339 	 */
340 	timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
341 	while (timeout_us) {
342 		reg = readl(uport->membase + offset);
343 		if ((reg & field) == val)
344 			return true;
345 		udelay(10);
346 		timeout_us -= 10;
347 	}
348 	return false;
349 }
350 
351 static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
352 				      unsigned int offset, u32 field, bool set)
353 {
354 	return qcom_geni_serial_poll_bitfield(uport, offset, field, set ? field : 0);
355 }
356 
357 static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
358 {
359 	u32 m_cmd;
360 
361 	writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
362 	m_cmd = UART_START_TX << M_OPCODE_SHFT;
363 	writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
364 }
365 
366 static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
367 {
368 	int done;
369 
370 	done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
371 						M_CMD_DONE_EN, true);
372 	if (!done) {
373 		writel(M_GENI_CMD_ABORT, uport->membase +
374 						SE_GENI_M_CMD_CTRL_REG);
375 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
376 							M_CMD_ABORT_EN, true);
377 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
378 	}
379 }
380 
381 static void qcom_geni_serial_abort_rx(struct uart_port *uport)
382 {
383 	u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
384 
385 	writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
386 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
387 					S_GENI_CMD_ABORT, false);
388 	writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
389 	writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
390 }
391 
392 #ifdef CONFIG_CONSOLE_POLL
393 static int qcom_geni_serial_get_char(struct uart_port *uport)
394 {
395 	struct qcom_geni_private_data *private_data = uport->private_data;
396 	u32 status;
397 	u32 word_cnt;
398 	int ret;
399 
400 	if (!private_data->poll_cached_bytes_cnt) {
401 		status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
402 		writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
403 
404 		status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
405 		writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
406 
407 		status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
408 		word_cnt = status & RX_FIFO_WC_MSK;
409 		if (!word_cnt)
410 			return NO_POLL_CHAR;
411 
412 		if (word_cnt == 1 && (status & RX_LAST))
413 			/*
414 			 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
415 			 * treated as if it was BYTES_PER_FIFO_WORD.
416 			 */
417 			private_data->poll_cached_bytes_cnt =
418 				(status & RX_LAST_BYTE_VALID_MSK) >>
419 				RX_LAST_BYTE_VALID_SHFT;
420 
421 		if (private_data->poll_cached_bytes_cnt == 0)
422 			private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
423 
424 		private_data->poll_cached_bytes =
425 			readl(uport->membase + SE_GENI_RX_FIFOn);
426 	}
427 
428 	private_data->poll_cached_bytes_cnt--;
429 	ret = private_data->poll_cached_bytes & 0xff;
430 	private_data->poll_cached_bytes >>= 8;
431 
432 	return ret;
433 }
434 
435 static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
436 							unsigned char c)
437 {
438 	if (qcom_geni_serial_main_active(uport)) {
439 		qcom_geni_serial_poll_tx_done(uport);
440 		__qcom_geni_serial_cancel_tx_cmd(uport);
441 	}
442 
443 	writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
444 	qcom_geni_serial_setup_tx(uport, 1);
445 	writel(c, uport->membase + SE_GENI_TX_FIFOn);
446 	qcom_geni_serial_poll_tx_done(uport);
447 }
448 
449 static int qcom_geni_serial_poll_init(struct uart_port *uport)
450 {
451 	struct qcom_geni_serial_port *port = to_dev_port(uport);
452 	int ret;
453 
454 	if (!port->setup) {
455 		ret = qcom_geni_serial_port_setup(uport);
456 		if (ret)
457 			return ret;
458 	}
459 
460 	if (!qcom_geni_serial_secondary_active(uport))
461 		geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
462 
463 	return 0;
464 }
465 #endif
466 
467 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
468 static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
469 {
470 	u32 i;
471 	unsigned char buf[sizeof(u32)];
472 	struct tty_port *tport;
473 	struct qcom_geni_serial_port *port = to_dev_port(uport);
474 
475 	tport = &uport->state->port;
476 	for (i = 0; i < bytes; ) {
477 		int c;
478 		int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
479 
480 		ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
481 		i += chunk;
482 		if (drop)
483 			continue;
484 
485 		for (c = 0; c < chunk; c++) {
486 			int sysrq;
487 
488 			uport->icount.rx++;
489 			if (port->brk && buf[c] == 0) {
490 				port->brk = false;
491 				if (uart_handle_break(uport))
492 					continue;
493 			}
494 
495 			sysrq = uart_prepare_sysrq_char(uport, buf[c]);
496 
497 			if (!sysrq)
498 				tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
499 		}
500 	}
501 	if (!drop)
502 		tty_flip_buffer_push(tport);
503 }
504 #else
505 static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
506 {
507 
508 }
509 #endif
510 
511 static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
512 {
513 	u32 status;
514 	u32 word_cnt;
515 	u32 last_word_byte_cnt;
516 	u32 last_word_partial;
517 	u32 total_bytes;
518 
519 	status = readl(uport->membase +	SE_GENI_RX_FIFO_STATUS);
520 	word_cnt = status & RX_FIFO_WC_MSK;
521 	last_word_partial = status & RX_LAST;
522 	last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
523 						RX_LAST_BYTE_VALID_SHFT;
524 
525 	if (!word_cnt)
526 		return;
527 	total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
528 	if (last_word_partial && last_word_byte_cnt)
529 		total_bytes += last_word_byte_cnt;
530 	else
531 		total_bytes += BYTES_PER_FIFO_WORD;
532 	handle_rx_console(uport, total_bytes, drop);
533 }
534 
535 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
536 /* Caller holds the UART port lock. */
537 static void qcom_geni_serial_poll_rx_fifo_locked(struct uart_port *uport)
538 {
539 	struct qcom_geni_serial_port *port = to_dev_port(uport);
540 	struct tty_port *tport = &uport->state->port;
541 	u32 s_irq_status;
542 	bool drop_rx = false;
543 
544 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
545 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
546 
547 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
548 		uport->icount.overrun++;
549 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
550 	}
551 
552 	if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
553 		if (s_irq_status & S_GP_IRQ_0_EN)
554 			uport->icount.parity++;
555 		drop_rx = true;
556 	} else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
557 		uport->icount.brk++;
558 		port->brk = true;
559 	}
560 
561 	qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
562 }
563 
564 static void qcom_geni_serial_drain_fifo(struct uart_port *uport)
565 {
566 	struct qcom_geni_serial_port *port = to_dev_port(uport);
567 
568 	qcom_geni_serial_poll_bitfield(uport, SE_GENI_M_GP_LENGTH, GP_LENGTH,
569 			port->tx_queued);
570 }
571 
572 static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
573 {
574 	struct qcom_geni_private_data *private_data = uport->private_data;
575 
576 	private_data->write_cached_bytes =
577 		(private_data->write_cached_bytes >> 8) | (ch << 24);
578 	private_data->write_cached_bytes_cnt++;
579 
580 	if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
581 		writel(private_data->write_cached_bytes,
582 		       uport->membase + SE_GENI_TX_FIFOn);
583 		private_data->write_cached_bytes_cnt = 0;
584 	}
585 }
586 
587 static void
588 __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
589 				 unsigned int count, bool poll_rx)
590 {
591 	struct qcom_geni_private_data *private_data = uport->private_data;
592 
593 	int i;
594 	u32 bytes_to_send = count;
595 
596 	for (i = 0; i < count; i++) {
597 		/*
598 		 * uart_console_write() adds a carriage return for each newline.
599 		 * Account for additional bytes to be written.
600 		 */
601 		if (s[i] == '\n')
602 			bytes_to_send++;
603 	}
604 
605 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
606 	writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
607 	qcom_geni_serial_setup_tx(uport, bytes_to_send);
608 	for (i = 0; i < count; ) {
609 		size_t chars_to_write = 0;
610 		size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
611 
612 		/*
613 		 * If the WM bit never set, then the Tx state machine is not
614 		 * in a valid state, so break, cancel/abort any existing
615 		 * command. Unfortunately the current data being written is
616 		 * lost.
617 		 */
618 		if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
619 						M_TX_FIFO_WATERMARK_EN, true))
620 			break;
621 		if (poll_rx)
622 			qcom_geni_serial_poll_rx_fifo_locked(uport);
623 		chars_to_write = min_t(size_t, count - i, avail / 2);
624 		uart_console_write(uport, s + i, chars_to_write,
625 						qcom_geni_serial_wr_char);
626 		writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
627 							SE_GENI_M_IRQ_CLEAR);
628 		i += chars_to_write;
629 	}
630 
631 	if (private_data->write_cached_bytes_cnt) {
632 		private_data->write_cached_bytes >>= BITS_PER_BYTE *
633 			(BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
634 		writel(private_data->write_cached_bytes,
635 		       uport->membase + SE_GENI_TX_FIFOn);
636 		private_data->write_cached_bytes_cnt = 0;
637 	}
638 
639 	qcom_geni_serial_poll_tx_done(uport);
640 }
641 
642 static void qcom_geni_serial_console_takeover(struct uart_port *uport,
643 					       bool preserve_tx)
644 {
645 	if (qcom_geni_serial_main_active(uport)) {
646 		struct qcom_geni_serial_port *port = to_dev_port(uport);
647 
648 		if (preserve_tx) {
649 			if (port->tx_remaining == 0)
650 				qcom_geni_serial_poll_tx_done(uport);
651 			else
652 				qcom_geni_serial_drain_fifo(uport);
653 		}
654 
655 		qcom_geni_serial_cancel_tx_cmd(uport);
656 	}
657 }
658 
659 static void qcom_geni_serial_console_write_thread(struct console *co,
660 						  struct nbcon_write_context *wctxt)
661 {
662 	struct qcom_geni_serial_port *port;
663 	struct uart_port *uport;
664 	unsigned int offset = 0;
665 
666 	port = get_port_from_line(co->index, true, NULL);
667 	if (IS_ERR(port))
668 		return;
669 
670 	uport = &port->uport;
671 	if (!nbcon_enter_unsafe(wctxt))
672 		return;
673 
674 	qcom_geni_serial_console_takeover(uport, true);
675 	if (!nbcon_exit_unsafe(wctxt))
676 		return;
677 
678 	while (offset < wctxt->len) {
679 		/*
680 		 * Printk records can be much larger than the FIFO. Limit one
681 		 * M-side command to 32 source bytes so atomic console output can
682 		 * take over between commands instead of waiting for the record.
683 		 */
684 		unsigned int count = min_t(unsigned int, wctxt->len - offset,
685 						   CONSOLE_TX_CHUNK_SIZE);
686 
687 		if (!nbcon_enter_unsafe(wctxt))
688 			return;
689 
690 		__qcom_geni_serial_console_write(uport, wctxt->outbuf + offset,
691 						 count, true);
692 		offset += count;
693 
694 		if (!nbcon_exit_unsafe(wctxt))
695 			return;
696 	}
697 
698 	if (!nbcon_enter_unsafe(wctxt))
699 		return;
700 
701 	qcom_geni_serial_resume_tx(uport);
702 	nbcon_exit_unsafe(wctxt);
703 }
704 
705 static void qcom_geni_serial_console_write_atomic(struct console *co,
706 						  struct nbcon_write_context *wctxt)
707 {
708 	struct qcom_geni_serial_port *port;
709 	struct uart_port *uport;
710 	u32 m_irq_en;
711 
712 	port = get_port_from_line(co->index, true, NULL);
713 	if (IS_ERR(port))
714 		return;
715 
716 	uport = &port->uport;
717 	if (!nbcon_enter_unsafe(wctxt))
718 		return;
719 
720 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
721 	writel(m_irq_en & ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN),
722 		uport->membase + SE_GENI_M_IRQ_EN);
723 
724 	/* Atomic console output takes priority over an active normal TX command. */
725 	qcom_geni_serial_console_takeover(uport, false);
726 	__qcom_geni_serial_console_write(uport, wctxt->outbuf, wctxt->len, false);
727 
728 	writel(m_irq_en, uport->membase + SE_GENI_M_IRQ_EN);
729 	nbcon_exit_unsafe(wctxt);
730 
731 	/* Restart TTY data left queued when atomic output canceled M TX. */
732 	if (READ_ONCE(port->tx_kick_enabled))
733 		irq_work_queue(&port->tx_kick);
734 }
735 
736 static void qcom_geni_serial_console_device_lock(struct console *co,
737 						  unsigned long *flags)
738 {
739 	struct qcom_geni_serial_port *port;
740 
741 	port = get_port_from_line(co->index, true, NULL);
742 	if (IS_ERR(port))
743 		return;
744 
745 	__uart_port_lock_irqsave(&port->uport, flags);
746 }
747 
748 static void qcom_geni_serial_console_device_unlock(struct console *co,
749 						    unsigned long flags)
750 {
751 	struct qcom_geni_serial_port *port;
752 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
753 	u8 sysrq_ch;
754 #endif
755 
756 	port = get_port_from_line(co->index, true, NULL);
757 	if (IS_ERR(port))
758 		return;
759 
760 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
761 	/* The threaded console writer can receive a SysRq character. */
762 	sysrq_ch = port->uport.sysrq_ch;
763 	port->uport.sysrq_ch = 0;
764 #endif
765 	__uart_port_unlock_irqrestore(&port->uport, flags);
766 
767 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
768 	if (sysrq_ch)
769 		handle_sysrq(sysrq_ch);
770 #endif
771 }
772 
773 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
774 
775 static void handle_rx_uart(struct uart_port *uport, u32 bytes)
776 {
777 	struct qcom_geni_serial_port *port = to_dev_port(uport);
778 	struct tty_port *tport = &uport->state->port;
779 	int ret;
780 
781 	ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
782 	if (ret != bytes) {
783 		dev_err_ratelimited(uport->dev, "failed to push data (%d < %u)\n",
784 				ret, bytes);
785 	}
786 	uport->icount.rx += ret;
787 	tty_flip_buffer_push(tport);
788 }
789 
790 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
791 {
792 	return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
793 }
794 
795 static void qcom_geni_serial_flush_buffer_dma(struct uart_port *uport)
796 {
797 	struct qcom_geni_serial_port *port = to_dev_port(uport);
798 
799 	qcom_geni_serial_stop_tx_dma(uport);
800 	port->tx_remaining = 0;
801 	port->tx_queued = 0;
802 }
803 
804 static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
805 {
806 	struct qcom_geni_serial_port *port = to_dev_port(uport);
807 
808 	if (qcom_geni_serial_main_active(uport))
809 		__qcom_geni_serial_cancel_tx_cmd(uport);
810 
811 	if (port->tx_dma_addr) {
812 		writel(1, uport->membase + SE_DMA_TX_FSM_RST);
813 		if (!qcom_geni_serial_poll_bit(uport, SE_DMA_TX_IRQ_STAT,
814 					       TX_RESET_DONE, true))
815 			dev_err_ratelimited(uport->dev, "TX DMA reset failed");
816 		writel(TX_RESET_DONE | TX_DMA_DONE,
817 		       uport->membase + SE_DMA_TX_IRQ_CLR);
818 
819 		geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
820 				      port->tx_remaining);
821 		port->tx_dma_addr = 0;
822 	}
823 }
824 
825 static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
826 {
827 	struct qcom_geni_serial_port *port = to_dev_port(uport);
828 	struct tty_port *tport = &uport->state->port;
829 	unsigned int xmit_size;
830 	u8 *tail;
831 	int ret;
832 
833 	if (port->tx_dma_addr)
834 		return;
835 
836 	if (kfifo_is_empty(&tport->xmit_fifo))
837 		return;
838 
839 	xmit_size = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail,
840 			UART_XMIT_SIZE);
841 
842 	trace_geni_serial_tx_data(uport->dev, tail, xmit_size);
843 
844 	qcom_geni_set_rs485_mode(uport, SER_RS485_RTS_ON_SEND);
845 
846 	qcom_geni_serial_setup_tx(uport, xmit_size);
847 
848 	ret = geni_se_tx_dma_prep(&port->se, tail, xmit_size,
849 				  &port->tx_dma_addr);
850 	if (ret) {
851 		dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
852 		qcom_geni_serial_stop_tx_dma(uport);
853 		return;
854 	}
855 
856 	port->tx_remaining = xmit_size;
857 	port->tx_dma_stale = false;
858 }
859 
860 static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
861 {
862 	unsigned char c;
863 	u32 irq_en;
864 
865 	/*
866 	 * Start a new transfer in case the previous command was cancelled and
867 	 * left data in the FIFO which may prevent the watermark interrupt
868 	 * from triggering. Note that the stale data is discarded.
869 	 */
870 	if (!qcom_geni_serial_main_active(uport) &&
871 	    !qcom_geni_serial_tx_empty(uport)) {
872 		if (uart_fifo_out(uport, &c, 1) == 1) {
873 			writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
874 			qcom_geni_serial_setup_tx(uport, 1);
875 			writel(c, uport->membase + SE_GENI_TX_FIFOn);
876 		}
877 	}
878 
879 	irq_en = readl(uport->membase +	SE_GENI_M_IRQ_EN);
880 	irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
881 	writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
882 	writel(irq_en, uport->membase +	SE_GENI_M_IRQ_EN);
883 }
884 
885 /* Caller holds the UART port lock. */
886 static void qcom_geni_serial_resume_tx(struct uart_port *uport)
887 {
888 	if (!uart_tx_stopped(uport) &&
889 	    !kfifo_is_empty(&uport->state->port.xmit_fifo))
890 		qcom_geni_serial_start_tx_fifo(uport);
891 }
892 
893 static void qcom_geni_serial_restart_tx(struct irq_work *work)
894 {
895 	struct qcom_geni_serial_port *port = container_of(work,
896 						      struct qcom_geni_serial_port, tx_kick);
897 	struct uart_port *uport = &port->uport;
898 
899 	if (!READ_ONCE(port->tx_kick_enabled) || !uport->state || uport->suspended)
900 		return;
901 
902 	uart_port_lock(uport);
903 	if (READ_ONCE(port->tx_kick_enabled) && uport->state && !uport->suspended)
904 		qcom_geni_serial_resume_tx(uport);
905 	uart_port_unlock(uport);
906 }
907 
908 static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
909 {
910 	u32 irq_en;
911 
912 	irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
913 	irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
914 	writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
915 	writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
916 }
917 
918 static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
919 {
920 	struct qcom_geni_serial_port *port = to_dev_port(uport);
921 
922 	geni_se_cancel_m_cmd(&port->se);
923 	if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
924 						M_CMD_CANCEL_EN, true)) {
925 		geni_se_abort_m_cmd(&port->se);
926 		qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
927 						M_CMD_ABORT_EN, true);
928 		writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
929 	}
930 	writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
931 }
932 
933 static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
934 {
935 	struct qcom_geni_serial_port *port = to_dev_port(uport);
936 
937 	if (!qcom_geni_serial_main_active(uport))
938 		return;
939 
940 	__qcom_geni_serial_cancel_tx_cmd(uport);
941 
942 	port->tx_remaining = 0;
943 	port->tx_queued = 0;
944 }
945 
946 static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
947 {
948 	u32 irq_en;
949 	struct qcom_geni_serial_port *port = to_dev_port(uport);
950 	u32 s_irq_status;
951 
952 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
953 	irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
954 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
955 
956 	if (!qcom_geni_serial_secondary_active(uport))
957 		return;
958 
959 	geni_se_cancel_s_cmd(&port->se);
960 	qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
961 					S_CMD_CANCEL_EN, true);
962 	/*
963 	 * If timeout occurs secondary engine remains active
964 	 * and Abort sequence is executed.
965 	 */
966 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
967 	/* Flush the Rx buffer */
968 	if (s_irq_status & S_RX_FIFO_LAST_EN)
969 		qcom_geni_serial_handle_rx_fifo(uport, true);
970 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
971 
972 	if (qcom_geni_serial_secondary_active(uport))
973 		qcom_geni_serial_abort_rx(uport);
974 }
975 
976 static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
977 {
978 	u32 irq_en;
979 	struct qcom_geni_serial_port *port = to_dev_port(uport);
980 
981 	if (qcom_geni_serial_secondary_active(uport))
982 		qcom_geni_serial_stop_rx_fifo(uport);
983 
984 	geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
985 
986 	irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
987 	irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
988 	writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
989 }
990 
991 static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
992 {
993 	struct qcom_geni_serial_port *port = to_dev_port(uport);
994 	bool done;
995 
996 	if (!qcom_geni_serial_secondary_active(uport))
997 		return;
998 
999 	geni_se_cancel_s_cmd(&port->se);
1000 	done = qcom_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
1001 			RX_EOT, true);
1002 	if (done) {
1003 		writel(RX_EOT | RX_DMA_DONE,
1004 				uport->membase + SE_DMA_RX_IRQ_CLR);
1005 	} else {
1006 		qcom_geni_serial_abort_rx(uport);
1007 
1008 		writel(1, uport->membase + SE_DMA_RX_FSM_RST);
1009 		qcom_geni_serial_poll_bit(uport, SE_DMA_RX_IRQ_STAT,
1010 				RX_RESET_DONE, true);
1011 		writel(RX_RESET_DONE | RX_DMA_DONE,
1012 				uport->membase + SE_DMA_RX_IRQ_CLR);
1013 	}
1014 
1015 }
1016 
1017 static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
1018 {
1019 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1020 
1021 	if (qcom_geni_serial_secondary_active(uport))
1022 		qcom_geni_serial_stop_rx_dma(uport);
1023 
1024 	geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
1025 
1026 	if (!port->rx_dma_addr) {
1027 		dev_err(uport->dev, "RX DMA buffer not mapped\n");
1028 		return;
1029 	}
1030 
1031 	dma_sync_single_for_device(uport->dev->parent, port->rx_dma_addr,
1032 				   DMA_RX_BUF_SIZE, DMA_FROM_DEVICE);
1033 	geni_se_rx_init_dma(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
1034 }
1035 
1036 static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
1037 {
1038 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1039 	u32 rx_in;
1040 
1041 	if (!qcom_geni_serial_secondary_active(uport))
1042 		return;
1043 
1044 	if (!port->rx_dma_addr)
1045 		return;
1046 
1047 	dma_sync_single_for_cpu(uport->dev->parent, port->rx_dma_addr,
1048 				DMA_RX_BUF_SIZE, DMA_FROM_DEVICE);
1049 
1050 	rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
1051 	if (!rx_in)
1052 		dev_warn_ratelimited(uport->dev, "serial engine reports 0 RX bytes in!\n");
1053 	else if (!drop) {
1054 		trace_geni_serial_rx_data(uport->dev, port->rx_buf, rx_in);
1055 		handle_rx_uart(uport, rx_in);
1056 	}
1057 
1058 	dma_sync_single_for_device(uport->dev->parent, port->rx_dma_addr,
1059 				   DMA_RX_BUF_SIZE, DMA_FROM_DEVICE);
1060 	geni_se_rx_init_dma(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
1061 }
1062 
1063 static void qcom_geni_serial_start_rx(struct uart_port *uport)
1064 {
1065 	uport->ops->start_rx(uport);
1066 }
1067 
1068 static void qcom_geni_serial_stop_rx(struct uart_port *uport)
1069 {
1070 	uport->ops->stop_rx(uport);
1071 }
1072 
1073 static void qcom_geni_serial_stop_tx(struct uart_port *uport)
1074 {
1075 	uport->ops->stop_tx(uport);
1076 }
1077 
1078 static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
1079 					     unsigned int chunk)
1080 {
1081 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1082 	unsigned int tx_bytes, remaining = chunk;
1083 	u8 buf[BYTES_PER_FIFO_WORD];
1084 
1085 	while (remaining) {
1086 		memset(buf, 0, sizeof(buf));
1087 		tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
1088 
1089 		uart_fifo_out(uport, buf, tx_bytes);
1090 
1091 		iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
1092 
1093 		remaining -= tx_bytes;
1094 		port->tx_remaining -= tx_bytes;
1095 	}
1096 }
1097 
1098 static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
1099 					    bool done, bool active)
1100 {
1101 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1102 	struct tty_port *tport = &uport->state->port;
1103 	size_t avail;
1104 	size_t pending;
1105 	u32 status;
1106 	u32 irq_en;
1107 	unsigned int chunk;
1108 
1109 	status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1110 
1111 	/* Complete the current tx command before taking newly added data */
1112 	if (active)
1113 		pending = port->tx_remaining;
1114 	else
1115 		pending = kfifo_len(&tport->xmit_fifo);
1116 
1117 	/* All data has been transmitted or command has been cancelled */
1118 	if (!pending && done) {
1119 		qcom_geni_serial_stop_tx_fifo(uport);
1120 		goto out_write_wakeup;
1121 	}
1122 
1123 	if (active)
1124 		avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
1125 	else
1126 		avail = port->tx_fifo_depth;
1127 
1128 	avail *= BYTES_PER_FIFO_WORD;
1129 
1130 	chunk = min(avail, pending);
1131 	if (!chunk)
1132 		goto out_write_wakeup;
1133 
1134 	if (!active) {
1135 		qcom_geni_serial_setup_tx(uport, pending);
1136 		port->tx_remaining = pending;
1137 		port->tx_queued = 0;
1138 
1139 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
1140 		if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
1141 			writel(irq_en | M_TX_FIFO_WATERMARK_EN,
1142 					uport->membase + SE_GENI_M_IRQ_EN);
1143 	}
1144 
1145 	qcom_geni_serial_send_chunk_fifo(uport, chunk);
1146 	port->tx_queued += chunk;
1147 
1148 	/*
1149 	 * The tx fifo watermark is level triggered and latched. Though we had
1150 	 * cleared it in qcom_geni_serial_isr it will have already reasserted
1151 	 * so we must clear it again here after our writes.
1152 	 */
1153 	writel(M_TX_FIFO_WATERMARK_EN,
1154 			uport->membase + SE_GENI_M_IRQ_CLEAR);
1155 
1156 out_write_wakeup:
1157 	if (!port->tx_remaining) {
1158 		irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
1159 		if (irq_en & M_TX_FIFO_WATERMARK_EN)
1160 			writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
1161 					uport->membase + SE_GENI_M_IRQ_EN);
1162 	}
1163 
1164 	if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1165 		uart_write_wakeup(uport);
1166 }
1167 
1168 static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
1169 {
1170 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1171 	struct tty_port *tport = &uport->state->port;
1172 	unsigned int fifo_len = kfifo_len(&tport->xmit_fifo);
1173 	bool tx_dma_stale = port->tx_dma_stale;
1174 
1175 	/*
1176 	 * Only advance the kfifo if it still contains the bytes that were
1177 	 * transferred. uart_flush_buffer() may have run before this IRQ
1178 	 * fired: it calls kfifo_reset() under the port lock, making
1179 	 * fifo_len = 0 while tx_remaining remains non-zero. Calling
1180 	 * uart_xmit_advance() in that case would underflow kfifo->out past
1181 	 * kfifo->in, making kfifo_len() wrap to UART_XMIT_SIZE - tx_remaining
1182 	 * and triggering a spurious large DMA transfer of stale data.
1183 	 */
1184 	if (!tx_dma_stale && fifo_len >= port->tx_remaining)
1185 		uart_xmit_advance(uport, port->tx_remaining);
1186 
1187 	geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
1188 	port->tx_dma_addr = 0;
1189 	port->tx_remaining = 0;
1190 	port->tx_dma_stale = false;
1191 
1192 	if (!kfifo_is_empty(&tport->xmit_fifo))
1193 		qcom_geni_serial_start_tx_dma(uport);
1194 
1195 	if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
1196 		uart_write_wakeup(uport);
1197 }
1198 
1199 static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
1200 {
1201 	u32 m_irq_en;
1202 	u32 m_irq_status;
1203 	u32 s_irq_status;
1204 	u32 geni_status;
1205 	u32 dma;
1206 	u32 dma_tx_status;
1207 	u32 dma_rx_status;
1208 	struct uart_port *uport = dev;
1209 	bool drop_rx = false;
1210 	struct tty_port *tport = &uport->state->port;
1211 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1212 
1213 	if (uport->suspended)
1214 		return IRQ_NONE;
1215 
1216 	uart_port_lock(uport);
1217 
1218 	s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
1219 	dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
1220 	writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
1221 	writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
1222 
1223 	if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
1224 		uport->icount.overrun++;
1225 		tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1226 	}
1227 
1228 	if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
1229 		if (s_irq_status & S_GP_IRQ_0_EN)
1230 			uport->icount.parity++;
1231 		drop_rx = true;
1232 	} else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
1233 		uport->icount.brk++;
1234 		port->brk = true;
1235 	}
1236 
1237 	m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
1238 	dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
1239 	geni_status = readl(uport->membase + SE_GENI_STATUS);
1240 	dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
1241 	m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
1242 
1243 	trace_geni_serial_irq(uport->dev, m_irq_status, s_irq_status,
1244 			      dma_tx_status, dma_rx_status);
1245 
1246 	writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
1247 	writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
1248 
1249 	if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
1250 		goto handle_rx;
1251 
1252 	if (dma) {
1253 		if (dma_tx_status & TX_DMA_DONE) {
1254 			qcom_geni_serial_handle_tx_dma(uport);
1255 			qcom_geni_set_rs485_mode(uport, SER_RS485_RTS_AFTER_SEND);
1256 		}
1257 	} else if (m_irq_status & m_irq_en &
1258 		   (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN)) {
1259 		qcom_geni_serial_handle_tx_fifo(uport,
1260 				m_irq_status & M_CMD_DONE_EN,
1261 				geni_status & M_GENI_CMD_ACTIVE);
1262 	}
1263 
1264 handle_rx:
1265 	if (dma) {
1266 		if (dma_rx_status) {
1267 			if (dma_rx_status & RX_RESET_DONE)
1268 				goto out_unlock;
1269 
1270 			if (dma_rx_status & RX_DMA_PARITY_ERR) {
1271 				uport->icount.parity++;
1272 				drop_rx = true;
1273 			}
1274 
1275 			if (dma_rx_status & RX_DMA_BREAK)
1276 				uport->icount.brk++;
1277 
1278 			if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
1279 				qcom_geni_serial_handle_rx_dma(uport, drop_rx);
1280 		}
1281 	} else {
1282 		if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
1283 			qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
1284 	}
1285 
1286 out_unlock:
1287 	uart_unlock_and_check_sysrq(uport);
1288 
1289 	return IRQ_HANDLED;
1290 }
1291 
1292 static int setup_fifos(struct qcom_geni_serial_port *port)
1293 {
1294 	struct uart_port *uport;
1295 
1296 	uport = &port->uport;
1297 	port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1298 	port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1299 	port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1300 	uport->fifosize =
1301 		(port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1302 
1303 	return 0;
1304 }
1305 
1306 
1307 static void qcom_geni_serial_shutdown(struct uart_port *uport)
1308 {
1309 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1310 
1311 	/* Atomic console output queues tx_kick without taking the port lock. */
1312 	WRITE_ONCE(port->tx_kick_enabled, false);
1313 	irq_work_sync(&port->tx_kick);
1314 	disable_irq(uport->irq);
1315 
1316 	uart_port_lock_irq(uport);
1317 	qcom_geni_serial_stop_tx(uport);
1318 	qcom_geni_serial_stop_rx(uport);
1319 
1320 	qcom_geni_serial_cancel_tx_cmd(uport);
1321 	uart_port_unlock_irq(uport);
1322 
1323 	pm_runtime_put_sync(uport->dev);
1324 }
1325 
1326 static void qcom_geni_serial_flush_buffer_fifo(struct uart_port *uport)
1327 {
1328 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1329 
1330 	if (port->tx_dma_addr)
1331 		port->tx_dma_stale = true;
1332 	qcom_geni_serial_cancel_tx_cmd(uport);
1333 }
1334 
1335 static int qcom_geni_serial_port_setup(struct uart_port *uport)
1336 {
1337 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1338 	u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1339 	u32 proto;
1340 	u32 pin_swap;
1341 	int ret;
1342 
1343 	proto = geni_se_read_proto(&port->se);
1344 	if (proto == GENI_SE_INVALID_PROTO) {
1345 		ret = geni_load_se_firmware(&port->se, GENI_SE_UART);
1346 		if (ret) {
1347 			dev_err(uport->dev, "UART firmware load failed ret: %d\n", ret);
1348 			return ret;
1349 		}
1350 	} else if (proto != GENI_SE_UART) {
1351 		dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1352 		return -ENXIO;
1353 	}
1354 
1355 	qcom_geni_serial_stop_rx(uport);
1356 
1357 	ret = setup_fifos(port);
1358 	if (ret)
1359 		return ret;
1360 
1361 	writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
1362 
1363 	pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
1364 	if (port->rx_tx_swap) {
1365 		pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
1366 		pin_swap |= IO_MACRO_IO2_IO3_SWAP;
1367 	}
1368 	if (port->cts_rts_swap) {
1369 		pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
1370 		pin_swap |= IO_MACRO_IO0_SEL;
1371 	}
1372 	/* Configure this register if RX-TX, CTS-RTS pins are swapped */
1373 	if (port->rx_tx_swap || port->cts_rts_swap)
1374 		writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
1375 
1376 	/*
1377 	 * Make an unconditional cancel on the main sequencer to reset
1378 	 * it else we could end up in data loss scenarios.
1379 	 */
1380 	if (uart_console(uport))
1381 		qcom_geni_serial_poll_tx_done(uport);
1382 	geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1383 			       false, true, true);
1384 	geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
1385 	geni_se_select_mode(&port->se, port->dev_data->mode);
1386 	port->setup = true;
1387 
1388 	return 0;
1389 }
1390 
1391 static int qcom_geni_serial_startup(struct uart_port *uport)
1392 {
1393 	int ret;
1394 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1395 	struct tty_port *tport = &uport->state->port;
1396 
1397 	ret = pm_runtime_resume_and_get(uport->dev);
1398 	if (ret < 0) {
1399 		dev_err(uport->dev, "Failed to resume and get %d\n", ret);
1400 		return ret;
1401 	}
1402 
1403 	if (!port->setup) {
1404 		ret = qcom_geni_serial_port_setup(uport);
1405 		if (ret) {
1406 			pm_runtime_put_sync(uport->dev);
1407 			return ret;
1408 		}
1409 	}
1410 
1411 	/*
1412 	 * Skip the close-time transmit drain for console ports so that
1413 	 * shutdown can proceed without waiting for pending TX completion.
1414 	 */
1415 	if (uart_console(uport))
1416 		tport->closing_wait = ASYNC_CLOSING_WAIT_NONE;
1417 
1418 	uart_port_lock_irq(uport);
1419 	qcom_geni_serial_start_rx(uport);
1420 	uart_port_unlock_irq(uport);
1421 
1422 	enable_irq(uport->irq);
1423 	WRITE_ONCE(port->tx_kick_enabled, true);
1424 
1425 	return 0;
1426 }
1427 
1428 static int geni_serial_set_rate(struct geni_se *se, unsigned long baud)
1429 {
1430 	struct qcom_geni_serial_port *port = dev_get_drvdata(se->dev);
1431 	struct uart_port *uport = &port->uport;
1432 	unsigned long clk_rate;
1433 	unsigned int avg_bw_core, clk_idx;
1434 	unsigned int clk_div;
1435 	u32 ver, sampling_rate;
1436 	u32 ser_clk_cfg;
1437 	int ret;
1438 
1439 	sampling_rate = UART_OVERSAMPLING;
1440 	/* Sampling rate is halved for IP versions >= 2.5 */
1441 	ver = geni_se_get_qup_hw_version(&port->se);
1442 	if (ver >= QUP_SE_VERSION_2_5)
1443 		sampling_rate /= 2;
1444 
1445 	ret = geni_se_clk_freq_match(&port->se, baud * sampling_rate, &clk_idx, &clk_rate, false);
1446 	if (ret) {
1447 		dev_err(port->se.dev, "Failed to find src clk for baud rate: %lu ret: %d\n",
1448 			baud, ret);
1449 		return ret;
1450 	}
1451 
1452 	clk_div = DIV_ROUND_UP(clk_rate, baud * sampling_rate);
1453 	/* Check if calculated divider exceeds maximum allowed value */
1454 	if (clk_div > (CLK_DIV_MSK >> CLK_DIV_SHFT)) {
1455 		dev_err(port->se.dev, "Calculated clock divider %u exceeds maximum\n", clk_div);
1456 		return -EINVAL;
1457 	}
1458 
1459 	trace_geni_serial_clk_cfg(uport->dev, baud * sampling_rate, clk_rate,
1460 				  clk_div, clk_idx);
1461 
1462 	uport->uartclk = clk_rate;
1463 	port->clk_rate = clk_rate;
1464 	dev_pm_opp_set_rate(uport->dev, clk_rate);
1465 	ser_clk_cfg = SER_CLK_EN;
1466 	ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1467 
1468 	/*
1469 	 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1470 	 * only.
1471 	 */
1472 	avg_bw_core = (baud > 115200) ? CORE_2X_50_MHZ : CORE_2X_19_2_MHZ;
1473 	port->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(avg_bw_core);
1474 	port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1475 	geni_icc_set_bw(&port->se);
1476 
1477 	writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1478 	writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1479 	/* Configure clock selection register with the selected clock index */
1480 	writel(clk_idx & CLK_SEL_MSK, uport->membase + SE_GENI_CLK_SEL);
1481 	return 0;
1482 }
1483 
1484 static void qcom_geni_serial_set_termios(struct uart_port *uport,
1485 					 struct ktermios *termios,
1486 					 const struct ktermios *old)
1487 {
1488 	struct qcom_geni_serial_port *port = to_dev_port(uport);
1489 	unsigned int baud;
1490 	unsigned long timeout;
1491 	u32 bits_per_char;
1492 	u32 tx_trans_cfg;
1493 	u32 tx_parity_cfg;
1494 	u32 rx_trans_cfg;
1495 	u32 rx_parity_cfg;
1496 	u32 stop_bit_len;
1497 	int ret = 0;
1498 
1499 	/* baud rate */
1500 	baud = uart_get_baud_rate(uport, termios, old, 300, 8000000);
1501 
1502 	ret = port->dev_data->set_rate(&port->se, baud);
1503 	if (ret)
1504 		return;
1505 
1506 	/* parity */
1507 	tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
1508 	tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
1509 	rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
1510 	rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1511 	if (termios->c_cflag & PARENB) {
1512 		tx_trans_cfg |= UART_TX_PAR_EN;
1513 		rx_trans_cfg |= UART_RX_PAR_EN;
1514 		tx_parity_cfg |= PAR_CALC_EN;
1515 		rx_parity_cfg |= PAR_CALC_EN;
1516 		if (termios->c_cflag & PARODD) {
1517 			tx_parity_cfg |= PAR_ODD;
1518 			rx_parity_cfg |= PAR_ODD;
1519 		} else if (termios->c_cflag & CMSPAR) {
1520 			tx_parity_cfg |= PAR_SPACE;
1521 			rx_parity_cfg |= PAR_SPACE;
1522 		} else {
1523 			tx_parity_cfg |= PAR_EVEN;
1524 			rx_parity_cfg |= PAR_EVEN;
1525 		}
1526 	} else {
1527 		tx_trans_cfg &= ~UART_TX_PAR_EN;
1528 		rx_trans_cfg &= ~UART_RX_PAR_EN;
1529 		tx_parity_cfg &= ~PAR_CALC_EN;
1530 		rx_parity_cfg &= ~PAR_CALC_EN;
1531 	}
1532 
1533 	/* bits per char */
1534 	bits_per_char = tty_get_char_size(termios->c_cflag);
1535 
1536 	/* stop bits */
1537 	if (termios->c_cflag & CSTOPB)
1538 		stop_bit_len = TX_STOP_BIT_LEN_2;
1539 	else
1540 		stop_bit_len = TX_STOP_BIT_LEN_1;
1541 
1542 	/* Configure flow control based on CRTSCTS flag.
1543 	 * When CRTSCTS is set, use HW/auto flow control mode, where HW
1544 	 * controls the RTS/CTS pin based FIFO state.
1545 	 * When CRTSCTS is clear, the CTS pin value is ignored for TX
1546 	 * path and RTS pin can be set/cleared using registers, for RX
1547 	 * path.
1548 	 */
1549 
1550 	if (termios->c_cflag & CRTSCTS) {
1551 		tx_trans_cfg &= ~UART_CTS_MASK;
1552 		port->manual_flow = false;
1553 	} else {
1554 		tx_trans_cfg |= UART_CTS_MASK;
1555 		port->manual_flow = true;
1556 	}
1557 
1558 	if (baud) {
1559 		uart_update_timeout(uport, termios->c_cflag, baud);
1560 
1561 		/*
1562 		 * Make sure that qcom_geni_serial_poll_bitfield() waits for
1563 		 * the FIFO, two-word intermediate transfer register and shift
1564 		 * register to clear.
1565 		 *
1566 		 * Note that uart_fifo_timeout() also adds a 20 ms margin.
1567 		 */
1568 		timeout = jiffies_to_usecs(uart_fifo_timeout(uport));
1569 		timeout += 3 * timeout / port->tx_fifo_depth;
1570 		WRITE_ONCE(port->poll_timeout_us, timeout);
1571 	}
1572 
1573 	if (!uart_console(uport))
1574 		writel(port->loopback,
1575 				uport->membase + SE_UART_LOOPBACK_CFG);
1576 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1577 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1578 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1579 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1580 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1581 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1582 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1583 
1584 	trace_geni_serial_set_termios(uport->dev, baud, bits_per_char,
1585 				      tx_trans_cfg, tx_parity_cfg, rx_trans_cfg,
1586 				      rx_parity_cfg, stop_bit_len);
1587 }
1588 
1589 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1590 static int qcom_geni_console_setup(struct console *co, char *options)
1591 {
1592 	struct uart_port *uport;
1593 	struct qcom_geni_serial_port *port;
1594 	int baud = 115200;
1595 	int bits = 8;
1596 	int parity = 'n';
1597 	int flow = 'n';
1598 	int ret;
1599 
1600 	if (co->index >= GENI_UART_CONS_PORTS  || co->index < 0)
1601 		return -ENXIO;
1602 
1603 	port = get_port_from_line(co->index, true, NULL);
1604 	if (IS_ERR(port)) {
1605 		pr_err("Invalid line %d\n", co->index);
1606 		return PTR_ERR(port);
1607 	}
1608 
1609 	uport = &port->uport;
1610 
1611 	if (unlikely(!uport->membase))
1612 		return -ENXIO;
1613 
1614 	ret = pm_runtime_resume_and_get(uport->dev);
1615 	if (ret < 0)
1616 		return ret;
1617 
1618 	if (!port->setup) {
1619 		ret = qcom_geni_serial_port_setup(uport);
1620 		if (ret) {
1621 			pm_runtime_put_sync(uport->dev);
1622 			return ret;
1623 		}
1624 	}
1625 
1626 	if (options)
1627 		uart_parse_options(options, &baud, &parity, &bits, &flow);
1628 
1629 	ret = uart_set_options(uport, co, baud, parity, bits, flow);
1630 	if (ret)
1631 		pm_runtime_put_sync(uport->dev);
1632 
1633 	return ret;
1634 }
1635 
1636 static int qcom_geni_console_exit(struct console *co)
1637 {
1638 	struct qcom_geni_serial_port *port;
1639 
1640 	port = get_port_from_line(co->index, true, NULL);
1641 	if (IS_ERR(port))
1642 		return PTR_ERR(port);
1643 
1644 	return pm_runtime_put_sync(port->uport.dev);
1645 }
1646 
1647 static void qcom_geni_serial_earlycon_write(struct console *con,
1648 					const char *s, unsigned int n)
1649 {
1650 	struct earlycon_device *dev = con->data;
1651 
1652 	__qcom_geni_serial_console_write(&dev->port, s, n, false);
1653 }
1654 
1655 #ifdef CONFIG_CONSOLE_POLL
1656 static int qcom_geni_serial_earlycon_read(struct console *con,
1657 					  char *s, unsigned int n)
1658 {
1659 	struct earlycon_device *dev = con->data;
1660 	struct uart_port *uport = &dev->port;
1661 	int num_read = 0;
1662 	int ch;
1663 
1664 	while (num_read < n) {
1665 		ch = qcom_geni_serial_get_char(uport);
1666 		if (ch == NO_POLL_CHAR)
1667 			break;
1668 		s[num_read++] = ch;
1669 	}
1670 
1671 	return num_read;
1672 }
1673 
1674 static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1675 						      struct console *con)
1676 {
1677 	geni_se_setup_s_cmd(se, UART_START_READ, 0);
1678 	con->read = qcom_geni_serial_earlycon_read;
1679 }
1680 #else
1681 static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1682 						      struct console *con) { }
1683 #endif
1684 
1685 static struct qcom_geni_private_data earlycon_private_data;
1686 
1687 static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
1688 								const char *opt)
1689 {
1690 	struct uart_port *uport = &dev->port;
1691 	u32 tx_trans_cfg;
1692 	u32 tx_parity_cfg = 0;	/* Disable Tx Parity */
1693 	u32 rx_trans_cfg = 0;
1694 	u32 rx_parity_cfg = 0;	/* Disable Rx Parity */
1695 	u32 stop_bit_len = 0;	/* Default stop bit length - 1 bit */
1696 	u32 bits_per_char;
1697 	struct geni_se se;
1698 
1699 	if (!uport->membase)
1700 		return -EINVAL;
1701 
1702 	uport->private_data = &earlycon_private_data;
1703 
1704 	memset(&se, 0, sizeof(se));
1705 	se.base = uport->membase;
1706 	if (geni_se_read_proto(&se) != GENI_SE_UART)
1707 		return -ENXIO;
1708 	/*
1709 	 * Ignore Flow control.
1710 	 * n = 8.
1711 	 */
1712 	tx_trans_cfg = UART_CTS_MASK;
1713 	bits_per_char = BITS_PER_BYTE;
1714 
1715 	/*
1716 	 * Make an unconditional cancel on the main sequencer to reset
1717 	 * it else we could end up in data loss scenarios.
1718 	 */
1719 	qcom_geni_serial_poll_tx_done(uport);
1720 	qcom_geni_serial_abort_rx(uport);
1721 	geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1722 			       false, true, true);
1723 	geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
1724 	geni_se_select_mode(&se, GENI_SE_FIFO);
1725 
1726 	writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1727 	writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1728 	writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1729 	writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1730 	writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1731 	writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1732 	writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1733 
1734 	dev->con->write = qcom_geni_serial_earlycon_write;
1735 	dev->con->setup = NULL;
1736 	qcom_geni_serial_enable_early_read(&se, dev->con);
1737 
1738 	return 0;
1739 }
1740 OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
1741 				qcom_geni_serial_earlycon_setup);
1742 
1743 static int __init console_register(struct uart_driver *drv)
1744 {
1745 	return uart_register_driver(drv);
1746 }
1747 
1748 static void console_unregister(struct uart_driver *drv)
1749 {
1750 	uart_unregister_driver(drv);
1751 }
1752 
1753 static struct console cons_ops = {
1754 	.name = "ttyMSM",
1755 	.write_atomic = qcom_geni_serial_console_write_atomic,
1756 	.write_thread = qcom_geni_serial_console_write_thread,
1757 	.device_lock = qcom_geni_serial_console_device_lock,
1758 	.device_unlock = qcom_geni_serial_console_device_unlock,
1759 	.device = uart_console_device,
1760 	.setup = qcom_geni_console_setup,
1761 	.exit = qcom_geni_console_exit,
1762 	.flags = CON_PRINTBUFFER | CON_NBCON,
1763 	.index = -1,
1764 	.data = &qcom_geni_console_driver,
1765 };
1766 
1767 static struct uart_driver qcom_geni_console_driver = {
1768 	.owner = THIS_MODULE,
1769 	.driver_name = "qcom_geni_console",
1770 	.dev_name = "ttyMSM",
1771 	.nr =  GENI_UART_CONS_PORTS,
1772 	.cons = &cons_ops,
1773 };
1774 #else
1775 static int console_register(struct uart_driver *drv)
1776 {
1777 	return 0;
1778 }
1779 
1780 static void console_unregister(struct uart_driver *drv)
1781 {
1782 }
1783 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1784 
1785 static struct uart_driver qcom_geni_uart_driver = {
1786 	.owner = THIS_MODULE,
1787 	.driver_name = "qcom_geni_uart",
1788 	.dev_name = "ttyHS",
1789 	.nr = CONFIG_SERIAL_QCOM_GENI_UART_PORTS,
1790 };
1791 
1792 /**
1793  * qcom_geni_rs485_config - Configure RS485 settings for the UART port
1794  * @uport: Pointer to the UART port structure
1795  * @termios: Pointer to the termios structure
1796  * @rs485: Pointer to the RS485 configuration structure
1797  * This function configures the RTS (Request to Send) pin behavior for RS485 mode.
1798  * When RS485 mode is enabled, the RTS pin is kept in default ACTIVE HIGH state.
1799  * Return: Always returns 0.
1800  */
1801 
1802 static int qcom_geni_rs485_config(struct uart_port *uport,
1803 				  struct ktermios *termios, struct serial_rs485 *rs485)
1804 {
1805 	qcom_geni_set_rs485_mode(uport, SER_RS485_ENABLED);
1806 
1807 	return 0;
1808 }
1809 
1810 static const struct uart_ops qcom_geni_console_pops = {
1811 	.tx_empty = qcom_geni_serial_tx_empty,
1812 	.stop_tx = qcom_geni_serial_stop_tx_fifo,
1813 	.start_tx = qcom_geni_serial_start_tx_fifo,
1814 	.stop_rx = qcom_geni_serial_stop_rx_fifo,
1815 	.start_rx = qcom_geni_serial_start_rx_fifo,
1816 	.set_termios = qcom_geni_serial_set_termios,
1817 	.startup = qcom_geni_serial_startup,
1818 	.request_port = qcom_geni_serial_request_port,
1819 	.config_port = qcom_geni_serial_config_port,
1820 	.shutdown = qcom_geni_serial_shutdown,
1821 	.flush_buffer = qcom_geni_serial_flush_buffer_fifo,
1822 	.type = qcom_geni_serial_get_type,
1823 	.set_mctrl = qcom_geni_serial_set_mctrl,
1824 	.get_mctrl = qcom_geni_serial_get_mctrl,
1825 #ifdef CONFIG_CONSOLE_POLL
1826 	.poll_get_char	= qcom_geni_serial_get_char,
1827 	.poll_put_char	= qcom_geni_serial_poll_put_char,
1828 	.poll_init = qcom_geni_serial_poll_init,
1829 #endif
1830 };
1831 
1832 static const struct uart_ops qcom_geni_uart_pops = {
1833 	.tx_empty = qcom_geni_serial_tx_empty,
1834 	.stop_tx = qcom_geni_serial_stop_tx_dma,
1835 	.start_tx = qcom_geni_serial_start_tx_dma,
1836 	.start_rx = qcom_geni_serial_start_rx_dma,
1837 	.stop_rx = qcom_geni_serial_stop_rx_dma,
1838 	.set_termios = qcom_geni_serial_set_termios,
1839 	.startup = qcom_geni_serial_startup,
1840 	.request_port = qcom_geni_serial_request_port,
1841 	.config_port = qcom_geni_serial_config_port,
1842 	.shutdown = qcom_geni_serial_shutdown,
1843 	.flush_buffer = qcom_geni_serial_flush_buffer_dma,
1844 	.type = qcom_geni_serial_get_type,
1845 	.set_mctrl = qcom_geni_serial_set_mctrl,
1846 	.get_mctrl = qcom_geni_serial_get_mctrl,
1847 };
1848 
1849 static int qcom_geni_serial_panic_notifier(struct notifier_block *nb,
1850 					   unsigned long action, void *data)
1851 {
1852 	struct qcom_geni_serial_port *port =
1853 		container_of(nb, struct qcom_geni_serial_port, panic_nb);
1854 	struct uart_port *uport = &port->uport;
1855 
1856 	if (pm_runtime_status_suspended(uport->dev))
1857 		return NOTIFY_OK;
1858 
1859 	qcom_geni_serial_stop_tx(uport);
1860 	qcom_geni_serial_stop_rx(uport);
1861 
1862 	return NOTIFY_OK;
1863 }
1864 
1865 static int qcom_geni_serial_probe(struct platform_device *pdev)
1866 {
1867 	int ret = 0;
1868 	int line;
1869 	struct qcom_geni_serial_port *port;
1870 	struct uart_port *uport;
1871 	struct resource *res;
1872 	int irq;
1873 	struct uart_driver *drv;
1874 	const struct qcom_geni_device_data *data;
1875 
1876 	data = of_device_get_match_data(&pdev->dev);
1877 	if (!data)
1878 		return -EINVAL;
1879 
1880 	if (data->console) {
1881 		drv = &qcom_geni_console_driver;
1882 		line = of_alias_get_id(pdev->dev.of_node, "serial");
1883 	} else {
1884 		drv = &qcom_geni_uart_driver;
1885 		line = of_alias_get_id(pdev->dev.of_node, "serial");
1886 		if (line == -ENODEV) /* compat with non-standard aliases */
1887 			line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1888 	}
1889 
1890 	port = get_port_from_line(line, data->console, &pdev->dev);
1891 	if (IS_ERR(port)) {
1892 		dev_err(&pdev->dev, "Invalid line %d\n", line);
1893 		return PTR_ERR(port);
1894 	}
1895 
1896 	uport = &port->uport;
1897 	/* Don't allow 2 drivers to access the same port */
1898 	if (uport->private_data)
1899 		return -ENODEV;
1900 
1901 	uport->dev = &pdev->dev;
1902 	port->dev_data = data;
1903 	port->se.dev = &pdev->dev;
1904 	port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1905 
1906 	ret = port->dev_data->resources_init(&port->se);
1907 	if (ret)
1908 		goto error;
1909 
1910 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1911 	if (!res) {
1912 		ret = -EINVAL;
1913 		goto error;
1914 	}
1915 
1916 	uport->mapbase = res->start;
1917 
1918 	uport->rs485_config = qcom_geni_rs485_config;
1919 	uport->rs485_supported = qcom_geni_rs485_supported;
1920 	port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1921 	port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1922 	port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1923 
1924 	if (!data->console) {
1925 		port->rx_buf = devm_kzalloc(uport->dev,
1926 					    DMA_RX_BUF_SIZE, GFP_KERNEL);
1927 		if (!port->rx_buf) {
1928 			ret = -ENOMEM;
1929 			goto error;
1930 		}
1931 
1932 		port->rx_dma_addr = dma_map_single(pdev->dev.parent, port->rx_buf,
1933 						   DMA_RX_BUF_SIZE, DMA_FROM_DEVICE);
1934 		if (dma_mapping_error(pdev->dev.parent, port->rx_dma_addr)) {
1935 			ret = -EIO;
1936 			dev_err(&pdev->dev, "Failed to map RX DMA buffer: %d\n", ret);
1937 			goto error;
1938 		}
1939 	}
1940 
1941 	port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1942 			"qcom_geni_serial_%s%d",
1943 			uart_console(uport) ? "console" : "uart", uport->line);
1944 	if (!port->name) {
1945 		ret = -ENOMEM;
1946 		goto error;
1947 	}
1948 
1949 	irq = platform_get_irq(pdev, 0);
1950 	if (irq < 0) {
1951 		ret = irq;
1952 		goto error;
1953 	}
1954 
1955 	uport->irq = irq;
1956 	uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1957 
1958 	if (!data->console) {
1959 		port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1960 		if (port->wakeup_irq < 0 && port->wakeup_irq != -ENXIO) {
1961 			ret = port->wakeup_irq;
1962 			goto error;
1963 		}
1964 	}
1965 
1966 	if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1967 		port->rx_tx_swap = true;
1968 
1969 	if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1970 		port->cts_rts_swap = true;
1971 
1972 	port->private_data.drv = drv;
1973 	uport->private_data = &port->private_data;
1974 	init_irq_work(&port->tx_kick, qcom_geni_serial_restart_tx);
1975 	platform_set_drvdata(pdev, port);
1976 
1977 	irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1978 	ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1979 			IRQF_TRIGGER_HIGH, port->name, uport);
1980 	if (ret) {
1981 		dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1982 		goto error;
1983 	}
1984 
1985 	ret = uart_get_rs485_mode(uport);
1986 	if (ret)
1987 		goto error;
1988 
1989 	if (port->wakeup_irq > 0) {
1990 		device_init_wakeup(&pdev->dev, true);
1991 		ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1992 						port->wakeup_irq);
1993 		if (ret) {
1994 			device_init_wakeup(&pdev->dev, false);
1995 			goto error;
1996 		}
1997 	}
1998 
1999 	devm_pm_runtime_enable(port->se.dev);
2000 
2001 	ret = pm_runtime_resume_and_get(uport->dev);
2002 	if (ret < 0) {
2003 		dev_err(uport->dev, "Failed to resume and get %d\n", ret);
2004 		goto error;
2005 	}
2006 
2007 	ret = uart_add_one_port(drv, uport);
2008 	pm_runtime_put(uport->dev);
2009 	if (ret)
2010 		goto error;
2011 
2012 	port->panic_nb.notifier_call = qcom_geni_serial_panic_notifier;
2013 	atomic_notifier_chain_register(&panic_notifier_list, &port->panic_nb);
2014 
2015 	return 0;
2016 
2017 error:
2018 	if (!data->console)
2019 		ida_free(&port_ida, uport->line);
2020 	if (port->rx_dma_addr) {
2021 		dma_unmap_single(pdev->dev.parent, port->rx_dma_addr,
2022 				 DMA_RX_BUF_SIZE, DMA_FROM_DEVICE);
2023 		port->rx_dma_addr = 0;
2024 	}
2025 	return ret;
2026 }
2027 
2028 static void qcom_geni_serial_remove(struct platform_device *pdev)
2029 {
2030 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
2031 	struct uart_port *uport = &port->uport;
2032 	struct uart_driver *drv = port->private_data.drv;
2033 
2034 	atomic_notifier_chain_unregister(&panic_notifier_list, &port->panic_nb);
2035 
2036 	WRITE_ONCE(port->tx_kick_enabled, false);
2037 	irq_work_sync(&port->tx_kick);
2038 	dev_pm_clear_wake_irq(&pdev->dev);
2039 	device_init_wakeup(&pdev->dev, false);
2040 	if (!port->dev_data->console)
2041 		ida_free(&port_ida, uport->line);
2042 	uart_remove_one_port(drv, &port->uport);
2043 
2044 	if (port->rx_dma_addr) {
2045 		dma_unmap_single(pdev->dev.parent, port->rx_dma_addr,
2046 				 DMA_RX_BUF_SIZE, DMA_FROM_DEVICE);
2047 		port->rx_dma_addr = 0;
2048 	}
2049 }
2050 
2051 static int __maybe_unused qcom_geni_serial_runtime_suspend(struct device *dev)
2052 {
2053 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
2054 
2055 	return port->dev_data->power_off ?
2056 	       port->dev_data->power_off(&port->se) : 0;
2057 }
2058 
2059 static int __maybe_unused qcom_geni_serial_runtime_resume(struct device *dev)
2060 {
2061 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
2062 	struct uart_port *uport = &port->uport;
2063 	int ret;
2064 
2065 	if (port->dev_data->power_on) {
2066 		ret = port->dev_data->power_on(&port->se);
2067 		if (ret)
2068 			return ret;
2069 	}
2070 
2071 	if (port->se.has_opp && port->clk_rate)
2072 		return dev_pm_opp_set_rate(uport->dev, port->clk_rate);
2073 
2074 	return 0;
2075 }
2076 
2077 static int qcom_geni_serial_suspend(struct device *dev)
2078 {
2079 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
2080 	struct uart_port *uport = &port->uport;
2081 	struct qcom_geni_private_data *private_data = uport->private_data;
2082 	int ret;
2083 
2084 	/*
2085 	 * This is done so we can hit the lowest possible state in suspend
2086 	 * even with no_console_suspend
2087 	 */
2088 	if (uart_console(uport)) {
2089 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
2090 		geni_icc_set_bw(&port->se);
2091 	}
2092 
2093 	ret = uart_suspend_port(private_data->drv, uport);
2094 	if (ret)
2095 		return ret;
2096 
2097 	/*
2098 	 * When no_console_suspend is set the console must remain active
2099 	 * across system sleep, so skip the force suspend path.
2100 	 */
2101 	if (!console_suspend_enabled && uart_console(uport))
2102 		return 0;
2103 
2104 	return pm_runtime_force_suspend(dev);
2105 }
2106 
2107 static int qcom_geni_serial_resume(struct device *dev)
2108 {
2109 	int ret;
2110 	struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
2111 	struct uart_port *uport = &port->uport;
2112 	struct qcom_geni_private_data *private_data = uport->private_data;
2113 
2114 	if (console_suspend_enabled || !uart_console(uport)) {
2115 		ret = pm_runtime_force_resume(dev);
2116 		if (ret)
2117 			return ret;
2118 	}
2119 
2120 	if (console_suspend_enabled && uart_console(uport)) {
2121 		ret = pm_runtime_resume(dev);
2122 		if (ret < 0)
2123 			return ret;
2124 	}
2125 
2126 	ret = uart_resume_port(private_data->drv, uport);
2127 	if (uart_console(uport)) {
2128 		geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
2129 		geni_icc_set_bw(&port->se);
2130 	}
2131 	return ret;
2132 }
2133 
2134 #if IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE)
2135 static const struct qcom_geni_device_data qcom_geni_console_data = {
2136 	.console = true,
2137 	.mode = GENI_SE_FIFO,
2138 	.resources_init = geni_se_resources_init,
2139 	.set_rate = geni_serial_set_rate,
2140 	.power_on = geni_se_resources_activate,
2141 	.power_off = geni_se_resources_deactivate,
2142 };
2143 
2144 static const struct qcom_geni_device_data sa8255p_qcom_geni_console_data = {
2145 	.console = true,
2146 	.mode = GENI_SE_FIFO,
2147 	.resources_init = geni_se_domain_attach,
2148 	.set_rate = geni_se_set_perf_level,
2149 };
2150 #endif
2151 
2152 static const struct qcom_geni_device_data qcom_geni_uart_data = {
2153 	.console = false,
2154 	.mode = GENI_SE_DMA,
2155 	.resources_init = geni_se_resources_init,
2156 	.set_rate = geni_serial_set_rate,
2157 	.power_on = geni_se_resources_activate,
2158 	.power_off = geni_se_resources_deactivate,
2159 };
2160 
2161 static const struct qcom_geni_device_data sa8255p_qcom_geni_uart_data = {
2162 	.console = false,
2163 	.mode = GENI_SE_DMA,
2164 	.resources_init = geni_se_domain_attach,
2165 	.set_rate = geni_se_set_perf_level,
2166 };
2167 
2168 static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
2169 	SET_RUNTIME_PM_OPS(qcom_geni_serial_runtime_suspend,
2170 			   qcom_geni_serial_runtime_resume, NULL)
2171 	SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_suspend, qcom_geni_serial_resume)
2172 };
2173 
2174 static void qcom_geni_serial_sys_shutdown(struct platform_device *pdev)
2175 {
2176 	struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
2177 	struct uart_port *uport = &port->uport;
2178 
2179 	if (pm_runtime_status_suspended(uport->dev))
2180 		return;
2181 
2182 	qcom_geni_serial_stop_tx(uport);
2183 	qcom_geni_serial_stop_rx(uport);
2184 }
2185 
2186 static const struct of_device_id qcom_geni_serial_match_table[] = {
2187 #if IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE)
2188 	{
2189 		.compatible = "qcom,geni-debug-uart",
2190 		.data = &qcom_geni_console_data,
2191 	},
2192 	{
2193 		.compatible = "qcom,sa8255p-geni-debug-uart",
2194 		.data = &sa8255p_qcom_geni_console_data,
2195 	},
2196 #endif
2197 	{
2198 		.compatible = "qcom,geni-uart",
2199 		.data = &qcom_geni_uart_data,
2200 	},
2201 	{
2202 		.compatible = "qcom,sa8255p-geni-uart",
2203 		.data = &sa8255p_qcom_geni_uart_data,
2204 	},
2205 	{}
2206 };
2207 MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
2208 
2209 static struct platform_driver qcom_geni_serial_platform_driver = {
2210 	.remove = qcom_geni_serial_remove,
2211 	.probe = qcom_geni_serial_probe,
2212 	.shutdown = qcom_geni_serial_sys_shutdown,
2213 	.driver = {
2214 		.name = "qcom_geni_serial",
2215 		.of_match_table = qcom_geni_serial_match_table,
2216 		.pm = &qcom_geni_serial_pm_ops,
2217 	},
2218 };
2219 
2220 static int __init qcom_geni_serial_init(void)
2221 {
2222 	int ret;
2223 
2224 	ret = console_register(&qcom_geni_console_driver);
2225 	if (ret)
2226 		return ret;
2227 
2228 	ret = uart_register_driver(&qcom_geni_uart_driver);
2229 	if (ret) {
2230 		console_unregister(&qcom_geni_console_driver);
2231 		return ret;
2232 	}
2233 
2234 	ret = platform_driver_register(&qcom_geni_serial_platform_driver);
2235 	if (ret) {
2236 		console_unregister(&qcom_geni_console_driver);
2237 		uart_unregister_driver(&qcom_geni_uart_driver);
2238 	}
2239 	return ret;
2240 }
2241 module_init(qcom_geni_serial_init);
2242 
2243 static void __exit qcom_geni_serial_exit(void)
2244 {
2245 	platform_driver_unregister(&qcom_geni_serial_platform_driver);
2246 	console_unregister(&qcom_geni_console_driver);
2247 	uart_unregister_driver(&qcom_geni_uart_driver);
2248 }
2249 module_exit(qcom_geni_serial_exit);
2250 
2251 MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
2252 MODULE_LICENSE("GPL v2");
2253