1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
4 /* Disable MMIO tracing to prevent excessive logging of unwanted MMIO traces */
5 #define __DISABLE_TRACE_MMIO__
8 #include <linux/console.h>
10 #include <linux/iopoll.h>
11 #include <linux/irq.h>
12 #include <linux/module.h>
14 #include <linux/pm_opp.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/pm_wakeirq.h>
18 #include <linux/soc/qcom/geni-se.h>
19 #include <linux/serial.h>
20 #include <linux/serial_core.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <dt-bindings/interconnect/qcom,icc.h>
26 /* UART specific GENI registers */
27 #define SE_UART_LOOPBACK_CFG 0x22c
28 #define SE_UART_IO_MACRO_CTRL 0x240
29 #define SE_UART_TX_TRANS_CFG 0x25c
30 #define SE_UART_TX_WORD_LEN 0x268
31 #define SE_UART_TX_STOP_BIT_LEN 0x26c
32 #define SE_UART_TX_TRANS_LEN 0x270
33 #define SE_UART_RX_TRANS_CFG 0x280
34 #define SE_UART_RX_WORD_LEN 0x28c
35 #define SE_UART_RX_STALE_CNT 0x294
36 #define SE_UART_TX_PARITY_CFG 0x2a4
37 #define SE_UART_RX_PARITY_CFG 0x2a8
38 #define SE_UART_MANUAL_RFR 0x2ac
40 /* SE_UART_TRANS_CFG */
41 #define UART_TX_PAR_EN BIT(0)
42 #define UART_CTS_MASK BIT(1)
44 /* SE_UART_TX_STOP_BIT_LEN */
45 #define TX_STOP_BIT_LEN_1 0
46 #define TX_STOP_BIT_LEN_2 2
48 /* SE_UART_RX_TRANS_CFG */
49 #define UART_RX_PAR_EN BIT(3)
51 /* SE_UART_RX_WORD_LEN */
52 #define RX_WORD_LEN_MASK GENMASK(9, 0)
54 /* SE_UART_RX_STALE_CNT */
55 #define RX_STALE_CNT GENMASK(23, 0)
57 /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
58 #define PAR_CALC_EN BIT(0)
61 #define PAR_SPACE 0x10
63 /* SE_UART_MANUAL_RFR register fields */
64 #define UART_MANUAL_RFR_EN BIT(31)
65 #define UART_RFR_NOT_READY BIT(1)
66 #define UART_RFR_READY BIT(0)
68 /* UART M_CMD OP codes */
69 #define UART_START_TX 0x1
70 /* UART S_CMD OP codes */
71 #define UART_START_READ 0x1
72 #define UART_PARAM 0x1
73 #define UART_PARAM_RFR_OPEN BIT(7)
75 #define UART_OVERSAMPLING 32
76 #define STALE_TIMEOUT 16
77 #define DEFAULT_BITS_PER_CHAR 10
78 #define GENI_UART_CONS_PORTS 1
79 #define GENI_UART_PORTS 3
80 #define DEF_FIFO_DEPTH_WORDS 16
82 #define DEF_FIFO_WIDTH_BITS 32
85 /* SE_UART_LOOPBACK_CFG */
86 #define RX_TX_SORTED BIT(0)
87 #define CTS_RTS_SORTED BIT(1)
88 #define RX_TX_CTS_RTS_SORTED (RX_TX_SORTED | CTS_RTS_SORTED)
90 /* UART pin swap value */
91 #define DEFAULT_IO_MACRO_IO0_IO1_MASK GENMASK(3, 0)
92 #define IO_MACRO_IO0_SEL 0x3
93 #define DEFAULT_IO_MACRO_IO2_IO3_MASK GENMASK(15, 4)
94 #define IO_MACRO_IO2_IO3_SWAP 0x4640
96 /* We always configure 4 bytes per FIFO word */
97 #define BYTES_PER_FIFO_WORD 4U
99 #define DMA_RX_BUF_SIZE 2048
101 struct qcom_geni_device_data {
103 enum geni_se_xfer_mode mode;
106 struct qcom_geni_private_data {
107 /* NOTE: earlycon port will have NULL here */
108 struct uart_driver *drv;
110 u32 poll_cached_bytes;
111 unsigned int poll_cached_bytes_cnt;
113 u32 write_cached_bytes;
114 unsigned int write_cached_bytes_cnt;
117 struct qcom_geni_serial_port {
118 struct uart_port uport;
124 dma_addr_t tx_dma_addr;
125 dma_addr_t rx_dma_addr;
127 unsigned long poll_timeout_us;
128 unsigned long clk_rate;
133 unsigned int tx_remaining;
134 unsigned int tx_queued;
139 struct qcom_geni_private_data private_data;
140 const struct qcom_geni_device_data *dev_data;
143 static const struct uart_ops qcom_geni_console_pops;
144 static const struct uart_ops qcom_geni_uart_pops;
145 static struct uart_driver qcom_geni_console_driver;
146 static struct uart_driver qcom_geni_uart_driver;
148 static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
149 static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport);
151 static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
153 return container_of(uport, struct qcom_geni_serial_port, uport);
156 static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
160 .ops = &qcom_geni_uart_pops,
161 .flags = UPF_BOOT_AUTOCONF,
168 .ops = &qcom_geni_uart_pops,
169 .flags = UPF_BOOT_AUTOCONF,
176 .ops = &qcom_geni_uart_pops,
177 .flags = UPF_BOOT_AUTOCONF,
183 static struct qcom_geni_serial_port qcom_geni_console_port = {
186 .ops = &qcom_geni_console_pops,
187 .flags = UPF_BOOT_AUTOCONF,
192 static int qcom_geni_serial_request_port(struct uart_port *uport)
194 struct platform_device *pdev = to_platform_device(uport->dev);
195 struct qcom_geni_serial_port *port = to_dev_port(uport);
197 uport->membase = devm_platform_ioremap_resource(pdev, 0);
198 if (IS_ERR(uport->membase))
199 return PTR_ERR(uport->membase);
200 port->se.base = uport->membase;
204 static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
206 if (cfg_flags & UART_CONFIG_TYPE) {
207 uport->type = PORT_MSM;
208 qcom_geni_serial_request_port(uport);
212 static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
214 unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
217 if (uart_console(uport)) {
220 geni_ios = readl(uport->membase + SE_GENI_IOS);
221 if (!(geni_ios & IO2_DATA_IN))
228 static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
231 u32 uart_manual_rfr = 0;
232 struct qcom_geni_serial_port *port = to_dev_port(uport);
234 if (uart_console(uport))
237 if (mctrl & TIOCM_LOOP)
238 port->loopback = RX_TX_CTS_RTS_SORTED;
240 if (!(mctrl & TIOCM_RTS) && !uport->suspended)
241 uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
242 writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
245 static const char *qcom_geni_serial_get_type(struct uart_port *uport)
250 static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
252 struct qcom_geni_serial_port *port;
253 int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
255 if (line < 0 || line >= nr_ports)
256 return ERR_PTR(-ENXIO);
258 port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
262 static bool qcom_geni_serial_main_active(struct uart_port *uport)
264 return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
267 static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
269 return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
272 static bool qcom_geni_serial_poll_bitfield(struct uart_port *uport,
273 unsigned int offset, u32 field, u32 val)
276 struct qcom_geni_serial_port *port;
277 unsigned long timeout_us = 20000;
278 struct qcom_geni_private_data *private_data = uport->private_data;
280 if (private_data->drv) {
281 port = to_dev_port(uport);
282 if (port->poll_timeout_us)
283 timeout_us = port->poll_timeout_us;
287 * Use custom implementation instead of readl_poll_atomic since ktimer
288 * is not ready at the time of early console.
290 timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
292 reg = readl(uport->membase + offset);
293 if ((reg & field) == val)
301 static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
302 unsigned int offset, u32 field, bool set)
304 return qcom_geni_serial_poll_bitfield(uport, offset, field, set ? field : 0);
307 static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
311 writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
312 m_cmd = UART_START_TX << M_OPCODE_SHFT;
313 writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
316 static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
320 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
321 M_CMD_DONE_EN, true);
323 writel(M_GENI_CMD_ABORT, uport->membase +
324 SE_GENI_M_CMD_CTRL_REG);
325 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
326 M_CMD_ABORT_EN, true);
327 writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
331 static void qcom_geni_serial_abort_rx(struct uart_port *uport)
333 u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
335 writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
336 qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
337 S_GENI_CMD_ABORT, false);
338 writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
339 writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
342 #ifdef CONFIG_CONSOLE_POLL
343 static int qcom_geni_serial_get_char(struct uart_port *uport)
345 struct qcom_geni_private_data *private_data = uport->private_data;
350 if (!private_data->poll_cached_bytes_cnt) {
351 status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
352 writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
354 status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
355 writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
357 status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
358 word_cnt = status & RX_FIFO_WC_MSK;
362 if (word_cnt == 1 && (status & RX_LAST))
364 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
365 * treated as if it was BYTES_PER_FIFO_WORD.
367 private_data->poll_cached_bytes_cnt =
368 (status & RX_LAST_BYTE_VALID_MSK) >>
369 RX_LAST_BYTE_VALID_SHFT;
371 if (private_data->poll_cached_bytes_cnt == 0)
372 private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
374 private_data->poll_cached_bytes =
375 readl(uport->membase + SE_GENI_RX_FIFOn);
378 private_data->poll_cached_bytes_cnt--;
379 ret = private_data->poll_cached_bytes & 0xff;
380 private_data->poll_cached_bytes >>= 8;
385 static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
388 if (qcom_geni_serial_main_active(uport)) {
389 qcom_geni_serial_poll_tx_done(uport);
390 __qcom_geni_serial_cancel_tx_cmd(uport);
393 writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
394 qcom_geni_serial_setup_tx(uport, 1);
395 writel(c, uport->membase + SE_GENI_TX_FIFOn);
396 qcom_geni_serial_poll_tx_done(uport);
400 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
401 static void qcom_geni_serial_drain_fifo(struct uart_port *uport)
403 struct qcom_geni_serial_port *port = to_dev_port(uport);
405 qcom_geni_serial_poll_bitfield(uport, SE_GENI_M_GP_LENGTH, GP_LENGTH,
409 static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
411 struct qcom_geni_private_data *private_data = uport->private_data;
413 private_data->write_cached_bytes =
414 (private_data->write_cached_bytes >> 8) | (ch << 24);
415 private_data->write_cached_bytes_cnt++;
417 if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
418 writel(private_data->write_cached_bytes,
419 uport->membase + SE_GENI_TX_FIFOn);
420 private_data->write_cached_bytes_cnt = 0;
425 __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
428 struct qcom_geni_private_data *private_data = uport->private_data;
431 u32 bytes_to_send = count;
433 for (i = 0; i < count; i++) {
435 * uart_console_write() adds a carriage return for each newline.
436 * Account for additional bytes to be written.
442 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
443 writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
444 qcom_geni_serial_setup_tx(uport, bytes_to_send);
445 for (i = 0; i < count; ) {
446 size_t chars_to_write = 0;
447 size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
450 * If the WM bit never set, then the Tx state machine is not
451 * in a valid state, so break, cancel/abort any existing
452 * command. Unfortunately the current data being written is
455 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
456 M_TX_FIFO_WATERMARK_EN, true))
458 chars_to_write = min_t(size_t, count - i, avail / 2);
459 uart_console_write(uport, s + i, chars_to_write,
460 qcom_geni_serial_wr_char);
461 writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
462 SE_GENI_M_IRQ_CLEAR);
466 if (private_data->write_cached_bytes_cnt) {
467 private_data->write_cached_bytes >>= BITS_PER_BYTE *
468 (BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
469 writel(private_data->write_cached_bytes,
470 uport->membase + SE_GENI_TX_FIFOn);
471 private_data->write_cached_bytes_cnt = 0;
474 qcom_geni_serial_poll_tx_done(uport);
477 static void qcom_geni_serial_console_write(struct console *co, const char *s,
480 struct uart_port *uport;
481 struct qcom_geni_serial_port *port;
482 u32 m_irq_en, s_irq_en;
486 WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
488 port = get_port_from_line(co->index, true);
492 uport = &port->uport;
493 if (oops_in_progress)
494 locked = uart_port_trylock_irqsave(uport, &flags);
496 uart_port_lock_irqsave(uport, &flags);
498 m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
499 s_irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
500 writel(0, uport->membase + SE_GENI_M_IRQ_EN);
501 writel(0, uport->membase + SE_GENI_S_IRQ_EN);
503 if (qcom_geni_serial_main_active(uport)) {
504 /* Wait for completion or drain FIFO */
505 if (!locked || port->tx_remaining == 0)
506 qcom_geni_serial_poll_tx_done(uport);
508 qcom_geni_serial_drain_fifo(uport);
510 qcom_geni_serial_cancel_tx_cmd(uport);
513 __qcom_geni_serial_console_write(uport, s, count);
515 writel(m_irq_en, uport->membase + SE_GENI_M_IRQ_EN);
516 writel(s_irq_en, uport->membase + SE_GENI_S_IRQ_EN);
519 uart_port_unlock_irqrestore(uport, flags);
522 static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
525 unsigned char buf[sizeof(u32)];
526 struct tty_port *tport;
527 struct qcom_geni_serial_port *port = to_dev_port(uport);
529 tport = &uport->state->port;
530 for (i = 0; i < bytes; ) {
532 int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
534 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
539 for (c = 0; c < chunk; c++) {
543 if (port->brk && buf[c] == 0) {
545 if (uart_handle_break(uport))
549 sysrq = uart_prepare_sysrq_char(uport, buf[c]);
552 tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
556 tty_flip_buffer_push(tport);
559 static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
563 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
565 static void handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
567 struct qcom_geni_serial_port *port = to_dev_port(uport);
568 struct tty_port *tport = &uport->state->port;
571 ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
573 dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
574 __func__, ret, bytes);
577 uport->icount.rx += ret;
578 tty_flip_buffer_push(tport);
581 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
583 return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
586 static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
588 struct qcom_geni_serial_port *port = to_dev_port(uport);
591 if (!qcom_geni_serial_main_active(uport))
594 if (port->tx_dma_addr) {
595 geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
597 port->tx_dma_addr = 0;
598 port->tx_remaining = 0;
601 geni_se_cancel_m_cmd(&port->se);
603 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
604 M_CMD_CANCEL_EN, true);
606 geni_se_abort_m_cmd(&port->se);
607 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
608 M_CMD_ABORT_EN, true);
610 dev_err_ratelimited(uport->dev, "M_CMD_ABORT_EN not set");
611 writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
614 writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
617 static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
619 struct qcom_geni_serial_port *port = to_dev_port(uport);
620 struct tty_port *tport = &uport->state->port;
621 unsigned int xmit_size;
625 if (port->tx_dma_addr)
628 if (kfifo_is_empty(&tport->xmit_fifo))
631 xmit_size = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail,
634 qcom_geni_serial_setup_tx(uport, xmit_size);
636 ret = geni_se_tx_dma_prep(&port->se, tail, xmit_size,
639 dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
640 qcom_geni_serial_stop_tx_dma(uport);
644 port->tx_remaining = xmit_size;
647 static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
653 * Start a new transfer in case the previous command was cancelled and
654 * left data in the FIFO which may prevent the watermark interrupt
655 * from triggering. Note that the stale data is discarded.
657 if (!qcom_geni_serial_main_active(uport) &&
658 !qcom_geni_serial_tx_empty(uport)) {
659 if (uart_fifo_out(uport, &c, 1) == 1) {
660 writel(M_CMD_DONE_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
661 qcom_geni_serial_setup_tx(uport, 1);
662 writel(c, uport->membase + SE_GENI_TX_FIFOn);
666 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
667 irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
668 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
669 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
672 static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
676 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
677 irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
678 writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
679 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
682 static void __qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
684 struct qcom_geni_serial_port *port = to_dev_port(uport);
686 geni_se_cancel_m_cmd(&port->se);
687 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
688 M_CMD_CANCEL_EN, true)) {
689 geni_se_abort_m_cmd(&port->se);
690 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
691 M_CMD_ABORT_EN, true);
692 writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
694 writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
697 static void qcom_geni_serial_cancel_tx_cmd(struct uart_port *uport)
699 struct qcom_geni_serial_port *port = to_dev_port(uport);
701 if (!qcom_geni_serial_main_active(uport))
704 __qcom_geni_serial_cancel_tx_cmd(uport);
706 port->tx_remaining = 0;
710 static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
714 u32 last_word_byte_cnt;
715 u32 last_word_partial;
718 status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
719 word_cnt = status & RX_FIFO_WC_MSK;
720 last_word_partial = status & RX_LAST;
721 last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
722 RX_LAST_BYTE_VALID_SHFT;
726 total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
727 if (last_word_partial && last_word_byte_cnt)
728 total_bytes += last_word_byte_cnt;
730 total_bytes += BYTES_PER_FIFO_WORD;
731 handle_rx_console(uport, total_bytes, drop);
734 static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
737 struct qcom_geni_serial_port *port = to_dev_port(uport);
740 irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
741 irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
742 writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
744 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
745 irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
746 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
748 if (!qcom_geni_serial_secondary_active(uport))
751 geni_se_cancel_s_cmd(&port->se);
752 qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
753 S_CMD_CANCEL_EN, true);
755 * If timeout occurs secondary engine remains active
756 * and Abort sequence is executed.
758 s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
759 /* Flush the Rx buffer */
760 if (s_irq_status & S_RX_FIFO_LAST_EN)
761 qcom_geni_serial_handle_rx_fifo(uport, true);
762 writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
764 if (qcom_geni_serial_secondary_active(uport))
765 qcom_geni_serial_abort_rx(uport);
768 static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
771 struct qcom_geni_serial_port *port = to_dev_port(uport);
773 if (qcom_geni_serial_secondary_active(uport))
774 qcom_geni_serial_stop_rx_fifo(uport);
776 geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
778 irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
779 irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
780 writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
782 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
783 irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
784 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
787 static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
789 struct qcom_geni_serial_port *port = to_dev_port(uport);
791 if (!qcom_geni_serial_secondary_active(uport))
794 geni_se_cancel_s_cmd(&port->se);
795 qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
796 S_CMD_CANCEL_EN, true);
798 if (qcom_geni_serial_secondary_active(uport))
799 qcom_geni_serial_abort_rx(uport);
801 if (port->rx_dma_addr) {
802 geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
804 port->rx_dma_addr = 0;
808 static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
810 struct qcom_geni_serial_port *port = to_dev_port(uport);
813 if (qcom_geni_serial_secondary_active(uport))
814 qcom_geni_serial_stop_rx_dma(uport);
816 geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
818 ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
822 dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
823 qcom_geni_serial_stop_rx_dma(uport);
827 static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
829 struct qcom_geni_serial_port *port = to_dev_port(uport);
833 if (!qcom_geni_serial_secondary_active(uport))
836 if (!port->rx_dma_addr)
839 geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
840 port->rx_dma_addr = 0;
842 rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
844 dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
849 handle_rx_uart(uport, rx_in, drop);
851 ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
855 dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
856 qcom_geni_serial_stop_rx_dma(uport);
860 static void qcom_geni_serial_start_rx(struct uart_port *uport)
862 uport->ops->start_rx(uport);
865 static void qcom_geni_serial_stop_rx(struct uart_port *uport)
867 uport->ops->stop_rx(uport);
870 static void qcom_geni_serial_stop_tx(struct uart_port *uport)
872 uport->ops->stop_tx(uport);
875 static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
878 struct qcom_geni_serial_port *port = to_dev_port(uport);
879 unsigned int tx_bytes, remaining = chunk;
880 u8 buf[BYTES_PER_FIFO_WORD];
883 memset(buf, 0, sizeof(buf));
884 tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
886 uart_fifo_out(uport, buf, tx_bytes);
888 iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
890 remaining -= tx_bytes;
891 port->tx_remaining -= tx_bytes;
895 static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
896 bool done, bool active)
898 struct qcom_geni_serial_port *port = to_dev_port(uport);
899 struct tty_port *tport = &uport->state->port;
906 status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
908 /* Complete the current tx command before taking newly added data */
910 pending = port->tx_remaining;
912 pending = kfifo_len(&tport->xmit_fifo);
914 /* All data has been transmitted or command has been cancelled */
915 if (!pending && done) {
916 qcom_geni_serial_stop_tx_fifo(uport);
917 goto out_write_wakeup;
921 avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
923 avail = port->tx_fifo_depth;
925 avail *= BYTES_PER_FIFO_WORD;
927 chunk = min(avail, pending);
929 goto out_write_wakeup;
932 qcom_geni_serial_setup_tx(uport, pending);
933 port->tx_remaining = pending;
936 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
937 if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
938 writel(irq_en | M_TX_FIFO_WATERMARK_EN,
939 uport->membase + SE_GENI_M_IRQ_EN);
942 qcom_geni_serial_send_chunk_fifo(uport, chunk);
943 port->tx_queued += chunk;
946 * The tx fifo watermark is level triggered and latched. Though we had
947 * cleared it in qcom_geni_serial_isr it will have already reasserted
948 * so we must clear it again here after our writes.
950 writel(M_TX_FIFO_WATERMARK_EN,
951 uport->membase + SE_GENI_M_IRQ_CLEAR);
954 if (!port->tx_remaining) {
955 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
956 if (irq_en & M_TX_FIFO_WATERMARK_EN)
957 writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
958 uport->membase + SE_GENI_M_IRQ_EN);
961 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
962 uart_write_wakeup(uport);
965 static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
967 struct qcom_geni_serial_port *port = to_dev_port(uport);
968 struct tty_port *tport = &uport->state->port;
970 uart_xmit_advance(uport, port->tx_remaining);
971 geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
972 port->tx_dma_addr = 0;
973 port->tx_remaining = 0;
975 if (!kfifo_is_empty(&tport->xmit_fifo))
976 qcom_geni_serial_start_tx_dma(uport);
978 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
979 uart_write_wakeup(uport);
982 static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
991 struct uart_port *uport = dev;
992 bool drop_rx = false;
993 struct tty_port *tport = &uport->state->port;
994 struct qcom_geni_serial_port *port = to_dev_port(uport);
996 if (uport->suspended)
999 uart_port_lock(uport);
1001 m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
1002 s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
1003 dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
1004 dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
1005 geni_status = readl(uport->membase + SE_GENI_STATUS);
1006 dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
1007 m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
1008 writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
1009 writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
1010 writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
1011 writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
1013 if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
1016 if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
1017 uport->icount.overrun++;
1018 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
1021 if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
1022 if (s_irq_status & S_GP_IRQ_0_EN)
1023 uport->icount.parity++;
1025 } else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
1026 uport->icount.brk++;
1031 if (dma_tx_status & TX_DMA_DONE)
1032 qcom_geni_serial_handle_tx_dma(uport);
1034 if (dma_rx_status) {
1035 if (dma_rx_status & RX_RESET_DONE)
1038 if (dma_rx_status & RX_DMA_PARITY_ERR) {
1039 uport->icount.parity++;
1043 if (dma_rx_status & RX_DMA_BREAK)
1044 uport->icount.brk++;
1046 if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
1047 qcom_geni_serial_handle_rx_dma(uport, drop_rx);
1050 if (m_irq_status & m_irq_en &
1051 (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
1052 qcom_geni_serial_handle_tx_fifo(uport,
1053 m_irq_status & M_CMD_DONE_EN,
1054 geni_status & M_GENI_CMD_ACTIVE);
1056 if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
1057 qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
1061 uart_unlock_and_check_sysrq(uport);
1066 static int setup_fifos(struct qcom_geni_serial_port *port)
1068 struct uart_port *uport;
1069 u32 old_rx_fifo_depth = port->rx_fifo_depth;
1071 uport = &port->uport;
1072 port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1073 port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1074 port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1076 (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1078 if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
1080 * Use krealloc rather than krealloc_array because rx_buf is
1081 * accessed as 1 byte entries as well as 4 byte entries so it's
1082 * not necessarily an array.
1084 port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
1085 port->rx_fifo_depth * sizeof(u32),
1095 static void qcom_geni_serial_shutdown(struct uart_port *uport)
1097 disable_irq(uport->irq);
1099 qcom_geni_serial_stop_tx(uport);
1100 qcom_geni_serial_stop_rx(uport);
1102 qcom_geni_serial_cancel_tx_cmd(uport);
1105 static void qcom_geni_serial_flush_buffer(struct uart_port *uport)
1107 qcom_geni_serial_cancel_tx_cmd(uport);
1110 static int qcom_geni_serial_port_setup(struct uart_port *uport)
1112 struct qcom_geni_serial_port *port = to_dev_port(uport);
1113 u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1118 proto = geni_se_read_proto(&port->se);
1119 if (proto != GENI_SE_UART) {
1120 dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1124 qcom_geni_serial_stop_rx(uport);
1126 ret = setup_fifos(port);
1130 writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
1132 pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
1133 if (port->rx_tx_swap) {
1134 pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
1135 pin_swap |= IO_MACRO_IO2_IO3_SWAP;
1137 if (port->cts_rts_swap) {
1138 pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
1139 pin_swap |= IO_MACRO_IO0_SEL;
1141 /* Configure this register if RX-TX, CTS-RTS pins are swapped */
1142 if (port->rx_tx_swap || port->cts_rts_swap)
1143 writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
1146 * Make an unconditional cancel on the main sequencer to reset
1147 * it else we could end up in data loss scenarios.
1149 if (uart_console(uport))
1150 qcom_geni_serial_poll_tx_done(uport);
1151 geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1153 geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
1154 geni_se_select_mode(&port->se, port->dev_data->mode);
1155 qcom_geni_serial_start_rx(uport);
1161 static int qcom_geni_serial_startup(struct uart_port *uport)
1164 struct qcom_geni_serial_port *port = to_dev_port(uport);
1167 ret = qcom_geni_serial_port_setup(uport);
1171 enable_irq(uport->irq);
1176 static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
1177 unsigned int *clk_div, unsigned int percent_tol)
1180 unsigned long div, maxdiv;
1182 unsigned long offset, abs_tol, achieved;
1184 abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
1185 maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
1187 while (div <= maxdiv) {
1188 mult = (u64)div * desired_clk;
1189 if (mult != (unsigned long)mult)
1192 offset = div * abs_tol;
1193 freq = clk_round_rate(clk, mult - offset);
1195 /* Can only get lower if we're done */
1196 if (freq < mult - offset)
1200 * Re-calculate div in case rounding skipped rates but we
1201 * ended up at a good one, then check for a match.
1203 div = DIV_ROUND_CLOSEST(freq, desired_clk);
1204 achieved = DIV_ROUND_CLOSEST(freq, div);
1205 if (achieved <= desired_clk + abs_tol &&
1206 achieved >= desired_clk - abs_tol) {
1211 div = DIV_ROUND_UP(freq, desired_clk);
1217 static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1218 unsigned int sampling_rate, unsigned int *clk_div)
1220 unsigned long ser_clk;
1221 unsigned long desired_clk;
1223 desired_clk = baud * sampling_rate;
1228 * try to find a clock rate within 2% tolerance, then within 5%
1230 ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1232 ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1237 static void qcom_geni_serial_set_termios(struct uart_port *uport,
1238 struct ktermios *termios,
1239 const struct ktermios *old)
1248 unsigned int clk_div;
1250 struct qcom_geni_serial_port *port = to_dev_port(uport);
1251 unsigned long clk_rate;
1252 u32 ver, sampling_rate;
1253 unsigned int avg_bw_core;
1254 unsigned long timeout;
1256 qcom_geni_serial_stop_rx(uport);
1258 baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1260 sampling_rate = UART_OVERSAMPLING;
1261 /* Sampling rate is halved for IP versions >= 2.5 */
1262 ver = geni_se_get_qup_hw_version(&port->se);
1263 if (ver >= QUP_SE_VERSION_2_5)
1266 clk_rate = get_clk_div_rate(port->se.clk, baud,
1267 sampling_rate, &clk_div);
1269 dev_err(port->se.dev,
1270 "Couldn't find suitable clock rate for %u\n",
1271 baud * sampling_rate);
1272 goto out_restart_rx;
1275 dev_dbg(port->se.dev, "desired_rate = %u, clk_rate = %lu, clk_div = %u\n",
1276 baud * sampling_rate, clk_rate, clk_div);
1278 uport->uartclk = clk_rate;
1279 port->clk_rate = clk_rate;
1280 dev_pm_opp_set_rate(uport->dev, clk_rate);
1281 ser_clk_cfg = SER_CLK_EN;
1282 ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1285 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1288 avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
1290 port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
1291 port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1292 geni_icc_set_bw(&port->se);
1295 tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
1296 tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
1297 rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
1298 rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1299 if (termios->c_cflag & PARENB) {
1300 tx_trans_cfg |= UART_TX_PAR_EN;
1301 rx_trans_cfg |= UART_RX_PAR_EN;
1302 tx_parity_cfg |= PAR_CALC_EN;
1303 rx_parity_cfg |= PAR_CALC_EN;
1304 if (termios->c_cflag & PARODD) {
1305 tx_parity_cfg |= PAR_ODD;
1306 rx_parity_cfg |= PAR_ODD;
1307 } else if (termios->c_cflag & CMSPAR) {
1308 tx_parity_cfg |= PAR_SPACE;
1309 rx_parity_cfg |= PAR_SPACE;
1311 tx_parity_cfg |= PAR_EVEN;
1312 rx_parity_cfg |= PAR_EVEN;
1315 tx_trans_cfg &= ~UART_TX_PAR_EN;
1316 rx_trans_cfg &= ~UART_RX_PAR_EN;
1317 tx_parity_cfg &= ~PAR_CALC_EN;
1318 rx_parity_cfg &= ~PAR_CALC_EN;
1322 bits_per_char = tty_get_char_size(termios->c_cflag);
1325 if (termios->c_cflag & CSTOPB)
1326 stop_bit_len = TX_STOP_BIT_LEN_2;
1328 stop_bit_len = TX_STOP_BIT_LEN_1;
1330 /* flow control, clear the CTS_MASK bit if using flow control. */
1331 if (termios->c_cflag & CRTSCTS)
1332 tx_trans_cfg &= ~UART_CTS_MASK;
1334 tx_trans_cfg |= UART_CTS_MASK;
1337 uart_update_timeout(uport, termios->c_cflag, baud);
1340 * Make sure that qcom_geni_serial_poll_bitfield() waits for
1341 * the FIFO, two-word intermediate transfer register and shift
1342 * register to clear.
1344 * Note that uart_fifo_timeout() also adds a 20 ms margin.
1346 timeout = jiffies_to_usecs(uart_fifo_timeout(uport));
1347 timeout += 3 * timeout / port->tx_fifo_depth;
1348 WRITE_ONCE(port->poll_timeout_us, timeout);
1351 if (!uart_console(uport))
1352 writel(port->loopback,
1353 uport->membase + SE_UART_LOOPBACK_CFG);
1354 writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1355 writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1356 writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1357 writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1358 writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1359 writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1360 writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1361 writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1362 writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1364 qcom_geni_serial_start_rx(uport);
1367 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1368 static int qcom_geni_console_setup(struct console *co, char *options)
1370 struct uart_port *uport;
1371 struct qcom_geni_serial_port *port;
1378 if (co->index >= GENI_UART_CONS_PORTS || co->index < 0)
1381 port = get_port_from_line(co->index, true);
1383 pr_err("Invalid line %d\n", co->index);
1384 return PTR_ERR(port);
1387 uport = &port->uport;
1389 if (unlikely(!uport->membase))
1393 ret = qcom_geni_serial_port_setup(uport);
1399 uart_parse_options(options, &baud, &parity, &bits, &flow);
1401 return uart_set_options(uport, co, baud, parity, bits, flow);
1404 static void qcom_geni_serial_earlycon_write(struct console *con,
1405 const char *s, unsigned int n)
1407 struct earlycon_device *dev = con->data;
1409 __qcom_geni_serial_console_write(&dev->port, s, n);
1412 #ifdef CONFIG_CONSOLE_POLL
1413 static int qcom_geni_serial_earlycon_read(struct console *con,
1414 char *s, unsigned int n)
1416 struct earlycon_device *dev = con->data;
1417 struct uart_port *uport = &dev->port;
1421 while (num_read < n) {
1422 ch = qcom_geni_serial_get_char(uport);
1423 if (ch == NO_POLL_CHAR)
1431 static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1432 struct console *con)
1434 geni_se_setup_s_cmd(se, UART_START_READ, 0);
1435 con->read = qcom_geni_serial_earlycon_read;
1438 static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1439 struct console *con) { }
1442 static struct qcom_geni_private_data earlycon_private_data;
1444 static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
1447 struct uart_port *uport = &dev->port;
1449 u32 tx_parity_cfg = 0; /* Disable Tx Parity */
1450 u32 rx_trans_cfg = 0;
1451 u32 rx_parity_cfg = 0; /* Disable Rx Parity */
1452 u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
1456 if (!uport->membase)
1459 uport->private_data = &earlycon_private_data;
1461 memset(&se, 0, sizeof(se));
1462 se.base = uport->membase;
1463 if (geni_se_read_proto(&se) != GENI_SE_UART)
1466 * Ignore Flow control.
1469 tx_trans_cfg = UART_CTS_MASK;
1470 bits_per_char = BITS_PER_BYTE;
1473 * Make an unconditional cancel on the main sequencer to reset
1474 * it else we could end up in data loss scenarios.
1476 qcom_geni_serial_poll_tx_done(uport);
1477 qcom_geni_serial_abort_rx(uport);
1478 geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1480 geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
1481 geni_se_select_mode(&se, GENI_SE_FIFO);
1483 writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1484 writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1485 writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1486 writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1487 writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1488 writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1489 writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1491 dev->con->write = qcom_geni_serial_earlycon_write;
1492 dev->con->setup = NULL;
1493 qcom_geni_serial_enable_early_read(&se, dev->con);
1497 OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
1498 qcom_geni_serial_earlycon_setup);
1500 static int __init console_register(struct uart_driver *drv)
1502 return uart_register_driver(drv);
1505 static void console_unregister(struct uart_driver *drv)
1507 uart_unregister_driver(drv);
1510 static struct console cons_ops = {
1512 .write = qcom_geni_serial_console_write,
1513 .device = uart_console_device,
1514 .setup = qcom_geni_console_setup,
1515 .flags = CON_PRINTBUFFER,
1517 .data = &qcom_geni_console_driver,
1520 static struct uart_driver qcom_geni_console_driver = {
1521 .owner = THIS_MODULE,
1522 .driver_name = "qcom_geni_console",
1523 .dev_name = "ttyMSM",
1524 .nr = GENI_UART_CONS_PORTS,
1528 static int console_register(struct uart_driver *drv)
1533 static void console_unregister(struct uart_driver *drv)
1536 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1538 static struct uart_driver qcom_geni_uart_driver = {
1539 .owner = THIS_MODULE,
1540 .driver_name = "qcom_geni_uart",
1541 .dev_name = "ttyHS",
1542 .nr = GENI_UART_PORTS,
1545 static void qcom_geni_serial_pm(struct uart_port *uport,
1546 unsigned int new_state, unsigned int old_state)
1548 struct qcom_geni_serial_port *port = to_dev_port(uport);
1550 /* If we've never been called, treat it as off */
1551 if (old_state == UART_PM_STATE_UNDEFINED)
1552 old_state = UART_PM_STATE_OFF;
1554 if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
1555 geni_icc_enable(&port->se);
1557 dev_pm_opp_set_rate(uport->dev, port->clk_rate);
1558 geni_se_resources_on(&port->se);
1559 } else if (new_state == UART_PM_STATE_OFF &&
1560 old_state == UART_PM_STATE_ON) {
1561 geni_se_resources_off(&port->se);
1562 dev_pm_opp_set_rate(uport->dev, 0);
1563 geni_icc_disable(&port->se);
1567 static const struct uart_ops qcom_geni_console_pops = {
1568 .tx_empty = qcom_geni_serial_tx_empty,
1569 .stop_tx = qcom_geni_serial_stop_tx_fifo,
1570 .start_tx = qcom_geni_serial_start_tx_fifo,
1571 .stop_rx = qcom_geni_serial_stop_rx_fifo,
1572 .start_rx = qcom_geni_serial_start_rx_fifo,
1573 .set_termios = qcom_geni_serial_set_termios,
1574 .startup = qcom_geni_serial_startup,
1575 .request_port = qcom_geni_serial_request_port,
1576 .config_port = qcom_geni_serial_config_port,
1577 .shutdown = qcom_geni_serial_shutdown,
1578 .flush_buffer = qcom_geni_serial_flush_buffer,
1579 .type = qcom_geni_serial_get_type,
1580 .set_mctrl = qcom_geni_serial_set_mctrl,
1581 .get_mctrl = qcom_geni_serial_get_mctrl,
1582 #ifdef CONFIG_CONSOLE_POLL
1583 .poll_get_char = qcom_geni_serial_get_char,
1584 .poll_put_char = qcom_geni_serial_poll_put_char,
1585 .poll_init = qcom_geni_serial_port_setup,
1587 .pm = qcom_geni_serial_pm,
1590 static const struct uart_ops qcom_geni_uart_pops = {
1591 .tx_empty = qcom_geni_serial_tx_empty,
1592 .stop_tx = qcom_geni_serial_stop_tx_dma,
1593 .start_tx = qcom_geni_serial_start_tx_dma,
1594 .start_rx = qcom_geni_serial_start_rx_dma,
1595 .stop_rx = qcom_geni_serial_stop_rx_dma,
1596 .set_termios = qcom_geni_serial_set_termios,
1597 .startup = qcom_geni_serial_startup,
1598 .request_port = qcom_geni_serial_request_port,
1599 .config_port = qcom_geni_serial_config_port,
1600 .shutdown = qcom_geni_serial_shutdown,
1601 .type = qcom_geni_serial_get_type,
1602 .set_mctrl = qcom_geni_serial_set_mctrl,
1603 .get_mctrl = qcom_geni_serial_get_mctrl,
1604 .pm = qcom_geni_serial_pm,
1607 static int qcom_geni_serial_probe(struct platform_device *pdev)
1611 struct qcom_geni_serial_port *port;
1612 struct uart_port *uport;
1613 struct resource *res;
1615 struct uart_driver *drv;
1616 const struct qcom_geni_device_data *data;
1618 data = of_device_get_match_data(&pdev->dev);
1622 if (data->console) {
1623 drv = &qcom_geni_console_driver;
1624 line = of_alias_get_id(pdev->dev.of_node, "serial");
1626 drv = &qcom_geni_uart_driver;
1627 line = of_alias_get_id(pdev->dev.of_node, "serial");
1628 if (line == -ENODEV) /* compat with non-standard aliases */
1629 line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1632 port = get_port_from_line(line, data->console);
1634 dev_err(&pdev->dev, "Invalid line %d\n", line);
1635 return PTR_ERR(port);
1638 uport = &port->uport;
1639 /* Don't allow 2 drivers to access the same port */
1640 if (uport->private_data)
1643 uport->dev = &pdev->dev;
1644 port->dev_data = data;
1645 port->se.dev = &pdev->dev;
1646 port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1647 port->se.clk = devm_clk_get(&pdev->dev, "se");
1648 if (IS_ERR(port->se.clk)) {
1649 ret = PTR_ERR(port->se.clk);
1650 dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1654 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1657 uport->mapbase = res->start;
1659 port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1660 port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1661 port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1663 if (!data->console) {
1664 port->rx_buf = devm_kzalloc(uport->dev,
1665 DMA_RX_BUF_SIZE, GFP_KERNEL);
1670 ret = geni_icc_get(&port->se, NULL);
1673 port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
1674 port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
1676 /* Set BW for register access */
1677 ret = geni_icc_set_bw(&port->se);
1681 port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1682 "qcom_geni_serial_%s%d",
1683 uart_console(uport) ? "console" : "uart", uport->line);
1687 irq = platform_get_irq(pdev, 0);
1691 uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1694 port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1696 if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1697 port->rx_tx_swap = true;
1699 if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1700 port->cts_rts_swap = true;
1702 ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1705 /* OPP table is optional */
1706 ret = devm_pm_opp_of_add_table(&pdev->dev);
1707 if (ret && ret != -ENODEV) {
1708 dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1712 port->private_data.drv = drv;
1713 uport->private_data = &port->private_data;
1714 platform_set_drvdata(pdev, port);
1716 irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1717 ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1718 IRQF_TRIGGER_HIGH, port->name, uport);
1720 dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1724 ret = uart_add_one_port(drv, uport);
1728 if (port->wakeup_irq > 0) {
1729 device_init_wakeup(&pdev->dev, true);
1730 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1733 device_init_wakeup(&pdev->dev, false);
1734 uart_remove_one_port(drv, uport);
1742 static void qcom_geni_serial_remove(struct platform_device *pdev)
1744 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1745 struct uart_driver *drv = port->private_data.drv;
1747 dev_pm_clear_wake_irq(&pdev->dev);
1748 device_init_wakeup(&pdev->dev, false);
1749 uart_remove_one_port(drv, &port->uport);
1752 static int qcom_geni_serial_sys_suspend(struct device *dev)
1754 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1755 struct uart_port *uport = &port->uport;
1756 struct qcom_geni_private_data *private_data = uport->private_data;
1759 * This is done so we can hit the lowest possible state in suspend
1760 * even with no_console_suspend
1762 if (uart_console(uport)) {
1763 geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
1764 geni_icc_set_bw(&port->se);
1766 return uart_suspend_port(private_data->drv, uport);
1769 static int qcom_geni_serial_sys_resume(struct device *dev)
1772 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1773 struct uart_port *uport = &port->uport;
1774 struct qcom_geni_private_data *private_data = uport->private_data;
1776 ret = uart_resume_port(private_data->drv, uport);
1777 if (uart_console(uport)) {
1778 geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
1779 geni_icc_set_bw(&port->se);
1784 static int qcom_geni_serial_sys_hib_resume(struct device *dev)
1787 struct uart_port *uport;
1788 struct qcom_geni_private_data *private_data;
1789 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1791 uport = &port->uport;
1792 private_data = uport->private_data;
1794 if (uart_console(uport)) {
1795 geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
1796 geni_icc_set_bw(&port->se);
1797 ret = uart_resume_port(private_data->drv, uport);
1799 * For hibernation usecase clients for
1800 * console UART won't call port setup during restore,
1801 * hence call port setup for console uart.
1803 qcom_geni_serial_port_setup(uport);
1806 * Peripheral register settings are lost during hibernation.
1807 * Update setup flag such that port setup happens again
1808 * during next session. Clients of HS-UART will close and
1809 * open the port during hibernation.
1811 port->setup = false;
1816 static const struct qcom_geni_device_data qcom_geni_console_data = {
1818 .mode = GENI_SE_FIFO,
1821 static const struct qcom_geni_device_data qcom_geni_uart_data = {
1823 .mode = GENI_SE_DMA,
1826 static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1827 .suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1828 .resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
1829 .freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1830 .poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1831 .restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1832 .thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1835 static const struct of_device_id qcom_geni_serial_match_table[] = {
1837 .compatible = "qcom,geni-debug-uart",
1838 .data = &qcom_geni_console_data,
1841 .compatible = "qcom,geni-uart",
1842 .data = &qcom_geni_uart_data,
1846 MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1848 static struct platform_driver qcom_geni_serial_platform_driver = {
1849 .remove_new = qcom_geni_serial_remove,
1850 .probe = qcom_geni_serial_probe,
1852 .name = "qcom_geni_serial",
1853 .of_match_table = qcom_geni_serial_match_table,
1854 .pm = &qcom_geni_serial_pm_ops,
1858 static int __init qcom_geni_serial_init(void)
1862 ret = console_register(&qcom_geni_console_driver);
1866 ret = uart_register_driver(&qcom_geni_uart_driver);
1868 console_unregister(&qcom_geni_console_driver);
1872 ret = platform_driver_register(&qcom_geni_serial_platform_driver);
1874 console_unregister(&qcom_geni_console_driver);
1875 uart_unregister_driver(&qcom_geni_uart_driver);
1879 module_init(qcom_geni_serial_init);
1881 static void __exit qcom_geni_serial_exit(void)
1883 platform_driver_unregister(&qcom_geni_serial_platform_driver);
1884 console_unregister(&qcom_geni_console_driver);
1885 uart_unregister_driver(&qcom_geni_uart_driver);
1887 module_exit(qcom_geni_serial_exit);
1889 MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1890 MODULE_LICENSE("GPL v2");