1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
9 #include <hwspinlock.h>
12 #define STM32_MUTEX_COREID BIT(8)
13 #define STM32_MUTEX_LOCK_BIT BIT(31)
14 #define STM32_MUTEX_NUM_LOCKS 32
16 struct stm32mp1_hws_priv {
20 static int stm32mp1_lock(struct udevice *dev, int index)
22 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
25 if (index >= STM32_MUTEX_NUM_LOCKS)
28 status = readl(priv->base + index * sizeof(u32));
29 if (status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
32 writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID,
33 priv->base + index * sizeof(u32));
35 status = readl(priv->base + index * sizeof(u32));
36 if (status != (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID))
42 static int stm32mp1_unlock(struct udevice *dev, int index)
44 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
46 if (index >= STM32_MUTEX_NUM_LOCKS)
49 writel(STM32_MUTEX_COREID, priv->base + index * sizeof(u32));
54 static int stm32mp1_hwspinlock_probe(struct udevice *dev)
56 struct stm32mp1_hws_priv *priv = dev_get_priv(dev);
60 priv->base = dev_read_addr(dev);
61 if (priv->base == FDT_ADDR_T_NONE)
64 ret = clk_get_by_index(dev, 0, &clk);
68 ret = clk_enable(&clk);
75 static const struct hwspinlock_ops stm32mp1_hwspinlock_ops = {
76 .lock = stm32mp1_lock,
77 .unlock = stm32mp1_unlock,
80 static const struct udevice_id stm32mp1_hwspinlock_ids[] = {
81 { .compatible = "st,stm32-hwspinlock" },
85 U_BOOT_DRIVER(hwspinlock_stm32mp1) = {
86 .name = "hwspinlock_stm32mp1",
87 .id = UCLASS_HWSPINLOCK,
88 .of_match = stm32mp1_hwspinlock_ids,
89 .ops = &stm32mp1_hwspinlock_ops,
90 .probe = stm32mp1_hwspinlock_probe,
91 .priv_auto_alloc_size = sizeof(struct stm32mp1_hws_priv),