Re: [PATCH v13 3/5] clk: en7523: protect concurrent access to reset and PCI register
From: Brian Masney
Date: Thu Nov 19 2026 - 19:03:19 EST
Hi Christian,
> Use spinlock for concurrent access to reset and PCI register to catch case
> when clock is enabled/disabled while a reset is also operated at the same
> time. This is needed as register are shared for both clock and reset
> operation and rmw operation require protection against writing inconsistent
> data.
>
> Some minor rework was needed for PCI unprepare case to permit calling it
> before clk_hw_register and reference the global spinlock.
This should be split out into it's own patch.
>
> Signed-off-by: Christian Marangi <ansuelsmth@xxxxxxxxx>
> ---
> drivers/clk/clk-en7523.c | 74 +++++++++++++++++++++++++++++++++++++---
> 1 file changed, 69 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/clk/clk-en7523.c b/drivers/clk/clk-en7523.c
> index d1d8935f0a1a..886858345cf0 100644
> --- a/drivers/clk/clk-en7523.c
> +++ b/drivers/clk/clk-en7523.c
> @@ -150,6 +150,7 @@ struct en_clk_soc_data {
> const struct clk_ops pcie_ops;
> int (*hw_init)(struct platform_device *pdev,
> struct clk_hw_onecell_data *clk_data);
> + void (*pci_unprepare)(struct device *dev, struct en_clk_gate *cg);
> };
>
> static const u32 gsw_base[] = { 400000000, 500000000 };
> @@ -584,51 +585,95 @@ static int en7523_pci_is_enabled(struct clk_hw *hw)
> static int en7523_pci_prepare(struct clk_hw *hw)
> {
> struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
> + struct device *dev = clk_hw_get_dev(hw);
> void __iomem *np_base = cg->base;
> + struct en_clk_priv *priv;
> + unsigned long flags;
> u32 val, mask;
>
> + priv = dev_get_drvdata(dev);
> +
> /* Need to pull device low before reset */
> + spin_lock_irqsave(&priv->lock, flags);
You can use:
scoped_guard(spinlock_irqsave, &priv->lock) {
...
}
> val = readl(np_base + REG_PCI_CONTROL);
> val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
> writel(val, np_base + REG_PCI_CONTROL);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> usleep_range(1000, 2000);
>
> /* Enable PCIe port 1 */
> + spin_lock_irqsave(&priv->lock, flags);
> + val = readl(np_base + REG_PCI_CONTROL);
> val |= REG_PCI_CONTROL_REFCLK_EN1;
> writel(val, np_base + REG_PCI_CONTROL);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> usleep_range(1000, 2000);
>
> /* Reset to default */
> + spin_lock_irqsave(&priv->lock, flags);
> val = readl(np_base + REG_RESET_CONTROL1);
> mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
> REG_RESET_CONTROL_PCIEHB;
> writel(val & ~mask, np_base + REG_RESET_CONTROL1);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> usleep_range(1000, 2000);
> +
> + spin_lock_irqsave(&priv->lock, flags);
> + val = readl(np_base + REG_RESET_CONTROL1);
> writel(val | mask, np_base + REG_RESET_CONTROL1);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> msleep(100);
> +
> + spin_lock_irqsave(&priv->lock, flags);
> + val = readl(np_base + REG_RESET_CONTROL1);
> writel(val & ~mask, np_base + REG_RESET_CONTROL1);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> usleep_range(5000, 10000);
>
> /* Release device */
> - mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
> + spin_lock_irqsave(&priv->lock, flags);
> val = readl(np_base + REG_PCI_CONTROL);
> + mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
> writel(val & ~mask, np_base + REG_PCI_CONTROL);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> usleep_range(1000, 2000);
> +
> + spin_lock_irqsave(&priv->lock, flags);
> + val = readl(np_base + REG_PCI_CONTROL);
> writel(val | mask, np_base + REG_PCI_CONTROL);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> msleep(250);
>
> return 0;
> }
>
> -static void en7523_pci_unprepare(struct clk_hw *hw)
> +static void __en7523_pci_unprepare(struct device *dev, struct en_clk_gate *cg)
> {
> - struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
> + struct en_clk_priv *priv = dev_get_drvdata(dev);
> void __iomem *np_base = cg->base;
> + unsigned long flags;
> u32 val;
>
> + spin_lock_irqsave(&priv->lock, flags);
> val = readl(np_base + REG_PCI_CONTROL);
> val &= ~REG_PCI_CONTROL_REFCLK_EN1;
> writel(val, np_base + REG_PCI_CONTROL);
> + spin_unlock_irqrestore(&priv->lock, flags);
> +}
> +
> +static void en7523_pci_unprepare(struct clk_hw *hw)
> +{
> + struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
> + struct device *dev = clk_hw_get_dev(hw);
> +
> + __en7523_pci_unprepare(dev, cg);
> }
>
> static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
> @@ -648,8 +693,8 @@ static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
> cg->base = np_base;
> cg->hw.init = &init;
>
> - if (init.ops->unprepare)
> - init.ops->unprepare(&cg->hw);
> + if (soc_data->pci_unprepare)
> + soc_data->pci_unprepare(dev, cg);
>
> if (devm_clk_hw_register(dev, &cg->hw))
> return NULL;
> @@ -663,6 +708,7 @@ static int en7581_pci_is_enabled(struct clk_hw *hw)
> u32 val, mask;
>
> mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
> +
Extra newline can be dropped.
Brian
--
Brian Masney <masneyb@xxxxxxxxxx>