serial: stm32: prevent TDR register overwrite when sending x_char
authorValentin Caron <valentin.caron@foss.st.com>
Tue, 11 Jan 2022 16:44:40 +0000 (17:44 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 26 Jan 2022 13:52:44 +0000 (14:52 +0100)
When sending x_char in stm32_usart_transmit_chars(), driver can overwrite
the value of TDR register by the value of x_char. If this happens, the
previous value that was present in TDR register will not be sent through
uart.

This code checks if the previous value in TDR register is sent before
writing the x_char value into register.

Fixes: 48a6092fb41f ("serial: stm32-usart: Add STM32 USART Driver")
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Valentin Caron <valentin.caron@foss.st.com>
Link: https://lore.kernel.org/r/20220111164441.6178-2-valentin.caron@foss.st.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/serial/stm32-usart.c

index 1f89ab0..c1b8828 100644 (file)
@@ -550,11 +550,23 @@ static void stm32_usart_transmit_chars(struct uart_port *port)
        struct stm32_port *stm32_port = to_stm32_port(port);
        const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
        struct circ_buf *xmit = &port->state->xmit;
+       u32 isr;
+       int ret;
 
        if (port->x_char) {
                if (stm32_usart_tx_dma_started(stm32_port) &&
                    stm32_usart_tx_dma_enabled(stm32_port))
                        stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
+
+               /* Check that TDR is empty before filling FIFO */
+               ret =
+               readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
+                                                 isr,
+                                                 (isr & USART_SR_TXE),
+                                                 10, 1000);
+               if (ret)
+                       dev_warn(port->dev, "1 character may be erased\n");
+
                writel_relaxed(port->x_char, port->membase + ofs->tdr);
                port->x_char = 0;
                port->icount.tx++;