[PATCH 2/3] serial: core: Fix function uart_update_timeout() to handle UPF_SPD_CUST flag

From: Pali Rohár
Date: Mon Mar 21 2022 - 12:32:03 EST


Function uart_update_timeout() currently handles UPF_SPD_HI, UPF_SPD_VHI,
UPF_SPD_SHI and UPF_SPD_WARP flags thanks to how uart_get_baud_rate()
works and how should be used. uart_get_baud_rate() already translates these
4 special flags to real baud rate value and therefore uart_update_timeout()
is called with the correct baud rate argument.

What is not handled in this function is the last remaining flag
UPF_SPD_CUST. It is because uart_get_baud_rate() returns hardcoded value
38400 when UPF_SPD_CUST is set and in use.

Implement support for UPF_SPD_CUST in uart_update_timeout(), so
port->timeout is calculated also when UPF_SPD_CUST flag is set.

For calculation use base uart clock and custom divisor. This calculation
does not have to be precise for all UART hardware but this the best
approximation which can be done. It is for sure better than hardcoded baud
rate value 38400.

Signed-off-by: Pali Rohár <pali@xxxxxxxxxx>
---
drivers/tty/serial/serial_core.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index d8fc2616d62b..34e085a038fe 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -343,6 +343,16 @@ uart_update_timeout(struct uart_port *port, unsigned int cflag,
{
unsigned int size;

+ /*
+ * Old custom speed handling. See function uart_get_divisor()
+ * and description in uart_get_baud_rate() function.
+ * This calculation does not have to be precise for all UART
+ * hardware but it is the best approximation which we can do here
+ * as we do not know the exact baud rate.
+ */
+ if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
+ baud = DIV_ROUND_CLOSEST(port->uartclk, 16 * port->custom_divisor);
+
size = tty_get_frame_size(cflag) * port->fifosize;

/*
--
2.20.1