[PATCH v17 4/8] clk: en7523: protect concurrent access to reset and PCI register
From: Christian Marangi
Date: Mon Nov 30 2026 - 13:13:24 EST
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 early PCI unprepare case to reference the
global spinlock.
Signed-off-by: Christian Marangi <ansuelsmth@xxxxxxxxx>
---
drivers/clk/clk-en7523.c | 94 ++++++++++++++++++++++++++++++----------
1 file changed, 72 insertions(+), 22 deletions(-)
diff --git a/drivers/clk/clk-en7523.c b/drivers/clk/clk-en7523.c
index ba66ac6de13b..6c522dca51e0 100644
--- a/drivers/clk/clk-en7523.c
+++ b/drivers/clk/clk-en7523.c
@@ -150,7 +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 en_clk_gate *cg);
+ void (*pci_unprepare)(struct device *dev, struct en_clk_gate *cg);
};
static const u32 gsw_base[] = { 400000000, 500000000 };
@@ -585,47 +585,82 @@ 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;
+ 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 en_clk_gate *cg)
+static void __en7523_pci_unprepare(struct device *dev, struct en_clk_gate *cg)
{
+ 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);
@@ -634,8 +669,9 @@ static void __en7523_pci_unprepare(struct en_clk_gate *cg)
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(cg);
+ __en7523_pci_unprepare(dev, cg);
}
static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
@@ -656,7 +692,7 @@ static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
cg->hw.init = &init;
if (soc_data->pci_unprepare)
- soc_data->pci_unprepare(cg);
+ soc_data->pci_unprepare(dev, cg);
if (devm_clk_hw_register(dev, &cg->hw))
return NULL;
@@ -677,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);
@@ -690,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);
}
@@ -794,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;
@@ -890,7 +941,6 @@ static int en7581_serdes_phy_set_mode(struct phy *phy, enum phy_mode mode,
struct en_serdes_phy_instance *instance = phy_get_drvdata(phy);
struct en_clk_priv *priv = dev_get_drvdata(phy->dev.parent);
u32 reg, mask, sel, val;
- unsigned long flags;
switch (instance->serdes_port) {
case AIROHA_SCU_SERDES_PCIE1:
@@ -977,12 +1027,12 @@ static int en7581_serdes_phy_set_mode(struct phy *phy, enum phy_mode mode,
return -EINVAL;
}
- spin_lock_irqsave(&priv->lock, flags);
+ guard(spinlock_irqsave)(&priv->lock);
+
val = readl(priv->base + reg);
val &= ~mask;
val |= sel;
writel(val, priv->base + reg);
- spin_unlock_irqrestore(&priv->lock, flags);
return 0;
}
--
2.55.0