Lines Matching +full:up +full:-
1 // SPDX-License-Identifier: GPL-2.0
3 * Atheros AR933X SoC built-in UART driver
31 #include <asm/mach-ath79/ar933x_uart.h>
35 #define DRIVER_NAME "ar933x-uart"
57 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up, in ar933x_uart_read() argument
60 return readl(up->port.membase + offset); in ar933x_uart_read()
63 static inline void ar933x_uart_write(struct ar933x_uart_port *up, in ar933x_uart_write() argument
66 writel(value, up->port.membase + offset); in ar933x_uart_write()
69 static inline void ar933x_uart_rmw(struct ar933x_uart_port *up, in ar933x_uart_rmw() argument
76 t = ar933x_uart_read(up, offset); in ar933x_uart_rmw()
79 ar933x_uart_write(up, offset, t); in ar933x_uart_rmw()
82 static inline void ar933x_uart_rmw_set(struct ar933x_uart_port *up, in ar933x_uart_rmw_set() argument
86 ar933x_uart_rmw(up, offset, 0, val); in ar933x_uart_rmw_set()
89 static inline void ar933x_uart_rmw_clear(struct ar933x_uart_port *up, in ar933x_uart_rmw_clear() argument
93 ar933x_uart_rmw(up, offset, val, 0); in ar933x_uart_rmw_clear()
96 static inline void ar933x_uart_start_tx_interrupt(struct ar933x_uart_port *up) in ar933x_uart_start_tx_interrupt() argument
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()
102 static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up) in ar933x_uart_stop_tx_interrupt() argument
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()
108 static inline void ar933x_uart_start_rx_interrupt(struct ar933x_uart_port *up) in ar933x_uart_start_rx_interrupt() argument
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()
114 static inline void ar933x_uart_stop_rx_interrupt(struct ar933x_uart_port *up) in ar933x_uart_stop_rx_interrupt() argument
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()
120 static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch) in ar933x_uart_putc() argument
126 ar933x_uart_write(up, AR933X_UART_DATA_REG, rdata); in ar933x_uart_putc()
131 struct ar933x_uart_port *up = in ar933x_uart_tx_empty() local
136 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_tx_empty()
137 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); in ar933x_uart_tx_empty()
138 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_tx_empty()
145 struct ar933x_uart_port *up = in ar933x_uart_get_mctrl() local
149 mctrl_gpio_get(up->gpios, &ret); in ar933x_uart_get_mctrl()
156 struct ar933x_uart_port *up = in ar933x_uart_set_mctrl() local
159 mctrl_gpio_set(up->gpios, mctrl); in ar933x_uart_set_mctrl()
164 struct ar933x_uart_port *up = in ar933x_uart_start_tx() local
167 ar933x_uart_start_tx_interrupt(up); in ar933x_uart_start_tx()
170 static void ar933x_uart_wait_tx_complete(struct ar933x_uart_port *up) in ar933x_uart_wait_tx_complete() argument
175 /* Wait up to 60ms for the character(s) to be sent. */ in ar933x_uart_wait_tx_complete()
177 status = ar933x_uart_read(up, AR933X_UART_CS_REG); in ar933x_uart_wait_tx_complete()
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()
187 static void ar933x_uart_rx_flush(struct ar933x_uart_port *up) in ar933x_uart_rx_flush() argument
192 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_RX_VALID); in ar933x_uart_rx_flush()
196 ar933x_uart_write(up, AR933X_UART_DATA_REG, AR933X_UART_DATA_RX_CSR); in ar933x_uart_rx_flush()
197 status = ar933x_uart_read(up, AR933X_UART_DATA_REG); in ar933x_uart_rx_flush()
203 struct ar933x_uart_port *up = in ar933x_uart_stop_tx() local
206 ar933x_uart_stop_tx_interrupt(up); in ar933x_uart_stop_tx()
211 struct ar933x_uart_port *up = in ar933x_uart_stop_rx() local
214 ar933x_uart_stop_rx_interrupt(up); in ar933x_uart_stop_rx()
219 struct ar933x_uart_port *up = in ar933x_uart_break_ctl() local
223 spin_lock_irqsave(&up->port.lock, flags); in ar933x_uart_break_ctl()
224 if (break_state == -1) in ar933x_uart_break_ctl()
225 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, in ar933x_uart_break_ctl()
228 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG, 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()
288 struct ar933x_uart_port *up = in ar933x_uart_set_termios() local
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()
324 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG, 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()
336 ar933x_uart_write(up, AR933X_UART_CLOCK_REG, in ar933x_uart_set_termios()
340 ar933x_uart_rmw(up, AR933X_UART_CS_REG, AR933X_UART_CS_PARITY_M, cs); in ar933x_uart_set_termios()
343 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, in ar933x_uart_set_termios()
347 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, in ar933x_uart_set_termios()
351 ar933x_uart_rmw(up, AR933X_UART_CS_REG, in ar933x_uart_set_termios()
355 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_set_termios()
361 static void ar933x_uart_rx_chars(struct ar933x_uart_port *up) in ar933x_uart_rx_chars() argument
363 struct tty_port *port = &up->port.state->port; in ar933x_uart_rx_chars()
370 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); in ar933x_uart_rx_chars()
375 ar933x_uart_write(up, AR933X_UART_DATA_REG, 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()
393 static void ar933x_uart_tx_chars(struct ar933x_uart_port *up) in ar933x_uart_tx_chars() argument
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()
405 ar933x_uart_stop_rx_interrupt(up); 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()
414 rdata = ar933x_uart_read(up, AR933X_UART_DATA_REG); 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()
438 ar933x_uart_start_tx_interrupt(up); in ar933x_uart_tx_chars()
440 ar933x_uart_wait_tx_complete(up); in ar933x_uart_tx_chars()
441 ar933x_uart_rx_flush(up); in ar933x_uart_tx_chars()
442 ar933x_uart_start_rx_interrupt(up); in ar933x_uart_tx_chars()
443 gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND)); in ar933x_uart_tx_chars()
449 struct ar933x_uart_port *up = dev_id; in ar933x_uart_interrupt() local
452 status = ar933x_uart_read(up, AR933X_UART_CS_REG); in ar933x_uart_interrupt()
456 spin_lock(&up->port.lock); in ar933x_uart_interrupt()
458 status = ar933x_uart_read(up, AR933X_UART_INT_REG); in ar933x_uart_interrupt()
459 status &= ar933x_uart_read(up, AR933X_UART_INT_EN_REG); in ar933x_uart_interrupt()
462 ar933x_uart_write(up, AR933X_UART_INT_REG, in ar933x_uart_interrupt()
464 ar933x_uart_rx_chars(up); in ar933x_uart_interrupt()
468 ar933x_uart_write(up, AR933X_UART_INT_REG, in ar933x_uart_interrupt()
470 ar933x_uart_stop_tx_interrupt(up); in ar933x_uart_interrupt()
471 ar933x_uart_tx_chars(up); in ar933x_uart_interrupt()
474 spin_unlock(&up->port.lock); in ar933x_uart_interrupt()
481 struct ar933x_uart_port *up = in ar933x_uart_startup() local
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()
494 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, in ar933x_uart_startup()
498 ar933x_uart_rmw_set(up, AR933X_UART_CS_REG, in ar933x_uart_startup()
502 ar933x_uart_start_rx_interrupt(up); in ar933x_uart_startup()
504 spin_unlock_irqrestore(&up->port.lock, flags); in ar933x_uart_startup()
511 struct ar933x_uart_port *up = in ar933x_uart_shutdown() local
515 up->ier = 0; in ar933x_uart_shutdown()
516 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); in ar933x_uart_shutdown()
519 ar933x_uart_rmw_clear(up, AR933X_UART_CS_REG, 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()
550 struct ar933x_uart_port *up = in ar933x_uart_verify_port() local
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()
588 struct ar933x_uart_port *up = in ar933x_config_rs485() local
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()
604 static void ar933x_uart_wait_xmitr(struct ar933x_uart_port *up) in ar933x_uart_wait_xmitr() argument
609 /* Wait up to 60ms for the character(s) to be sent. */ in ar933x_uart_wait_xmitr()
611 status = ar933x_uart_read(up, AR933X_UART_DATA_REG); in ar933x_uart_wait_xmitr()
612 if (--timeout == 0) in ar933x_uart_wait_xmitr()
620 struct ar933x_uart_port *up = in ar933x_uart_console_putchar() local
623 ar933x_uart_wait_xmitr(up); in ar933x_uart_console_putchar()
624 ar933x_uart_putc(up, ch); in ar933x_uart_console_putchar()
630 struct ar933x_uart_port *up = ar933x_console_ports[co->index]; in ar933x_uart_console_write() local
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()
647 int_en = ar933x_uart_read(up, AR933X_UART_INT_EN_REG); in ar933x_uart_console_write()
648 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, 0); in ar933x_uart_console_write()
650 uart_console_write(&up->port, s, count, ar933x_uart_console_putchar); in ar933x_uart_console_write()
656 ar933x_uart_wait_xmitr(up); in ar933x_uart_console_write()
657 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, int_en); in ar933x_uart_console_write()
659 ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_ALLINTS); in ar933x_uart_console_write()
662 spin_unlock(&up->port.lock); in ar933x_uart_console_write()
669 struct ar933x_uart_port *up; in ar933x_uart_console_setup() local
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()
679 if (!up) 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,
709 struct ar933x_uart_port *up; in ar933x_uart_probe() local
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()
743 if (!up) 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()
813 platform_set_drvdata(pdev, up); in ar933x_uart_probe()
817 clk_disable_unprepare(up->clk); in ar933x_uart_probe()
823 struct ar933x_uart_port *up; in ar933x_uart_remove() local
825 up = platform_get_drvdata(pdev); in ar933x_uart_remove()
827 if (up) { in ar933x_uart_remove()
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" },