Commit d1fec5ec authored by Lukas Wunner's avatar Lukas Wunner Committed by Lipeng Sang
Browse files

serial: Deassert Transmit Enable on probe in driver-specific way

stable inclusion
from stable-v5.10.153
commit 26a2b9c468de495902fb914c050b4e8611764b2a
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I64YCA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=26a2b9c468de495902fb914c050b4e8611764b2a

--------------------------------

commit 7c7f9bc9 upstream.

When a UART port is newly registered, uart_configure_port() seeks to
deassert RS485 Transmit Enable by setting the RTS bit in port->mctrl.
However a number of UART drivers interpret a set RTS bit as *assertion*
instead of deassertion:  Affected drivers include those using
serial8250_em485_config() (except 8250_bcm2835aux.c) and some using
mctrl_gpio (e.g. imx.c).

Since the interpretation of the RTS bit is driver-specific, it is not
suitable as a means to centrally deassert Transmit Enable in the serial
core.  Instead, the serial core must call on drivers to deassert it in
their driver-specific way.  One way to achieve that is to call
->rs485_config().  It implicitly deasserts Transmit Enable.

So amend uart_configure_port() and uart_resume_port() to invoke
uart_rs485_config().  That allows removing calls to uart_rs485_config()
from drivers' ->probe() hooks and declaring the function static.

Skip any invocation of ->set_mctrl() if RS485 is enabled.  RS485 has no
hardware flow control, so the modem control lines are irrelevant and
need not be touched.  When leaving RS485 mode, reset the modem control
lines to the state stored in port->mctrl.  That way, UARTs which are
muxed between RS485 and RS232 transceivers drive the lines correctly
when switched to RS232.  (serial8250_do_startup() historically raises
the OUT1 modem signal because otherwise interrupts are not signaled on
ancient PC UARTs, but I believe that no longer applies to modern,
RS485-capable UARTs and is thus safe to be skipped.)

imx.c modifies port->mctrl whenever Transmit Enable is asserted and
deasserted.  Stop it from doing that so port->mctrl reflects the RS232
line state.

8250_omap.c deasserts Transmit Enable on ->runtime_resume() by calling
->set_mctrl().  Because that is now a no-op in RS485 mode, amend the
function to call serial8250_em485_stop_tx().

fsl_lpuart.c retrieves and applies the RS485 device tree properties
after registering the UART port.  Because applying now happens on
registration in uart_configure_port(), move retrieval of the properties
ahead of uart_add_one_port().

Link: https://lore.kernel.org/all/20220329085050.311408-1-matthias.schiffer@ew.tq-group.com/
Link: https://lore.kernel.org/all/8f538a8903795f22f9acc94a9a31b03c9c4ccacb.camel@ginzinger.com/


Fixes: d3b3404d ("serial: Fix incorrect rs485 polarity on uart open")
Cc: stable@vger.kernel.org # v4.14+
Reported-by: default avatarMatthias Schiffer <matthias.schiffer@ew.tq-group.com>
Reported-by: default avatarRoosen Henri <Henri.Roosen@ginzinger.com>
Tested-by: default avatarMatthias Schiffer <matthias.schiffer@ew.tq-group.com>
Reviewed-by: default avatarIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: default avatarLukas Wunner <lukas@wunner.de>
Link: https://lore.kernel.org/r/2de36eba3fbe11278d5002e4e501afe0ceaca039.1663863805.git.lukas@wunner.de


Signed-off-by: default avatarDaisuke Mizobuchi <mizo@atmark-techno.com>
Signed-off-by: default avatarDominique Martinet <dominique.martinet@atmark-techno.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarLipeng Sang <sanglipeng1@jd.com>
parent c24c5f83
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -342,6 +342,9 @@ static void omap8250_restore_regs(struct uart_8250_port *up)
	omap8250_update_mdr1(up, priv);

	up->port.ops->set_mctrl(&up->port, up->port.mctrl);

	if (up->port.rs485.flags & SER_RS485_ENABLED)
		serial8250_em485_stop_tx(up);
}

