Skip to content

Commit 9be1064

Browse files
dangowrtgregkh
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9fa3c4b commit 9be1064

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed

drivers/tty/serial/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ config SERIAL_AR933X
12691269
tristate "AR933X serial port support"
12701270
depends on HAVE_CLK && ATH79
12711271
select SERIAL_CORE
1272+
select SERIAL_MCTRL_GPIO if GPIOLIB
12721273
help
12731274
If you have an Atheros AR933X SOC based board and want to use the
12741275
built-in UART of the SoC, say Y to this option.

drivers/tty/serial/ar933x_uart.c

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/console.h>
1414
#include <linux/sysrq.h>
1515
#include <linux/delay.h>
16+
#include <linux/gpio/consumer.h>
1617
#include <linux/platform_device.h>
1718
#include <linux/of.h>
1819
#include <linux/of_platform.h>
@@ -29,6 +30,8 @@
2930

3031
#include <asm/mach-ath79/ar933x_uart.h>
3132

33+
#include "serial_mctrl_gpio.h"
34+
3235
#define DRIVER_NAME "ar933x-uart"
3336

3437
#define AR933X_UART_MAX_SCALE 0xff
@@ -47,6 +50,8 @@ struct ar933x_uart_port {
4750
unsigned int min_baud;
4851
unsigned int max_baud;
4952
struct clk *clk;
53+
struct mctrl_gpios *gpios;
54+
struct gpio_desc *rts_gpiod;
5055
};
5156

5257
static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
@@ -100,6 +105,18 @@ static inline void ar933x_uart_stop_tx_interrupt(struct ar933x_uart_port *up)
100105
ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
101106
}
102107

108+
static inline void ar933x_uart_start_rx_interrupt(struct ar933x_uart_port *up)
109+
{
110+
up->ier |= AR933X_UART_INT_RX_VALID;
111+
ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
112+
}
113+
114+
static inline void ar933x_uart_stop_rx_interrupt(struct ar933x_uart_port *up)
115+
{
116+
up->ier &= ~AR933X_UART_INT_RX_VALID;
117+
ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
118+
}
119+
103120
static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
104121
{
105122
unsigned int rdata;
@@ -125,11 +142,21 @@ static unsigned int ar933x_uart_tx_empty(struct uart_port *port)
125142

126143
static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
127144
{
128-
return TIOCM_CAR;
145+
struct ar933x_uart_port *up =
146+
container_of(port, struct ar933x_uart_port, port);
147+
int ret = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
148+
149+
mctrl_gpio_get(up->gpios, &ret);
150+
151+
return ret;
129152
}
130153

131154
static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
132155
{
156+
struct ar933x_uart_port *up =
157+
container_of(port, struct ar933x_uart_port, port);
158+
159+
mctrl_gpio_set(up->gpios, mctrl);
133160
}
134161

135162
static void ar933x_uart_start_tx(struct uart_port *port)
@@ -140,6 +167,37 @@ static void ar933x_uart_start_tx(struct uart_port *port)
140167
ar933x_uart_start_tx_interrupt(up);
141168
}
142169

170+
static void ar933x_uart_wait_tx_complete(struct ar933x_uart_port *up)
171+
{
172+
unsigned int status;
173+
unsigned int timeout = 60000;
174+
175+
/* Wait up to 60ms for the character(s) to be sent. */
176+
do {
177+
status = ar933x_uart_read(up, AR933X_UART_CS_REG);
178+
if (--timeout == 0)
179+
break;
180+
udelay(1);
181+
} while (status & AR933X_UART_CS_TX_BUSY);
182+
183+
if (timeout == 0)
184+
dev_err(up->port.dev, "waiting for TX timed out\n");
185+
}
186+
187+
static void ar933x_uart_rx_flush(struct ar933x_uart_port *up)
188+
{
189+
unsigned int status;
190+
191+
/* clear RX_VALID interrupt */
192+
ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_RX_VALID);
193+
194+
/* remove characters from the RX FIFO */
195+
do {
196+
ar933x_uart_write(up, AR933X_UART_DATA_REG, AR933X_UART_DATA_RX_CSR);
197+
status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
198+
} while (status & AR933X_UART_DATA_RX_CSR);
199+
}
200+
143201
static void ar933x_uart_stop_tx(struct uart_port *port)
144202
{
145203
struct ar933x_uart_port *up =
@@ -153,8 +211,7 @@ static void ar933x_uart_stop_rx(struct uart_port *port)
153211
struct ar933x_uart_port *up =
154212
container_of(port, struct ar933x_uart_port, port);
155213

156-
up->ier &= ~AR933X_UART_INT_RX_VALID;
157-
ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
214+
ar933x_uart_stop_rx_interrupt(up);
158215
}
159216

