Skip to contentSkip to conversation

serial: 8250_of: set UART_CAP_NOTEMT for ports with rts-gpios

PATCH In review Nicolas Thibert 2 participants 3 messages Dec 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 serial: 8250_of: set UART_CAP_NOTEMT for ports with rts-gpios

Since commit 0c11b88883db ("tty: 8250_of: Use software emulated RS485 direction control"), 8250_of uses the software RS485 emulation (em485). At the end of a transmission, em485 releases the direction line only once the transmitter is empty (TEMT). A 16550 has no interrupt on TEMT, so when the last THRE interrupt comes before the shift register is empty, __stop_tx() returns and nothing releases the direction line
later: it stays asserted and the board cannot receive the answer from the other RS485 node.

8250_dw solves the same problem by setting UART_CAP_NOTEMT, which makes __stop_tx() release the line one frame time later instead. Do the same in 8250_of for ports whose direction line is a GPIO (rts-gpios).

UART_CAP_NOTEMT is only used by the em485 stop path, so ports without RS485 are not affected.

Tested on a QCA9531 board (ns16550a UART) with a CA-IS2092A RS485 transceiver whose DE/RE pins are driven by a GPIO (rts-gpios, linux,rs485-enabled-at-boot-time). Without this patch the GPIO stays high after the first transmission and no answer is received. With it, the GPIO goes low after each transmission and the answers are received.

The problem was analysed and this patch written with the help of an AI coding assistant (Claude, Anthropic); the tests were run on hardware.

Fixes 0c11b88883db ("tty: 8250_of: Use software emulated RS485 direction control")

---
v2:
- Rewrite the commit message.
- Drop the code comment.
- Put Signed-off-by last.

Answers to the review of v1:
- What changes on existing systems: UART_CAP_NOTEMT is only checked in the em485 stop path, so only RS485 ports whose direction line is a GPIO are affected. If the transmitter is already empty at the last THRE interrupt, nothing changes. Otherwise the direction line is now released one frame time later, instead of staying asserted.
- How it was tested: see the last paragraph of the commit message.
- v1 was sent twice by mistake, first with a mail client, then with git send-email.

 drivers/tty/serial/8250/8250_of.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/8250/8250_of.c +3 -0
index b41ca604083f..b58535f73cb8 100644
@@ -292,6 +292,9 @@ static int of_platform_serial_probe(struct platform_device *ofdev)
 			&port8250.overrun_backoff_time_ms) != 0)
 		port8250.overrun_backoff_time_ms = 0;
 
+	if (of_property_present(ofdev->dev.of_node, "rts-gpios"))
+		port8250.capabilities |= UART_CAP_NOTEMT;
+
 	info->uport = serial8250_register_8250_port(&port8250);
 	if (IS_ERR(info->uport)) {
 		ret = PTR_ERR(info->uport);
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] serial: 8250_of: setting UART_CAP_NOTEMT skips uart_config default flags - [Medium] serial: 8250_of…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] serial: 8250_of: setting UART_CAP_NOTEMT skips uart_config default flags
- [Medium] serial: 8250_of: gating UART_CAP_NOTEMT on rts-gpios misses native RTS em485
--

commit 3ce247ebc56619daf138861af9c248e9f1ac7b45
Author: Nicolas Thibert <[email protected]>

serial: 8250_of: set UART_CAP_NOTEMT for ports with rts-gpios

This commit sets UART_CAP_NOTEMT on 8250_of ports when the rts-gpios Device Tree property is present so that software-emulated RS485 direction control releases the direction line after transmission completes.

Quoted 5 lines
drivers/tty/serial/8250/8250_of.c +0 -0
index b41ca604083f6..b58535f73cb88 100644

drivers/tty/serial/8250/8250_of.c static int of_platform_serial_probe(struct platform_device *ofdev) · line 300

Quoted 9 lines
drivers/tty/serial/8250/8250_of.c +3 -0
@@ -292,6 +292,9 @@ static int of_platform_serial_probe(struct platform_device *ofdev)
 			&port8250.overrun_backoff_time_ms) != 0)
 		port8250.overrun_backoff_time_ms = 0;
 
+	if (of_property_present(ofdev->dev.of_node, "rts-gpios"))
+		port8250.capabilities |= UART_CAP_NOTEMT;
+
 	info->uport = serial8250_register_8250_port(&port8250);
 	if (IS_ERR(info->uport)) {
 		ret = PTR_ERR(info->uport);

[Severity: High]
Does setting UART_CAP_NOTEMT here drop UART_CAP_FIFO and the rest of uart_config[type].flags when the Device Tree node omits the optional fifo-size property?

Earlier in of_platform_serial_probe(), UART_CAP_FIFO is only set if port8250.port.fifosize is already non-zero. When fifo-size is not in Device Tree, fifosize and capabilities are still 0 here, and serial8250_register_8250_port() -> serial8250_set_defaults() only loads the per-type default flags when capabilities is 0:

drivers/tty/serial/8250/8250_port.c:serial8250_set_defaults() {
    ...
		if (!up->capabilities)
			up->capabilities = uart_config[type].flags;
    ...
}

Because capabilities is now UART_CAP_NOTEMT, uart_config[type].flags is skipped. Without UART_CAP_FIFO, serial8250_clear_fifos() becomes a no-op (including during serial8250_em485_stop_tx()) and console writes fall back from FIFO bursts to single-byte polling.

[Severity: Medium]
Should UART_CAP_NOTEMT also be set for RS485 ports that drive the transceiver direction pin with the UART's native RTS signal instead of rts-gpios?

of_platform_serial_setup() enables software-emulated RS485 (serial8250_em485_config) for all 8250_of ports, and serial8250_out_MCR() toggles UART_MCR_RTS as well as any rts-gpios. Gating UART_CAP_NOTEMT on rts-gpios leaves native-RTS RS485 ports with the same bug where __stop_tx() returns early when UART_LSR_TEMT is not yet set and never deasserts RTS.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 4 lines

- [High] serial: 8250_of: setting UART_CAP_NOTEMT skips uart_config default flags
- [Medium] serial: 8250_of: gating UART_CAP_NOTEMT on rts-gpios misses native RTS em485

Thanks, both points are right: setting the capability before registration drops UART_CAP_FIFO when there is no fifo-size property, and em485 also drives the native RTS. I will send a v3 that sets UART_CAP_NOTEMT after serial8250_register_8250_port() for all ports.