Lines Matching +full:rs485 +full:- +full:rts +full:- +full:delay
1 // SPDX-License-Identifier: GPL-2.0
3 * Atheros AR933X SoC built-in UART driver
15 #include <linux/delay.h>
31 #include <asm/mach-ath79/ar933x_uart.h>
35 #define DRIVER_NAME "ar933x-uart"
60 return readl(up->port.membase + offset); in ar933x_uart_read()
66 writel(value, up->port.membase + offset); in ar933x_uart_write()
98 up->ier |= AR933X_UART_INT_TX_EMPTY; in ar933x_uart_start_tx_interrupt()
99 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_start_tx_interrupt()
104 up->ier &= ~AR933X_UART_INT_TX_EMPTY; in ar933x_uart_stop_tx_interrupt()
105 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_stop_tx_interrupt()
110 up->ier |= AR933X_UART_INT_RX_VALID; in ar933x_uart_start_rx_interrupt()
111 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_start_rx_interrupt()
116 up->ier &= ~AR933X_UART_INT_RX_VALID; in ar933x_uart_stop_rx_interrupt()
117 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_stop_rx_interrupt()
136 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_tx_empty()
138 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_tx_empty()
149 mctrl_gpio_get(up->gpios, &ret); in ar933x_uart_get_mctrl()
159 mctrl_gpio_set(up->gpios, mctrl); in ar933x_uart_set_mctrl()
178 if (--timeout == 0) in ar933x_uart_wait_tx_complete()
184 dev_err(up->port.dev, "waiting for TX timed out\n"); in ar933x_uart_wait_tx_complete()
223 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_break_ctl()
224 if (break_state == -1) in ar933x_uart_break_ctl()
230 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_break_ctl()
275 diff = abs(ar933x_uart_get_baud(clk, tscale, tstep) - baud); in ar933x_uart_get_scale_step()
295 new->c_cflag &= ~CSIZE; in ar933x_uart_set_termios()
296 new->c_cflag |= CS8; in ar933x_uart_set_termios()
299 new->c_cflag &= ~CSTOPB; in ar933x_uart_set_termios()
302 if (new->c_cflag & PARENB) { in ar933x_uart_set_termios()
303 if (!(new->c_cflag & PARODD)) in ar933x_uart_set_termios()
312 new->c_cflag &= ~CMSPAR; in ar933x_uart_set_termios()
314 baud = uart_get_baud_rate(port, new, old, up->min_baud, up->max_baud); in ar933x_uart_set_termios()
315 ar933x_uart_get_scale_step(port->uartclk, baud, &scale, &step); in ar933x_uart_set_termios()
321 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_set_termios()
327 /* Update the per-port timeout. */ in ar933x_uart_set_termios()
328 uart_update_timeout(port, new->c_cflag, baud); in ar933x_uart_set_termios()
330 up->port.ignore_status_mask = 0; in ar933x_uart_set_termios()
333 if ((new->c_cflag & CREAD) == 0) in ar933x_uart_set_termios()
334 up->port.ignore_status_mask |= AR933X_DUMMY_STATUS_RD; in ar933x_uart_set_termios()
355 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_set_termios()
363 struct tty_port *port = &up->port.state->port; in ar933x_uart_rx_chars()
378 up->port.icount.rx++; in ar933x_uart_rx_chars()
381 if (uart_handle_sysrq_char(&up->port, ch)) in ar933x_uart_rx_chars()
384 if ((up->port.ignore_status_mask & AR933X_DUMMY_STATUS_RD) == 0) in ar933x_uart_rx_chars()
386 } while (max_count-- > 0); in ar933x_uart_rx_chars()
393 struct circ_buf *xmit = &up->port.state->xmit; in ar933x_uart_tx_chars()
394 struct serial_rs485 *rs485conf = &up->port.rs485; in ar933x_uart_tx_chars()
398 if (uart_tx_stopped(&up->port)) in ar933x_uart_tx_chars()
401 if ((rs485conf->flags & SER_RS485_ENABLED) && in ar933x_uart_tx_chars()
402 (up->port.x_char || !uart_circ_empty(xmit))) { in ar933x_uart_tx_chars()
404 gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_ON_SEND)); in ar933x_uart_tx_chars()
408 count = up->port.fifosize; in ar933x_uart_tx_chars()
416 if (up->port.x_char) { in ar933x_uart_tx_chars()
417 ar933x_uart_putc(up, up->port.x_char); in ar933x_uart_tx_chars()
418 up->port.icount.tx++; in ar933x_uart_tx_chars()
419 up->port.x_char = 0; in ar933x_uart_tx_chars()
426 ar933x_uart_putc(up, xmit->buf[xmit->tail]); in ar933x_uart_tx_chars()
428 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); in ar933x_uart_tx_chars()
429 up->port.icount.tx++; in ar933x_uart_tx_chars()
430 } while (--count > 0); in ar933x_uart_tx_chars()
433 uart_write_wakeup(&up->port); in ar933x_uart_tx_chars()
441 gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND)); in ar933x_uart_tx_chars()
454 spin_lock(&up->port.lock); in ar933x_uart_interrupt()
472 spin_unlock(&up->port.lock); in ar933x_uart_interrupt()
484 ret = request_irq(up->port.irq, ar933x_uart_interrupt, in ar933x_uart_startup()
485 up->port.irqflags, dev_name(up->port.dev), up); in ar933x_uart_startup()
489 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_startup()
502 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_startup()
513 up->ier = 0; in ar933x_uart_shutdown()
514 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_shutdown()
520 free_irq(up->port.irq, up); in ar933x_uart_shutdown()
525 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL; in ar933x_uart_type()
542 port->type = PORT_AR933X; in ar933x_uart_config_port()
551 if (ser->type != PORT_UNKNOWN && in ar933x_uart_verify_port()
552 ser->type != PORT_AR933X) in ar933x_uart_verify_port()
553 return -EINVAL; in ar933x_uart_verify_port()
555 if (ser->irq < 0 || ser->irq >= NR_IRQS) in ar933x_uart_verify_port()
556 return -EINVAL; in ar933x_uart_verify_port()
558 if (ser->baud_base < up->min_baud || in ar933x_uart_verify_port()
559 ser->baud_base > up->max_baud) in ar933x_uart_verify_port()
560 return -EINVAL; in ar933x_uart_verify_port()
589 if ((rs485conf->flags & SER_RS485_ENABLED) && in ar933x_config_rs485()
590 !up->rts_gpiod) { in ar933x_config_rs485()
591 dev_err(port->dev, "RS485 needs rts-gpio\n"); in ar933x_config_rs485()
594 port->rs485 = *rs485conf; in ar933x_config_rs485()
610 if (--timeout == 0) in ar933x_uart_wait_xmitr()
628 struct ar933x_uart_port *up = ar933x_console_ports[co->index]; in ar933x_uart_console_write()
635 if (up->port.sysrq) in ar933x_uart_console_write()
638 locked = spin_trylock(&up->port.lock); in ar933x_uart_console_write()
640 spin_lock(&up->port.lock); in ar933x_uart_console_write()
648 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar); in ar933x_uart_console_write()
660 spin_unlock(&up->port.lock); in ar933x_uart_console_write()
673 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS) in ar933x_uart_console_setup()
674 return -EINVAL; in ar933x_uart_console_setup()
676 up = ar933x_console_ports[co->index]; in ar933x_uart_console_setup()
678 return -ENODEV; in ar933x_uart_console_setup()
683 return uart_set_options(&up->port, co, baud, parity, bits, flow); in ar933x_uart_console_setup()
692 .index = -1,
716 np = pdev->dev.of_node; in ar933x_uart_probe()
720 dev_err(&pdev->dev, "unable to get alias id, err=%d\n", in ar933x_uart_probe()
725 id = pdev->id; in ar933x_uart_probe()
726 if (id == -1) in ar933x_uart_probe()
731 return -EINVAL; in ar933x_uart_probe()
735 dev_err(&pdev->dev, "no IRQ resource\n"); in ar933x_uart_probe()
736 return -EINVAL; in ar933x_uart_probe()
739 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port), in ar933x_uart_probe()
742 return -ENOMEM; in ar933x_uart_probe()
744 up->clk = devm_clk_get(&pdev->dev, "uart"); in ar933x_uart_probe()
745 if (IS_ERR(up->clk)) { in ar933x_uart_probe()
746 dev_err(&pdev->dev, "unable to get UART clock\n"); in ar933x_uart_probe()
747 return PTR_ERR(up->clk); in ar933x_uart_probe()
750 port = &up->port; in ar933x_uart_probe()
753 port->membase = devm_ioremap_resource(&pdev->dev, mem_res); in ar933x_uart_probe()
754 if (IS_ERR(port->membase)) in ar933x_uart_probe()
755 return PTR_ERR(port->membase); in ar933x_uart_probe()
757 ret = clk_prepare_enable(up->clk); in ar933x_uart_probe()
761 port->uartclk = clk_get_rate(up->clk); in ar933x_uart_probe()
762 if (!port->uartclk) { in ar933x_uart_probe()
763 ret = -EINVAL; in ar933x_uart_probe()
767 port->mapbase = mem_res->start; in ar933x_uart_probe()
768 port->line = id; in ar933x_uart_probe()
769 port->irq = irq_res->start; in ar933x_uart_probe()
770 port->dev = &pdev->dev; in ar933x_uart_probe()
771 port->type = PORT_AR933X; in ar933x_uart_probe()
772 port->iotype = UPIO_MEM32; in ar933x_uart_probe()
774 port->regshift = 2; in ar933x_uart_probe()
775 port->fifosize = AR933X_UART_FIFO_SIZE; in ar933x_uart_probe()
776 port->ops = &ar933x_uart_ops; in ar933x_uart_probe()
777 port->rs485_config = ar933x_config_rs485; in ar933x_uart_probe()
779 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1); in ar933x_uart_probe()
780 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD); in ar933x_uart_probe()
782 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP); in ar933x_uart_probe()
783 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD); in ar933x_uart_probe()
789 up->gpios = mctrl_gpio_init(port, 0); in ar933x_uart_probe()
790 if (IS_ERR(up->gpios) && PTR_ERR(up->gpios) != -ENOSYS) { in ar933x_uart_probe()
791 ret = PTR_ERR(up->gpios); in ar933x_uart_probe()
795 up->rts_gpiod = mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS); in ar933x_uart_probe()
797 if ((port->rs485.flags & SER_RS485_ENABLED) && in ar933x_uart_probe()
798 !up->rts_gpiod) { in ar933x_uart_probe()
799 dev_err(&pdev->dev, "lacking rts-gpio, disabling RS485\n"); in ar933x_uart_probe()
800 port->rs485.flags &= ~SER_RS485_ENABLED; in ar933x_uart_probe()
804 ar933x_console_ports[up->port.line] = up; in ar933x_uart_probe()
807 ret = uart_add_one_port(&ar933x_uart_driver, &up->port); in ar933x_uart_probe()
815 clk_disable_unprepare(up->clk); in ar933x_uart_probe()
826 uart_remove_one_port(&ar933x_uart_driver, &up->port); in ar933x_uart_remove()
827 clk_disable_unprepare(up->clk); in ar933x_uart_remove()
835 { .compatible = "qca,ar9330-uart" },