Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

serial: ar933x_uart: add RS485 support

Emulate half-duplex operation and use mctrl_gpio to add support for
RS485 tranceiver with transmit/receive switch hooked to RTS GPIO line.
This is needed to make use of the RS485 port found on Teltonika RUT955.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Link: https://lore.kernel.org/r/20200221212331.GA21467@makrotopia.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Daniel Golle and committed by
Greg Kroah-Hartman
9be1064f 9fa3c4b1

+108 -6
+1
drivers/tty/serial/Kconfig
··· 1269 1269 tristate "AR933X serial port support" 1270 1270 depends on HAVE_CLK && ATH79 1271 1271 select SERIAL_CORE 1272 + select SERIAL_MCTRL_GPIO if GPIOLIB 1272 1273 help 1273 1274 If you have an Atheros AR933X SOC based board and want to use the 1274 1275 built-in UART of the SoC, say Y to this option.
+107 -6
drivers/tty/serial/ar933x_uart.c
··· 13 13 #include <linux/console.h> 14 14 #include <linux/sysrq.h> 15 15 #include <linux/delay.h> 16 + #include <linux/gpio/consumer.h> 16 17 #include <linux/platform_device.h> 17 18 #include <linux/of.h> 18 19 #include <linux/of_platform.h> ··· 29 28 #include <asm/div64.h> 30 29 31 30 #include <asm/mach-ath79/ar933x_uart.h> 31 + 32 + #include "serial_mctrl_gpio.h" 32 33 33 34 #define DRIVER_NAME "ar933x-uart" 34 35 ··· 50 47 unsigned int min_baud; 51 48 unsigned int max_baud; 52 49 struct clk *clk; 50 + struct mctrl_gpios *gpios; 51 + struct gpio_desc *rts_gpiod; 53 52 }; 54 53 55 54 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up, ··· 105 100 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); 106 101 } 107 102 103 + static inline void ar933x_uart_start_rx_interrupt(struct ar933x_uart_port *up) 104 + { 105 + up->ier |= AR933X_UART_INT_RX_VALID; 106 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); 107 + } 108 + 109 + static inline void ar933x_uart_stop_rx_interrupt(struct ar933x_uart_port *up) 110 + { 111 + up->ier &= ~AR933X_UART_INT_RX_VALID; 112 + ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); 113 + } 114 + 108 115 static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch) 109 116 { 110 117 unsigned int rdata; ··· 142 125 143 126 static unsigned int ar933x_uart_get_mctrl(struct uart_port *port) 144 127 { 145 - return TIOCM_CAR; 128 + struct ar933x_uart_port *up = 129 + container_of(port, struct ar933x_uart_port, port); 130 + int ret = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR; 131 + 132 + mctrl_gpio_get(up->gpios, &ret); 133 + 134 + return ret; 146 135 } 147 136 148 137 static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl) 149 138 { 139 + struct ar933x_uart_port *up = 140 + container_of(port, struct ar933x_uart_port, port); 141 + 142 + mctrl_gpio_set(up->gpios, mctrl); 150 143 } 151 144 152 145 static void ar933x_uart_start_tx(struct uart_port *port) ··· 165 138 container_of(port, struct ar933x_uart_port, port); 166 139 167 140 ar933x_uart_start_tx_interrupt(up); 141 + } 142 + 143 + static void ar933x_uart_wait_tx_complete(struct ar933x_uart_port *up) 144 + { 145 + unsigned int status; 146 + unsigned int timeout = 60000; 147 + 148 + /* Wait up to 60ms for the character(s) to be sent. */ 149 + do { 150 + status = ar933x_uart_read(up, AR933X_UART_CS_REG); 151 + if (--timeout == 0) 152 + break; 153 + udelay(1); 154 + } while (status & AR933X_UART_CS_TX_BUSY); 155 + 156 + if (timeout == 0) 157 + dev_err(up->port.dev, "waiting for TX timed out\n"); 158 + } 159 + 160 + static void ar933x_uart_rx_flush(struct ar933x_uart_port *up) 161 + { 162 + unsigned int status; 163 + 164 + /* clear RX_VALID interrupt */ 165 + ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_RX_VALID); 166 + 167 + /* remove characters from the RX FIFO */ 168 + do { 169 + ar933x_uart_write(up, AR933X_UART_DATA_REG, AR933X_UART_DATA_RX_CSR); 170 + status = ar933x_uart_read(up, AR933X_UART_DATA_REG); 171 + } while (status & AR933X_UART_DATA_RX_CSR); 168 172 } 169 173 170 174 static void ar933x_uart_stop_tx(struct uart_port *port) ··· 211 153 struct ar933x_uart_port *up = 212 154 container_of(port, struct ar933x_uart_port, port); 213 155 214 - up->ier &= ~AR933X_UART_INT_RX_VALID; 215 - ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); 156 + ar933x_uart_stop_rx_interrupt(up); 216 157 } 217 158 218 159 static void ar933x_uart_break_ctl(struct uart_port *port, int break_state) ··· 393 336 static void ar933x_uart_tx_chars(struct ar933x_uart_port *up) 394 337 { 395 338 struct circ_buf *xmit = &up->port.state->xmit; 339 + struct serial_rs485 *rs485conf = &up->port.rs485; 396 340 int count; 341 + bool half_duplex_send = false; 397 342 398 343 if (uart_tx_stopped(&up->port)) 399 344 return; 345 + 346 + if ((rs485conf->flags & SER_RS485_ENABLED) && 347 + (up->port.x_char || !uart_circ_empty(xmit))) { 348 + ar933x_uart_stop_rx_interrupt(up); 349 + gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_ON_SEND)); 350 + half_duplex_send = true; 351 + } 400 352 401 353 count = up->port.fifosize; 402 354 do { ··· 434 368 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 435 369 uart_write_wakeup(&up->port); 436 370 437 - if (!uart_circ_empty(xmit)) 371 + if (!uart_circ_empty(xmit)) { 438 372 ar933x_uart_start_tx_interrupt(up); 373 + } else if (half_duplex_send) { 374 + ar933x_uart_wait_tx_complete(up); 375 + ar933x_uart_rx_flush(up); 376 + ar933x_uart_start_rx_interrupt(up); 377 + gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND)); 378 + } 439 379 } 440 380 441 381 static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id) ··· 499 427 AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE); 500 428 501 429 /* Enable RX interrupts */ 502 - up->ier = AR933X_UART_INT_RX_VALID; 503 - ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier); 430 + ar933x_uart_start_rx_interrupt(up); 504 431 505 432 spin_unlock_irqrestore(&up->port.lock, flags); 506 433 ··· 581 510 .config_port = ar933x_uart_config_port, 582 511 .verify_port = ar933x_uart_verify_port, 583 512 }; 513 + 514 + static int ar933x_config_rs485(struct uart_port *port, 515 + struct serial_rs485 *rs485conf) 516 + { 517 + struct ar933x_uart_port *up = 518 + container_of(port, struct ar933x_uart_port, port); 519 + 520 + if ((rs485conf->flags & SER_RS485_ENABLED) && 521 + !up->rts_gpiod) { 522 + dev_err(port->dev, "RS485 needs rts-gpio\n"); 523 + return 1; 524 + } 525 + port->rs485 = *rs485conf; 526 + return 0; 527 + } 584 528 585 529 #ifdef CONFIG_SERIAL_AR933X_CONSOLE 586 530 static struct ar933x_uart_port * ··· 766 680 goto err_disable_clk; 767 681 } 768 682 683 + uart_get_rs485_mode(&pdev->dev, &port->rs485); 684 + 769 685 port->mapbase = mem_res->start; 770 686 port->line = id; 771 687 port->irq = irq_res->start; ··· 778 690 port->regshift = 2; 779 691 port->fifosize = AR933X_UART_FIFO_SIZE; 780 692 port->ops = &ar933x_uart_ops; 693 + port->rs485_config = ar933x_config_rs485; 781 694 782 695 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1); 783 696 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD); 784 697 785 698 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP); 786 699 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD); 700 + 701 + up->gpios = mctrl_gpio_init(port, 0); 702 + if (IS_ERR(up->gpios) && PTR_ERR(up->gpios) != -ENOSYS) 703 + return PTR_ERR(up->gpios); 704 + 705 + up->rts_gpiod = mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS); 706 + 707 + if ((port->rs485.flags & SER_RS485_ENABLED) && 708 + !up->rts_gpiod) { 709 + dev_err(&pdev->dev, "lacking rts-gpio, disabling RS485\n"); 710 + port->rs485.flags &= ~SER_RS485_ENABLED; 711 + } 787 712 788 713 #ifdef CONFIG_SERIAL_AR933X_CONSOLE 789 714 ar933x_console_ports[up->port.line] = up;