1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Spreadtrum hardware spinlock driver
4  * Copyright (C) 2017 Spreadtrum  - http://www.spreadtrum.com
5  */
6 
7 #include <linux/bitops.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/hwspinlock.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 
20 #include "hwspinlock_internal.h"
21 
22 /* hwspinlock registers definition */
23 #define HWSPINLOCK_RECCTRL		0x4
24 #define HWSPINLOCK_MASTERID(_X_)	(0x80 + 0x4 * (_X_))
25 #define HWSPINLOCK_TOKEN(_X_)		(0x800 + 0x4 * (_X_))
26 
27 /* unlocked value */
28 #define HWSPINLOCK_NOTTAKEN		0x55aa10c5
29 /* bits definition of RECCTRL reg */
30 #define HWSPINLOCK_USER_BITS		0x1
31 
32 /* hwspinlock number */
33 #define SPRD_HWLOCKS_NUM		32
34 
35 struct sprd_hwspinlock_dev {
36 	void __iomem *base;
37 	struct clk *clk;
38 	struct hwspinlock_device bank;
39 };
40 
41 /* try to lock the hardware spinlock */
42 static int sprd_hwspinlock_trylock(struct hwspinlock *lock)
43 {
44 	struct sprd_hwspinlock_dev *sprd_hwlock =
45 		dev_get_drvdata(lock->bank->dev);
46 	void __iomem *addr = lock->priv;
47 	int user_id, lock_id;
48 
49 	if (!readl(addr))
50 		return 1;
51 
52 	lock_id = hwlock_to_id(lock);
53 	/* get the hardware spinlock master/user id */
54 	user_id = readl(sprd_hwlock->base + HWSPINLOCK_MASTERID(lock_id));
55 	dev_warn(sprd_hwlock->bank.dev,
56 		 "hwspinlock [%d] lock failed and master/user id = %d!\n",
57 		 lock_id, user_id);
58 	return 0;
59 }
60 
61 /* unlock the hardware spinlock */
62 static void sprd_hwspinlock_unlock(struct hwspinlock *lock)
63 {
64 	void __iomem *lock_addr = lock->priv;
65 
66 	writel(HWSPINLOCK_NOTTAKEN, lock_addr);
67 }
68 
69 /* The specs recommended below number as the retry delay time */
70 static void sprd_hwspinlock_relax(struct hwspinlock *lock)
71 {
72 	ndelay(10);
73 }
74 
75 static const struct hwspinlock_ops sprd_hwspinlock_ops = {
76 	.trylock = sprd_hwspinlock_trylock,
77 	.unlock = sprd_hwspinlock_unlock,
78 	.relax = sprd_hwspinlock_relax,
79 };
80 
81 static void sprd_hwspinlock_disable(void *data)
82 {
83 	struct sprd_hwspinlock_dev *sprd_hwlock = data;
84 
85 	clk_disable_unprepare(sprd_hwlock->clk);
86 }
87 
88 static int sprd_hwspinlock_probe(struct platform_device *pdev)
89 {
90 	struct sprd_hwspinlock_dev *sprd_hwlock;
91 	struct hwspinlock *lock;
92 	int i, ret;
93 
94 	if (!pdev->dev.of_node)
95 		return -ENODEV;
96 
97 	sprd_hwlock = devm_kzalloc(&pdev->dev,
98 				   sizeof(struct sprd_hwspinlock_dev) +
99 				   SPRD_HWLOCKS_NUM * sizeof(*lock),
100 				   GFP_KERNEL);
101 	if (!sprd_hwlock)
102 		return -ENOMEM;
103 
104 	sprd_hwlock->base = devm_platform_ioremap_resource(pdev, 0);
105 	if (IS_ERR(sprd_hwlock->base))
106 		return PTR_ERR(sprd_hwlock->base);
107 
108 	sprd_hwlock->clk = devm_clk_get(&pdev->dev, "enable");
109 	if (IS_ERR(sprd_hwlock->clk)) {
110 		dev_err(&pdev->dev, "get hwspinlock clock failed!\n");
111 		return PTR_ERR(sprd_hwlock->clk);
112 	}
113 
114 	ret = clk_prepare_enable(sprd_hwlock->clk);
115 	if (ret)
116 		return ret;
117 
118 	ret = devm_add_action_or_reset(&pdev->dev, sprd_hwspinlock_disable,
119 				       sprd_hwlock);
120 	if (ret) {
121 		dev_err(&pdev->dev,
122 			"Failed to add hwspinlock disable action\n");
123 		return ret;
124 	}
125 
126 	/* set the hwspinlock to record user id to identify subsystems */
127 	writel(HWSPINLOCK_USER_BITS, sprd_hwlock->base + HWSPINLOCK_RECCTRL);
128 
129 	for (i = 0; i < SPRD_HWLOCKS_NUM; i++) {
130 		lock = &sprd_hwlock->bank.lock[i];
131 		lock->priv = sprd_hwlock->base + HWSPINLOCK_TOKEN(i);
132 	}
133 
134 	platform_set_drvdata(pdev, sprd_hwlock);
135 
136 	return devm_hwspin_lock_register(&pdev->dev, &sprd_hwlock->bank,
137 					 &sprd_hwspinlock_ops, 0,
138 					 SPRD_HWLOCKS_NUM);
139 }
140 
141 static const struct of_device_id sprd_hwspinlock_of_match[] = {
142 	{ .compatible = "sprd,hwspinlock-r3p0", },
143 	{ /* sentinel */ }
144 };
145 MODULE_DEVICE_TABLE(of, sprd_hwspinlock_of_match);
146 
147 static struct platform_driver sprd_hwspinlock_driver = {
148 	.probe = sprd_hwspinlock_probe,
149 	.driver = {
150 		.name = "sprd_hwspinlock",
151 		.of_match_table = of_match_ptr(sprd_hwspinlock_of_match),
152 	},
153 };
154 
155 static int __init sprd_hwspinlock_init(void)
156 {
157 	return platform_driver_register(&sprd_hwspinlock_driver);
158 }
159 postcore_initcall(sprd_hwspinlock_init);
160 
161 static void __exit sprd_hwspinlock_exit(void)
162 {
163 	platform_driver_unregister(&sprd_hwspinlock_driver);
164 }
165 module_exit(sprd_hwspinlock_exit);
166 
167 MODULE_LICENSE("GPL v2");
168 MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
169 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
170 MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
171 MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");
172