serial: 8250: Add support for rs485 half/full duplex on puma/ringneck-haikou
Subsystems (3)
v5 cover serial: 8250: Add support for rs485 half/full duplex on puma/ringneck-haikou
This series tries to revive the work of Heiko Stuebner from 2020
On the boards that we are using (ringneck/puma-haikou) a hardware switch can set the rs485 transceiver into half or full duplex mode.
In half-duplex mode the DE/RE signal of the rs485 transceiver is not connected to an RTS signal whose control is already handled in the rs485 emulation (start/stop callbacks), but rather to a gpio. And since enabling the receiver requires setting this gpio active we need to do that in em485 while receiving and disable it while sending to enable the driver mode.
In full-duplex mode RE is grounded and separated from DE. Meanwhile the rx-enable gpio remains connected to the DE pin. In this case the receiver-enable gpio should be disabled to enable driver mode in parallel to the enabled receiver.
This patch-series adds support for controlling the receiver mode using a gpio in em485 for half-duplex mode while allowing users to keep using the full-duplex feature if em485 is disabled.
Changes in v5:
- set port->rs485_re_gpio in "serial: 8250: Support separate rs485 rx-enable GPIO"
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- define the state of rx-enable gpio when em485 is disabled
- add rs485 half/full duplex support to ringneck/puma-haikou
- use dev_err_probe instead of dev_err if error is -EPROBE_DEFER
Changes from the 2020 submission include:
- external gpio for optional receiver-enable handling
- Link to v3: https://lore.kernel.org/all/[email protected]/
---
Farouk Bouabid (4):
dt-bindings: serial: add binding for rs485 rx-enable state when rs485 is disabled
serial: 8250: set rx-enable gpio state when rs485 is disabled
arm64: dts: rockchip: rk3399-puma-haikou: add rs485 support on uart2
arm64: dts: rockchip: px30-ringneck-haikou: add rs485 support on uart5
Heiko Stuebner (2):
dt-bindings: serial: Add binding for rs485 receiver enable GPIO
serial: 8250: Support separate rs485 rx-enable GPIO
Documentation/devicetree/bindings/serial/rs485.yaml | 9 +++++++++ arch/arm64/boot/dts/rockchip/px30-ringneck-haikou.dts | 2 ++ arch/arm64/boot/dts/rockchip/rk3399-puma-haikou.dts | 4 +++- drivers/tty/serial/8250/8250_port.c | 11 ++++++++++- drivers/tty/serial/serial_core.c | 11 +++++++++++ include/linux/serial_core.h | 2 ++ 6 files changed, 37 insertions(+), 2 deletions(-)
---
base-commit: 5ebe731c2a586b379103f736cd498bcca3cf1ea9
change-id: 20240125-dev-rx-enable-d8818dbf7c28
Best regards,
Signature · 1 line
Farouk Bouabid <[email protected]>
PATCH v5 1/6 dt-bindings: serial: Add binding for rs485 receiver enable GPIO
From: Heiko Stuebner <[email protected]>
RS485 has two signals to control transmissions "driver enable" (DE) and "receiver enable" (RE). DE is already handled via the uarts RTS signal while the RE signal on most implementations doesn't get handled separately at all.
As there still will be cases where this is needed though add a gpio property for declaring this signal pin.
--- Documentation/devicetree/bindings/serial/rs485.yaml | 4 ++++ 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/serial/rs485.yaml +4 -0
@@ -51,6 +51,10 @@ properties: description: enables the receiving of data even while sending data. $ref: /schemas/types.yaml#/definitions/flag + rs485-rx-enable-gpios: + description: GPIO to handle a separate RS485 receive enable signal + maxItems: 1 + rs485-term-gpios: description: GPIO pin to enable RS485 bus termination. maxItems: 1
Signature · 1 line
2.34.1
Re: [PATCH v5 1/6] dt-bindings: serial: Add binding for rs485 receiver enable GPIO
Quoted 7 lines · Farouk Bouabid
From: Heiko Stuebner <[email protected]>
RS485 has two signals to control transmissions "driver enable" (DE) and "receiver enable" (RE). DE is already handled via the uarts RTS signal while the RE signal on most implementations doesn't get handled separately at all.
Quoted 9 lines
As there still will be cases where this is needed though add a gpio property for declaring this signal pin.
Signed-off-by Heiko Stuebner <[email protected]>Signed-off-by Farouk Bouabid <[email protected]>--- Documentation/devicetree/bindings/serial/rs485.yaml | 4 ++++ 1 file changed, 4 insertions(+)
PATCH v5 2/6 serial: 8250: Support separate rs485 rx-enable GPIO
In reply to Farouk BouabidFrom: Heiko Stuebner <[email protected]>
The RE signal is used to control the duplex mode of transmissions, aka receiving data while sending in full duplex mode, while stopping receiving data in half-duplex mode.
On a number of boards the !RE signal is tied to ground so reception is always enabled except if the UART allows disabling the receiver. This can be taken advantage of to implement half-duplex mode - like done on 8250_bcm2835aux.
Another solution is to tie !RE to RTS always forcing half-duplex mode.
And finally there is the option to control the RE signal separately, like done here by introducing a new rs485-specific gpio that can be set depending on the RX_DURING_TX setting in the common em485 callbacks.
--- drivers/tty/serial/8250/8250_port.c | 7 ++++++- drivers/tty/serial/serial_core.c | 7 +++++++ include/linux/serial_core.h | 1 + 3 files changed, 14 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_port.c +6 -1
@@ -1402,6 +1402,7 @@ static void serial8250_stop_rx(struct uart_port *port)void serial8250_em485_stop_tx (struct uart_8250_port *p) {unsigned char mcr =serial8250_in_MCR (p); +struct uart_port *port = &p->port;/* Port locked to synchronize UART_IER access against the console. */ lockdep_assert_held_once (&p->port.lock);
@@ -1418,6 +1419,7 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p)* Enable previously disabled RX interrupts. */ if (!(p->port.rs485.flags &SER_RS485_RX_DURING_TX )) { +gpiod_set_value (port->rs485_re_gpio,1 );serial8250_clear_and_reinit_fifos (p); p->ier |=UART_IER_RLSI |UART_IER_RDI ;
@@ -1567,9 +1569,12 @@ static inline void __start_tx(struct uart_port *port)void serial8250_em485_start_tx (struct uart_8250_port *up) {unsigned char mcr =serial8250_in_MCR (up); +struct uart_port *port = &up->port; -if (!(up->port.rs485.flags &SER_RS485_RX_DURING_TX )) +if (!(up->port.rs485.flags &SER_RS485_RX_DURING_TX )) { +gpiod_set_value (port->rs485_re_gpio,0 );serial8250_stop_rx (&up->port); + }if (up->port.rs485.flags &SER_RS485_RTS_ON_SEND ) mcr |=UART_MCR_RTS ;
drivers/tty/serial/serial_core.c +7 -0
@@ -3663,6 +3663,13 @@ int uart_get_rs485_mode(struct uart_port *port)if (port->rs485_rx_during_tx_gpio) port->rs485_supported.flags |=SER_RS485_RX_DURING_TX ; + port->rs485_re_gpio =devm_gpiod_get_optional (dev,"rs485-rx-enable" ,GPIOD_OUT_HIGH ); +if (IS_ERR (port->rs485_re_gpio)) { + ret =PTR_ERR (port->rs485_re_gpio); + port->rs485_re_gpio =NULL ; +return dev_err_probe (dev, ret,"Cannot get rs485-rx-enable-gpios\n" ); + } +return 0 ; }EXPORT_SYMBOL_GPL (uart_get_rs485_mode);
include/linux/serial_core.h +1 -0
@@ -584,6 +584,7 @@ struct uart_port {struct serial_rs485 rs485_supported;/* Supported mask for serial_rs485 */ struct gpio_desc *rs485_term_gpio;/* enable RS485 bus termination */ struct gpio_desc *rs485_rx_during_tx_gpio;/* Output GPIO that sets the state of RS485 RX during TX */ +struct gpio_desc *rs485_re_gpio;/* gpio RS485 receive enable */ struct serial_iso7816 iso7816;void *private_data;/* generic platform data pointer */ };
Signature · 1 line
2.34.1
Re: [PATCH v5 2/6] serial: 8250: Support separate rs485 rx-enable GPIO
Hi,
Quoted 11 lines · Farouk Bouabid
From: Heiko Stuebner <[email protected]>
The RE signal is used to control the duplex mode of transmissions, aka receiving data while sending in full duplex mode, while stopping receiving data in half-duplex mode.
On a number of boards the !RE signal is tied to ground so reception is always enabled except if the UART allows disabling the receiver. This can be taken advantage of to implement half-duplex mode - like done on 8250_bcm2835aux.
Quoted 6 lines
Another solution is to tie !RE to RTS always forcing half-duplex mode.
And finally there is the option to control the RE signal separately, like done here by introducing a new rs485-specific gpio that can be set depending on the
RX_DURING_TXsetting in the common em485 callbacks.
we just added the rx_during_tx_gpio to the serial core. Why cant you use this GPIO for your purpose?
Regards,
Lino
Re: [PATCH v5 2/6] serial: 8250: Support separate rs485 rx-enable GPIO
Hi,
Quoted 22 lines · Lino Sanfilippo
[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Hi,
Quoted 11 lines · Farouk Bouabid
From: Heiko Stuebner <[email protected]>
The RE signal is used to control the duplex mode of transmissions, aka receiving data while sending in full duplex mode, while stopping receiving data in half-duplex mode.
On a number of boards the !RE signal is tied to ground so reception is always enabled except if the UART allows disabling the receiver. This can be taken advantage of to implement half-duplex mode - like done on 8250_bcm2835aux.
Quoted 6 lines
Another solution is to tie !RE to RTS always forcing half-duplex mode.
And finally there is the option to control the RE signal separately, like done here by introducing a new rs485-specific gpio that can be set depending on the
RX_DURING_TXsetting in the common em485 callbacks.
Quoted 5 lines
we just added the
rx_during_tx_gpioto the serial core. Why cant you use this GPIO for your purpose?Regards,
Lino.
What we are trying to implement is a gpio that emulates the RTS signal itself as we do not have a dedicated RTS signal that can be controlled through MCR. The rx during tx state in our case is a fixed state of "NO_RX_WHILE_TX"
Regards,
Quoted 1 line
RE: [PATCH v5 2/6] serial: 8250: Support separate rs485 rx-enable GPIO
From: Farouk Bouabid
Sent: Monday, January 29, 2024 9:57 AM
Hi,
Quoted 26 lines · Lino Sanfilippo
Quoted 25 lines
[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Hi,
Quoted 11 lines · Farouk Bouabid
From: Heiko Stuebner <[email protected]>
The RE signal is used to control the duplex mode of transmissions, aka receiving data while sending in full duplex mode, while stopping receiving data in half-duplex mode.
On a number of boards the !RE signal is tied to ground so reception is always enabled except if the UART allows disabling the receiver. This can be taken advantage of to implement half-duplex mode - like done on 8250_bcm2835aux.
Quoted 6 lines
Another solution is to tie !RE to RTS always forcing half-duplex mode.
And finally there is the option to control the RE signal separately, like done here by introducing a new rs485-specific gpio that can be set depending on the
RX_DURING_TXsetting in the common em485 callbacks.we just added the
rx_during_tx_gpioto the serial core. Why cant you use this GPIO for your purpose?
Quoted 6 lines
Quoted 2 lines
Regards,
Lino.What we are trying to implement is a gpio that emulates the RTS signal itself as we do not have a dedicated RTS signal that can be controlled through MCR. The rx during tx state in our case is a fixed state of "
NO_RX_WHILE_TX"
Why can't the property rts-gpios be used here for this purpose?
Regards
Christoph
_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip
Re: [PATCH v5 2/6] serial: 8250: Support separate rs485 rx-enable GPIO
Hi Christoph,
Quoted 33 lines · Christoph Niedermaier
[Einige Personen, die diese Nachricht erhalten haben, erhalten häufig keine E-Mails von [email protected]. Weitere Informationen, warum dies wichtig ist, finden Sie unter https://aka.ms/LearnAboutSenderIdentification ]
From: Farouk Bouabid
Sent: Monday, January 29, 2024 9:57 AMHi,
Quoted 26 lines · Lino Sanfilippo
Quoted 25 lines
[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Hi,
Quoted 11 lines · Farouk Bouabid
From: Heiko Stuebner <[email protected]>
The RE signal is used to control the duplex mode of transmissions, aka receiving data while sending in full duplex mode, while stopping receiving data in half-duplex mode.
On a number of boards the !RE signal is tied to ground so reception is always enabled except if the UART allows disabling the receiver. This can be taken advantage of to implement half-duplex mode - like done on 8250_bcm2835aux.
Quoted 6 lines
Another solution is to tie !RE to RTS always forcing half-duplex mode.
And finally there is the option to control the RE signal separately, like done here by introducing a new rs485-specific gpio that can be set depending on the
RX_DURING_TXsetting in the common em485 callbacks.we just added the
rx_during_tx_gpioto the serial core. Why cant you use this GPIO for your purpose?
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Regards,
Lino.What we are trying to implement is a gpio that emulates the RTS signal itself as we do not have a dedicated RTS signal that can be controlled through MCR. The rx during tx state in our case is a fixed state of "
NO_RX_WHILE_TX"Why can't the property rts-gpios be used here for this purpose?
Yes that indeed serves our purpose. Thank you for the suggestion.
Regards
Farouk
Quoted 4 lines
Regards
Christoph
PATCH v5 3/6 dt-bindings: serial: add binding for rs485 rx-enable state when rs485 is disabled
In reply to Farouk BouabidRS485 can have a receiver-enable gpio (rx-enable-gpios). When rs485 is enabled, this gpio, if provided, must be driven active while receiving. However when RS485 is disabled this gpio should not have an undefined state. In that case, as DE and RE pins can be connected both to this gpio, if its state is not properly defined, can cause unexpected transceiver behavior.
This binding depend on rx-enable-gpios to be implemented.
--- Documentation/devicetree/bindings/serial/rs485.yaml | 5 +++++ 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/serial/rs485.yaml +5 -0
@@ -55,6 +55,11 @@ properties: description: GPIO to handle a separate RS485 receive enable signal maxItems: 1 + rs485-rx-enable-inactive-when-rs485-disabled: + description: rx-enable GPIO is not active when RS485 is disabled. If missing, active-state + is assumed. + $ref: /schemas/types.yaml#/definitions/flag + rs485-term-gpios: description: GPIO pin to enable RS485 bus termination. maxItems: 1
Signature · 1 line
2.34.1
Rob Herring
maintainerRe: [PATCH v5 3/6] dt-bindings: serial: add binding for rs485 rx-enable state when rs485 is disabled
Quoted 1 line · Farouk Bouabid
RS485 can have a receiver-enable gpio (rx-enable-gpios). When rs485 is
s/gpio/GPIO/
Quoted 6 lines
enabled, this gpio, if provided, must be driven active while receiving. However when RS485 is disabled this gpio should not have an undefined state. In that case, as DE and RE pins can be connected both to this gpio, if its state is not properly defined, can cause unexpected transceiver behavior.
This binding depend on rx-enable-gpios to be implemented.
Sounds like a constraint:
dependencies:
rs485-rx-enable-inactive-when-rs485-disabled: [ rx-enable-gpios ]
Quoted 11 lines
Signed-off-by Farouk Bouabid <[email protected]>--- Documentation/devicetree/bindings/serial/rs485.yaml | 5 +++++ 1 file changed, 5 insertions(+)Documentation/devicetree/bindings/serial/rs485.yaml +0
-0
Documentation/devicetree/bindings/serial/rs485.yaml properties: · line 58
Quoted 4 lines
Documentation/devicetree/bindings/serial/rs485.yaml +1
-0@@ -55,6 +55,11 @@ properties: description: GPIO to handle a separate RS485 receive enable signal maxItems: 1 + rs485-rx-enable-inactive-when-rs485-disabled:
For something a bit shorter, perhaps: rs485-rx-enable-inactive-on-disable
Quoted 11 lines
Documentation/devicetree/bindings/serial/rs485.yaml +4
-0+ description: rx-enable GPIO is not active when RS485 is disabled. If missing, active-state + is assumed. + $ref: /schemas/types.yaml#/definitions/flag + rs485-term-gpios: description: GPIO pin to enable RS485 bus termination. maxItems: 1Signature · 2 lines
-- 2.34.1
PATCH v5 4/6 serial: 8250: set rx-enable gpio state when rs485 is disabled
In reply to Farouk BouabidAdd the possibility to de-activate rx-enable gpio when rs485 is disabled. This defines the state of RE or DE/RE signal when em485 is disabled.
--- drivers/tty/serial/8250/8250_port.c | 4 ++++ drivers/tty/serial/serial_core.c | 6 +++++- include/linux/serial_core.h | 1 + 3 files changed, 10 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_port.c +4 -0
@@ -582,12 +582,16 @@ static int serial8250_em485_init(struct uart_8250_port *p)*/ void serial8250_em485_destroy (struct uart_8250_port *p) { +struct uart_port *port = &p->port; +if (!p->em485)return ;hrtimer_cancel (&p->em485->start_tx_timer);hrtimer_cancel (&p->em485->stop_tx_timer); +gpiod_set_value (port->rs485_re_gpio, !port->rs485_re_gpio_inactive_when_rs485_disabled); +kfree (p->em485); p->em485 =NULL ; }
drivers/tty/serial/serial_core.c +5 -1
@@ -3663,7 +3663,11 @@ int uart_get_rs485_mode(struct uart_port *port)if (port->rs485_rx_during_tx_gpio) port->rs485_supported.flags |=SER_RS485_RX_DURING_TX ; - port->rs485_re_gpio =devm_gpiod_get_optional (dev,"rs485-rx-enable" ,GPIOD_OUT_HIGH ); + port->rs485_re_gpio_inactive_when_rs485_disabled =device_property_read_bool (dev, +"rs485-rx-enable-inactive-when-rs485-disabled" ); + + port->rs485_re_gpio =devm_gpiod_get_optional (dev,"rs485-rx-enable" , + port->rs485_re_gpio_inactive_when_rs485_disabled ?GPIOD_OUT_LOW :GPIOD_OUT_HIGH );if (IS_ERR (port->rs485_re_gpio)) { ret =PTR_ERR (port->rs485_re_gpio); port->rs485_re_gpio =NULL ;
include/linux/serial_core.h +1 -0
@@ -585,6 +585,7 @@ struct uart_port {struct gpio_desc *rs485_term_gpio;/* enable RS485 bus termination */ struct gpio_desc *rs485_rx_during_tx_gpio;/* Output GPIO that sets the state of RS485 RX during TX */ struct gpio_desc *rs485_re_gpio;/* gpio RS485 receive enable */ +bool rs485_re_gpio_inactive_when_rs485_disabled;struct serial_iso7816 iso7816;void *private_data;/* generic platform data pointer */ };
Signature · 1 line
2.34.1
PATCH v5 5/6 arm64: dts: rockchip: rk3399-puma-haikou: add rs485 support on uart2
In reply to Farouk BouabidA hardware switch can set the rs485 transceiver into half or full duplex mode.
Switching to the half-duplex mode requires the user to enable em485 on uart2 using ioctl, DE/RE are both connected to GPIO2_C3 which is the RTS signal for uart0. Which means GPIO2_C3 is implemented as rs485 rx-enable gpio.
In full-duplex mode (em485 is disabled), DE is connected to GPIO2_C3 and RE is grounded (enabled). This requires rx-enable gpio to be inactive to enable DE as well.
--- arch/arm64/boot/dts/rockchip/rk3399-puma-haikou.dts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/boot/dts/rockchip/rk3399-puma-haikou.dts +3 -1
@@ -273,11 +273,13 @@ &u2phy0_host { &uart0 { pinctrl-names = "default"; - pinctrl-0 = <&uart0_xfer &uart0_cts &uart0_rts>; + pinctrl-0 = <&uart0_xfer>; status = "okay"; }; &uart2 { + rs485-rx-enable-gpios = <&gpio2 RK_PC3 GPIO_ACTIVE_LOW>; + rs485-rx-enable-inactive-when-rs485-disabled; status = "okay"; };
Signature · 1 line
2.34.1
PATCH v5 6/6 arm64: dts: rockchip: px30-ringneck-haikou: add rs485 support on uart5
In reply to Farouk BouabidA hardware switch can set the rs485 transceiver into half or full duplex mode.
Switching to the half-duplex mode requires the user to enable em485 on uart5 using ioctl, DE/RE are both connected to GPIO0_B5 which is the RTS signal for uart5. Which means GPIO0_B5 is implemented as rs485 rx-enable gpio.
In full-duplex mode (em485 is disabled), DE is connected to GPIO0_B5 and RE is grounded (active). This requires rx-enable gpio to be inactive to enable DE as well.
--- arch/arm64/boot/dts/rockchip/px30-ringneck-haikou.dts | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/boot/dts/rockchip/px30-ringneck-haikou.dts +2 -0
@@ -227,6 +227,8 @@ &uart0 { &uart5 { pinctrl-0 = <&uart5_xfer>; + rs485-rx-enable-gpios = <&gpio0 RK_PB5 GPIO_ACTIVE_LOW>; + rs485-rx-enable-inactive-when-rs485-disabled; status = "okay"; };
Signature · 1 line
2.34.1