/*
+1 −8
Original line number Diff line number Diff line
@@ -1531,7 +1531,6 @@ static int pci_fintek_init(struct pci_dev *dev)
	resource_size_t bar_data[3];
	u8 config_base;
	struct serial_private *priv = pci_get_drvdata(dev);
	struct uart_8250_port *port;

	if (!(pci_resource_flags(dev, 5) & IORESOURCE_IO) ||
			!(pci_resource_flags(dev, 4) & IORESOURCE_IO) ||
@@ -1578,13 +1577,7 @@ static int pci_fintek_init(struct pci_dev *dev)

		pci_write_config_byte(dev, config_base + 0x06, dev->irq);

		if (priv) {
			/* re-apply RS232/485 mode when
			 * pciserial_resume_ports()
			 */
			port = serial8250_get_port(priv->line[i]);
			pci_fintek_rs485_config(&port->port, NULL);
		} else {
		if (!priv) {
			/* First init without port data
			 * force init to RS232 Mode
			 */
+7 −5
Original line number Diff line number Diff line
@@ -592,7 +592,7 @@ EXPORT_SYMBOL_GPL(serial8250_rpm_put);
static int serial8250_em485_init(struct uart_8250_port *p)
{
	if (p->em485)
		return 0;
		goto deassert_rts;

	p->em485 = kmalloc(sizeof(struct uart_8250_em485), GFP_ATOMIC);
	if (!p->em485)
@@ -608,6 +608,8 @@ static int serial8250_em485_init(struct uart_8250_port *p)
	p->em485->active_timer = NULL;
	p->em485->tx_stopped = true;

deassert_rts:
	if (p->em485->tx_stopped)
		p->rs485_stop_tx(p);

	return 0;
@@ -2030,6 +2032,9 @@ EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);

static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	if (port->rs485.flags & SER_RS485_ENABLED)
		return;

	if (port->set_mctrl)
		port->set_mctrl(port, mctrl);
	else
@@ -3161,9 +3166,6 @@ static void serial8250_config_port(struct uart_port *port, int flags)
	if (flags & UART_CONFIG_TYPE)
		autoconfig(up);

	if (port->rs485.flags & SER_RS485_ENABLED)
		port->rs485_config(port, &port->rs485);

	/* if access method is AU, it is a 16550 with a quirk */
	if (port->type == PORT_16550A && port->iotype == UPIO_AU)
		up->bugs |= UART_BUG_NOMSR;
+3 −5
Original line number Diff line number Diff line
@@ -2669,10 +2669,6 @@ static int lpuart_probe(struct platform_device *pdev)
	if (ret)
		goto failed_irq_request;

	ret = uart_add_one_port(&lpuart_reg, &sport->port);
	if (ret)
		goto failed_attach_port;

	ret = uart_get_rs485_mode(&sport->port);
	if (ret)
		goto failed_get_rs485;
@@ -2684,7 +2680,9 @@ static int lpuart_probe(struct platform_device *pdev)
	    sport->port.rs485.delay_rts_after_send)
		dev_err(&pdev->dev, "driver doesn't support RTS delays\n");

	sport->port.rs485_config(&sport->port, &sport->port.rs485);
	ret = uart_add_one_port(&lpuart_reg, &sport->port);
	if (ret)
		goto failed_attach_port;

	return 0;

+2 −6
Original line number Diff line number Diff line
@@ -398,8 +398,7 @@ static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2)
{
	*ucr2 &= ~(UCR2_CTSC | UCR2_CTS);

	sport->port.mctrl |= TIOCM_RTS;
	mctrl_gpio_set(sport->gpios, sport->port.mctrl);
	mctrl_gpio_set(sport->gpios, sport->port.mctrl | TIOCM_RTS);
}

/* called with port.lock taken and irqs caller dependent */
@@ -408,8 +407,7 @@ static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2)
	*ucr2 &= ~UCR2_CTSC;
	*ucr2 |= UCR2_CTS;

	sport->port.mctrl &= ~TIOCM_RTS;
	mctrl_gpio_set(sport->gpios, sport->port.mctrl);
	mctrl_gpio_set(sport->gpios, sport->port.mctrl & ~TIOCM_RTS);
}

static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
@@ -2381,8 +2379,6 @@ static int imx_uart_probe(struct platform_device *pdev)
		dev_err(&pdev->dev,
			"low-active RTS not possible when receiver is off, enabling receiver\n");

	imx_uart_rs485_config(&sport->port, &sport->port.rs485);

	/* Disable interrupts before requesting them */
	ucr1 = imx_uart_readl(sport, UCR1);
	ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN);
Loading