1 // SPDX-License-Identifier: GPL-2.0
3 * Spreadtrum hardware spinlock driver
4 * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/hwspinlock.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
18 #include "hwspinlock_internal.h"
20 /* hwspinlock registers definition */
21 #define HWSPINLOCK_RECCTRL 0x4
22 #define HWSPINLOCK_MASTERID(_X_) (0x80 + 0x4 * (_X_))
23 #define HWSPINLOCK_TOKEN(_X_) (0x800 + 0x4 * (_X_))
26 #define HWSPINLOCK_NOTTAKEN 0x55aa10c5
27 /* bits definition of RECCTRL reg */
28 #define HWSPINLOCK_USER_BITS 0x1
30 /* hwspinlock number */
31 #define SPRD_HWLOCKS_NUM 32
33 struct sprd_hwspinlock_dev
{
36 struct hwspinlock_device bank
;
39 /* try to lock the hardware spinlock */
40 static int sprd_hwspinlock_trylock(struct hwspinlock
*lock
)
42 struct sprd_hwspinlock_dev
*sprd_hwlock
=
43 dev_get_drvdata(lock
->bank
->dev
);
44 void __iomem
*addr
= lock
->priv
;
50 lock_id
= hwlock_to_id(lock
);
51 /* get the hardware spinlock master/user id */
52 user_id
= readl(sprd_hwlock
->base
+ HWSPINLOCK_MASTERID(lock_id
));
53 dev_warn(sprd_hwlock
->bank
.dev
,
54 "hwspinlock [%d] lock failed and master/user id = %d!\n",
59 /* unlock the hardware spinlock */
60 static void sprd_hwspinlock_unlock(struct hwspinlock
*lock
)
62 void __iomem
*lock_addr
= lock
->priv
;
64 writel(HWSPINLOCK_NOTTAKEN
, lock_addr
);
67 /* The specs recommended below number as the retry delay time */
68 static void sprd_hwspinlock_relax(struct hwspinlock
*lock
)
73 static const struct hwspinlock_ops sprd_hwspinlock_ops
= {
74 .trylock
= sprd_hwspinlock_trylock
,
75 .unlock
= sprd_hwspinlock_unlock
,
76 .relax
= sprd_hwspinlock_relax
,
79 static void sprd_hwspinlock_disable(void *data
)
81 struct sprd_hwspinlock_dev
*sprd_hwlock
= data
;
83 clk_disable_unprepare(sprd_hwlock
->clk
);
86 static int sprd_hwspinlock_probe(struct platform_device
*pdev
)
88 struct sprd_hwspinlock_dev
*sprd_hwlock
;
89 struct hwspinlock
*lock
;
92 if (!pdev
->dev
.of_node
)
95 sprd_hwlock
= devm_kzalloc(&pdev
->dev
,
96 struct_size(sprd_hwlock
, bank
.lock
, SPRD_HWLOCKS_NUM
),
101 sprd_hwlock
->base
= devm_platform_ioremap_resource(pdev
, 0);
102 if (IS_ERR(sprd_hwlock
->base
))
103 return PTR_ERR(sprd_hwlock
->base
);
105 sprd_hwlock
->clk
= devm_clk_get(&pdev
->dev
, "enable");
106 if (IS_ERR(sprd_hwlock
->clk
)) {
107 dev_err(&pdev
->dev
, "get hwspinlock clock failed!\n");
108 return PTR_ERR(sprd_hwlock
->clk
);
111 ret
= clk_prepare_enable(sprd_hwlock
->clk
);
115 ret
= devm_add_action_or_reset(&pdev
->dev
, sprd_hwspinlock_disable
,
119 "Failed to add hwspinlock disable action\n");
123 /* set the hwspinlock to record user id to identify subsystems */
124 writel(HWSPINLOCK_USER_BITS
, sprd_hwlock
->base
+ HWSPINLOCK_RECCTRL
);
126 for (i
= 0; i
< SPRD_HWLOCKS_NUM
; i
++) {
127 lock
= &sprd_hwlock
->bank
.lock
[i
];
128 lock
->priv
= sprd_hwlock
->base
+ HWSPINLOCK_TOKEN(i
);
131 platform_set_drvdata(pdev
, sprd_hwlock
);
133 return devm_hwspin_lock_register(&pdev
->dev
, &sprd_hwlock
->bank
,
134 &sprd_hwspinlock_ops
, 0,
138 static const struct of_device_id sprd_hwspinlock_of_match
[] = {
139 { .compatible
= "sprd,hwspinlock-r3p0", },
142 MODULE_DEVICE_TABLE(of
, sprd_hwspinlock_of_match
);
144 static struct platform_driver sprd_hwspinlock_driver
= {
145 .probe
= sprd_hwspinlock_probe
,
147 .name
= "sprd_hwspinlock",
148 .of_match_table
= sprd_hwspinlock_of_match
,
151 module_platform_driver(sprd_hwspinlock_driver
);
153 MODULE_LICENSE("GPL v2");
154 MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
155 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
156 MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
157 MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");