1*3c881e05SWilken Gottwalt // SPDX-License-Identifier: GPL-2.0-or-later
2*3c881e05SWilken Gottwalt /*
3*3c881e05SWilken Gottwalt * sun6i_hwspinlock.c - hardware spinlock driver for sun6i compatible Allwinner SoCs
4*3c881e05SWilken Gottwalt * Copyright (C) 2020 Wilken Gottwalt <wilken.gottwalt@posteo.net>
5*3c881e05SWilken Gottwalt */
6*3c881e05SWilken Gottwalt
7*3c881e05SWilken Gottwalt #include <linux/clk.h>
8*3c881e05SWilken Gottwalt #include <linux/debugfs.h>
9*3c881e05SWilken Gottwalt #include <linux/errno.h>
10*3c881e05SWilken Gottwalt #include <linux/hwspinlock.h>
11*3c881e05SWilken Gottwalt #include <linux/io.h>
12*3c881e05SWilken Gottwalt #include <linux/module.h>
13*3c881e05SWilken Gottwalt #include <linux/of.h>
14*3c881e05SWilken Gottwalt #include <linux/platform_device.h>
15*3c881e05SWilken Gottwalt #include <linux/reset.h>
16*3c881e05SWilken Gottwalt #include <linux/slab.h>
17*3c881e05SWilken Gottwalt #include <linux/spinlock.h>
18*3c881e05SWilken Gottwalt #include <linux/types.h>
19*3c881e05SWilken Gottwalt
20*3c881e05SWilken Gottwalt #include "hwspinlock_internal.h"
21*3c881e05SWilken Gottwalt
22*3c881e05SWilken Gottwalt #define DRIVER_NAME "sun6i_hwspinlock"
23*3c881e05SWilken Gottwalt
24*3c881e05SWilken Gottwalt #define SPINLOCK_BASE_ID 0 /* there is only one hwspinlock device per SoC */
25*3c881e05SWilken Gottwalt #define SPINLOCK_SYSSTATUS_REG 0x0000
26*3c881e05SWilken Gottwalt #define SPINLOCK_LOCK_REGN 0x0100
27*3c881e05SWilken Gottwalt #define SPINLOCK_NOTTAKEN 0
28*3c881e05SWilken Gottwalt
29*3c881e05SWilken Gottwalt struct sun6i_hwspinlock_data {
30*3c881e05SWilken Gottwalt struct hwspinlock_device *bank;
31*3c881e05SWilken Gottwalt struct reset_control *reset;
32*3c881e05SWilken Gottwalt struct clk *ahb_clk;
33*3c881e05SWilken Gottwalt struct dentry *debugfs;
34*3c881e05SWilken Gottwalt int nlocks;
35*3c881e05SWilken Gottwalt };
36*3c881e05SWilken Gottwalt
37*3c881e05SWilken Gottwalt #ifdef CONFIG_DEBUG_FS
38*3c881e05SWilken Gottwalt
hwlocks_supported_show(struct seq_file * seqf,void * unused)39*3c881e05SWilken Gottwalt static int hwlocks_supported_show(struct seq_file *seqf, void *unused)
40*3c881e05SWilken Gottwalt {
41*3c881e05SWilken Gottwalt struct sun6i_hwspinlock_data *priv = seqf->private;
42*3c881e05SWilken Gottwalt
43*3c881e05SWilken Gottwalt seq_printf(seqf, "%d\n", priv->nlocks);
44*3c881e05SWilken Gottwalt
45*3c881e05SWilken Gottwalt return 0;
46*3c881e05SWilken Gottwalt }
47*3c881e05SWilken Gottwalt DEFINE_SHOW_ATTRIBUTE(hwlocks_supported);
48*3c881e05SWilken Gottwalt
sun6i_hwspinlock_debugfs_init(struct sun6i_hwspinlock_data * priv)49*3c881e05SWilken Gottwalt static void sun6i_hwspinlock_debugfs_init(struct sun6i_hwspinlock_data *priv)
50*3c881e05SWilken Gottwalt {
51*3c881e05SWilken Gottwalt priv->debugfs = debugfs_create_dir(DRIVER_NAME, NULL);
52*3c881e05SWilken Gottwalt debugfs_create_file("supported", 0444, priv->debugfs, priv, &hwlocks_supported_fops);
53*3c881e05SWilken Gottwalt }
54*3c881e05SWilken Gottwalt
55*3c881e05SWilken Gottwalt #else
56*3c881e05SWilken Gottwalt
sun6i_hwspinlock_debugfs_init(struct sun6i_hwspinlock_data * priv)57*3c881e05SWilken Gottwalt static void sun6i_hwspinlock_debugfs_init(struct sun6i_hwspinlock_data *priv)
58*3c881e05SWilken Gottwalt {
59*3c881e05SWilken Gottwalt }
60*3c881e05SWilken Gottwalt
61*3c881e05SWilken Gottwalt #endif
62*3c881e05SWilken Gottwalt
sun6i_hwspinlock_trylock(struct hwspinlock * lock)63*3c881e05SWilken Gottwalt static int sun6i_hwspinlock_trylock(struct hwspinlock *lock)
64*3c881e05SWilken Gottwalt {
65*3c881e05SWilken Gottwalt void __iomem *lock_addr = lock->priv;
66*3c881e05SWilken Gottwalt
67*3c881e05SWilken Gottwalt return (readl(lock_addr) == SPINLOCK_NOTTAKEN);
68*3c881e05SWilken Gottwalt }
69*3c881e05SWilken Gottwalt
sun6i_hwspinlock_unlock(struct hwspinlock * lock)70*3c881e05SWilken Gottwalt static void sun6i_hwspinlock_unlock(struct hwspinlock *lock)
71*3c881e05SWilken Gottwalt {
72*3c881e05SWilken Gottwalt void __iomem *lock_addr = lock->priv;
73*3c881e05SWilken Gottwalt
74*3c881e05SWilken Gottwalt writel(SPINLOCK_NOTTAKEN, lock_addr);
75*3c881e05SWilken Gottwalt }
76*3c881e05SWilken Gottwalt
77*3c881e05SWilken Gottwalt static const struct hwspinlock_ops sun6i_hwspinlock_ops = {
78*3c881e05SWilken Gottwalt .trylock = sun6i_hwspinlock_trylock,
79*3c881e05SWilken Gottwalt .unlock = sun6i_hwspinlock_unlock,
80*3c881e05SWilken Gottwalt };
81*3c881e05SWilken Gottwalt
sun6i_hwspinlock_disable(void * data)82*3c881e05SWilken Gottwalt static void sun6i_hwspinlock_disable(void *data)
83*3c881e05SWilken Gottwalt {
84*3c881e05SWilken Gottwalt struct sun6i_hwspinlock_data *priv = data;
85*3c881e05SWilken Gottwalt
86*3c881e05SWilken Gottwalt debugfs_remove_recursive(priv->debugfs);
87*3c881e05SWilken Gottwalt clk_disable_unprepare(priv->ahb_clk);
88*3c881e05SWilken Gottwalt reset_control_assert(priv->reset);
89*3c881e05SWilken Gottwalt }
90*3c881e05SWilken Gottwalt
sun6i_hwspinlock_probe(struct platform_device * pdev)91*3c881e05SWilken Gottwalt static int sun6i_hwspinlock_probe(struct platform_device *pdev)
92*3c881e05SWilken Gottwalt {
93*3c881e05SWilken Gottwalt struct sun6i_hwspinlock_data *priv;
94*3c881e05SWilken Gottwalt struct hwspinlock *hwlock;
95*3c881e05SWilken Gottwalt void __iomem *io_base;
96*3c881e05SWilken Gottwalt u32 num_banks;
97*3c881e05SWilken Gottwalt int err, i;
98*3c881e05SWilken Gottwalt
99*3c881e05SWilken Gottwalt io_base = devm_platform_ioremap_resource(pdev, SPINLOCK_BASE_ID);
100*3c881e05SWilken Gottwalt if (IS_ERR(io_base))
101*3c881e05SWilken Gottwalt return PTR_ERR(io_base);
102*3c881e05SWilken Gottwalt
103*3c881e05SWilken Gottwalt priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
104*3c881e05SWilken Gottwalt if (!priv)
105*3c881e05SWilken Gottwalt return -ENOMEM;
106*3c881e05SWilken Gottwalt
107*3c881e05SWilken Gottwalt priv->ahb_clk = devm_clk_get(&pdev->dev, "ahb");
108*3c881e05SWilken Gottwalt if (IS_ERR(priv->ahb_clk)) {
109*3c881e05SWilken Gottwalt err = PTR_ERR(priv->ahb_clk);
110*3c881e05SWilken Gottwalt dev_err(&pdev->dev, "unable to get AHB clock (%d)\n", err);
111*3c881e05SWilken Gottwalt return err;
112*3c881e05SWilken Gottwalt }
113*3c881e05SWilken Gottwalt
114*3c881e05SWilken Gottwalt priv->reset = devm_reset_control_get(&pdev->dev, "ahb");
115*3c881e05SWilken Gottwalt if (IS_ERR(priv->reset))
116*3c881e05SWilken Gottwalt return dev_err_probe(&pdev->dev, PTR_ERR(priv->reset),
117*3c881e05SWilken Gottwalt "unable to get reset control\n");
118*3c881e05SWilken Gottwalt
119*3c881e05SWilken Gottwalt err = reset_control_deassert(priv->reset);
120*3c881e05SWilken Gottwalt if (err) {
121*3c881e05SWilken Gottwalt dev_err(&pdev->dev, "deassert reset control failure (%d)\n", err);
122*3c881e05SWilken Gottwalt return err;
123*3c881e05SWilken Gottwalt }
124*3c881e05SWilken Gottwalt
125*3c881e05SWilken Gottwalt err = clk_prepare_enable(priv->ahb_clk);
126*3c881e05SWilken Gottwalt if (err) {
127*3c881e05SWilken Gottwalt dev_err(&pdev->dev, "unable to prepare AHB clk (%d)\n", err);
128*3c881e05SWilken Gottwalt goto clk_fail;
129*3c881e05SWilken Gottwalt }
130*3c881e05SWilken Gottwalt
131*3c881e05SWilken Gottwalt /*
132*3c881e05SWilken Gottwalt * bit 28 and 29 represents the hwspinlock setup
133*3c881e05SWilken Gottwalt *
134*3c881e05SWilken Gottwalt * every datasheet (A64, A80, A83T, H3, H5, H6 ...) says the default value is 0x1 and 0x1
135*3c881e05SWilken Gottwalt * to 0x4 represent 32, 64, 128 and 256 locks
136*3c881e05SWilken Gottwalt * but later datasheets (H5, H6) say 00, 01, 10, 11 represent 32, 64, 128 and 256 locks,
137*3c881e05SWilken Gottwalt * but that would mean H5 and H6 have 64 locks, while their datasheets talk about 32 locks
138*3c881e05SWilken Gottwalt * all the time, not a single mentioning of 64 locks
139*3c881e05SWilken Gottwalt * the 0x4 value is also not representable by 2 bits alone, so some datasheets are not
140*3c881e05SWilken Gottwalt * correct
141*3c881e05SWilken Gottwalt * one thing have all in common, default value of the sysstatus register is 0x10000000,
142*3c881e05SWilken Gottwalt * which results in bit 28 being set
143*3c881e05SWilken Gottwalt * this is the reason 0x1 is considered being 32 locks and bit 30 is taken into account
144*3c881e05SWilken Gottwalt * verified on H2+ (datasheet 0x1 = 32 locks) and H5 (datasheet 01 = 64 locks)
145*3c881e05SWilken Gottwalt */
146*3c881e05SWilken Gottwalt num_banks = readl(io_base + SPINLOCK_SYSSTATUS_REG) >> 28;
147*3c881e05SWilken Gottwalt switch (num_banks) {
148*3c881e05SWilken Gottwalt case 1 ... 4:
149*3c881e05SWilken Gottwalt priv->nlocks = 1 << (4 + num_banks);
150*3c881e05SWilken Gottwalt break;
151*3c881e05SWilken Gottwalt default:
152*3c881e05SWilken Gottwalt err = -EINVAL;
153*3c881e05SWilken Gottwalt dev_err(&pdev->dev, "unsupported hwspinlock setup (%d)\n", num_banks);
154*3c881e05SWilken Gottwalt goto bank_fail;
155*3c881e05SWilken Gottwalt }
156*3c881e05SWilken Gottwalt
157*3c881e05SWilken Gottwalt priv->bank = devm_kzalloc(&pdev->dev, struct_size(priv->bank, lock, priv->nlocks),
158*3c881e05SWilken Gottwalt GFP_KERNEL);
159*3c881e05SWilken Gottwalt if (!priv->bank) {
160*3c881e05SWilken Gottwalt err = -ENOMEM;
161*3c881e05SWilken Gottwalt goto bank_fail;
162*3c881e05SWilken Gottwalt }
163*3c881e05SWilken Gottwalt
164*3c881e05SWilken Gottwalt for (i = 0; i < priv->nlocks; ++i) {
165*3c881e05SWilken Gottwalt hwlock = &priv->bank->lock[i];
166*3c881e05SWilken Gottwalt hwlock->priv = io_base + SPINLOCK_LOCK_REGN + sizeof(u32) * i;
167*3c881e05SWilken Gottwalt }
168*3c881e05SWilken Gottwalt
169*3c881e05SWilken Gottwalt /* failure of debugfs is considered non-fatal */
170*3c881e05SWilken Gottwalt sun6i_hwspinlock_debugfs_init(priv);
171*3c881e05SWilken Gottwalt if (IS_ERR(priv->debugfs))
172*3c881e05SWilken Gottwalt priv->debugfs = NULL;
173*3c881e05SWilken Gottwalt
174*3c881e05SWilken Gottwalt err = devm_add_action_or_reset(&pdev->dev, sun6i_hwspinlock_disable, priv);
175*3c881e05SWilken Gottwalt if (err) {
176*3c881e05SWilken Gottwalt dev_err(&pdev->dev, "failed to add hwspinlock disable action\n");
177*3c881e05SWilken Gottwalt goto bank_fail;
178*3c881e05SWilken Gottwalt }
179*3c881e05SWilken Gottwalt
180*3c881e05SWilken Gottwalt platform_set_drvdata(pdev, priv);
181*3c881e05SWilken Gottwalt
182*3c881e05SWilken Gottwalt return devm_hwspin_lock_register(&pdev->dev, priv->bank, &sun6i_hwspinlock_ops,
183*3c881e05SWilken Gottwalt SPINLOCK_BASE_ID, priv->nlocks);
184*3c881e05SWilken Gottwalt
185*3c881e05SWilken Gottwalt bank_fail:
186*3c881e05SWilken Gottwalt clk_disable_unprepare(priv->ahb_clk);
187*3c881e05SWilken Gottwalt clk_fail:
188*3c881e05SWilken Gottwalt reset_control_assert(priv->reset);
189*3c881e05SWilken Gottwalt
190*3c881e05SWilken Gottwalt return err;
191*3c881e05SWilken Gottwalt }
192*3c881e05SWilken Gottwalt
193*3c881e05SWilken Gottwalt static const struct of_device_id sun6i_hwspinlock_ids[] = {
194*3c881e05SWilken Gottwalt { .compatible = "allwinner,sun6i-a31-hwspinlock", },
195*3c881e05SWilken Gottwalt {},
196*3c881e05SWilken Gottwalt };
197*3c881e05SWilken Gottwalt MODULE_DEVICE_TABLE(of, sun6i_hwspinlock_ids);
198*3c881e05SWilken Gottwalt
199*3c881e05SWilken Gottwalt static struct platform_driver sun6i_hwspinlock_driver = {
200*3c881e05SWilken Gottwalt .probe = sun6i_hwspinlock_probe,
201*3c881e05SWilken Gottwalt .driver = {
202*3c881e05SWilken Gottwalt .name = DRIVER_NAME,
203*3c881e05SWilken Gottwalt .of_match_table = sun6i_hwspinlock_ids,
204*3c881e05SWilken Gottwalt },
205*3c881e05SWilken Gottwalt };
206*3c881e05SWilken Gottwalt module_platform_driver(sun6i_hwspinlock_driver);
207*3c881e05SWilken Gottwalt
208*3c881e05SWilken Gottwalt MODULE_LICENSE("GPL");
209*3c881e05SWilken Gottwalt MODULE_DESCRIPTION("SUN6I hardware spinlock driver");
210*3c881e05SWilken Gottwalt MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");
211