Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

serial: sc16is7xx: fix bug in sc16is7xx_set_baud() when using prescaler

When using a high speed clock with a low baud rate, the 4x prescaler is
automatically selected if required. In that case, sc16is7xx_set_baud()
properly configures the chip registers, but returns an incorrect baud
rate by not taking into account the prescaler value. This incorrect baud
rate is then fed to uart_update_timeout().

For example, with an input clock of 80MHz, and a selected baud rate of 50,
sc16is7xx_set_baud() will return 200 instead of 50.

Fix this by first changing the prescaler variable to hold the selected
prescaler value instead of the MCR bitfield. Then properly take into
account the selected prescaler value in the return value computation.

Also add better documentation about the divisor value computation.

Fixes: dfeae619d781 ("serial: sc16is7xx")
Cc: stable@vger.kernel.org
Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
Reviewed-by: Jiri Slaby <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20240430200431.4102923-1-hugo@hugovil.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Hugo Villeneuve and committed by
Greg Kroah-Hartman
8492bd91 614a19b8

+18 -5
+18 -5
drivers/tty/serial/sc16is7xx.c
··· 554 554 return reg == SC16IS7XX_RHR_REG; 555 555 } 556 556 557 + /* 558 + * Configure programmable baud rate generator (divisor) according to the 559 + * desired baud rate. 560 + * 561 + * From the datasheet, the divisor is computed according to: 562 + * 563 + * XTAL1 input frequency 564 + * ----------------------- 565 + * prescaler 566 + * divisor = --------------------------- 567 + * baud-rate x sampling-rate 568 + */ 557 569 static int sc16is7xx_set_baud(struct uart_port *port, int baud) 558 570 { 559 571 struct sc16is7xx_one *one = to_sc16is7xx_one(port, port); 560 572 u8 lcr; 561 - u8 prescaler = 0; 573 + unsigned int prescaler = 1; 562 574 unsigned long clk = port->uartclk, div = clk / 16 / baud; 563 575 564 576 if (div >= BIT(16)) { 565 - prescaler = SC16IS7XX_MCR_CLKSEL_BIT; 566 - div /= 4; 577 + prescaler = 4; 578 + div /= prescaler; 567 579 } 568 580 569 581 /* Enable enhanced features */ ··· 585 573 SC16IS7XX_EFR_ENABLE_BIT); 586 574 sc16is7xx_efr_unlock(port); 587 575 576 + /* If bit MCR_CLKSEL is set, the divide by 4 prescaler is activated. */ 588 577 sc16is7xx_port_update(port, SC16IS7XX_MCR_REG, 589 578 SC16IS7XX_MCR_CLKSEL_BIT, 590 - prescaler); 579 + prescaler == 1 ? 0 : SC16IS7XX_MCR_CLKSEL_BIT); 591 580 592 581 /* Backup LCR and access special register set (DLL/DLH) */ 593 582 lcr = sc16is7xx_port_read(port, SC16IS7XX_LCR_REG); ··· 604 591 /* Restore LCR and access to general register set */ 605 592 sc16is7xx_port_write(port, SC16IS7XX_LCR_REG, lcr); 606 593 607 - return DIV_ROUND_CLOSEST(clk / 16, div); 594 + return DIV_ROUND_CLOSEST((clk / prescaler) / 16, div); 608 595 } 609 596 610 597 static void sc16is7xx_handle_rx(struct uart_port *port, unsigned int rxlen,