[PATCH] can: flexcan: enable PER clock before obtaining its rate

From: Ciprian Costea
Date: Mon Apr 14 2025 - 03:41:30 EST


From: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>

The FlexCan driver assumes that the frequency of the 'per' clock can be
obtained even on disabled clocks, which is not always true.

According to 'clk_get_rate' documentation, it is only valid once the clock
source has been enabled.

Co-developed-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>
Signed-off-by: Ghennadi Procopciuc <ghennadi.procopciuc@xxxxxxx>
Signed-off-by: Ciprian Marian Costea <ciprianmarian.costea@xxxxxxxxxxx>
---
drivers/net/can/flexcan/flexcan-core.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/net/can/flexcan/flexcan-core.c b/drivers/net/can/flexcan/flexcan-core.c
index 6d80c341b26f..b142aa60620e 100644
--- a/drivers/net/can/flexcan/flexcan-core.c
+++ b/drivers/net/can/flexcan/flexcan-core.c
@@ -2056,6 +2056,26 @@ static int flexcan_setup_stop_mode(struct platform_device *pdev)
return 0;
}

+static unsigned long get_per_clk_rate(struct clk *clk)
+{
+ unsigned long rate;
+ int err;
+
+ rate = clk_get_rate(clk);
+ if (rate)
+ return rate;
+
+ /* Just in case this clock is disabled by default */
+ err = clk_prepare_enable(clk);
+ if (err)
+ return 0;
+
+ rate = clk_get_rate(clk);
+ clk_disable_unprepare(clk);
+
+ return rate;
+}
+
static const struct of_device_id flexcan_of_match[] = {
{ .compatible = "fsl,imx8qm-flexcan", .data = &fsl_imx8qm_devtype_data, },
{ .compatible = "fsl,imx8mp-flexcan", .data = &fsl_imx8mp_devtype_data, },
@@ -2137,7 +2157,7 @@ static int flexcan_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "no per clock defined\n");
return PTR_ERR(clk_per);
}
- clock_freq = clk_get_rate(clk_per);
+ clock_freq = get_per_clk_rate(clk_per);
}

irq = platform_get_irq(pdev, 0);
--
2.45.2