Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tapio Reijonen" <tapio.reijonen@vaisala.com>
Cc: linux-serial@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH RESEND v2 3/3] serial: core: Drive the transceiver interface select GPIO
Date: Sun, 06 Dec 2026 09:15:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-252306@kernel.org> (raw)
In-Reply-To: <20261206-serial-rs485-mode-gpio-v2-3-b9ea0dc2af3a@vaisala.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] serial: rs485_select_gpio driven before async rs485_config()
  parks RTS
- [Medium] serial: uart_set_rs485_config() leaves rs485_term_gpio high
  on disable
- [Medium] serial: core: set_mctrl() runs before uart_rs485_config()
  leaves RS485
--

commit 42e26c01d500a86e5f47a0d41e2915cadf1d5b27
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>

serial: core: Drive the transceiver interface select GPIO

This commit adds an optional rs485-select-gpios property to
uart_get_rs485_mode() and drives it from uart_rs485_config() and
uart_set_rs485_config() to select between RS485 and an alternative
transceiver interface.

> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index 9516f50a72139..6c0a6d9dcb851 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c

[ ... ]

> @@ -1446,13 +1447,22 @@ static void uart_set_rs485_rx_during_tx(struct uart_port *port,
>  				 !!(rs485->flags & SER_RS485_RX_DURING_TX));
>  }
>  
> +static void uart_set_rs485_select_gpio(struct uart_port *port,
> +				       const struct serial_rs485 *rs485)
> +{
> +	gpiod_set_value_cansleep(port->rs485_select_gpio,
> +				 !!(rs485->flags & SER_RS485_ENABLED));
> +}
> +
>  static int uart_rs485_config(struct uart_port *port)
>  {
>  	struct serial_rs485 *rs485 = &port->rs485;
>  	int ret;
>  
> -	if (!(rs485->flags & SER_RS485_ENABLED))
> +	if (!(rs485->flags & SER_RS485_ENABLED)) {
> +		uart_set_rs485_select_gpio(port, rs485);
>  		return 0;
> +	}

[Severity: Medium]
Does uart_configure_port() drive RTS via set_mctrl() before this path
takes the transceiver out of RS485 mode?

When rs485-select-gpios is strapped active and
linux,rs485-enabled-at-boot-time is not set, uart_get_rs485_mode() keeps
the select line active through probe.  uart_configure_port() then calls
port->ops->set_mctrl(port, port->mctrl) before uart_rs485_config().

On drivers where clearing TIOCM_RTS drives the physical RTS/DE pin high
(such as imx_uart_set_mctrl() or pl011_set_mctrl()), that enables the
RS485 driver onto the shared bus until gpiod_set_value_cansleep()
switches the transceiver to the alternative interface.

Deasserting rs485_select_gpio before set_mctrl() in uart_configure_port(),
matching uart_set_rs485_config(), would avoid the glitch.

[ ... ]

> @@ -1503,6 +1515,10 @@ static int uart_set_rs485_config(struct tty_struct *tty, struct uart_port *port,
>  	uart_set_rs485_termination(port, &rs485);
>  	uart_set_rs485_rx_during_tx(port, &rs485);
>  
> +	/* Leave RS485 before RTS reverts to modem control below */
> +	if (!(rs485.flags & SER_RS485_ENABLED))
> +		uart_set_rs485_select_gpio(port, &rs485);

[Severity: Medium]
Does disabling RS485 here leave rs485_term_gpio asserted after switching
to the alternative interface?

When userspace enables RS485 with SER_RS485_TERMINATE_BUS and later
disables RS485 via TIOCSRS485, uart_set_rs485_termination() returns early
because SER_RS485_ENABLED is clear.  uart_set_rs485_select_gpio() then
switches the transceiver to RS232 on the same bus pins while
rs485_term_gpio stays high and TIOCGRS485 reports termination off.

On boards where termination is not internally gated by the select pin,
the resistor stays connected across the RS232 lines and overloads the
driver.  Dropping the early return in uart_set_rs485_termination() would
clear it, since uart_sanitize_serial_rs485() already clears the flag.

[ ... ]

> +	/* Enter RS485 only once the driver has parked RTS */
> +	if (rs485.flags & SER_RS485_ENABLED)
> +		uart_set_rs485_select_gpio(port, &rs485);

[Severity: Medium]
Can uart_set_rs485_select_gpio() here (and in uart_rs485_config()) enable
RS485 before drivers with asynchronous rs485_config() callbacks park RTS?

SPI/I2C drivers such as max310x and sc16is7xx call uart_get_rs485_mode()
but defer hardware setup to a worker because port->rs485_config() runs
under port->lock.  In max310x, the worker blocks on port->mutex, which
serial_core holds across the GPIO update:

uart_ioctl()                        kworker (max310x_rs_proc)
mutex_lock(&port->mutex)
uart_set_rs485_config()
  max310x_rs485_config()
    schedule_work(&one->rs_work)
                                    mutex_lock(&port->mutex)  <-- blocks
  uart_set_rs485_select_gpio()  <-- RS485 enabled with unparked RTS
mutex_unlock(&port->mutex)
                                    max310x_set_rts_ctl_params()

sc16is7xx_config_rs485() similarly queues reg_work on s->kworker and
races with uart_set_rs485_select_gpio().  If RTS is asserted from RS232
modem control on an open port, switching to RS485 before the worker parks
RTS enables the RS485 transmitter onto the shared bus.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206-serial-rs485-mode-gpio-v2-0-b9ea0dc2af3a@vaisala.com?part=3

      reply	other threads:[~2026-12-06  9:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
2026-12-06  9:19   ` sashiko-bot
2026-12-06  8:50 ` [PATCH RESEND v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
2026-12-06  8:50 ` [PATCH RESEND v2 3/3] serial: core: Drive the " Tapio Reijonen
2026-12-06  9:15   ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-252306@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tapio.reijonen@vaisala.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox