blob: 36dc8038bbb4e791ce828e0396207c22ede086ea [file] [log] [blame]
Suman Anna0c3e8902018-05-11 12:03:21 -05001// SPDX-License-Identifier: GPL-2.0
Baolin Wangd8c8bbb2017-05-17 13:59:29 +08002/*
3 * Spreadtrum hardware spinlock driver
4 * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
Baolin Wangd8c8bbb2017-05-17 13:59:29 +08005 */
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>
Baolin Wangd8c8bbb2017-05-17 13:59:29 +080018#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
35struct 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 */
42static 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 */
62static 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 */
70static void sprd_hwspinlock_relax(struct hwspinlock *lock)
71{
72 ndelay(10);
73}
74
75static const struct hwspinlock_ops sprd_hwspinlock_ops = {
76 .trylock = sprd_hwspinlock_trylock,
77 .unlock = sprd_hwspinlock_unlock,
78 .relax = sprd_hwspinlock_relax,
79};
80
Baolin Wangb4d64192019-09-27 15:10:46 +080081static 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
Baolin Wangd8c8bbb2017-05-17 13:59:29 +080088static int sprd_hwspinlock_probe(struct platform_device *pdev)
89{
90 struct sprd_hwspinlock_dev *sprd_hwlock;
91 struct hwspinlock *lock;
Baolin Wangd8c8bbb2017-05-17 13:59:29 +080092 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
Baolin Wang74cfa952019-09-27 15:10:44 +0800104 sprd_hwlock->base = devm_platform_ioremap_resource(pdev, 0);
Baolin Wangd8c8bbb2017-05-17 13:59:29 +0800105 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
Baolin Wang3070c412019-09-27 15:10:45 +0800114 ret = clk_prepare_enable(sprd_hwlock->clk);
115 if (ret)
116 return ret;
Baolin Wangd8c8bbb2017-05-17 13:59:29 +0800117
Baolin Wangb4d64192019-09-27 15:10:46 +0800118 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
Baolin Wangd8c8bbb2017-05-17 13:59:29 +0800126 /* 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);
Baolin Wangd8c8bbb2017-05-17 13:59:29 +0800135
Baolin Wang4d0c1c52019-10-14 15:07:45 +0800136 return devm_hwspin_lock_register(&pdev->dev, &sprd_hwlock->bank,
137 &sprd_hwspinlock_ops, 0,
138 SPRD_HWLOCKS_NUM);
Baolin Wangd8c8bbb2017-05-17 13:59:29 +0800139}
140
141static const struct of_device_id sprd_hwspinlock_of_match[] = {
142 { .compatible = "sprd,hwspinlock-r3p0", },
143 { /* sentinel */ }
144};
145MODULE_DEVICE_TABLE(of, sprd_hwspinlock_of_match);
146
147static struct platform_driver sprd_hwspinlock_driver = {
148 .probe = sprd_hwspinlock_probe,
Baolin Wangd8c8bbb2017-05-17 13:59:29 +0800149 .driver = {
150 .name = "sprd_hwspinlock",
151 .of_match_table = of_match_ptr(sprd_hwspinlock_of_match),
152 },
153};
154
155static int __init sprd_hwspinlock_init(void)
156{
157 return platform_driver_register(&sprd_hwspinlock_driver);
158}
159postcore_initcall(sprd_hwspinlock_init);
160
161static void __exit sprd_hwspinlock_exit(void)
162{
163 platform_driver_unregister(&sprd_hwspinlock_driver);
164}
165module_exit(sprd_hwspinlock_exit);
166
167MODULE_LICENSE("GPL v2");
168MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
169MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
170MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
171MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");