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()
388 spin_unlock(&up->port.lock); in ar933x_uart_rx_chars()
390 spin_lock(&up->port.lock); in ar933x_uart_rx_chars()
395 struct circ_buf *xmit = &up->port.state->xmit; in ar933x_uart_tx_chars()
396 struct serial_rs485 *rs485conf = &up->port.rs485; in ar933x_uart_tx_chars()
400 if (uart_tx_stopped(&up->port)) in ar933x_uart_tx_chars()
403 if ((rs485conf->flags & SER_RS485_ENABLED) && in ar933x_uart_tx_chars()
404 (up->port.x_char || !uart_circ_empty(xmit))) { in ar933x_uart_tx_chars()
406 gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_ON_SEND)); in ar933x_uart_tx_chars()
410 count = up->port.fifosize; in ar933x_uart_tx_chars()
418 if (up->port.x_char) { in ar933x_uart_tx_chars()
419 ar933x_uart_putc(up, up->port.x_char); in ar933x_uart_tx_chars()
420 up->port.icount.tx++; in ar933x_uart_tx_chars()
421 up->port.x_char = 0; in ar933x_uart_tx_chars()
428 ar933x_uart_putc(up, xmit->buf[xmit->tail]); in ar933x_uart_tx_chars()
430 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); in ar933x_uart_tx_chars()
431 up->port.icount.tx++; in ar933x_uart_tx_chars()
432 } while (--count > 0); in ar933x_uart_tx_chars()
435 uart_write_wakeup(&up->port); in ar933x_uart_tx_chars()
443 gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND)); in ar933x_uart_tx_chars()
456 spin_lock(&up->port.lock); in ar933x_uart_interrupt()
474 spin_unlock(&up->port.lock); in ar933x_uart_interrupt()
486 ret = request_irq(up->port.irq, ar933x_uart_interrupt, in ar933x_uart_startup()
487 up->port.irqflags, dev_name(up->port.dev), up); in ar933x_uart_startup()
491 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_startup()
504 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_startup()
515 up->ier = 0; in ar933x_uart_shutdown()
516 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_shutdown()
522 free_irq(up->port.irq, up); in ar933x_uart_shutdown()
527 return (port->type == PORT_AR933X) ? "AR933X UART" : NULL; in ar933x_uart_type()
544 port->type = PORT_AR933X; in ar933x_uart_config_port()
553 if (ser->type != PORT_UNKNOWN && in ar933x_uart_verify_port()
554 ser->type != PORT_AR933X) in ar933x_uart_verify_port()
555 return -EINVAL; in ar933x_uart_verify_port()
557 if (ser->irq < 0 || ser->irq >= NR_IRQS) in ar933x_uart_verify_port()
558 return -EINVAL; in ar933x_uart_verify_port()
560 if (ser->baud_base < up->min_baud || in ar933x_uart_verify_port()
561 ser->baud_base > up->max_baud) in ar933x_uart_verify_port()
562 return -EINVAL; in ar933x_uart_verify_port()
591 if ((rs485conf->flags & SER_RS485_ENABLED) && in ar933x_config_rs485()
592 !up->rts_gpiod) { in ar933x_config_rs485()
593 dev_err(port->dev, "RS485 needs rts-gpio\n"); in ar933x_config_rs485()
596 port->rs485 = *rs485conf; in ar933x_config_rs485()
612 if (--timeout == 0) in ar933x_uart_wait_xmitr()
630 struct ar933x_uart_port *up = ar933x_console_ports[co->index]; in ar933x_uart_console_write()
637 if (up->port.sysrq) in ar933x_uart_console_write()
640 locked = spin_trylock(&up->port.lock); in ar933x_uart_console_write()
642 spin_lock(&up->port.lock); in ar933x_uart_console_write()
650 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar); in ar933x_uart_console_write()
662 spin_unlock(&up->port.lock); in ar933x_uart_console_write()
675 if (co->index < 0 || co->index >= CONFIG_SERIAL_AR933X_NR_UARTS) in ar933x_uart_console_setup()
676 return -EINVAL; in ar933x_uart_console_setup()
678 up = ar933x_console_ports[co->index]; in ar933x_uart_console_setup()
680 return -ENODEV; in ar933x_uart_console_setup()
685 return uart_set_options(&up->port, co, baud, parity, bits, flow); in ar933x_uart_console_setup()
694 .index = -1,
718 np = pdev->dev.of_node; in ar933x_uart_probe()
722 dev_err(&pdev->dev, "unable to get alias id, err=%d\n", in ar933x_uart_probe()
727 id = pdev->id; in ar933x_uart_probe()
728 if (id == -1) in ar933x_uart_probe()
733 return -EINVAL; in ar933x_uart_probe()
737 dev_err(&pdev->dev, "no IRQ resource\n"); in ar933x_uart_probe()
738 return -EINVAL; in ar933x_uart_probe()
741 up = devm_kzalloc(&pdev->dev, sizeof(struct ar933x_uart_port), in ar933x_uart_probe()
744 return -ENOMEM; in ar933x_uart_probe()
746 up->clk = devm_clk_get(&pdev->dev, "uart"); in ar933x_uart_probe()
747 if (IS_ERR(up->clk)) { in ar933x_uart_probe()
748 dev_err(&pdev->dev, "unable to get UART clock\n"); in ar933x_uart_probe()
749 return PTR_ERR(up->clk); in ar933x_uart_probe()
752 port = &up->port; in ar933x_uart_probe()
755 port->membase = devm_ioremap_resource(&pdev->dev, mem_res); in ar933x_uart_probe()
756 if (IS_ERR(port->membase)) in ar933x_uart_probe()
757 return PTR_ERR(port->membase); in ar933x_uart_probe()
759 ret = clk_prepare_enable(up->clk); in ar933x_uart_probe()
763 port->uartclk = clk_get_rate(up->clk); in ar933x_uart_probe()
764 if (!port->uartclk) { in ar933x_uart_probe()
765 ret = -EINVAL; in ar933x_uart_probe()
769 port->mapbase = mem_res->start; in ar933x_uart_probe()
770 port->line = id; in ar933x_uart_probe()
771 port->irq = irq_res->start; in ar933x_uart_probe()
772 port->dev = &pdev->dev; in ar933x_uart_probe()
773 port->type = PORT_AR933X; in ar933x_uart_probe()
774 port->iotype = UPIO_MEM32; in ar933x_uart_probe()
776 port->regshift = 2; in ar933x_uart_probe()
777 port->fifosize = AR933X_UART_FIFO_SIZE; in ar933x_uart_probe()
778 port->ops = &ar933x_uart_ops; in ar933x_uart_probe()
779 port->rs485_config = ar933x_config_rs485; in ar933x_uart_probe()
781 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1); in ar933x_uart_probe()
782 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD); in ar933x_uart_probe()
784 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP); in ar933x_uart_probe()
785 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD); in ar933x_uart_probe()
791 up->gpios = mctrl_gpio_init(port, 0); in ar933x_uart_probe()
792 if (IS_ERR(up->gpios) && PTR_ERR(up->gpios) != -ENOSYS) { in ar933x_uart_probe()
793 ret = PTR_ERR(up->gpios); in ar933x_uart_probe()
797 up->rts_gpiod = mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS); in ar933x_uart_probe()
799 if ((port->rs485.flags & SER_RS485_ENABLED) && in ar933x_uart_probe()
800 !up->rts_gpiod) { in ar933x_uart_probe()
801 dev_err(&pdev->dev, "lacking rts-gpio, disabling RS485\n"); in ar933x_uart_probe()
802 port->rs485.flags &= ~SER_RS485_ENABLED; in ar933x_uart_probe()
806 ar933x_console_ports[up->port.line] = up; in ar933x_uart_probe()
809 ret = uart_add_one_port(&ar933x_uart_driver, &up->port); in ar933x_uart_probe()
817 clk_disable_unprepare(up->clk); in ar933x_uart_probe()
828 uart_remove_one_port(&ar933x_uart_driver, &up->port); in ar933x_uart_remove()
829 clk_disable_unprepare(up->clk); in ar933x_uart_remove()
837 { .compatible = "qca,ar9330-uart" },