Re: [PATCH v15 3/5] clk: en7523: protect concurrent access to reset and PCI register
From: Brian Masney
Date: Sun Nov 29 2026 - 18:50:34 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.
>
> Signed-off-by: Christian Marangi <ansuelsmth@xxxxxxxxx>
> ---
> drivers/clk/clk-en7523.c | 104 ++++++++++++++++++++++++++++++---------
> 1 file changed, 82 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/clk/clk-en7523.c b/drivers/clk/clk-en7523.c
> index d1d8935f0a1a..280918ec25e3 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,53 +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;
> u32 val, mask;
>
> + priv = dev_get_drvdata(dev);
> +
> /* Need to pull device low before reset */
> - val = readl(np_base + REG_PCI_CONTROL);
> - val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
> - writel(val, np_base + REG_PCI_CONTROL);
> + 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);
> + }
> +
> usleep_range(1000, 2000);
>
> /* Enable PCIe port 1 */
> - val |= REG_PCI_CONTROL_REFCLK_EN1;
> - writel(val, np_base + REG_PCI_CONTROL);
> + scoped_guard(spinlock_irqsave, &priv->lock) {
> + val = readl(np_base + REG_PCI_CONTROL);
> + val |= REG_PCI_CONTROL_REFCLK_EN1;
> + writel(val, np_base + REG_PCI_CONTROL);
> + }
> +
> usleep_range(1000, 2000);
>
> /* Reset to default */
> - 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);
> + scoped_guard(spinlock_irqsave, &priv->lock) {
> + val = readl(np_base + REG_RESET_CONTROL1);
> + mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
> + REG_RESET_CONTROL_PCIEHB;
The continuation line lost it's alignment and should be aligned with
REG_RESET_CONTROL_PCIE1.
> + writel(val & ~mask, np_base + REG_RESET_CONTROL1);
> + }
> +
> usleep_range(1000, 2000);
> - writel(val | mask, np_base + REG_RESET_CONTROL1);
> +
> + scoped_guard(spinlock_irqsave, &priv->lock) {
> + val = readl(np_base + REG_RESET_CONTROL1);
> + writel(val | mask, np_base + REG_RESET_CONTROL1);
> + }
> +
> msleep(100);
> - writel(val & ~mask, np_base + REG_RESET_CONTROL1);
> +
> + scoped_guard(spinlock_irqsave, &priv->lock) {
> + val = readl(np_base + REG_RESET_CONTROL1);
> + writel(val & ~mask, np_base + REG_RESET_CONTROL1);
> + }
> +
> usleep_range(5000, 10000);
>
> /* Release device */
> - mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
> - val = readl(np_base + REG_PCI_CONTROL);
> - writel(val & ~mask, np_base + REG_PCI_CONTROL);
> + scoped_guard(spinlock_irqsave, &priv->lock) {
> + val = readl(np_base + REG_PCI_CONTROL);
> + mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
> + writel(val & ~mask, np_base + REG_PCI_CONTROL);
> + }
> +
> usleep_range(1000, 2000);
> - writel(val | mask, np_base + REG_PCI_CONTROL);
> +
> + scoped_guard(spinlock_irqsave, &priv->lock) {
> + val = readl(np_base + REG_PCI_CONTROL);
> + writel(val | mask, np_base + REG_PCI_CONTROL);
> + }
> +
> 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;
> u32 val;
>
> + guard(spinlock_irqsave)(&priv->lock);
> +
> val = readl(np_base + REG_PCI_CONTROL);
> val &= ~REG_PCI_CONTROL_REFCLK_EN1;
> writel(val, np_base + REG_PCI_CONTROL);
> }
>
> +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,
> void __iomem *np_base)
> {
> @@ -648,8 +691,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);
This should be a separate commit from the scoped guard changes.
>
> if (devm_clk_hw_register(dev, &cg->hw))
> return NULL;
> @@ -670,10 +713,16 @@ static int en7581_pci_is_enabled(struct clk_hw *hw)
> static int en7581_pci_enable(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;
> u32 val, mask;
>
> + priv = dev_get_drvdata(dev);
> mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
> +
> + guard(spinlock_irqsave)(&priv->lock);
> +
> val = readl(np_base + REG_PCI_CONTROL);
> writel(val | mask, np_base + REG_PCI_CONTROL);
>
> @@ -683,10 +732,16 @@ static int en7581_pci_enable(struct clk_hw *hw)
> static void en7581_pci_disable(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;
> u32 val, mask;
>
> + priv = dev_get_drvdata(dev);
> mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
> +
> + guard(spinlock_irqsave)(&priv->lock);
> +
> val = readl(np_base + REG_PCI_CONTROL);
> writel(val & ~mask, np_base + REG_PCI_CONTROL);
> }
> @@ -787,11 +842,14 @@ static int en7523_reset_update(struct reset_controller_dev *rcdev,
> unsigned long id, bool assert)
> {
> struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
> + struct en_clk_priv *priv = dev_get_drvdata(rcdev->dev);
> u32 offset = rst_data->bank_ofs[id / RST_NR_PER_BANK];
> void __iomem *addr = rst_data->base + offset;
> bool inverted = false;
> u32 val;
>
> + guard(spinlock_irqsave)(&priv->lock);
> +
> /* For PCIC reset logic is inverted, 0:assert 1:deassert */
> if (offset == REG_NP_SCU_PCIC)
> inverted = true;
> @@ -832,6 +890,7 @@ static int en7523_reset_status(struct reset_controller_dev *rcdev,
> inverted = true;
>
> val = readl(addr) & BIT(id % RST_NR_PER_BANK);
> +
Drop the whitespace change since nothing else in this function is
touched.
Brian
--
Brian Masney <masneyb@xxxxxxxxxx>