1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2018
8 #include <linux/delay.h>
9 #include <linux/hwspinlock.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
17 #include "hwspinlock_internal.h"
19 #define STM32_MUTEX_COREID BIT(8)
20 #define STM32_MUTEX_LOCK_BIT BIT(31)
21 #define STM32_MUTEX_NUM_LOCKS 32
23 struct stm32_hwspinlock {
25 struct hwspinlock_device bank;
28 static int stm32_hwspinlock_trylock(struct hwspinlock *lock)
30 void __iomem *lock_addr = lock->priv;
33 writel(STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID, lock_addr);
34 status = readl(lock_addr);
36 return status == (STM32_MUTEX_LOCK_BIT | STM32_MUTEX_COREID);
39 static void stm32_hwspinlock_unlock(struct hwspinlock *lock)
41 void __iomem *lock_addr = lock->priv;
43 writel(STM32_MUTEX_COREID, lock_addr);
46 static void stm32_hwspinlock_relax(struct hwspinlock *lock)
51 static const struct hwspinlock_ops stm32_hwspinlock_ops = {
52 .trylock = stm32_hwspinlock_trylock,
53 .unlock = stm32_hwspinlock_unlock,
54 .relax = stm32_hwspinlock_relax,
57 static void stm32_hwspinlock_disable_clk(void *data)
59 struct platform_device *pdev = data;
60 struct stm32_hwspinlock *hw = platform_get_drvdata(pdev);
61 struct device *dev = &pdev->dev;
63 pm_runtime_get_sync(dev);
64 pm_runtime_disable(dev);
65 pm_runtime_set_suspended(dev);
66 pm_runtime_put_noidle(dev);
68 clk_disable_unprepare(hw->clk);
71 static int stm32_hwspinlock_probe(struct platform_device *pdev)
73 struct device *dev = &pdev->dev;
74 struct stm32_hwspinlock *hw;
75 void __iomem *io_base;
79 io_base = devm_platform_ioremap_resource(pdev, 0);
81 return PTR_ERR(io_base);
83 array_size = STM32_MUTEX_NUM_LOCKS * sizeof(struct hwspinlock);
84 hw = devm_kzalloc(dev, sizeof(*hw) + array_size, GFP_KERNEL);
88 hw->clk = devm_clk_get(dev, "hsem");
90 return PTR_ERR(hw->clk);
92 ret = clk_prepare_enable(hw->clk);
94 dev_err(dev, "Failed to prepare_enable clock\n");
98 platform_set_drvdata(pdev, hw);
100 pm_runtime_get_noresume(dev);
101 pm_runtime_set_active(dev);
102 pm_runtime_enable(dev);
105 ret = devm_add_action_or_reset(dev, stm32_hwspinlock_disable_clk, pdev);
107 dev_err(dev, "Failed to register action\n");
111 for (i = 0; i < STM32_MUTEX_NUM_LOCKS; i++)
112 hw->bank.lock[i].priv = io_base + i * sizeof(u32);
114 ret = devm_hwspin_lock_register(dev, &hw->bank, &stm32_hwspinlock_ops,
115 0, STM32_MUTEX_NUM_LOCKS);
118 dev_err(dev, "Failed to register hwspinlock\n");
123 static int __maybe_unused stm32_hwspinlock_runtime_suspend(struct device *dev)
125 struct stm32_hwspinlock *hw = dev_get_drvdata(dev);
127 clk_disable_unprepare(hw->clk);
132 static int __maybe_unused stm32_hwspinlock_runtime_resume(struct device *dev)
134 struct stm32_hwspinlock *hw = dev_get_drvdata(dev);
136 clk_prepare_enable(hw->clk);
141 static const struct dev_pm_ops stm32_hwspinlock_pm_ops = {
142 SET_RUNTIME_PM_OPS(stm32_hwspinlock_runtime_suspend,
143 stm32_hwspinlock_runtime_resume,
147 static const struct of_device_id stm32_hwpinlock_ids[] = {
148 { .compatible = "st,stm32-hwspinlock", },
151 MODULE_DEVICE_TABLE(of, stm32_hwpinlock_ids);
153 static struct platform_driver stm32_hwspinlock_driver = {
154 .probe = stm32_hwspinlock_probe,
156 .name = "stm32_hwspinlock",
157 .of_match_table = stm32_hwpinlock_ids,
158 .pm = &stm32_hwspinlock_pm_ops,
162 static int __init stm32_hwspinlock_init(void)
164 return platform_driver_register(&stm32_hwspinlock_driver);
166 /* board init code might need to reserve hwspinlocks for predefined purposes */
167 postcore_initcall(stm32_hwspinlock_init);
169 static void __exit stm32_hwspinlock_exit(void)
171 platform_driver_unregister(&stm32_hwspinlock_driver);
173 module_exit(stm32_hwspinlock_exit);
175 MODULE_LICENSE("GPL v2");
176 MODULE_DESCRIPTION("Hardware spinlock driver for STM32 SoCs");