Lines Matching +full:zynq +full:- +full:can +full:- +full:1

1 // SPDX-License-Identifier: GPL-2.0+
3 * Cadence UART driver (found in Xilinx Zynq)
5 * Copyright (c) 2011 - 2014 Xilinx, Inc.
7 * This driver has originally been pushed by Xilinx using a Zynq-branding. This
42 MODULE_PARM_DESC(rx_trigger_level, "Rx trigger level, 1-63 bytes");
47 MODULE_PARM_DESC(rx_timeout, "Rx timeout, 1-255");
90 #define CDNS_UART_MR_CLKSEL 0x00000001 /* Pre-scalar selection */
96 #define CDNS_UART_MR_STOPMODE_1_BIT 0x00000000 /* 1 stop bit */
114 * Writing a 1 to IER Enables an interrupt, writing a 1 to IDR disables an
151 #define CDNS_UART_RXBS_SUPPORT BIT(1)
190 * struct cdns_uart - device data
225 .delay_rts_before_send = 1,
226 .delay_rts_after_send = 1,
233 * cdns_uart_handle_rx - Handle the received bytes along with Rx errors.
241 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_handle_rx()
249 is_rxbs_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT; in cdns_uart_handle_rx()
251 while ((readl(port->membase + CDNS_UART_SR) & in cdns_uart_handle_rx()
254 rxbs_status = readl(port->membase + CDNS_UART_RXBS); in cdns_uart_handle_rx()
255 data = readl(port->membase + CDNS_UART_FIFO); in cdns_uart_handle_rx()
256 port->icount.rx++; in cdns_uart_handle_rx()
258 * There is no hardware break detection in Zynq, so we interpret in cdns_uart_handle_rx()
259 * framing error with all-zeros data as a break sequence. in cdns_uart_handle_rx()
260 * Most of the time, there's another non-zero byte at the in cdns_uart_handle_rx()
265 port->read_status_mask |= CDNS_UART_IXR_BRK; in cdns_uart_handle_rx()
266 framerrprocessed = 1; in cdns_uart_handle_rx()
271 port->icount.brk++; in cdns_uart_handle_rx()
277 isrstatus &= port->read_status_mask; in cdns_uart_handle_rx()
278 isrstatus &= ~port->ignore_status_mask; in cdns_uart_handle_rx()
279 status_mask = port->read_status_mask; in cdns_uart_handle_rx()
280 status_mask &= ~port->ignore_status_mask; in cdns_uart_handle_rx()
283 (port->read_status_mask & CDNS_UART_IXR_BRK)) { in cdns_uart_handle_rx()
284 port->read_status_mask &= ~CDNS_UART_IXR_BRK; in cdns_uart_handle_rx()
285 port->icount.brk++; in cdns_uart_handle_rx()
296 port->icount.parity++; in cdns_uart_handle_rx()
301 port->icount.frame++; in cdns_uart_handle_rx()
306 port->icount.parity++; in cdns_uart_handle_rx()
311 port->icount.frame++; in cdns_uart_handle_rx()
316 port->icount.overrun++; in cdns_uart_handle_rx()
317 tty_insert_flip_char(&port->state->port, 0, in cdns_uart_handle_rx()
320 tty_insert_flip_char(&port->state->port, data, status); in cdns_uart_handle_rx()
324 tty_flip_buffer_push(&port->state->port); in cdns_uart_handle_rx()
328 * cdns_rts_gpio_enable - Configure RTS/GPIO to high/low
336 if (cdns_uart->gpiod_rts) { in cdns_rts_gpio_enable()
337 gpiod_set_value(cdns_uart->gpiod_rts, enable); in cdns_rts_gpio_enable()
339 val = readl(cdns_uart->port->membase + CDNS_UART_MODEMCR); in cdns_rts_gpio_enable()
344 writel(val, cdns_uart->port->membase + CDNS_UART_MODEMCR); in cdns_rts_gpio_enable()
349 * cdns_rs485_tx_setup - Tx setup specific to rs485
356 enable = cdns_uart->port->rs485.flags & SER_RS485_RTS_ON_SEND; in cdns_rs485_tx_setup()
359 cdns_uart->rs485_tx_started = true; in cdns_rs485_tx_setup()
363 * cdns_rs485_rx_setup - Rx setup specific to rs485
370 enable = cdns_uart->port->rs485.flags & SER_RS485_RTS_AFTER_SEND; in cdns_rs485_rx_setup()
373 cdns_uart->rs485_tx_started = false; in cdns_rs485_rx_setup()
377 * cdns_uart_tx_empty - Check whether TX is empty
386 status = readl(port->membase + CDNS_UART_SR); in cdns_uart_tx_empty()
392 * cdns_rs485_rx_callback - Timer rx callback handler for rs485.
409 * cdns_calc_after_tx_delay - calculate delay required for after tx.
417 return cdns_uart->port->frame_time in cdns_calc_after_tx_delay()
418 + DIV_ROUND_UP(cdns_uart->port->frame_time, 7) in cdns_calc_after_tx_delay()
419 + (u64)cdns_uart->port->rs485.delay_rts_after_send * NSEC_PER_MSEC; in cdns_calc_after_tx_delay()
423 * cdns_uart_handle_tx - Handle the bytes to be transmitted.
430 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_handle_tx()
431 struct tty_port *tport = &port->state->port; in cdns_uart_handle_tx()
435 if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) { in cdns_uart_handle_tx()
437 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_IDR); in cdns_uart_handle_tx()
441 numbytes = port->fifosize; in cdns_uart_handle_tx()
443 !(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXFULL) && in cdns_uart_handle_tx()
445 writel(ch, port->membase + CDNS_UART_FIFO); in cdns_uart_handle_tx()
446 numbytes--; in cdns_uart_handle_tx()
449 if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) in cdns_uart_handle_tx()
453 writel(CDNS_UART_IXR_TXEMPTY, cdns_uart->port->membase + CDNS_UART_IER); in cdns_uart_handle_tx()
455 if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED && in cdns_uart_handle_tx()
456 (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port))) { in cdns_uart_handle_tx()
457 cdns_uart->tx_timer.function = &cdns_rs485_rx_callback; in cdns_uart_handle_tx()
458 hrtimer_start(&cdns_uart->tx_timer, in cdns_uart_handle_tx()
464 * cdns_uart_isr - Interrupt handler
480 isrstatus = readl(port->membase + CDNS_UART_ISR); in cdns_uart_isr()
481 writel(isrstatus, port->membase + CDNS_UART_ISR); in cdns_uart_isr()
488 isrstatus &= port->read_status_mask; in cdns_uart_isr()
489 isrstatus &= ~port->ignore_status_mask; in cdns_uart_isr()
495 !(readl(port->membase + CDNS_UART_CR) & CDNS_UART_CR_RX_DIS)) in cdns_uart_isr()
503 * cdns_uart_calc_baud_divs - Calculate baud rate divisors
513 * baud_tx/rx rate = clk/CD * (BDIV + 1)
531 if (baud < clk / ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX)) { in cdns_uart_calc_baud_divs()
532 *div8 = 1; in cdns_uart_calc_baud_divs()
539 cd = DIV_ROUND_CLOSEST(clk, baud * (bdiv + 1)); in cdns_uart_calc_baud_divs()
540 if (cd < 1 || cd > CDNS_UART_CD_MAX) in cdns_uart_calc_baud_divs()
543 calc_baud = clk / (cd * (bdiv + 1)); in cdns_uart_calc_baud_divs()
546 bauderror = baud - calc_baud; in cdns_uart_calc_baud_divs()
548 bauderror = calc_baud - baud; in cdns_uart_calc_baud_divs()
565 * cdns_uart_set_baud_rate - Calculate and set the baud rate
578 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_set_baud_rate()
580 calc_baud = cdns_uart_calc_baud_divs(port->uartclk, baud, &bdiv, &cd, in cdns_uart_set_baud_rate()
584 mreg = readl(port->membase + CDNS_UART_MR); in cdns_uart_set_baud_rate()
589 writel(mreg, port->membase + CDNS_UART_MR); in cdns_uart_set_baud_rate()
590 writel(cd, port->membase + CDNS_UART_BAUDGEN); in cdns_uart_set_baud_rate()
591 writel(bdiv, port->membase + CDNS_UART_BAUDDIV); in cdns_uart_set_baud_rate()
592 cdns_uart->baud = baud; in cdns_uart_set_baud_rate()
599 * cdns_uart_clk_notifier_cb - Clock notifier callback
615 port = cdns_uart->port; in cdns_uart_clk_notifier_cb()
616 if (port->suspended) in cdns_uart_clk_notifier_cb()
626 * Find out if current baud-rate can be achieved with new clock in cdns_uart_clk_notifier_cb()
629 if (!cdns_uart_calc_baud_divs(ndata->new_rate, cdns_uart->baud, in cdns_uart_clk_notifier_cb()
631 dev_warn(port->dev, "clock rate change rejected\n"); in cdns_uart_clk_notifier_cb()
635 uart_port_lock_irqsave(cdns_uart->port, &flags); in cdns_uart_clk_notifier_cb()
638 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_clk_notifier_cb()
640 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_clk_notifier_cb()
642 uart_port_unlock_irqrestore(cdns_uart->port, flags); in cdns_uart_clk_notifier_cb()
652 uart_port_lock_irqsave(cdns_uart->port, &flags); in cdns_uart_clk_notifier_cb()
654 locked = 1; in cdns_uart_clk_notifier_cb()
655 port->uartclk = ndata->new_rate; in cdns_uart_clk_notifier_cb()
657 cdns_uart->baud = cdns_uart_set_baud_rate(cdns_uart->port, in cdns_uart_clk_notifier_cb()
658 cdns_uart->baud); in cdns_uart_clk_notifier_cb()
662 uart_port_lock_irqsave(cdns_uart->port, &flags); in cdns_uart_clk_notifier_cb()
665 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_clk_notifier_cb()
667 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_clk_notifier_cb()
669 while (readl(port->membase + CDNS_UART_CR) & in cdns_uart_clk_notifier_cb()
678 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT); in cdns_uart_clk_notifier_cb()
679 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_clk_notifier_cb()
682 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_clk_notifier_cb()
684 uart_port_unlock_irqrestore(cdns_uart->port, flags); in cdns_uart_clk_notifier_cb()
694 * cdns_rs485_tx_callback - Timer tx callback handler for rs485.
701 uart_port_lock(cdns_uart->port); in cdns_rs485_tx_callback()
702 cdns_uart_handle_tx(cdns_uart->port); in cdns_rs485_tx_callback()
703 uart_port_unlock(cdns_uart->port); in cdns_rs485_tx_callback()
709 * cdns_uart_start_tx - Start transmitting bytes
715 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_start_tx()
724 status = readl(port->membase + CDNS_UART_CR); in cdns_uart_start_tx()
727 writel(status, port->membase + CDNS_UART_CR); in cdns_uart_start_tx()
729 if (kfifo_is_empty(&port->state->port.xmit_fifo)) in cdns_uart_start_tx()
733 writel(CDNS_UART_IXR_TXEMPTY, port->membase + CDNS_UART_ISR); in cdns_uart_start_tx()
735 if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED) { in cdns_uart_start_tx()
736 if (!cdns_uart->rs485_tx_started) { in cdns_uart_start_tx()
737 cdns_uart->tx_timer.function = &cdns_rs485_tx_callback; in cdns_uart_start_tx()
739 return hrtimer_start(&cdns_uart->tx_timer, in cdns_uart_start_tx()
740 ms_to_ktime(port->rs485.delay_rts_before_send), in cdns_uart_start_tx()
748 * cdns_uart_stop_tx - Stop TX
754 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_stop_tx()
756 if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED) in cdns_uart_stop_tx()
759 regval = readl(port->membase + CDNS_UART_CR); in cdns_uart_stop_tx()
762 writel(regval, port->membase + CDNS_UART_CR); in cdns_uart_stop_tx()
766 * cdns_uart_stop_rx - Stop RX
774 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IDR); in cdns_uart_stop_rx()
777 regval = readl(port->membase + CDNS_UART_CR); in cdns_uart_stop_rx()
779 writel(regval, port->membase + CDNS_UART_CR); in cdns_uart_stop_rx()
783 * cdns_uart_break_ctl - Based on the input ctl we have to start or stop
795 status = readl(port->membase + CDNS_UART_CR); in cdns_uart_break_ctl()
797 if (ctl == -1) in cdns_uart_break_ctl()
799 port->membase + CDNS_UART_CR); in cdns_uart_break_ctl()
803 port->membase + CDNS_UART_CR); in cdns_uart_break_ctl()
809 * cdns_uart_set_termios - termios operations, handling data length, parity,
827 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_set_termios()
829 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_set_termios()
833 * min and max baud should be calculated here based on port->uartclk. in cdns_uart_set_termios()
834 * this way we get a valid baud and can safely call set_baud() in cdns_uart_set_termios()
836 minbaud = port->uartclk / in cdns_uart_set_termios()
837 ((CDNS_UART_BDIV_MAX + 1) * CDNS_UART_CD_MAX * 8); in cdns_uart_set_termios()
838 maxbaud = port->uartclk / (CDNS_UART_BDIV_MIN + 1); in cdns_uart_set_termios()
844 /* Update the per-port timeout. */ in cdns_uart_set_termios()
845 uart_update_timeout(port, termios->c_cflag, baud); in cdns_uart_set_termios()
848 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_set_termios()
850 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_set_termios()
852 while (readl(port->membase + CDNS_UART_CR) & in cdns_uart_set_termios()
860 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_set_termios()
863 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_set_termios()
865 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT); in cdns_uart_set_termios()
867 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG | in cdns_uart_set_termios()
869 port->ignore_status_mask = 0; in cdns_uart_set_termios()
871 if (termios->c_iflag & INPCK) in cdns_uart_set_termios()
872 port->read_status_mask |= CDNS_UART_IXR_PARITY | in cdns_uart_set_termios()
875 if (termios->c_iflag & IGNPAR) in cdns_uart_set_termios()
876 port->ignore_status_mask |= CDNS_UART_IXR_PARITY | in cdns_uart_set_termios()
880 if ((termios->c_cflag & CREAD) == 0) in cdns_uart_set_termios()
881 port->ignore_status_mask |= CDNS_UART_IXR_RXTRIG | in cdns_uart_set_termios()
885 mode_reg = readl(port->membase + CDNS_UART_MR); in cdns_uart_set_termios()
888 switch (termios->c_cflag & CSIZE) { in cdns_uart_set_termios()
898 termios->c_cflag &= ~CSIZE; in cdns_uart_set_termios()
899 termios->c_cflag |= CS8; in cdns_uart_set_termios()
904 if (termios->c_cflag & CSTOPB) in cdns_uart_set_termios()
907 cval |= CDNS_UART_MR_STOPMODE_1_BIT; /* 1 STOP bit */ in cdns_uart_set_termios()
909 if (termios->c_cflag & PARENB) { in cdns_uart_set_termios()
911 if (termios->c_cflag & CMSPAR) { in cdns_uart_set_termios()
912 if (termios->c_cflag & PARODD) in cdns_uart_set_termios()
917 if (termios->c_cflag & PARODD) in cdns_uart_set_termios()
925 cval |= mode_reg & 1; in cdns_uart_set_termios()
926 writel(cval, port->membase + CDNS_UART_MR); in cdns_uart_set_termios()
928 cval = readl(port->membase + CDNS_UART_MODEMCR); in cdns_uart_set_termios()
929 if (termios->c_cflag & CRTSCTS) in cdns_uart_set_termios()
933 writel(cval, port->membase + CDNS_UART_MODEMCR); in cdns_uart_set_termios()
939 * cdns_uart_startup - Called when an application opens a cdns_uart port
946 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_startup()
952 is_brk_support = cdns_uart->quirks & CDNS_UART_RXBS_SUPPORT; in cdns_uart_startup()
954 ret = reset_control_deassert(cdns_uart->rstc); in cdns_uart_startup()
962 port->membase + CDNS_UART_CR); in cdns_uart_startup()
968 port->membase + CDNS_UART_CR); in cdns_uart_startup()
970 while (readl(port->membase + CDNS_UART_CR) & in cdns_uart_startup()
974 if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED) in cdns_uart_startup()
981 status = readl(port->membase + CDNS_UART_CR); in cdns_uart_startup()
984 writel(status, port->membase + CDNS_UART_CR); in cdns_uart_startup()
986 /* Set the Mode Register with normal mode,8 data bits,1 stop bit, in cdns_uart_startup()
991 port->membase + CDNS_UART_MR); in cdns_uart_startup()
995 * can be tuned with a module parameter in cdns_uart_startup()
997 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM); in cdns_uart_startup()
1001 * can be tuned with a module parameter in cdns_uart_startup()
1003 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT); in cdns_uart_startup()
1006 writel(readl(port->membase + CDNS_UART_ISR), in cdns_uart_startup()
1007 port->membase + CDNS_UART_ISR); in cdns_uart_startup()
1011 ret = request_irq(port->irq, cdns_uart_isr, 0, CDNS_UART_NAME, port); in cdns_uart_startup()
1013 dev_err(port->dev, "request_irq '%d' failed with %d\n", in cdns_uart_startup()
1014 port->irq, ret); in cdns_uart_startup()
1021 port->membase + CDNS_UART_IER); in cdns_uart_startup()
1023 writel(CDNS_UART_RX_IRQS, port->membase + CDNS_UART_IER); in cdns_uart_startup()
1029 * cdns_uart_shutdown - Called when an application closes a cdns_uart port
1036 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_shutdown()
1038 if (cdns_uart->port->rs485.flags & SER_RS485_ENABLED) in cdns_uart_shutdown()
1039 hrtimer_cancel(&cdns_uart->tx_timer); in cdns_uart_shutdown()
1044 status = readl(port->membase + CDNS_UART_IMR); in cdns_uart_shutdown()
1045 writel(status, port->membase + CDNS_UART_IDR); in cdns_uart_shutdown()
1046 writel(0xffffffff, port->membase + CDNS_UART_ISR); in cdns_uart_shutdown()
1050 port->membase + CDNS_UART_CR); in cdns_uart_shutdown()
1054 free_irq(port->irq, port); in cdns_uart_shutdown()
1058 * cdns_uart_type - Set UART type to cdns_uart port
1065 return port->type == PORT_XUARTPS ? CDNS_UART_NAME : NULL; in cdns_uart_type()
1069 * cdns_uart_verify_port - Verify the port params
1078 if (ser->type != PORT_UNKNOWN && ser->type != PORT_XUARTPS) in cdns_uart_verify_port()
1079 return -EINVAL; in cdns_uart_verify_port()
1080 if (port->irq != ser->irq) in cdns_uart_verify_port()
1081 return -EINVAL; in cdns_uart_verify_port()
1082 if (ser->io_type != UPIO_MEM) in cdns_uart_verify_port()
1083 return -EINVAL; in cdns_uart_verify_port()
1084 if (port->iobase != ser->port) in cdns_uart_verify_port()
1085 return -EINVAL; in cdns_uart_verify_port()
1086 if (ser->hub6 != 0) in cdns_uart_verify_port()
1087 return -EINVAL; in cdns_uart_verify_port()
1092 * cdns_uart_request_port - Claim the memory region attached to cdns_uart port,
1101 if (!request_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE, in cdns_uart_request_port()
1103 return -ENOMEM; in cdns_uart_request_port()
1106 port->membase = ioremap(port->mapbase, CDNS_UART_REGISTER_SPACE); in cdns_uart_request_port()
1107 if (!port->membase) { in cdns_uart_request_port()
1108 dev_err(port->dev, "Unable to map registers\n"); in cdns_uart_request_port()
1109 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE); in cdns_uart_request_port()
1110 return -ENOMEM; in cdns_uart_request_port()
1116 * cdns_uart_release_port - Release UART port
1124 release_mem_region(port->mapbase, CDNS_UART_REGISTER_SPACE); in cdns_uart_release_port()
1125 iounmap(port->membase); in cdns_uart_release_port()
1126 port->membase = NULL; in cdns_uart_release_port()
1130 * cdns_uart_config_port - Configure UART port
1137 port->type = PORT_XUARTPS; in cdns_uart_config_port()
1141 * cdns_uart_get_mctrl - Get the modem control state
1150 struct cdns_uart *cdns_uart_data = port->private_data; in cdns_uart_get_mctrl()
1152 if (cdns_uart_data->cts_override) in cdns_uart_get_mctrl()
1155 val = readl(port->membase + CDNS_UART_MODEMSR); in cdns_uart_get_mctrl()
1172 struct cdns_uart *cdns_uart_data = port->private_data; in cdns_uart_set_mctrl()
1174 if (cdns_uart_data->cts_override) in cdns_uart_set_mctrl()
1177 val = readl(port->membase + CDNS_UART_MODEMCR); in cdns_uart_set_mctrl()
1178 mode_reg = readl(port->membase + CDNS_UART_MR); in cdns_uart_set_mctrl()
1185 if (cdns_uart_data->gpiod_rts) in cdns_uart_set_mctrl()
1186 gpiod_set_value(cdns_uart_data->gpiod_rts, !(mctrl & TIOCM_RTS)); in cdns_uart_set_mctrl()
1194 writel(val, port->membase + CDNS_UART_MODEMCR); in cdns_uart_set_mctrl()
1195 writel(mode_reg, port->membase + CDNS_UART_MR); in cdns_uart_set_mctrl()
1207 if (readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_RXEMPTY) in cdns_uart_poll_get_char()
1210 c = (unsigned char) readl(port->membase + CDNS_UART_FIFO); in cdns_uart_poll_get_char()
1224 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY)) in cdns_uart_poll_put_char()
1228 writel(c, port->membase + CDNS_UART_FIFO); in cdns_uart_poll_put_char()
1231 while (!(readl(port->membase + CDNS_UART_SR) & CDNS_UART_SR_TXEMPTY)) in cdns_uart_poll_put_char()
1243 pm_runtime_mark_last_busy(port->dev); in cdns_uart_pm()
1244 pm_runtime_put_autosuspend(port->dev); in cdns_uart_pm()
1247 pm_runtime_get_sync(port->dev); in cdns_uart_pm()
1279 * cdns_uart_console_putchar - write the character to the FIFO buffer
1289 while (1) { in cdns_uart_console_putchar()
1290 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_console_putchar()
1294 dev_warn(port->dev, in cdns_uart_console_putchar()
1302 while (1) { in cdns_uart_console_putchar()
1303 ctrl_reg = readl(port->membase + CDNS_UART_SR); in cdns_uart_console_putchar()
1308 dev_warn(port->dev, in cdns_uart_console_putchar()
1314 writel(ch, port->membase + CDNS_UART_FIFO); in cdns_uart_console_putchar()
1320 struct earlycon_device *dev = con->data; in cdns_early_write()
1322 uart_console_write(&dev->port, s, n, cdns_uart_console_putchar); in cdns_early_write()
1328 struct uart_port *port = &device->port; in cdns_early_console_setup()
1330 if (!port->membase) in cdns_early_console_setup()
1331 return -ENODEV; in cdns_early_console_setup()
1335 port->membase + CDNS_UART_CR); in cdns_early_console_setup()
1337 /* only set baud if specified on command line - otherwise in cdns_early_console_setup()
1340 if (port->uartclk && device->baud) { in cdns_early_console_setup()
1345 cdns_uart_calc_baud_divs(port->uartclk, device->baud, in cdns_early_console_setup()
1351 writel(mr, port->membase + CDNS_UART_MR); in cdns_early_console_setup()
1352 writel(cd, port->membase + CDNS_UART_BAUDGEN); in cdns_early_console_setup()
1353 writel(bdiv, port->membase + CDNS_UART_BAUDDIV); in cdns_early_console_setup()
1356 device->con->write = cdns_early_write; in cdns_early_console_setup()
1361 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p8", cdns_early_console_setup);
1362 OF_EARLYCON_DECLARE(cdns, "cdns,uart-r1p12", cdns_early_console_setup);
1363 OF_EARLYCON_DECLARE(cdns, "xlnx,zynqmp-uart", cdns_early_console_setup);
1370 * cdns_uart_console_write - perform write operation
1381 int locked = 1; in cdns_uart_console_write()
1383 if (port->sysrq) in cdns_uart_console_write()
1391 imr = readl(port->membase + CDNS_UART_IMR); in cdns_uart_console_write()
1392 writel(imr, port->membase + CDNS_UART_IDR); in cdns_uart_console_write()
1398 ctrl = readl(port->membase + CDNS_UART_CR); in cdns_uart_console_write()
1401 writel(ctrl, port->membase + CDNS_UART_CR); in cdns_uart_console_write()
1408 writel(imr, port->membase + CDNS_UART_IER); in cdns_uart_console_write()
1415 * cdns_uart_console_setup - Initialize the uart to default config
1431 if (!port->membase) { in cdns_uart_console_setup()
1433 co->index); in cdns_uart_console_setup()
1434 return -ENODEV; in cdns_uart_console_setup()
1456 .index = -1, /* Specified on the cmdline (e.g. console=ttyPS ) */
1463 * cdns_uart_suspend - suspend event
1471 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_suspend()
1480 /* Empty the receive FIFO 1st before making changes */ in cdns_uart_suspend()
1481 while (!(readl(port->membase + CDNS_UART_SR) & in cdns_uart_suspend()
1483 readl(port->membase + CDNS_UART_FIFO); in cdns_uart_suspend()
1484 /* set RX trigger level to 1 */ in cdns_uart_suspend()
1485 writel(1, port->membase + CDNS_UART_RXWM); in cdns_uart_suspend()
1487 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IDR); in cdns_uart_suspend()
1495 return uart_suspend_port(cdns_uart->cdns_uart_driver, port); in cdns_uart_suspend()
1499 * cdns_uart_resume - Resume after a previous suspend
1507 struct cdns_uart *cdns_uart = port->private_data; in cdns_uart_resume()
1516 ret = clk_enable(cdns_uart->pclk); in cdns_uart_resume()
1520 ret = clk_enable(cdns_uart->uartclk); in cdns_uart_resume()
1522 clk_disable(cdns_uart->pclk); in cdns_uart_resume()
1529 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_resume()
1531 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_resume()
1532 while (readl(port->membase + CDNS_UART_CR) & in cdns_uart_resume()
1537 writel(rx_timeout, port->membase + CDNS_UART_RXTOUT); in cdns_uart_resume()
1539 ctrl_reg = readl(port->membase + CDNS_UART_CR); in cdns_uart_resume()
1542 writel(ctrl_reg, port->membase + CDNS_UART_CR); in cdns_uart_resume()
1544 clk_disable(cdns_uart->uartclk); in cdns_uart_resume()
1545 clk_disable(cdns_uart->pclk); in cdns_uart_resume()
1550 writel(rx_trigger_level, port->membase + CDNS_UART_RXWM); in cdns_uart_resume()
1552 writel(CDNS_UART_IXR_TOUT, port->membase + CDNS_UART_IER); in cdns_uart_resume()
1556 return uart_resume_port(cdns_uart->cdns_uart_driver, port); in cdns_uart_resume()
1562 struct cdns_uart *cdns_uart = port->private_data; in cdns_runtime_suspend()
1564 clk_disable(cdns_uart->uartclk); in cdns_runtime_suspend()
1565 clk_disable(cdns_uart->pclk); in cdns_runtime_suspend()
1572 struct cdns_uart *cdns_uart = port->private_data; in cdns_runtime_resume()
1575 ret = clk_enable(cdns_uart->pclk); in cdns_runtime_resume()
1579 ret = clk_enable(cdns_uart->uartclk); in cdns_runtime_resume()
1581 clk_disable(cdns_uart->pclk); in cdns_runtime_resume()
1599 { .compatible = "cdns,uart-r1p8", },
1600 { .compatible = "cdns,uart-r1p12", .data = &zynqmp_uart_def },
1601 { .compatible = "xlnx,zynqmp-uart", .data = &zynqmp_uart_def },
1610 * cdns_rs485_config - Called when an application calls TIOCSRS485 ioctl.
1621 struct cdns_uart *cdns_uart = port->private_data; in cdns_rs485_config()
1623 if (rs485->flags & SER_RS485_ENABLED) { in cdns_rs485_config()
1624 dev_dbg(port->dev, "Setting UART to RS485\n"); in cdns_rs485_config()
1626 val = readl(port->membase + CDNS_UART_MODEMCR); in cdns_rs485_config()
1628 writel(val, port->membase + CDNS_UART_MODEMCR); in cdns_rs485_config()
1631 hrtimer_init(&cdns_uart->tx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); in cdns_rs485_config()
1632 cdns_uart->tx_timer.function = &cdns_rs485_tx_callback; in cdns_rs485_config()
1637 hrtimer_cancel(&cdns_uart->tx_timer); in cdns_rs485_config()
1643 * cdns_uart_probe - Platform driver probe
1656 cdns_uart_data = devm_kzalloc(&pdev->dev, sizeof(*cdns_uart_data), in cdns_uart_probe()
1659 return -ENOMEM; in cdns_uart_probe()
1660 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL); in cdns_uart_probe()
1662 return -ENOMEM; in cdns_uart_probe()
1665 id = of_alias_get_id(pdev->dev.of_node, "serial"); in cdns_uart_probe()
1670 dev_err(&pdev->dev, "Cannot get uart_port structure\n"); in cdns_uart_probe()
1671 return -ENODEV; in cdns_uart_probe()
1687 dev_err(&pdev->dev, "Failed to register driver\n"); in cdns_uart_probe()
1692 cdns_uart_data->cdns_uart_driver = &cdns_uart_uart_driver; in cdns_uart_probe()
1694 match = of_match_node(cdns_uart_of_match, pdev->dev.of_node); in cdns_uart_probe()
1695 if (match && match->data) { in cdns_uart_probe()
1696 const struct cdns_platform_data *data = match->data; in cdns_uart_probe()
1698 cdns_uart_data->quirks = data->quirks; in cdns_uart_probe()
1701 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "pclk"); in cdns_uart_probe()
1702 if (PTR_ERR(cdns_uart_data->pclk) == -EPROBE_DEFER) { in cdns_uart_probe()
1703 rc = PTR_ERR(cdns_uart_data->pclk); in cdns_uart_probe()
1707 if (IS_ERR(cdns_uart_data->pclk)) { in cdns_uart_probe()
1708 cdns_uart_data->pclk = devm_clk_get(&pdev->dev, "aper_clk"); in cdns_uart_probe()
1709 if (IS_ERR(cdns_uart_data->pclk)) { in cdns_uart_probe()
1710 rc = PTR_ERR(cdns_uart_data->pclk); in cdns_uart_probe()
1713 dev_err(&pdev->dev, "clock name 'aper_clk' is deprecated.\n"); in cdns_uart_probe()
1716 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "uart_clk"); in cdns_uart_probe()
1717 if (PTR_ERR(cdns_uart_data->uartclk) == -EPROBE_DEFER) { in cdns_uart_probe()
1718 rc = PTR_ERR(cdns_uart_data->uartclk); in cdns_uart_probe()
1722 if (IS_ERR(cdns_uart_data->uartclk)) { in cdns_uart_probe()
1723 cdns_uart_data->uartclk = devm_clk_get(&pdev->dev, "ref_clk"); in cdns_uart_probe()
1724 if (IS_ERR(cdns_uart_data->uartclk)) { in cdns_uart_probe()
1725 rc = PTR_ERR(cdns_uart_data->uartclk); in cdns_uart_probe()
1728 dev_err(&pdev->dev, "clock name 'ref_clk' is deprecated.\n"); in cdns_uart_probe()
1731 cdns_uart_data->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL); in cdns_uart_probe()
1732 if (IS_ERR(cdns_uart_data->rstc)) { in cdns_uart_probe()
1733 rc = PTR_ERR(cdns_uart_data->rstc); in cdns_uart_probe()
1734 dev_err_probe(&pdev->dev, rc, "Cannot get UART reset\n"); in cdns_uart_probe()
1738 rc = clk_prepare_enable(cdns_uart_data->pclk); in cdns_uart_probe()
1740 dev_err(&pdev->dev, "Unable to enable pclk clock.\n"); in cdns_uart_probe()
1743 rc = clk_prepare_enable(cdns_uart_data->uartclk); in cdns_uart_probe()
1745 dev_err(&pdev->dev, "Unable to enable device clock.\n"); in cdns_uart_probe()
1751 rc = -ENODEV; in cdns_uart_probe()
1762 cdns_uart_data->clk_rate_change_nb.notifier_call = in cdns_uart_probe()
1764 if (clk_notifier_register(cdns_uart_data->uartclk, in cdns_uart_probe()
1765 &cdns_uart_data->clk_rate_change_nb)) in cdns_uart_probe()
1766 dev_warn(&pdev->dev, "Unable to register clock notifier.\n"); in cdns_uart_probe()
1770 spin_lock_init(&port->lock); in cdns_uart_probe()
1771 port->type = PORT_UNKNOWN; in cdns_uart_probe()
1772 port->iotype = UPIO_MEM32; in cdns_uart_probe()
1773 port->flags = UPF_BOOT_AUTOCONF; in cdns_uart_probe()
1774 port->ops = &cdns_uart_ops; in cdns_uart_probe()
1775 port->fifosize = CDNS_UART_FIFO_SIZE; in cdns_uart_probe()
1776 port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_XILINX_PS_UART_CONSOLE); in cdns_uart_probe()
1777 port->line = id; in cdns_uart_probe()
1784 port->mapbase = res->start; in cdns_uart_probe()
1785 port->irq = irq; in cdns_uart_probe()
1786 port->dev = &pdev->dev; in cdns_uart_probe()
1787 port->uartclk = clk_get_rate(cdns_uart_data->uartclk); in cdns_uart_probe()
1788 port->private_data = cdns_uart_data; in cdns_uart_probe()
1789 port->read_status_mask = CDNS_UART_IXR_TXEMPTY | CDNS_UART_IXR_RXTRIG | in cdns_uart_probe()
1791 port->rs485_config = cdns_rs485_config; in cdns_uart_probe()
1792 port->rs485_supported = cdns_rs485_supported; in cdns_uart_probe()
1793 cdns_uart_data->port = port; in cdns_uart_probe()
1800 cdns_uart_data->gpiod_rts = devm_gpiod_get_optional(&pdev->dev, "rts", in cdns_uart_probe()
1802 if (IS_ERR(cdns_uart_data->gpiod_rts)) { in cdns_uart_probe()
1803 rc = PTR_ERR(cdns_uart_data->gpiod_rts); in cdns_uart_probe()
1804 dev_err(port->dev, "xuartps: devm_gpiod_get_optional failed\n"); in cdns_uart_probe()
1808 pm_runtime_use_autosuspend(&pdev->dev); in cdns_uart_probe()
1809 pm_runtime_set_autosuspend_delay(&pdev->dev, UART_AUTOSUSPEND_TIMEOUT); in cdns_uart_probe()
1810 pm_runtime_set_active(&pdev->dev); in cdns_uart_probe()
1811 pm_runtime_enable(&pdev->dev); in cdns_uart_probe()
1812 device_init_wakeup(port->dev, true); in cdns_uart_probe()
1826 if (cdns_uart_data->port->rs485.flags & SER_RS485_ENABLED) in cdns_uart_probe()
1831 dev_err(&pdev->dev, in cdns_uart_probe()
1841 cdns_uart_console.index = -1; in cdns_uart_probe()
1845 cdns_uart_data->cts_override = of_property_read_bool(pdev->dev.of_node, in cdns_uart_probe()
1846 "cts-override"); in cdns_uart_probe()
1853 pm_runtime_disable(&pdev->dev); in cdns_uart_probe()
1854 pm_runtime_set_suspended(&pdev->dev); in cdns_uart_probe()
1855 pm_runtime_dont_use_autosuspend(&pdev->dev); in cdns_uart_probe()
1858 clk_notifier_unregister(cdns_uart_data->uartclk, in cdns_uart_probe()
1859 &cdns_uart_data->clk_rate_change_nb); in cdns_uart_probe()
1862 clk_disable_unprepare(cdns_uart_data->uartclk); in cdns_uart_probe()
1864 clk_disable_unprepare(cdns_uart_data->pclk); in cdns_uart_probe()
1867 uart_unregister_driver(cdns_uart_data->cdns_uart_driver); in cdns_uart_probe()
1872 * cdns_uart_remove - called when the platform driver is unregistered
1878 struct cdns_uart *cdns_uart_data = port->private_data; in cdns_uart_remove()
1882 clk_notifier_unregister(cdns_uart_data->uartclk, in cdns_uart_remove()
1883 &cdns_uart_data->clk_rate_change_nb); in cdns_uart_remove()
1885 uart_remove_one_port(cdns_uart_data->cdns_uart_driver, port); in cdns_uart_remove()
1886 port->mapbase = 0; in cdns_uart_remove()
1887 clk_disable_unprepare(cdns_uart_data->uartclk); in cdns_uart_remove()
1888 clk_disable_unprepare(cdns_uart_data->pclk); in cdns_uart_remove()
1889 pm_runtime_disable(&pdev->dev); in cdns_uart_remove()
1890 pm_runtime_set_suspended(&pdev->dev); in cdns_uart_remove()
1891 pm_runtime_dont_use_autosuspend(&pdev->dev); in cdns_uart_remove()
1892 device_init_wakeup(&pdev->dev, false); in cdns_uart_remove()
1898 reset_control_assert(cdns_uart_data->rstc); in cdns_uart_remove()
1900 if (!--instances) in cdns_uart_remove()
1901 uart_unregister_driver(cdns_uart_data->cdns_uart_driver); in cdns_uart_remove()