[PATCH mainline-linux 3/4] serial: sh-sci: rsci: Add RS485 support
From: Diogo Ivo
Date: Fri Nov 20 2026 - 10:50:35 EST
The RSCI IP block on RZ/G3E and RZ/G3L has hardware support for RS485,
via a dedicated DE (Driver Enable) output signal meant to control the
driver of the RS485 transceiver. Add support for this feature to the
RSCI driver.
As there is no RE (Receive Enable) signal on RSCI we handle half-duplex
mode in software, by disabling reception while we transmit data and
reenabling it once we finish transmitting data in order to avoid
capturing our echo.
Signed-off-by: Diogo Ivo <diogo.ivo.sx@xxxxxxxxxxx>
---
drivers/tty/serial/rsci.c | 89 ++++++++++++++++++++++++++++++++++++++
drivers/tty/serial/sh-sci-common.h | 3 ++
drivers/tty/serial/sh-sci.c | 14 ++++++
3 files changed, 106 insertions(+)
diff --git a/drivers/tty/serial/rsci.c b/drivers/tty/serial/rsci.c
index 80087be11808..00c48937efb0 100644
--- a/drivers/tty/serial/rsci.c
+++ b/drivers/tty/serial/rsci.c
@@ -26,6 +26,7 @@ MODULE_IMPORT_NS("SH_SCI");
#define CCR3 0x14
#define CCR4 0x18
#define FCR 0x24
+#define DCR 0x30
#define CSR 0x48
#define FRSR 0x50
#define FTSR 0x54
@@ -63,11 +64,13 @@ MODULE_IMPORT_NS("SH_SCI");
/* CCR2 (Common Control Register 2) */
#define CCR2_INIT 0xFF000004
+#define CCR2_CKS GENMASK(21, 20) /* Clock select */
#define CCR2_CKS_TCLK (0) /* TCLK clock */
#define CCR2_CKS_TCLK_DIV4 BIT(20) /* TCLK/4 clock */
#define CCR2_CKS_TCLK_DIV16 BIT(21) /* TCLK16 clock */
#define CCR2_CKS_TCLK_DIV64 (BIT(21) | BIT(20)) /* TCLK/64 clock */
#define CCR2_BRME BIT(16) /* Bitrate Modulation Enable */
+#define CCR2_BRR GENMASK(15, 8) /* Bitrate Setting */
#define CCR2_ABCSE BIT(6) /* Asynchronous Mode Extended Base Clock Select */
#define CCR2_ABCS BIT(5) /* Asynchronous Mode Base Clock Select */
#define CCR2_BGDM BIT(4) /* Baud Rate Generator Double-Speed Mode Select */
@@ -103,6 +106,11 @@ MODULE_IMPORT_NS("SH_SCI");
#define FCR_RTRG4_0 GENMASK(20, 16)
#define FCR_TTRG GENMASK(12, 8)
+/* DCR (Driver Control Register) */
+#define DCR_DENGT GENMASK(20, 16) /* Driver Negation Time */
+#define DCR_DEAST GENMASK(12, 8) /* Driver Assertion Time */
+#define DCR_DEPOL BIT(0) /* Driver Effective Polarity Select */
+
/* CSR (Common Status Register) */
#define CSR_RDRF BIT(31) /* Receive Data Full */
#define CSR_TEND BIT(30) /* Transmit End Flag */
@@ -213,6 +221,76 @@ static int rsci_scif_set_rtrg(struct uart_port *port, int rx_trig)
return rx_trig;
}
+static unsigned long rsci_get_base_clock_rate(struct uart_port *port)
+{
+ unsigned int cks, brr, prediv;
+ u32 ccr2;
+
+ ccr2 = rsci_serial_in(port, CCR2);
+ cks = FIELD_GET(CCR2_CKS, ccr2);
+ brr = FIELD_GET(CCR2_BRR, ccr2);
+
+ if (ccr2 & (CCR2_BGDM | CCR2_ABCSE))
+ prediv = 1 << (2 * cks);
+ else
+ prediv = 2 << (2 * cks);
+
+ return DIV_ROUND_CLOSEST(port->uartclk, prediv * (brr + 1));
+}
+
+static int rsci_rs485_config(struct uart_port *port, struct ktermios *termios,
+ struct serial_rs485 *rs485)
+{
+ u32 deast_clks, dengt_clks, base_rate;
+ u32 ccr0, ccr3, dcr;
+
+ ccr0 = rsci_serial_in(port, CCR0);
+ if (ccr0 & (CCR0_TE | CCR0_RE))
+ rsci_serial_out(port, CCR0, ccr0 & ~(CCR0_TE | CCR0_RE));
+
+ ccr3 = rsci_serial_in(port, CCR3);
+ dcr = rsci_serial_in(port, DCR);
+
+ dcr &= ~(DCR_DEPOL | DCR_DEAST | DCR_DENGT);
+
+ if (rs485->flags & SER_RS485_ENABLED) {
+ if (!(rs485->flags & SER_RS485_RTS_ON_SEND))
+ dcr |= DCR_DEPOL;
+
+ base_rate = rsci_get_base_clock_rate(port);
+
+ deast_clks = DIV_ROUND_UP_ULL((u64)rs485->delay_rts_before_send * base_rate,
+ MSEC_PER_SEC);
+ dengt_clks = DIV_ROUND_UP_ULL((u64)rs485->delay_rts_after_send * base_rate,
+ MSEC_PER_SEC);
+
+ /* Per the HW manual a value of 0 clocks is not valid so clamp to 1 */
+ deast_clks = clamp(deast_clks, 1U, FIELD_MAX(DCR_DEAST));
+ dengt_clks = clamp(dengt_clks, 1U, FIELD_MAX(DCR_DENGT));
+
+ dcr |= FIELD_PREP(DCR_DEAST, deast_clks) |
+ FIELD_PREP(DCR_DENGT, dengt_clks);
+ ccr3 |= CCR3_DEN;
+ } else {
+ ccr3 &= ~CCR3_DEN;
+ }
+
+ rsci_serial_out(port, CCR3, ccr3);
+ rsci_serial_out(port, DCR, dcr);
+
+ if (ccr0 & (CCR0_TE | CCR0_RE))
+ rsci_serial_out(port, CCR0, ccr0);
+
+ return 0;
+}
+
+static const struct serial_rs485 rsci_rs485_supported = {
+ .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
+ SER_RS485_RTS_AFTER_SEND | SER_RS485_RX_DURING_TX,
+ .delay_rts_before_send = 1,
+ .delay_rts_after_send = 1,
+};
+
static void rsci_set_termios(struct uart_port *port, struct ktermios *termios,
const struct ktermios *old)
{
@@ -315,6 +393,9 @@ static void rsci_set_termios(struct uart_port *port, struct ktermios *termios,
rsci_serial_out(port, CFCLR, CFCLR_CLRFLAG);
rsci_serial_out(port, FFCLR, FFCLR_DRC);
+ if ((port->rs485.flags & SER_RS485_ENABLED) && port->rs485_config)
+ rsci_rs485_config(port, termios, &port->rs485);
+
ccr0_val |= CCR0_RE;
rsci_serial_out(port, CCR0, ccr0_val);
@@ -400,6 +481,12 @@ static void rsci_start_tx(struct uart_port *port)
*/
ctrl = rsci_serial_in(port, CCR0);
ctrl |= CCR0_TIE | CCR0_TE;
+
+ /* Prevent echo in RS485 half-duplex mode */
+ if ((port->rs485.flags & SER_RS485_ENABLED) &&
+ !(port->rs485.flags & SER_RS485_RX_DURING_TX))
+ ctrl &= ~CCR0_RE;
+
rsci_serial_out(port, CCR0, ctrl);
}
@@ -631,6 +718,7 @@ static const struct sci_port_params rsci_rzg3e_port_params = {
.error_clear = RSCI_ERROR_CLEAR,
.param_bits = &rsci_port_param_bits,
.common_regs = &rsci_common_regs,
+ .rs485_supported = &rsci_rs485_supported,
};
static const struct sci_port_params rsci_rzt2h_port_params = {
@@ -676,6 +764,7 @@ static const struct sci_port_ops rsci_port_ops = {
.suspend_regs_size = rsci_suspend_regs_size,
.set_rtrg = rsci_scif_set_rtrg,
.shutdown_complete = rsci_shutdown_complete,
+ .rs485_config = rsci_rs485_config,
};
struct sci_of_data of_rsci_rzg3e_data = {
diff --git a/drivers/tty/serial/sh-sci-common.h b/drivers/tty/serial/sh-sci-common.h
index b75e5e1d3075..b4f27740a8b9 100644
--- a/drivers/tty/serial/sh-sci-common.h
+++ b/drivers/tty/serial/sh-sci-common.h
@@ -72,6 +72,7 @@ struct sci_port_params {
const struct plat_sci_reg regs[SCI_NR_REGS];
const struct sci_common_regs *common_regs;
const struct sci_port_params_bits *param_bits;
+ const struct serial_rs485 *rs485_supported;
unsigned int fifosize;
unsigned int overrun_reg;
unsigned int overrun_mask;
@@ -100,6 +101,8 @@ struct sci_port_ops {
void (*console_save)(struct uart_port *port);
void (*console_restore)(struct uart_port *port);
size_t (*suspend_regs_size)(void);
+ int (*rs485_config)(struct uart_port *port, struct ktermios *termios,
+ struct serial_rs485 *rs485);
};
struct sci_of_data {
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index 1ab0e5677791..4d8d42e4f08a 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -2043,6 +2043,10 @@ static irqreturn_t sci_tx_end_interrupt(int irq, void *ptr)
goto out_unlock;
}
+ if ((port->rs485.flags & SER_RS485_ENABLED) &&
+ !(port->rs485.flags & SER_RS485_RX_DURING_TX))
+ ctrl |= bits->rxtx_enable;
+
ctrl &= ~bits->te_clear;
s->ops->write_reg(port, regs->control, ctrl);
out_unlock:
@@ -3865,6 +3869,16 @@ static int sci_probe_single(struct platform_device *dev,
sciport->port.flags |= UPF_HARD_FLOW;
}
+ if (sciport->params->rs485_supported) {
+ sciport->port.rs485_config = sciport->ops->rs485_config;
+ sciport->port.rs485_supported = *sciport->params->rs485_supported;
+
+ ret = uart_get_rs485_mode(&sciport->port);
+ if (ret)
+ return dev_err_probe(&dev->dev, ret,
+ "Failed to get RS485 mode\n");
+ }
+
if (sci_uart_earlycon && sci_ports[0].port.mapbase == sci_res->start) {
/*
* In case:
--
2.56.1