1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
9 #include <hwspinlock.h>
12 #include <linux/bitops.h>
14 #define STM32_MUTEX_COREID BIT(8)
15 #define STM32_MUTEX_LOCK_BIT BIT(31)
16 #define STM32_MUTEX_NUM_LOCKS 32
18 struct stm32mp1_hws_priv {
22 static int stm32mp1_lock(struct udevice *dev, int index)
24 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
27 if (index >= STM32_MUTEX_NUM_LOCKS)
30 status = readl(priv->base + index * sizeof(u32));
31 if (status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
34 writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID,
35 priv->base + index * sizeof(u32));
37 status = readl(priv->base + index * sizeof(u32));
38 if (status != (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
44 static int stm32mp1_unlock(struct udevice *dev, int index)
46 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
48 if (index >= STM32_MUTEX_NUM_LOCKS)
51 writel(STM32_MUTEX_COREID, priv->base + index * sizeof(u32));
56 static int stm32mp1_hwspinlock_probe(struct udevice *dev)
58 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
62 priv->base = dev_read_addr(dev);
63 if (priv->base == FDT_ADDR_T_NONE)
66 ret = clk_get_by_index(dev, 0, &clk);
70 ret = clk_enable(&clk);
77 static const struct hwspinlock_ops stm32mp1_hwspinlock_ops = {
78 .lock = stm32mp1_lock,
79 .unlock = stm32mp1_unlock,
82 static const struct udevice_id stm32mp1_hwspinlock_ids[] = {
83 { .compatible = "st,stm32-hwspinlock" },
87 U_BOOT_DRIVER(hwspinlock_stm32mp1) = {
88 .name = "hwspinlock_stm32mp1",
89 .id = UCLASS_HWSPINLOCK,
90 .of_match = stm32mp1_hwspinlock_ids,
91 .ops = &stm32mp1_hwspinlock_ops,
92 .probe = stm32mp1_hwspinlock_probe,
93 .priv_auto_alloc_size = sizeof(struct stm32mp1_hws_priv),