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