blob: cb38e487c6c42b7ed822f6f551e69856ccaf1788 [file] [log] [blame]
Wei Chencc16d662015-05-26 08:28:29 +00001/*
2 * SIRF hardware spinlock driver
3 *
4 * Copyright (c) 2015 Cambridge Silicon Radio Limited, a CSR plc group company.
5 *
6 * Licensed under GPLv2.
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/device.h>
12#include <linux/io.h>
13#include <linux/pm_runtime.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/hwspinlock.h>
17#include <linux/platform_device.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
20
21#include "hwspinlock_internal.h"
22
23struct sirf_hwspinlock {
24 void __iomem *io_base;
25 struct hwspinlock_device bank;
26};
27
28/* Number of Hardware Spinlocks*/
29#define HW_SPINLOCK_NUMBER 30
30
31/* Hardware spinlock register offsets */
32#define HW_SPINLOCK_BASE 0x404
33#define HW_SPINLOCK_OFFSET(x) (HW_SPINLOCK_BASE + 0x4 * (x))
34
35static int sirf_hwspinlock_trylock(struct hwspinlock *lock)
36{
37 void __iomem *lock_addr = lock->priv;
38
39 /* attempt to acquire the lock by reading value == 1 from it */
40 return !!readl(lock_addr);
41}
42
43static void sirf_hwspinlock_unlock(struct hwspinlock *lock)
44{
45 void __iomem *lock_addr = lock->priv;
46
47 /* release the lock by writing 0 to it */
48 writel(0, lock_addr);
49}
50
51static const struct hwspinlock_ops sirf_hwspinlock_ops = {
52 .trylock = sirf_hwspinlock_trylock,
53 .unlock = sirf_hwspinlock_unlock,
54};
55
56static int sirf_hwspinlock_probe(struct platform_device *pdev)
57{
58 struct sirf_hwspinlock *hwspin;
59 struct hwspinlock *hwlock;
60 int idx, ret;
61
62 if (!pdev->dev.of_node)
63 return -ENODEV;
64
Kees Cook0ed2dd02018-05-08 16:08:53 -070065 hwspin = devm_kzalloc(&pdev->dev,
66 struct_size(hwspin, bank.lock,
67 HW_SPINLOCK_NUMBER),
68 GFP_KERNEL);
Wei Chencc16d662015-05-26 08:28:29 +000069 if (!hwspin)
70 return -ENOMEM;
71
72 /* retrieve io base */
73 hwspin->io_base = of_iomap(pdev->dev.of_node, 0);
74 if (!hwspin->io_base)
75 return -ENOMEM;
76
77 for (idx = 0; idx < HW_SPINLOCK_NUMBER; idx++) {
78 hwlock = &hwspin->bank.lock[idx];
79 hwlock->priv = hwspin->io_base + HW_SPINLOCK_OFFSET(idx);
80 }
81
82 platform_set_drvdata(pdev, hwspin);
83
84 pm_runtime_enable(&pdev->dev);
85
86 ret = hwspin_lock_register(&hwspin->bank, &pdev->dev,
87 &sirf_hwspinlock_ops, 0,
88 HW_SPINLOCK_NUMBER);
89 if (ret)
90 goto reg_failed;
91
92 return 0;
93
94reg_failed:
95 pm_runtime_disable(&pdev->dev);
96 iounmap(hwspin->io_base);
97
98 return ret;
99}
100
101static int sirf_hwspinlock_remove(struct platform_device *pdev)
102{
103 struct sirf_hwspinlock *hwspin = platform_get_drvdata(pdev);
104 int ret;
105
106 ret = hwspin_lock_unregister(&hwspin->bank);
107 if (ret) {
108 dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
109 return ret;
110 }
111
112 pm_runtime_disable(&pdev->dev);
113
114 iounmap(hwspin->io_base);
115
116 return 0;
117}
118
119static const struct of_device_id sirf_hwpinlock_ids[] = {
120 { .compatible = "sirf,hwspinlock", },
121 {},
122};
123MODULE_DEVICE_TABLE(of, sirf_hwpinlock_ids);
124
125static struct platform_driver sirf_hwspinlock_driver = {
126 .probe = sirf_hwspinlock_probe,
127 .remove = sirf_hwspinlock_remove,
128 .driver = {
129 .name = "atlas7_hwspinlock",
130 .of_match_table = of_match_ptr(sirf_hwpinlock_ids),
131 },
132};
133
134module_platform_driver(sirf_hwspinlock_driver);
135
136MODULE_LICENSE("GPL v2");
137MODULE_DESCRIPTION("SIRF Hardware spinlock driver");
138MODULE_AUTHOR("Wei Chen <wei.chen@csr.com>");