Skip to content

Commit c80ee36

Browse files
jhovoldgregkh
authored andcommitted
serial: qcom-geni: fix fifo polling timeout
The qcom_geni_serial_poll_bit() can be used to wait for events like command completion and is supposed to wait for the time it takes to clear a full fifo before timing out. As noted by Doug, the current implementation does not account for start, stop and parity bits when determining the timeout. The helper also does not currently account for the shift register and the two-word intermediate transfer register. A too short timeout can specifically lead to lost characters when waiting for a transfer to complete as the transfer is cancelled on timeout. Instead of determining the poll timeout on every call, store the fifo timeout when updating it in set_termios() and make sure to take the shift and intermediate registers into account. Note that serial core has already added a 20 ms margin to the fifo timeout. Also note that the current uart_fifo_timeout() interface does unnecessary calculations on every call and did not exist in earlier kernels so only store its result once. This facilitates backports too as earlier kernels can derive the timeout from uport->timeout, which has since been removed. Fixes: c4f5287 ("tty: serial: msm_geni_serial: Add serial driver support for GENI based QUP") Cc: [email protected] # 4.17 Reported-by: Douglas Anderson <[email protected]> Tested-by: Nícolas F. R. A. Prado <[email protected]> Signed-off-by: Johan Hovold <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent f1ec92a commit c80ee36

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

drivers/tty/serial/qcom_geni_serial.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ struct qcom_geni_serial_port {
124124
dma_addr_t tx_dma_addr;
125125
dma_addr_t rx_dma_addr;
126126
bool setup;
127-
unsigned int baud;
127+
unsigned long poll_timeout_us;
128128
unsigned long clk_rate;
129129
void *rx_buf;
130130
u32 loopback;
@@ -270,22 +270,13 @@ static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
270270
{
271271
u32 reg;
272272
struct qcom_geni_serial_port *port;
273-
unsigned int baud;
274-
unsigned int fifo_bits;
275273
unsigned long timeout_us = 20000;
276274
struct qcom_geni_private_data *private_data = uport->private_data;
277275

278276
if (private_data->drv) {
279277
port = to_dev_port(uport);
280-
baud = port->baud;
281-
if (!baud)
282-
baud = 115200;
283-
fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
284-
/*
285-
* Total polling iterations based on FIFO worth of bytes to be
286-
* sent at current baud. Add a little fluff to the wait.
287-
*/
288-
timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
278+
if (port->poll_timeout_us)
279+
timeout_us = port->poll_timeout_us;
289280
}
290281

291282
/*
@@ -1244,11 +1235,11 @@ static void qcom_geni_serial_set_termios(struct uart_port *uport,
12441235
unsigned long clk_rate;
12451236
u32 ver, sampling_rate;
12461237
unsigned int avg_bw_core;
1238+
unsigned long timeout;
12471239

12481240
qcom_geni_serial_stop_rx(uport);
12491241
/* baud rate */
12501242
baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1251-
port->baud = baud;
12521243

12531244
sampling_rate = UART_OVERSAMPLING;
12541245
/* Sampling rate is halved for IP versions >= 2.5 */
@@ -1326,9 +1317,21 @@ static void qcom_geni_serial_set_termios(struct uart_port *uport,
13261317
else
13271318
tx_trans_cfg |= UART_CTS_MASK;
13281319

1329-
if (baud)
1320+
if (baud) {
13301321
uart_update_timeout(uport, termios->c_cflag, baud);
13311322

1323+
/*
1324+
* Make sure that qcom_geni_serial_poll_bitfield() waits for
1325+
* the FIFO, two-word intermediate transfer register and shift
1326+
* register to clear.
1327+
*
1328+
* Note that uart_fifo_timeout() also adds a 20 ms margin.
1329+
*/
1330+
timeout = jiffies_to_usecs(uart_fifo_timeout(uport));
1331+
timeout += 3 * timeout / port->tx_fifo_depth;
1332+
WRITE_ONCE(port->poll_timeout_us, timeout);
1333+
}
1334+
13321335
if (!uart_console(uport))
13331336
writel(port->loopback,
13341337
uport->membase + SE_UART_LOOPBACK_CFG);

0 commit comments

Comments
 (0)