serial: mtk: clock rate fixes
v3 cover serial: mtk: clock rate fixes
While working on SPL support, we noticed a few bugs in the MediaTek serial driver related to clock rate handling. Here are the fixes.
---
Changes in v3:
- Use clk_is_valid().
- Drop the check before calling clk_enable().
- Add a new patch to remove an existing redundant check before a
different clk_enable().
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Moved return value change to a new patch.
- Link to v1: https://patch.msgid.link/[email protected]
---
David Lechner (2):
serial: mtk: return error value from clk_get_rate()
serial: serial_mtk: drop check before clk_bus enable
Suhrid Subramaniam (2):
serial: mtk: use ulong for clk_rate
serial: mtk: guard clk ops on clk.dev
drivers/serial/serial_mtk.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
---
base-commit: 5c215cb75c3723cbf77c36cbac3e60b001721c79
change-id: 20260727-serial-mtk-clock-fixes-266e61d2d917
Best regards,
Signature · 1 line
David Lechner <[email protected]>
PATCH v3 1/4 serial: mtk: use ulong for clk_rate
From: Suhrid Subramaniam <[email protected]>
Use ulong for return value of clk_get_rate() in the MediaTek serial driver.
IS_ERR_VALUE() does a signed 64-bit comparison against the range of possible error codes. If clk_get_rate() returns an error, assigning this to a u32 truncates the top 32 bits making the value smaller, defeating IS_ERR_VALUE() and producing a garbage divisor that hangs the UART.
--- drivers/serial/serial_mtk.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/serial/serial_mtk.c +4 -3
@@ -213,7 +213,7 @@ static int _mtk_serial_pending(struct mtk_serial_priv *priv, bool input)static int mtk_serial_setbrg (struct udevice *dev,int baudrate) {struct mtk_serial_priv *priv =dev_get_priv (dev); -u32 clk_rate; +ulong clk_rate; clk_rate =clk_get_rate (&priv->clk);if (IS_ERR_VALUE (clk_rate) || clk_rate ==0 )
@@ -266,6 +266,7 @@ static int mtk_serial_of_to_plat(struct udevice *dev) {struct mtk_serial_priv *priv =dev_get_priv (dev);fdt_addr_t addr; +ulong clk_rate;int err; addr =dev_read_addr (dev);
@@ -282,8 +283,8 @@ static int mtk_serial_of_to_plat(struct udevice *dev)return -EINVAL ; } }else { - err =clk_get_rate (&priv->clk); -if (IS_ERR_VALUE (err)) { + clk_rate =clk_get_rate (&priv->clk); +if (IS_ERR_VALUE (clk_rate)) {dev_err (dev,"invalid baud clock\n" );return -EINVAL ; }
Signature · 1 line
2.43.0
PATCH v3 2/4 serial: mtk: guard clk ops on clk.dev
In reply to David LechnerFrom: Suhrid Subramaniam <[email protected]>
Check that priv->clk has been populated before trying to use it in the MediaTek serial driver.
The clock is optional and may not be populated in all cases (in which case it is expected that there was a fixed clock rate provided.)
--- drivers/serial/serial_mtk.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/serial/serial_mtk.c +6 -2
@@ -215,9 +215,13 @@ static int mtk_serial_setbrg(struct udevice *dev, int baudrate)struct mtk_serial_priv *priv =dev_get_priv (dev);ulong clk_rate; - clk_rate =clk_get_rate (&priv->clk); -if (IS_ERR_VALUE (clk_rate) || clk_rate ==0 ) +if (clk_is_valid (&priv->clk)) { + clk_rate =clk_get_rate (&priv->clk); +if (IS_ERR_VALUE (clk_rate) || clk_rate ==0 ) +return -EINVAL ; + }else { clk_rate = priv->fixed_clk_rate; + }_mtk_serial_setbrg (priv, baudrate, clk_rate);
Signature · 1 line
2.43.0
Re: [PATCH v3 2/4] serial: mtk: guard clk ops on clk.dev
Quoted 20 lines · David Lechner
From: Suhrid Subramaniam <[email protected]>
Check that
priv->clkhas been populated before trying to use it in the MediaTek serial driver.The clock is optional and may not be populated in all cases (in which case it is expected that there was a fixed clock rate provided.)
Fixes 3b17f2e2c2a9 ("serial: mtk: add support for using dynamic baud clock souce")Signed-off-by Suhrid Subramaniam <[email protected]>Signed-off-by David Lechner <[email protected]>--- drivers/serial/serial_mtk.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/serial/serial_mtk.c +0
-0
drivers/serial/serial_mtk.c static int mtk_serial_setbrg(struct udevice *dev, int baudrate) · line 218
Quoted 6 lines
drivers/serial/serial_mtk.c +1
-2@@ -215,9 +215,13 @@ static int mtk_serial_setbrg(struct udevice *dev, int baudrate)struct mtk_serial_priv *priv =dev_get_priv (dev);ulong clk_rate; - clk_rate =clk_get_rate (&priv->clk); -if (IS_ERR_VALUE (clk_rate) || clk_rate ==0 ) +if (clk_is_valid (&priv->clk)) {
Bah, I got moving too fast and didn't test properly. This is supposed to be clk_valid().
Quoted 10 lines
drivers/serial/serial_mtk.c +5
-0+ clk_rate =clk_get_rate (&priv->clk); +if (IS_ERR_VALUE (clk_rate) || clk_rate ==0 ) +return -EINVAL ; + }else { clk_rate = priv->fixed_clk_rate; + }_mtk_serial_setbrg (priv, baudrate, clk_rate);
PATCH v3 3/4 serial: mtk: return error value from clk_get_rate()
In reply to David LechnerPass the return value from clk_get_rate() instead of changing it to -EINVAL. These return values are either ignored or logged, so it should make debugging easier in the cases where it is logged.
--- drivers/serial/serial_mtk.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/serial/serial_mtk.c +2 -2
@@ -218,7 +218,7 @@ static int mtk_serial_setbrg(struct udevice *dev, int baudrate)if (clk_is_valid (&priv->clk)) { clk_rate =clk_get_rate (&priv->clk);if (IS_ERR_VALUE (clk_rate) || clk_rate ==0 ) -return -EINVAL ; +return (int )clk_rate ?: -EINVAL ; }else { clk_rate = priv->fixed_clk_rate; }
@@ -290,7 +290,7 @@ static int mtk_serial_of_to_plat(struct udevice *dev) clk_rate =clk_get_rate (&priv->clk);if (IS_ERR_VALUE (clk_rate)) {dev_err (dev,"invalid baud clock\n" ); -return -EINVAL ; +return (int )clk_rate; } }
Signature · 1 line
2.43.0
PATCH v3 4/4 serial: serial_mtk: drop check before clk_bus enable
In reply to David LechnerDrop the redundant check that clk_bus.dev is valid before enabling it. clk_enable() already calls clk_is_valid() which does the same check.
--- drivers/serial/serial_mtk.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/serial/serial_mtk.c +1 -2
@@ -260,8 +260,7 @@ static int mtk_serial_probe(struct udevice *dev)writel (UART_FCRVAL , &priv->regs->fcr);clk_enable (&priv->clk); -if (priv->clk_bus.dev) -clk_enable (&priv->clk_bus); +clk_enable (&priv->clk_bus);return 0 ; }
Signature · 1 line
2.43.0
David Lechner
AppliedQuoted 4 lines · David Lechner
serial: mtk: clock rate fixes
While working on SPL support, we noticed a few bugs in the MediaTek serial driver related to clock rate handling. Here are the fixes.
Fixed the clk_is_valid() vs. clk_valid() issue while applying.
Applied to mediatek-for-next, thanks!
[1/4] serial: mtk: use ulong for clk_rate
commit: 50c148cfcaa30a584a827a5b54a2864c5dc8a998
[2/4] serial: mtk: guard clk_get_rate() with clk_valid()
commit: 71f33b1834f6850d00d844696495333a733c1856
[3/4] serial: mtk: return error value from clk_get_rate()
commit: be8761822e434d22ea07d3568b16f0ae68619bdb
[4/4] serial: mtk: drop check before clk_bus enable
commit: c42e44689b8c07aee616e7cc03bc92d756b8dfb5Best regards,
Signature · 1 line
David Lechner <[email protected]>