1 // SPDX-License-Identifier: GPL-2.0
3 * Spreadtrum hardware spinlock driver
4 * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
7 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/hwspinlock.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
21 #include "hwspinlock_internal.h"
23 /* hwspinlock registers definition */
24 #define HWSPINLOCK_RECCTRL 0x4
25 #define HWSPINLOCK_MASTERID(_X_) (0x80 + 0x4 * (_X_))
26 #define HWSPINLOCK_TOKEN(_X_) (0x800 + 0x4 * (_X_))
29 #define HWSPINLOCK_NOTTAKEN 0x55aa10c5
30 /* bits definition of RECCTRL reg */
31 #define HWSPINLOCK_USER_BITS 0x1
33 /* hwspinlock number */
34 #define SPRD_HWLOCKS_NUM 32
36 struct sprd_hwspinlock_dev
{
39 struct hwspinlock_device bank
;
42 /* try to lock the hardware spinlock */
43 static int sprd_hwspinlock_trylock(struct hwspinlock
*lock
)
45 struct sprd_hwspinlock_dev
*sprd_hwlock
=
46 dev_get_drvdata(lock
->bank
->dev
);
47 void __iomem
*addr
= lock
->priv
;
53 lock_id
= hwlock_to_id(lock
);
54 /* get the hardware spinlock master/user id */
55 user_id
= readl(sprd_hwlock
->base
+ HWSPINLOCK_MASTERID(lock_id
));
56 dev_warn(sprd_hwlock
->bank
.dev
,
57 "hwspinlock [%d] lock failed and master/user id = %d!\n",
62 /* unlock the hardware spinlock */
63 static void sprd_hwspinlock_unlock(struct hwspinlock
*lock
)
65 void __iomem
*lock_addr
= lock
->priv
;
67 writel(HWSPINLOCK_NOTTAKEN
, lock_addr
);
70 /* The specs recommended below number as the retry delay time */
71 static void sprd_hwspinlock_relax(struct hwspinlock
*lock
)
76 static const struct hwspinlock_ops sprd_hwspinlock_ops
= {
77 .trylock
= sprd_hwspinlock_trylock
,
78 .unlock
= sprd_hwspinlock_unlock
,
79 .relax
= sprd_hwspinlock_relax
,
82 static int sprd_hwspinlock_probe(struct platform_device
*pdev
)
84 struct sprd_hwspinlock_dev
*sprd_hwlock
;
85 struct hwspinlock
*lock
;
89 if (!pdev
->dev
.of_node
)
92 sprd_hwlock
= devm_kzalloc(&pdev
->dev
,
93 sizeof(struct sprd_hwspinlock_dev
) +
94 SPRD_HWLOCKS_NUM
* sizeof(*lock
),
99 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
100 sprd_hwlock
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
101 if (IS_ERR(sprd_hwlock
->base
))
102 return PTR_ERR(sprd_hwlock
->base
);
104 sprd_hwlock
->clk
= devm_clk_get(&pdev
->dev
, "enable");
105 if (IS_ERR(sprd_hwlock
->clk
)) {
106 dev_err(&pdev
->dev
, "get hwspinlock clock failed!\n");
107 return PTR_ERR(sprd_hwlock
->clk
);
110 clk_prepare_enable(sprd_hwlock
->clk
);
112 /* set the hwspinlock to record user id to identify subsystems */
113 writel(HWSPINLOCK_USER_BITS
, sprd_hwlock
->base
+ HWSPINLOCK_RECCTRL
);
115 for (i
= 0; i
< SPRD_HWLOCKS_NUM
; i
++) {
116 lock
= &sprd_hwlock
->bank
.lock
[i
];
117 lock
->priv
= sprd_hwlock
->base
+ HWSPINLOCK_TOKEN(i
);
120 platform_set_drvdata(pdev
, sprd_hwlock
);
121 pm_runtime_enable(&pdev
->dev
);
123 ret
= hwspin_lock_register(&sprd_hwlock
->bank
, &pdev
->dev
,
124 &sprd_hwspinlock_ops
, 0, SPRD_HWLOCKS_NUM
);
126 pm_runtime_disable(&pdev
->dev
);
127 clk_disable_unprepare(sprd_hwlock
->clk
);
134 static int sprd_hwspinlock_remove(struct platform_device
*pdev
)
136 struct sprd_hwspinlock_dev
*sprd_hwlock
= platform_get_drvdata(pdev
);
138 hwspin_lock_unregister(&sprd_hwlock
->bank
);
139 pm_runtime_disable(&pdev
->dev
);
140 clk_disable_unprepare(sprd_hwlock
->clk
);
144 static const struct of_device_id sprd_hwspinlock_of_match
[] = {
145 { .compatible
= "sprd,hwspinlock-r3p0", },
148 MODULE_DEVICE_TABLE(of
, sprd_hwspinlock_of_match
);
150 static struct platform_driver sprd_hwspinlock_driver
= {
151 .probe
= sprd_hwspinlock_probe
,
152 .remove
= sprd_hwspinlock_remove
,
154 .name
= "sprd_hwspinlock",
155 .of_match_table
= of_match_ptr(sprd_hwspinlock_of_match
),
159 static int __init
sprd_hwspinlock_init(void)
161 return platform_driver_register(&sprd_hwspinlock_driver
);
163 postcore_initcall(sprd_hwspinlock_init
);
165 static void __exit
sprd_hwspinlock_exit(void)
167 platform_driver_unregister(&sprd_hwspinlock_driver
);
169 module_exit(sprd_hwspinlock_exit
);
171 MODULE_LICENSE("GPL v2");
172 MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
173 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
174 MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
175 MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");