1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
6 #define LOG_CATEGORY UCLASS_HWSPINLOCK
11 #include <hwspinlock.h>
14 #include <linux/bitops.h>
16 #define STM32_MUTEX_COREID BIT(8)
17 #define STM32_MUTEX_LOCK_BIT BIT(31)
18 #define STM32_MUTEX_NUM_LOCKS 32
20 struct stm32mp1_hws_priv {
24 static int stm32mp1_lock(struct udevice *dev, int index)
26 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
29 if (index >= STM32_MUTEX_NUM_LOCKS)
32 status = readl(priv->base + index * sizeof(u32));
33 if (status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
36 writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID,
37 priv->base + index * sizeof(u32));
39 status = readl(priv->base + index * sizeof(u32));
40 if (status != (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
46 static int stm32mp1_unlock(struct udevice *dev, int index)
48 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
50 if (index >= STM32_MUTEX_NUM_LOCKS)
53 writel(STM32_MUTEX_COREID, priv->base + index * sizeof(u32));
58 static int stm32mp1_hwspinlock_probe(struct udevice *dev)
60 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
64 priv->base = dev_read_addr(dev);
65 if (priv->base == FDT_ADDR_T_NONE)
68 ret = clk_get_by_index(dev, 0, &clk);
72 ret = clk_enable(&clk);
79 static const struct hwspinlock_ops stm32mp1_hwspinlock_ops = {
80 .lock = stm32mp1_lock,
81 .unlock = stm32mp1_unlock,
84 static const struct udevice_id stm32mp1_hwspinlock_ids[] = {
85 { .compatible = "st,stm32-hwspinlock" },
89 U_BOOT_DRIVER(hwspinlock_stm32mp1) = {
90 .name = "hwspinlock_stm32mp1",
91 .id = UCLASS_HWSPINLOCK,
92 .of_match = stm32mp1_hwspinlock_ids,
93 .ops = &stm32mp1_hwspinlock_ops,
94 .probe = stm32mp1_hwspinlock_probe,
95 .priv_auto = sizeof(struct stm32mp1_hws_priv),