Skip to contentSkip to conversation

tty: serial: Add RS485 RTS GPIO control

PATCH Superseded Marek Vasut 2 participants 2 messages Jul 24, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty: serial: Add RS485 RTS GPIO control

While the STM32 does support RS485 drive-enable control within the UART IP itself, some systems have the drive-enable line connected to a pin which cannot be pinmuxed as RTS. Add support for toggling the RTS GPIO line using the modem control GPIOs to provide at least some sort of emulation.

Cc Andy Shevchenko <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Fabrice Gasnier <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/tty/serial/stm32-usart.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/tty/serial/stm32-usart.c +9 -0
index d1c5e536a167..22c8477571ce 100644
@@ -34,6 +34,7 @@
 #include "serial_mctrl_gpio.h"
 #include "stm32-usart.h"
 
+static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl);
 static void stm32_stop_tx(struct uart_port *port);
 static void stm32_transmit_chars(struct uart_port *port);
 
@@ -129,9 +130,13 @@ static int stm32_config_rs485(struct uart_port *port,
 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
 			cr3 &= ~USART_CR3_DEP;
 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl & ~TIOCM_RTS);
 		} else {
 			cr3 |= USART_CR3_DEP;
 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl | TIOCM_RTS);
 		}
 
 		writel_relaxed(cr3, port->membase + ofs->cr3);
@@ -847,9 +852,13 @@ static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
 			cr3 &= ~USART_CR3_DEP;
 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl & ~TIOCM_RTS);
 		} else {
 			cr3 |= USART_CR3_DEP;
 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl | TIOCM_RTS);
 		}
 
 	} else {
Signature · 1 line
2.27.0
#1 lore raw
Quoted 11 lines · Marek Vasut

While the STM32 does support RS485 drive-enable control within the UART IP itself, some systems have the drive-enable line connected to a pin which cannot be pinmuxed as RTS. Add support for toggling the RTS GPIO line using the modem control GPIOs to provide at least some sort of emulation.

Cc Andy Shevchenko <[email protected]>
Cc Manivannan Sadhasivam <[email protected]>
Cc Fabrice Gasnier <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>

Hi Marek,

You could add stm32 in the commit title.
'git log --oneline' gives "serial: stm32: ..." most of the time here.

Also could you please CC STM32 maintainers and mailing list?

Please find other remark below.

Quoted 17 lines
---
 drivers/tty/serial/stm32-usart.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/tty/serial/stm32-usart.c +1 -0
index d1c5e536a167..22c8477571ce 100644
@@ -34,6 +34,7 @@
 #include "serial_mctrl_gpio.h"
 #include "stm32-usart.h"
 
+static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl);
 static void stm32_stop_tx(struct uart_port *port);
 static void stm32_transmit_chars(struct uart_port *port);
 

drivers/tty/serial/stm32-usart.c static int stm32_config_rs485(struct uart_port *port, · line 134

Quoted 5 lines
drivers/tty/serial/stm32-usart.c +2 -0
@@ -129,9 +130,13 @@ static int stm32_config_rs485(struct uart_port *port,
 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
 			cr3 &= ~USART_CR3_DEP;
 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl & ~TIOCM_RTS);

Calling stm32_set_mctrl() will also set or clear bit in cr3. But right after the if() {} else {} statement here there's a:
- writel_relaxed(cr3, port->membase + ofs->cr3);

That's basically overwritten... I guess that's undesired.

So I suggest to simply call mctrl_gpio_set() instead. This is probably all you need here ?

Quoted 9 lines
drivers/tty/serial/stm32-usart.c +2 -0
 		} else {
 			cr3 |= USART_CR3_DEP;
 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl | TIOCM_RTS);
 		}
 
 		writel_relaxed(cr3, port->membase + ofs->cr3);

drivers/tty/serial/stm32-usart.c static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, · line 861

Quoted 10 lines
drivers/tty/serial/stm32-usart.c +4 -0
@@ -847,9 +852,13 @@ static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
 		if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
 			cr3 &= ~USART_CR3_DEP;
 			rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl & ~TIOCM_RTS);
 		} else {
 			cr3 |= USART_CR3_DEP;
 			rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
+			stm32_set_mctrl(port,
+					stm32_port->port.mctrl | TIOCM_RTS);

Same as above.

Best Regards,
Fabrice

Quoted 4 lines
drivers/tty/serial/stm32-usart.c +0 -0
 		}
 
 	} else {