160217
static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
@@ -336,11 +393,20 @@ static void ar933x_uart_rx_chars(struct ar933x_uart_port *up)
336393
static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
337394
{
338395
struct circ_buf *xmit = &up->port.state->xmit;
396+
struct serial_rs485 *rs485conf = &up->port.rs485;
339397
int count;
398+
bool half_duplex_send = false;
340399

341400
if (uart_tx_stopped(&up->port))
342401
return;
343402

403+
if ((rs485conf->flags & SER_RS485_ENABLED) &&
404+
(up->port.x_char || !uart_circ_empty(xmit))) {
405+
ar933x_uart_stop_rx_interrupt(up);
406+
gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_ON_SEND));
407+
half_duplex_send = true;
408+
}
409+
344410
count = up->port.fifosize;
345411
do {
346412
unsigned int rdata;
@@ -368,8 +434,14 @@ static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
368434
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
369435
uart_write_wakeup(&up->port);
370436

371-
if (!uart_circ_empty(xmit))
437+
if (!uart_circ_empty(xmit)) {
372438
ar933x_uart_start_tx_interrupt(up);
439+
} else if (half_duplex_send) {
440+
ar933x_uart_wait_tx_complete(up);
441+
ar933x_uart_rx_flush(up);
442+
ar933x_uart_start_rx_interrupt(up);
443+
gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND));
444+
}
373445
}
374446

375447
static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
@@ -427,8 +499,7 @@ static int ar933x_uart_startup(struct uart_port *port)
427499
AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE);
428500

429501
/* Enable RX interrupts */
430-
up->ier = AR933X_UART_INT_RX_VALID;
431-
ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
502+
ar933x_uart_start_rx_interrupt(up);
432503

433504
spin_unlock_irqrestore(&up->port.lock, flags);
434505

@@ -511,6 +582,21 @@ static const struct uart_ops ar933x_uart_ops = {
511582
.verify_port = ar933x_uart_verify_port,
512583
};
513584

585+
static int ar933x_config_rs485(struct uart_port *port,
586+
struct serial_rs485 *rs485conf)
587+
{
588+
struct ar933x_uart_port *up =
589+
container_of(port, struct ar933x_uart_port, port);
590+
591+
if ((rs485conf->flags & SER_RS485_ENABLED) &&
592+
!up->rts_gpiod) {
593+
dev_err(port->dev, "RS485 needs rts-gpio\n");
594+
return 1;
595+
}
596+
port->rs485 = *rs485conf;
597+
return 0;
598+
}
599+
514600
#ifdef CONFIG_SERIAL_AR933X_CONSOLE
515601
static struct ar933x_uart_port *
516602
ar933x_console_ports[CONFIG_SERIAL_AR933X_NR_UARTS];
@@ -680,6 +766,8 @@ static int ar933x_uart_probe(struct platform_device *pdev)
680766
goto err_disable_clk;
681767
}
682768

769+
uart_get_rs485_mode(&pdev->dev, &port->rs485);
770+
683771
port->mapbase = mem_res->start;
684772
port->line = id;
685773
port->irq = irq_res->start;
@@ -690,13 +778,26 @@ static int ar933x_uart_probe(struct platform_device *pdev)
690778
port->regshift = 2;
691779
port->fifosize = AR933X_UART_FIFO_SIZE;
692780
port->ops = &ar933x_uart_ops;
781+
port->rs485_config = ar933x_config_rs485;
693782

694783
baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
695784
up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
696785

697786
baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
698787
up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
699788

789+
up->gpios = mctrl_gpio_init(port, 0);
790+
if (IS_ERR(up->gpios) && PTR_ERR(up->gpios) != -ENOSYS)
791+
return PTR_ERR(up->gpios);
792+
793+
up->rts_gpiod = mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS);
794+
795+
if ((port->rs485.flags & SER_RS485_ENABLED) &&
796+
!up->rts_gpiod) {
797+
dev_err(&pdev->dev, "lacking rts-gpio, disabling RS485\n");
798+
port->rs485.flags &= ~SER_RS485_ENABLED;
799+
}
800+
700801
#ifdef CONFIG_SERIAL_AR933X_CONSOLE
701802
ar933x_console_ports[up->port.line] = up;
702803
#endif

0 commit comments

Comments
 (0)