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;
128 unsigned long clk_rate;
133 unsigned int tx_remaining;
138 struct qcom_geni_private_data private_data;
139 const struct qcom_geni_device_data *dev_data;
142 static const struct uart_ops qcom_geni_console_pops;
143 static const struct uart_ops qcom_geni_uart_pops;
144 static struct uart_driver qcom_geni_console_driver;
145 static struct uart_driver qcom_geni_uart_driver;
147 static inline struct qcom_geni_serial_port *to_dev_port(struct uart_port *uport)
149 return container_of(uport, struct qcom_geni_serial_port, uport);
152 static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
156 .ops = &qcom_geni_uart_pops,
157 .flags = UPF_BOOT_AUTOCONF,
164 .ops = &qcom_geni_uart_pops,
165 .flags = UPF_BOOT_AUTOCONF,
172 .ops = &qcom_geni_uart_pops,
173 .flags = UPF_BOOT_AUTOCONF,
179 static struct qcom_geni_serial_port qcom_geni_console_port = {
182 .ops = &qcom_geni_console_pops,
183 .flags = UPF_BOOT_AUTOCONF,
188 static int qcom_geni_serial_request_port(struct uart_port *uport)
190 struct platform_device *pdev = to_platform_device(uport->dev);
191 struct qcom_geni_serial_port *port = to_dev_port(uport);
193 uport->membase = devm_platform_ioremap_resource(pdev, 0);
194 if (IS_ERR(uport->membase))
195 return PTR_ERR(uport->membase);
196 port->se.base = uport->membase;
200 static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
202 if (cfg_flags & UART_CONFIG_TYPE) {
203 uport->type = PORT_MSM;
204 qcom_geni_serial_request_port(uport);
208 static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
210 unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
213 if (uart_console(uport)) {
216 geni_ios = readl(uport->membase + SE_GENI_IOS);
217 if (!(geni_ios & IO2_DATA_IN))
224 static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
227 u32 uart_manual_rfr = 0;
228 struct qcom_geni_serial_port *port = to_dev_port(uport);
230 if (uart_console(uport))
233 if (mctrl & TIOCM_LOOP)
234 port->loopback = RX_TX_CTS_RTS_SORTED;
236 if (!(mctrl & TIOCM_RTS) && !uport->suspended)
237 uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
238 writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
241 static const char *qcom_geni_serial_get_type(struct uart_port *uport)
246 static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
248 struct qcom_geni_serial_port *port;
249 int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
251 if (line < 0 || line >= nr_ports)
252 return ERR_PTR(-ENXIO);
254 port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
258 static bool qcom_geni_serial_main_active(struct uart_port *uport)
260 return readl(uport->membase + SE_GENI_STATUS) & M_GENI_CMD_ACTIVE;
263 static bool qcom_geni_serial_secondary_active(struct uart_port *uport)
265 return readl(uport->membase + SE_GENI_STATUS) & S_GENI_CMD_ACTIVE;
268 static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
269 int offset, int field, bool set)
272 struct qcom_geni_serial_port *port;
274 unsigned int fifo_bits;
275 unsigned long timeout_us = 20000;
276 struct qcom_geni_private_data *private_data = uport->private_data;
278 if (private_data->drv) {
279 port = to_dev_port(uport);
283 fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
285 * Total polling iterations based on FIFO worth of bytes to be
286 * sent at current baud. Add a little fluff to the wait.
288 timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
292 * Use custom implementation instead of readl_poll_atomic since ktimer
293 * is not ready at the time of early console.
295 timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
297 reg = readl(uport->membase + offset);
298 if ((bool)(reg & field) == set)
306 static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
310 writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
311 m_cmd = UART_START_TX << M_OPCODE_SHFT;
312 writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
315 static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
318 u32 irq_clear = M_CMD_DONE_EN;
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 irq_clear |= M_CMD_ABORT_EN;
326 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
327 M_CMD_ABORT_EN, true);
329 writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
332 static void qcom_geni_serial_abort_rx(struct uart_port *uport)
334 u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
336 writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
337 qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
338 S_GENI_CMD_ABORT, false);
339 writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
340 writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
343 #ifdef CONFIG_CONSOLE_POLL
344 static int qcom_geni_serial_get_char(struct uart_port *uport)
346 struct qcom_geni_private_data *private_data = uport->private_data;
351 if (!private_data->poll_cached_bytes_cnt) {
352 status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
353 writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
355 status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
356 writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
358 status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
359 word_cnt = status & RX_FIFO_WC_MSK;
363 if (word_cnt == 1 && (status & RX_LAST))
365 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
366 * treated as if it was BYTES_PER_FIFO_WORD.
368 private_data->poll_cached_bytes_cnt =
369 (status & RX_LAST_BYTE_VALID_MSK) >>
370 RX_LAST_BYTE_VALID_SHFT;
372 if (private_data->poll_cached_bytes_cnt == 0)
373 private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
375 private_data->poll_cached_bytes =
376 readl(uport->membase + SE_GENI_RX_FIFOn);
379 private_data->poll_cached_bytes_cnt--;
380 ret = private_data->poll_cached_bytes & 0xff;
381 private_data->poll_cached_bytes >>= 8;
386 static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
389 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
390 qcom_geni_serial_setup_tx(uport, 1);
391 WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
392 M_TX_FIFO_WATERMARK_EN, true));
393 writel(c, uport->membase + SE_GENI_TX_FIFOn);
394 writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
395 qcom_geni_serial_poll_tx_done(uport);
399 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
400 static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
402 struct qcom_geni_private_data *private_data = uport->private_data;
404 private_data->write_cached_bytes =
405 (private_data->write_cached_bytes >> 8) | (ch << 24);
406 private_data->write_cached_bytes_cnt++;
408 if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
409 writel(private_data->write_cached_bytes,
410 uport->membase + SE_GENI_TX_FIFOn);
411 private_data->write_cached_bytes_cnt = 0;
416 __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
419 struct qcom_geni_private_data *private_data = uport->private_data;
422 u32 bytes_to_send = count;
424 for (i = 0; i < count; i++) {
426 * uart_console_write() adds a carriage return for each newline.
427 * Account for additional bytes to be written.
433 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
434 qcom_geni_serial_setup_tx(uport, bytes_to_send);
435 for (i = 0; i < count; ) {
436 size_t chars_to_write = 0;
437 size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
440 * If the WM bit never set, then the Tx state machine is not
441 * in a valid state, so break, cancel/abort any existing
442 * command. Unfortunately the current data being written is
445 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
446 M_TX_FIFO_WATERMARK_EN, true))
448 chars_to_write = min_t(size_t, count - i, avail / 2);
449 uart_console_write(uport, s + i, chars_to_write,
450 qcom_geni_serial_wr_char);
451 writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
452 SE_GENI_M_IRQ_CLEAR);
456 if (private_data->write_cached_bytes_cnt) {
457 private_data->write_cached_bytes >>= BITS_PER_BYTE *
458 (BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
459 writel(private_data->write_cached_bytes,
460 uport->membase + SE_GENI_TX_FIFOn);
461 private_data->write_cached_bytes_cnt = 0;
464 qcom_geni_serial_poll_tx_done(uport);
467 static void qcom_geni_serial_console_write(struct console *co, const char *s,
470 struct uart_port *uport;
471 struct qcom_geni_serial_port *port;
477 WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
479 port = get_port_from_line(co->index, true);
483 uport = &port->uport;
484 if (oops_in_progress)
485 locked = uart_port_trylock_irqsave(uport, &flags);
487 uart_port_lock_irqsave(uport, &flags);
489 geni_status = readl(uport->membase + SE_GENI_STATUS);
493 * We can only get here if an oops is in progress then we were
494 * unable to get the lock. This means we can't safely access
495 * our state variables like tx_remaining. About the best we
496 * can do is wait for the FIFO to be empty before we start our
497 * transfer, so we'll do that.
499 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
500 M_TX_FIFO_NOT_EMPTY_EN, false);
501 } else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
503 * It seems we can't interrupt existing transfers if all data
504 * has been sent, in which case we need to look for done first.
506 qcom_geni_serial_poll_tx_done(uport);
508 if (!kfifo_is_empty(&uport->state->port.xmit_fifo)) {
509 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
510 writel(irq_en | M_TX_FIFO_WATERMARK_EN,
511 uport->membase + SE_GENI_M_IRQ_EN);
515 __qcom_geni_serial_console_write(uport, s, count);
519 if (port->tx_remaining)
520 qcom_geni_serial_setup_tx(uport, port->tx_remaining);
521 uart_port_unlock_irqrestore(uport, flags);
525 static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
528 unsigned char buf[sizeof(u32)];
529 struct tty_port *tport;
530 struct qcom_geni_serial_port *port = to_dev_port(uport);
532 tport = &uport->state->port;
533 for (i = 0; i < bytes; ) {
535 int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
537 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
542 for (c = 0; c < chunk; c++) {
546 if (port->brk && buf[c] == 0) {
548 if (uart_handle_break(uport))
552 sysrq = uart_prepare_sysrq_char(uport, buf[c]);
555 tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
559 tty_flip_buffer_push(tport);
562 static void handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
566 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
568 static void handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
570 struct qcom_geni_serial_port *port = to_dev_port(uport);
571 struct tty_port *tport = &uport->state->port;
574 ret = tty_insert_flip_string(tport, port->rx_buf, bytes);
576 dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
577 __func__, ret, bytes);
580 uport->icount.rx += ret;
581 tty_flip_buffer_push(tport);
584 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
586 return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
589 static void qcom_geni_serial_stop_tx_dma(struct uart_port *uport)
591 struct qcom_geni_serial_port *port = to_dev_port(uport);
594 if (!qcom_geni_serial_main_active(uport))
597 if (port->tx_dma_addr) {
598 geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr,
600 port->tx_dma_addr = 0;
601 port->tx_remaining = 0;
604 geni_se_cancel_m_cmd(&port->se);
606 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
607 M_CMD_CANCEL_EN, true);
609 geni_se_abort_m_cmd(&port->se);
610 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
611 M_CMD_ABORT_EN, true);
613 dev_err_ratelimited(uport->dev, "M_CMD_ABORT_EN not set");
614 writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
617 writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
620 static void qcom_geni_serial_start_tx_dma(struct uart_port *uport)
622 struct qcom_geni_serial_port *port = to_dev_port(uport);
623 struct tty_port *tport = &uport->state->port;
624 unsigned int xmit_size;
628 if (port->tx_dma_addr)
631 if (kfifo_is_empty(&tport->xmit_fifo))
634 xmit_size = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail,
637 qcom_geni_serial_setup_tx(uport, xmit_size);
639 ret = geni_se_tx_dma_prep(&port->se, tail, xmit_size,
642 dev_err(uport->dev, "unable to start TX SE DMA: %d\n", ret);
643 qcom_geni_serial_stop_tx_dma(uport);
647 port->tx_remaining = xmit_size;
650 static void qcom_geni_serial_start_tx_fifo(struct uart_port *uport)
654 if (qcom_geni_serial_main_active(uport) ||
655 !qcom_geni_serial_tx_empty(uport))
658 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
659 irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
661 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
662 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
665 static void qcom_geni_serial_stop_tx_fifo(struct uart_port *uport)
668 struct qcom_geni_serial_port *port = to_dev_port(uport);
670 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
671 irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
672 writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
673 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
674 /* Possible stop tx is called multiple times. */
675 if (!qcom_geni_serial_main_active(uport))
678 geni_se_cancel_m_cmd(&port->se);
679 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
680 M_CMD_CANCEL_EN, true)) {
681 geni_se_abort_m_cmd(&port->se);
682 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
683 M_CMD_ABORT_EN, true);
684 writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
686 writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
689 static void qcom_geni_serial_handle_rx_fifo(struct uart_port *uport, bool drop)
693 u32 last_word_byte_cnt;
694 u32 last_word_partial;
697 status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
698 word_cnt = status & RX_FIFO_WC_MSK;
699 last_word_partial = status & RX_LAST;
700 last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
701 RX_LAST_BYTE_VALID_SHFT;
705 total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
706 if (last_word_partial && last_word_byte_cnt)
707 total_bytes += last_word_byte_cnt;
709 total_bytes += BYTES_PER_FIFO_WORD;
710 handle_rx_console(uport, total_bytes, drop);
713 static void qcom_geni_serial_stop_rx_fifo(struct uart_port *uport)
716 struct qcom_geni_serial_port *port = to_dev_port(uport);
719 irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
720 irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
721 writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
723 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
724 irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
725 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
727 if (!qcom_geni_serial_secondary_active(uport))
730 geni_se_cancel_s_cmd(&port->se);
731 qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
732 S_CMD_CANCEL_EN, true);
734 * If timeout occurs secondary engine remains active
735 * and Abort sequence is executed.
737 s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
738 /* Flush the Rx buffer */
739 if (s_irq_status & S_RX_FIFO_LAST_EN)
740 qcom_geni_serial_handle_rx_fifo(uport, true);
741 writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
743 if (qcom_geni_serial_secondary_active(uport))
744 qcom_geni_serial_abort_rx(uport);
747 static void qcom_geni_serial_start_rx_fifo(struct uart_port *uport)
750 struct qcom_geni_serial_port *port = to_dev_port(uport);
752 if (qcom_geni_serial_secondary_active(uport))
753 qcom_geni_serial_stop_rx_fifo(uport);
755 geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
757 irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
758 irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
759 writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
761 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
762 irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
763 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
766 static void qcom_geni_serial_stop_rx_dma(struct uart_port *uport)
768 struct qcom_geni_serial_port *port = to_dev_port(uport);
770 if (!qcom_geni_serial_secondary_active(uport))
773 geni_se_cancel_s_cmd(&port->se);
774 qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
775 S_CMD_CANCEL_EN, true);
777 if (qcom_geni_serial_secondary_active(uport))
778 qcom_geni_serial_abort_rx(uport);
780 if (port->rx_dma_addr) {
781 geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr,
783 port->rx_dma_addr = 0;
787 static void qcom_geni_serial_start_rx_dma(struct uart_port *uport)
789 struct qcom_geni_serial_port *port = to_dev_port(uport);
792 if (qcom_geni_serial_secondary_active(uport))
793 qcom_geni_serial_stop_rx_dma(uport);
795 geni_se_setup_s_cmd(&port->se, UART_START_READ, UART_PARAM_RFR_OPEN);
797 ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
801 dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
802 qcom_geni_serial_stop_rx_dma(uport);
806 static void qcom_geni_serial_handle_rx_dma(struct uart_port *uport, bool drop)
808 struct qcom_geni_serial_port *port = to_dev_port(uport);
812 if (!qcom_geni_serial_secondary_active(uport))
815 if (!port->rx_dma_addr)
818 geni_se_rx_dma_unprep(&port->se, port->rx_dma_addr, DMA_RX_BUF_SIZE);
819 port->rx_dma_addr = 0;
821 rx_in = readl(uport->membase + SE_DMA_RX_LEN_IN);
823 dev_warn(uport->dev, "serial engine reports 0 RX bytes in!\n");
828 handle_rx_uart(uport, rx_in, drop);
830 ret = geni_se_rx_dma_prep(&port->se, port->rx_buf,
834 dev_err(uport->dev, "unable to start RX SE DMA: %d\n", ret);
835 qcom_geni_serial_stop_rx_dma(uport);
839 static void qcom_geni_serial_start_rx(struct uart_port *uport)
841 uport->ops->start_rx(uport);
844 static void qcom_geni_serial_stop_rx(struct uart_port *uport)
846 uport->ops->stop_rx(uport);
849 static void qcom_geni_serial_stop_tx(struct uart_port *uport)
851 uport->ops->stop_tx(uport);
854 static void qcom_geni_serial_send_chunk_fifo(struct uart_port *uport,
857 struct qcom_geni_serial_port *port = to_dev_port(uport);
858 struct tty_port *tport = &uport->state->port;
859 unsigned int tx_bytes, remaining = chunk;
860 u8 buf[BYTES_PER_FIFO_WORD];
863 memset(buf, 0, sizeof(buf));
864 tx_bytes = min(remaining, BYTES_PER_FIFO_WORD);
866 tx_bytes = kfifo_out(&tport->xmit_fifo, buf, tx_bytes);
867 uart_xmit_advance(uport, tx_bytes);
869 iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
871 remaining -= tx_bytes;
872 port->tx_remaining -= tx_bytes;
876 static void qcom_geni_serial_handle_tx_fifo(struct uart_port *uport,
877 bool done, bool active)
879 struct qcom_geni_serial_port *port = to_dev_port(uport);
880 struct tty_port *tport = &uport->state->port;
887 status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
889 /* Complete the current tx command before taking newly added data */
891 pending = port->tx_remaining;
893 pending = kfifo_len(&tport->xmit_fifo);
895 /* All data has been transmitted and acknowledged as received */
896 if (!pending && !status && done) {
897 qcom_geni_serial_stop_tx_fifo(uport);
898 goto out_write_wakeup;
901 avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
902 avail *= BYTES_PER_FIFO_WORD;
904 chunk = min(avail, pending);
906 goto out_write_wakeup;
908 if (!port->tx_remaining) {
909 qcom_geni_serial_setup_tx(uport, pending);
910 port->tx_remaining = pending;
912 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
913 if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
914 writel(irq_en | M_TX_FIFO_WATERMARK_EN,
915 uport->membase + SE_GENI_M_IRQ_EN);
918 qcom_geni_serial_send_chunk_fifo(uport, chunk);
921 * The tx fifo watermark is level triggered and latched. Though we had
922 * cleared it in qcom_geni_serial_isr it will have already reasserted
923 * so we must clear it again here after our writes.
925 writel(M_TX_FIFO_WATERMARK_EN,
926 uport->membase + SE_GENI_M_IRQ_CLEAR);
929 if (!port->tx_remaining) {
930 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
931 if (irq_en & M_TX_FIFO_WATERMARK_EN)
932 writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
933 uport->membase + SE_GENI_M_IRQ_EN);
936 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
937 uart_write_wakeup(uport);
940 static void qcom_geni_serial_handle_tx_dma(struct uart_port *uport)
942 struct qcom_geni_serial_port *port = to_dev_port(uport);
943 struct tty_port *tport = &uport->state->port;
945 uart_xmit_advance(uport, port->tx_remaining);
946 geni_se_tx_dma_unprep(&port->se, port->tx_dma_addr, port->tx_remaining);
947 port->tx_dma_addr = 0;
948 port->tx_remaining = 0;
950 if (!kfifo_is_empty(&tport->xmit_fifo))
951 qcom_geni_serial_start_tx_dma(uport);
953 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
954 uart_write_wakeup(uport);
957 static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
966 struct uart_port *uport = dev;
967 bool drop_rx = false;
968 struct tty_port *tport = &uport->state->port;
969 struct qcom_geni_serial_port *port = to_dev_port(uport);
971 if (uport->suspended)
974 uart_port_lock(uport);
976 m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
977 s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
978 dma_tx_status = readl(uport->membase + SE_DMA_TX_IRQ_STAT);
979 dma_rx_status = readl(uport->membase + SE_DMA_RX_IRQ_STAT);
980 geni_status = readl(uport->membase + SE_GENI_STATUS);
981 dma = readl(uport->membase + SE_GENI_DMA_MODE_EN);
982 m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
983 writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
984 writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
985 writel(dma_tx_status, uport->membase + SE_DMA_TX_IRQ_CLR);
986 writel(dma_rx_status, uport->membase + SE_DMA_RX_IRQ_CLR);
988 if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
991 if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
992 uport->icount.overrun++;
993 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
996 if (s_irq_status & (S_GP_IRQ_0_EN | S_GP_IRQ_1_EN)) {
997 if (s_irq_status & S_GP_IRQ_0_EN)
998 uport->icount.parity++;
1000 } else if (s_irq_status & (S_GP_IRQ_2_EN | S_GP_IRQ_3_EN)) {
1001 uport->icount.brk++;
1006 if (dma_tx_status & TX_DMA_DONE)
1007 qcom_geni_serial_handle_tx_dma(uport);
1009 if (dma_rx_status) {
1010 if (dma_rx_status & RX_RESET_DONE)
1013 if (dma_rx_status & RX_DMA_PARITY_ERR) {
1014 uport->icount.parity++;
1018 if (dma_rx_status & RX_DMA_BREAK)
1019 uport->icount.brk++;
1021 if (dma_rx_status & (RX_DMA_DONE | RX_EOT))
1022 qcom_geni_serial_handle_rx_dma(uport, drop_rx);
1025 if (m_irq_status & m_irq_en &
1026 (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
1027 qcom_geni_serial_handle_tx_fifo(uport,
1028 m_irq_status & M_CMD_DONE_EN,
1029 geni_status & M_GENI_CMD_ACTIVE);
1031 if (s_irq_status & (S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN))
1032 qcom_geni_serial_handle_rx_fifo(uport, drop_rx);
1036 uart_unlock_and_check_sysrq(uport);
1041 static int setup_fifos(struct qcom_geni_serial_port *port)
1043 struct uart_port *uport;
1044 u32 old_rx_fifo_depth = port->rx_fifo_depth;
1046 uport = &port->uport;
1047 port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
1048 port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
1049 port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
1051 (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
1053 if (port->rx_buf && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
1055 * Use krealloc rather than krealloc_array because rx_buf is
1056 * accessed as 1 byte entries as well as 4 byte entries so it's
1057 * not necessarily an array.
1059 port->rx_buf = devm_krealloc(uport->dev, port->rx_buf,
1060 port->rx_fifo_depth * sizeof(u32),
1070 static void qcom_geni_serial_shutdown(struct uart_port *uport)
1072 disable_irq(uport->irq);
1074 if (uart_console(uport))
1077 qcom_geni_serial_stop_tx(uport);
1078 qcom_geni_serial_stop_rx(uport);
1081 static int qcom_geni_serial_port_setup(struct uart_port *uport)
1083 struct qcom_geni_serial_port *port = to_dev_port(uport);
1084 u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
1089 proto = geni_se_read_proto(&port->se);
1090 if (proto != GENI_SE_UART) {
1091 dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
1095 qcom_geni_serial_stop_rx(uport);
1097 ret = setup_fifos(port);
1101 writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
1103 pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
1104 if (port->rx_tx_swap) {
1105 pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
1106 pin_swap |= IO_MACRO_IO2_IO3_SWAP;
1108 if (port->cts_rts_swap) {
1109 pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
1110 pin_swap |= IO_MACRO_IO0_SEL;
1112 /* Configure this register if RX-TX, CTS-RTS pins are swapped */
1113 if (port->rx_tx_swap || port->cts_rts_swap)
1114 writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
1117 * Make an unconditional cancel on the main sequencer to reset
1118 * it else we could end up in data loss scenarios.
1120 if (uart_console(uport))
1121 qcom_geni_serial_poll_tx_done(uport);
1122 geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1124 geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
1125 geni_se_select_mode(&port->se, port->dev_data->mode);
1126 qcom_geni_serial_start_rx(uport);
1132 static int qcom_geni_serial_startup(struct uart_port *uport)
1135 struct qcom_geni_serial_port *port = to_dev_port(uport);
1138 ret = qcom_geni_serial_port_setup(uport);
1142 enable_irq(uport->irq);
1147 static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
1148 unsigned int *clk_div, unsigned int percent_tol)
1151 unsigned long div, maxdiv;
1153 unsigned long offset, abs_tol, achieved;
1155 abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
1156 maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
1158 while (div <= maxdiv) {
1159 mult = (u64)div * desired_clk;
1160 if (mult != (unsigned long)mult)
1163 offset = div * abs_tol;
1164 freq = clk_round_rate(clk, mult - offset);
1166 /* Can only get lower if we're done */
1167 if (freq < mult - offset)
1171 * Re-calculate div in case rounding skipped rates but we
1172 * ended up at a good one, then check for a match.
1174 div = DIV_ROUND_CLOSEST(freq, desired_clk);
1175 achieved = DIV_ROUND_CLOSEST(freq, div);
1176 if (achieved <= desired_clk + abs_tol &&
1177 achieved >= desired_clk - abs_tol) {
1182 div = DIV_ROUND_UP(freq, desired_clk);
1188 static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
1189 unsigned int sampling_rate, unsigned int *clk_div)
1191 unsigned long ser_clk;
1192 unsigned long desired_clk;
1194 desired_clk = baud * sampling_rate;
1199 * try to find a clock rate within 2% tolerance, then within 5%
1201 ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
1203 ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1208 static void qcom_geni_serial_set_termios(struct uart_port *uport,
1209 struct ktermios *termios,
1210 const struct ktermios *old)
1219 unsigned int clk_div;
1221 struct qcom_geni_serial_port *port = to_dev_port(uport);
1222 unsigned long clk_rate;
1223 u32 ver, sampling_rate;
1224 unsigned int avg_bw_core;
1226 qcom_geni_serial_stop_rx(uport);
1228 baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1231 sampling_rate = UART_OVERSAMPLING;
1232 /* Sampling rate is halved for IP versions >= 2.5 */
1233 ver = geni_se_get_qup_hw_version(&port->se);
1234 if (ver >= QUP_SE_VERSION_2_5)
1237 clk_rate = get_clk_div_rate(port->se.clk, baud,
1238 sampling_rate, &clk_div);
1240 dev_err(port->se.dev,
1241 "Couldn't find suitable clock rate for %u\n",
1242 baud * sampling_rate);
1243 goto out_restart_rx;
1246 dev_dbg(port->se.dev, "desired_rate = %u, clk_rate = %lu, clk_div = %u\n",
1247 baud * sampling_rate, clk_rate, clk_div);
1249 uport->uartclk = clk_rate;
1250 port->clk_rate = clk_rate;
1251 dev_pm_opp_set_rate(uport->dev, clk_rate);
1252 ser_clk_cfg = SER_CLK_EN;
1253 ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1256 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1259 avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
1261 port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
1262 port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1263 geni_icc_set_bw(&port->se);
1266 tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
1267 tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
1268 rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
1269 rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1270 if (termios->c_cflag & PARENB) {
1271 tx_trans_cfg |= UART_TX_PAR_EN;
1272 rx_trans_cfg |= UART_RX_PAR_EN;
1273 tx_parity_cfg |= PAR_CALC_EN;
1274 rx_parity_cfg |= PAR_CALC_EN;
1275 if (termios->c_cflag & PARODD) {
1276 tx_parity_cfg |= PAR_ODD;
1277 rx_parity_cfg |= PAR_ODD;
1278 } else if (termios->c_cflag & CMSPAR) {
1279 tx_parity_cfg |= PAR_SPACE;
1280 rx_parity_cfg |= PAR_SPACE;
1282 tx_parity_cfg |= PAR_EVEN;
1283 rx_parity_cfg |= PAR_EVEN;
1286 tx_trans_cfg &= ~UART_TX_PAR_EN;
1287 rx_trans_cfg &= ~UART_RX_PAR_EN;
1288 tx_parity_cfg &= ~PAR_CALC_EN;
1289 rx_parity_cfg &= ~PAR_CALC_EN;
1293 bits_per_char = tty_get_char_size(termios->c_cflag);
1296 if (termios->c_cflag & CSTOPB)
1297 stop_bit_len = TX_STOP_BIT_LEN_2;
1299 stop_bit_len = TX_STOP_BIT_LEN_1;
1301 /* flow control, clear the CTS_MASK bit if using flow control. */
1302 if (termios->c_cflag & CRTSCTS)
1303 tx_trans_cfg &= ~UART_CTS_MASK;
1305 tx_trans_cfg |= UART_CTS_MASK;
1308 uart_update_timeout(uport, termios->c_cflag, baud);
1310 if (!uart_console(uport))
1311 writel(port->loopback,
1312 uport->membase + SE_UART_LOOPBACK_CFG);
1313 writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1314 writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1315 writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1316 writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1317 writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1318 writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1319 writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1320 writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1321 writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1323 qcom_geni_serial_start_rx(uport);
1326 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
1327 static int qcom_geni_console_setup(struct console *co, char *options)
1329 struct uart_port *uport;
1330 struct qcom_geni_serial_port *port;
1337 if (co->index >= GENI_UART_CONS_PORTS || co->index < 0)
1340 port = get_port_from_line(co->index, true);
1342 pr_err("Invalid line %d\n", co->index);
1343 return PTR_ERR(port);
1346 uport = &port->uport;
1348 if (unlikely(!uport->membase))
1352 ret = qcom_geni_serial_port_setup(uport);
1358 uart_parse_options(options, &baud, &parity, &bits, &flow);
1360 return uart_set_options(uport, co, baud, parity, bits, flow);
1363 static void qcom_geni_serial_earlycon_write(struct console *con,
1364 const char *s, unsigned int n)
1366 struct earlycon_device *dev = con->data;
1368 __qcom_geni_serial_console_write(&dev->port, s, n);
1371 #ifdef CONFIG_CONSOLE_POLL
1372 static int qcom_geni_serial_earlycon_read(struct console *con,
1373 char *s, unsigned int n)
1375 struct earlycon_device *dev = con->data;
1376 struct uart_port *uport = &dev->port;
1380 while (num_read < n) {
1381 ch = qcom_geni_serial_get_char(uport);
1382 if (ch == NO_POLL_CHAR)
1390 static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1391 struct console *con)
1393 geni_se_setup_s_cmd(se, UART_START_READ, 0);
1394 con->read = qcom_geni_serial_earlycon_read;
1397 static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1398 struct console *con) { }
1401 static struct qcom_geni_private_data earlycon_private_data;
1403 static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
1406 struct uart_port *uport = &dev->port;
1408 u32 tx_parity_cfg = 0; /* Disable Tx Parity */
1409 u32 rx_trans_cfg = 0;
1410 u32 rx_parity_cfg = 0; /* Disable Rx Parity */
1411 u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
1415 if (!uport->membase)
1418 uport->private_data = &earlycon_private_data;
1420 memset(&se, 0, sizeof(se));
1421 se.base = uport->membase;
1422 if (geni_se_read_proto(&se) != GENI_SE_UART)
1425 * Ignore Flow control.
1428 tx_trans_cfg = UART_CTS_MASK;
1429 bits_per_char = BITS_PER_BYTE;
1432 * Make an unconditional cancel on the main sequencer to reset
1433 * it else we could end up in data loss scenarios.
1435 qcom_geni_serial_poll_tx_done(uport);
1436 qcom_geni_serial_abort_rx(uport);
1437 geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1439 geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
1440 geni_se_select_mode(&se, GENI_SE_FIFO);
1442 writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1443 writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1444 writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1445 writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1446 writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1447 writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1448 writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1450 dev->con->write = qcom_geni_serial_earlycon_write;
1451 dev->con->setup = NULL;
1452 qcom_geni_serial_enable_early_read(&se, dev->con);
1456 OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
1457 qcom_geni_serial_earlycon_setup);
1459 static int __init console_register(struct uart_driver *drv)
1461 return uart_register_driver(drv);
1464 static void console_unregister(struct uart_driver *drv)
1466 uart_unregister_driver(drv);
1469 static struct console cons_ops = {
1471 .write = qcom_geni_serial_console_write,
1472 .device = uart_console_device,
1473 .setup = qcom_geni_console_setup,
1474 .flags = CON_PRINTBUFFER,
1476 .data = &qcom_geni_console_driver,
1479 static struct uart_driver qcom_geni_console_driver = {
1480 .owner = THIS_MODULE,
1481 .driver_name = "qcom_geni_console",
1482 .dev_name = "ttyMSM",
1483 .nr = GENI_UART_CONS_PORTS,
1487 static int console_register(struct uart_driver *drv)
1492 static void console_unregister(struct uart_driver *drv)
1495 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1497 static struct uart_driver qcom_geni_uart_driver = {
1498 .owner = THIS_MODULE,
1499 .driver_name = "qcom_geni_uart",
1500 .dev_name = "ttyHS",
1501 .nr = GENI_UART_PORTS,
1504 static void qcom_geni_serial_pm(struct uart_port *uport,
1505 unsigned int new_state, unsigned int old_state)
1507 struct qcom_geni_serial_port *port = to_dev_port(uport);
1509 /* If we've never been called, treat it as off */
1510 if (old_state == UART_PM_STATE_UNDEFINED)
1511 old_state = UART_PM_STATE_OFF;
1513 if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
1514 geni_icc_enable(&port->se);
1516 dev_pm_opp_set_rate(uport->dev, port->clk_rate);
1517 geni_se_resources_on(&port->se);
1518 } else if (new_state == UART_PM_STATE_OFF &&
1519 old_state == UART_PM_STATE_ON) {
1520 geni_se_resources_off(&port->se);
1521 dev_pm_opp_set_rate(uport->dev, 0);
1522 geni_icc_disable(&port->se);
1526 static const struct uart_ops qcom_geni_console_pops = {
1527 .tx_empty = qcom_geni_serial_tx_empty,
1528 .stop_tx = qcom_geni_serial_stop_tx_fifo,
1529 .start_tx = qcom_geni_serial_start_tx_fifo,
1530 .stop_rx = qcom_geni_serial_stop_rx_fifo,
1531 .start_rx = qcom_geni_serial_start_rx_fifo,
1532 .set_termios = qcom_geni_serial_set_termios,
1533 .startup = qcom_geni_serial_startup,
1534 .request_port = qcom_geni_serial_request_port,
1535 .config_port = qcom_geni_serial_config_port,
1536 .shutdown = qcom_geni_serial_shutdown,
1537 .type = qcom_geni_serial_get_type,
1538 .set_mctrl = qcom_geni_serial_set_mctrl,
1539 .get_mctrl = qcom_geni_serial_get_mctrl,
1540 #ifdef CONFIG_CONSOLE_POLL
1541 .poll_get_char = qcom_geni_serial_get_char,
1542 .poll_put_char = qcom_geni_serial_poll_put_char,
1543 .poll_init = qcom_geni_serial_port_setup,
1545 .pm = qcom_geni_serial_pm,
1548 static const struct uart_ops qcom_geni_uart_pops = {
1549 .tx_empty = qcom_geni_serial_tx_empty,
1550 .stop_tx = qcom_geni_serial_stop_tx_dma,
1551 .start_tx = qcom_geni_serial_start_tx_dma,
1552 .start_rx = qcom_geni_serial_start_rx_dma,
1553 .stop_rx = qcom_geni_serial_stop_rx_dma,
1554 .set_termios = qcom_geni_serial_set_termios,
1555 .startup = qcom_geni_serial_startup,
1556 .request_port = qcom_geni_serial_request_port,
1557 .config_port = qcom_geni_serial_config_port,
1558 .shutdown = qcom_geni_serial_shutdown,
1559 .type = qcom_geni_serial_get_type,
1560 .set_mctrl = qcom_geni_serial_set_mctrl,
1561 .get_mctrl = qcom_geni_serial_get_mctrl,
1562 .pm = qcom_geni_serial_pm,
1565 static int qcom_geni_serial_probe(struct platform_device *pdev)
1569 struct qcom_geni_serial_port *port;
1570 struct uart_port *uport;
1571 struct resource *res;
1573 struct uart_driver *drv;
1574 const struct qcom_geni_device_data *data;
1576 data = of_device_get_match_data(&pdev->dev);
1580 if (data->console) {
1581 drv = &qcom_geni_console_driver;
1582 line = of_alias_get_id(pdev->dev.of_node, "serial");
1584 drv = &qcom_geni_uart_driver;
1585 line = of_alias_get_id(pdev->dev.of_node, "serial");
1586 if (line == -ENODEV) /* compat with non-standard aliases */
1587 line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1590 port = get_port_from_line(line, data->console);
1592 dev_err(&pdev->dev, "Invalid line %d\n", line);
1593 return PTR_ERR(port);
1596 uport = &port->uport;
1597 /* Don't allow 2 drivers to access the same port */
1598 if (uport->private_data)
1601 uport->dev = &pdev->dev;
1602 port->dev_data = data;
1603 port->se.dev = &pdev->dev;
1604 port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1605 port->se.clk = devm_clk_get(&pdev->dev, "se");
1606 if (IS_ERR(port->se.clk)) {
1607 ret = PTR_ERR(port->se.clk);
1608 dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1612 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1615 uport->mapbase = res->start;
1617 port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1618 port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1619 port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1621 if (!data->console) {
1622 port->rx_buf = devm_kzalloc(uport->dev,
1623 DMA_RX_BUF_SIZE, GFP_KERNEL);
1628 ret = geni_icc_get(&port->se, NULL);
1631 port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
1632 port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
1634 /* Set BW for register access */
1635 ret = geni_icc_set_bw(&port->se);
1639 port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1640 "qcom_geni_serial_%s%d",
1641 uart_console(uport) ? "console" : "uart", uport->line);
1645 irq = platform_get_irq(pdev, 0);
1649 uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1652 port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1654 if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1655 port->rx_tx_swap = true;
1657 if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1658 port->cts_rts_swap = true;
1660 ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1663 /* OPP table is optional */
1664 ret = devm_pm_opp_of_add_table(&pdev->dev);
1665 if (ret && ret != -ENODEV) {
1666 dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1670 port->private_data.drv = drv;
1671 uport->private_data = &port->private_data;
1672 platform_set_drvdata(pdev, port);
1674 irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1675 ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1676 IRQF_TRIGGER_HIGH, port->name, uport);
1678 dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1682 ret = uart_add_one_port(drv, uport);
1686 if (port->wakeup_irq > 0) {
1687 device_init_wakeup(&pdev->dev, true);
1688 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1691 device_init_wakeup(&pdev->dev, false);
1692 uart_remove_one_port(drv, uport);
1700 static void qcom_geni_serial_remove(struct platform_device *pdev)
1702 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1703 struct uart_driver *drv = port->private_data.drv;
1705 dev_pm_clear_wake_irq(&pdev->dev);
1706 device_init_wakeup(&pdev->dev, false);
1707 uart_remove_one_port(drv, &port->uport);
1710 static int qcom_geni_serial_sys_suspend(struct device *dev)
1712 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1713 struct uart_port *uport = &port->uport;
1714 struct qcom_geni_private_data *private_data = uport->private_data;
1717 * This is done so we can hit the lowest possible state in suspend
1718 * even with no_console_suspend
1720 if (uart_console(uport)) {
1721 geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ACTIVE_ONLY);
1722 geni_icc_set_bw(&port->se);
1724 return uart_suspend_port(private_data->drv, uport);
1727 static int qcom_geni_serial_sys_resume(struct device *dev)
1730 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1731 struct uart_port *uport = &port->uport;
1732 struct qcom_geni_private_data *private_data = uport->private_data;
1734 ret = uart_resume_port(private_data->drv, uport);
1735 if (uart_console(uport)) {
1736 geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
1737 geni_icc_set_bw(&port->se);
1742 static int qcom_geni_serial_sys_hib_resume(struct device *dev)
1745 struct uart_port *uport;
1746 struct qcom_geni_private_data *private_data;
1747 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1749 uport = &port->uport;
1750 private_data = uport->private_data;
1752 if (uart_console(uport)) {
1753 geni_icc_set_tag(&port->se, QCOM_ICC_TAG_ALWAYS);
1754 geni_icc_set_bw(&port->se);
1755 ret = uart_resume_port(private_data->drv, uport);
1757 * For hibernation usecase clients for
1758 * console UART won't call port setup during restore,
1759 * hence call port setup for console uart.
1761 qcom_geni_serial_port_setup(uport);
1764 * Peripheral register settings are lost during hibernation.
1765 * Update setup flag such that port setup happens again
1766 * during next session. Clients of HS-UART will close and
1767 * open the port during hibernation.
1769 port->setup = false;
1774 static const struct qcom_geni_device_data qcom_geni_console_data = {
1776 .mode = GENI_SE_FIFO,
1779 static const struct qcom_geni_device_data qcom_geni_uart_data = {
1781 .mode = GENI_SE_DMA,
1784 static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1785 .suspend = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1786 .resume = pm_sleep_ptr(qcom_geni_serial_sys_resume),
1787 .freeze = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1788 .poweroff = pm_sleep_ptr(qcom_geni_serial_sys_suspend),
1789 .restore = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1790 .thaw = pm_sleep_ptr(qcom_geni_serial_sys_hib_resume),
1793 static const struct of_device_id qcom_geni_serial_match_table[] = {
1795 .compatible = "qcom,geni-debug-uart",
1796 .data = &qcom_geni_console_data,
1799 .compatible = "qcom,geni-uart",
1800 .data = &qcom_geni_uart_data,
1804 MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1806 static struct platform_driver qcom_geni_serial_platform_driver = {
1807 .remove_new = qcom_geni_serial_remove,
1808 .probe = qcom_geni_serial_probe,
1810 .name = "qcom_geni_serial",
1811 .of_match_table = qcom_geni_serial_match_table,
1812 .pm = &qcom_geni_serial_pm_ops,
1816 static int __init qcom_geni_serial_init(void)
1820 ret = console_register(&qcom_geni_console_driver);
1824 ret = uart_register_driver(&qcom_geni_uart_driver);
1826 console_unregister(&qcom_geni_console_driver);
1830 ret = platform_driver_register(&qcom_geni_serial_platform_driver);
1832 console_unregister(&qcom_geni_console_driver);
1833 uart_unregister_driver(&qcom_geni_uart_driver);
1837 module_init(qcom_geni_serial_init);
1839 static void __exit qcom_geni_serial_exit(void)
1841 platform_driver_unregister(&qcom_geni_serial_platform_driver);
1842 console_unregister(&qcom_geni_console_driver);
1843 uart_unregister_driver(&qcom_geni_uart_driver);
1845 module_exit(qcom_geni_serial_exit);
1847 MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1848 MODULE_LICENSE("GPL v2");