Skip to content

Commit 0be3a15

Browse files
committed
reset: starfive-jh7100: Add StarFive JH7100 reset driver
Add a driver for the StarFive JH7100 reset controller. Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Emil Renner Berthing <kernel@esmil.dk>
1 parent d7d456a commit 0be3a15

4 files changed

Lines changed: 187 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18137,6 +18137,13 @@ F: Documentation/devicetree/bindings/clock/starfive,jh7100-clkgen.yaml
1813718137
F: drivers/clk/starfive/clk-starfive-jh7100.c
1813818138
F: include/dt-bindings/clock/starfive-jh7100.h
1813918139

18140+
STARFIVE JH7100 RESET CONTROLLER DRIVER
18141+
M: Emil Renner Berthing <kernel@esmil.dk>
18142+
S: Maintained
18143+
F: Documentation/devicetree/bindings/reset/starfive,jh7100-reset.yaml
18144+
F: drivers/reset/reset-starfive-jh7100.c
18145+
F: include/dt-bindings/reset/starfive-jh7100.h
18146+
1814018147
STATIC BRANCH/CALL
1814118148
M: Peter Zijlstra <peterz@infradead.org>
1814218149
M: Josh Poimboeuf <jpoimboe@redhat.com>

drivers/reset/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ config RESET_SOCFPGA
224224
This enables the reset driver for the SoCFPGA ARMv7 platforms. This
225225
driver gets initialized early during platform init calls.
226226

227+
config RESET_STARFIVE_JH7100
228+
bool "StarFive JH7100 Reset Driver"
229+
depends on SOC_STARFIVE || COMPILE_TEST
230+
default SOC_STARFIVE
231+
help
232+
This enables the reset controller driver for the StarFive JH7100 SoC.
233+
227234
config RESET_SUNXI
228235
bool "Allwinner SoCs Reset Driver" if COMPILE_TEST && !ARCH_SUNXI
229236
default ARCH_SUNXI

drivers/reset/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ obj-$(CONFIG_RESET_RZG2L_USBPHY_CTRL) += reset-rzg2l-usbphy-ctrl.o
2929
obj-$(CONFIG_RESET_SCMI) += reset-scmi.o
3030
obj-$(CONFIG_RESET_SIMPLE) += reset-simple.o
3131
obj-$(CONFIG_RESET_SOCFPGA) += reset-socfpga.o
32+
obj-$(CONFIG_RESET_STARFIVE_JH7100) += reset-starfive-jh7100.o
3233
obj-$(CONFIG_RESET_SUNXI) += reset-sunxi.o
3334
obj-$(CONFIG_RESET_TI_SCI) += reset-ti-sci.o
3435
obj-$(CONFIG_RESET_TI_SYSCON) += reset-ti-syscon.o
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Reset driver for the StarFive JH7100 SoC
4+
*
5+
* Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
6+
*/
7+
8+
#include <linux/bitmap.h>
9+
#include <linux/io.h>
10+
#include <linux/iopoll.h>
11+
#include <linux/mod_devicetable.h>
12+
#include <linux/platform_device.h>
13+
#include <linux/reset-controller.h>
14+
#include <linux/spinlock.h>
15+
16+
#include <dt-bindings/reset/starfive-jh7100.h>
17+
18+
/* register offsets */
19+
#define JH7100_RESET_ASSERT0 0x00
20+
#define JH7100_RESET_ASSERT1 0x04
21+
#define JH7100_RESET_ASSERT2 0x08
22+
#define JH7100_RESET_ASSERT3 0x0c
23+
#define JH7100_RESET_STATUS0 0x10
24+
#define JH7100_RESET_STATUS1 0x14
25+
#define JH7100_RESET_STATUS2 0x18
26+
#define JH7100_RESET_STATUS3 0x1c
27+
28+
/*
29+
* Writing a 1 to the n'th bit of the m'th ASSERT register asserts
30+
* line 32m + n, and writing a 0 deasserts the same line.
31+
* Most reset lines have their status inverted so a 0 bit in the STATUS
32+
* register means the line is asserted and a 1 means it's deasserted. A few
33+
* lines don't though, so store the expected value of the status registers when
34+
* all lines are asserted.
35+
*/
36+
static const u64 jh7100_reset_asserted[2] = {
37+
/* STATUS0 */
38+
BIT_ULL_MASK(JH7100_RST_U74) |
39+
BIT_ULL_MASK(JH7100_RST_VP6_DRESET) |
40+
BIT_ULL_MASK(JH7100_RST_VP6_BRESET) |
41+
/* STATUS1 */
42+
BIT_ULL_MASK(JH7100_RST_HIFI4_DRESET) |
43+
BIT_ULL_MASK(JH7100_RST_HIFI4_BRESET),
44+
/* STATUS2 */
45+
BIT_ULL_MASK(JH7100_RST_E24) |
46+
/* STATUS3 */
47+
0,
48+
};
49+
50+
struct jh7100_reset {
51+
struct reset_controller_dev rcdev;
52+
/* protect registers against concurrent read-modify-write */
53+
spinlock_t lock;
54+
void __iomem *base;
55+
};
56+
57+
static inline struct jh7100_reset *
58+
jh7100_reset_from(struct reset_controller_dev *rcdev)
59+
{
60+
return container_of(rcdev, struct jh7100_reset, rcdev);
61+
}
62+
63+
static int jh7100_reset_update(struct reset_controller_dev *rcdev,
64+
unsigned long id, bool assert)
65+
{
66+
struct jh7100_reset *data = jh7100_reset_from(rcdev);
67+
unsigned long offset = BIT_ULL_WORD(id);
68+
u64 mask = BIT_ULL_MASK(id);
69+
void __iomem *reg_assert = data->base + JH7100_RESET_ASSERT0 + offset * sizeof(u64);
70+
void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
71+
u64 done = jh7100_reset_asserted[offset] & mask;
72+
u64 value;
73+
unsigned long flags;
74+
int ret;
75+
76+
if (!assert)
77+
done ^= mask;
78+
79+
spin_lock_irqsave(&data->lock, flags);
80+
81+
value = readq(reg_assert);
82+
if (assert)
83+
value |= mask;
84+
else
85+
value &= ~mask;
86+
writeq(value, reg_assert);
87+
88+
/* if the associated clock is gated, deasserting might otherwise hang forever */
89+
ret = readq_poll_timeout_atomic(reg_status, value, (value & mask) == done, 0, 1000);
90+
91+
spin_unlock_irqrestore(&data->lock, flags);
92+
return ret;
93+
}
94+
95+
static int jh7100_reset_assert(struct reset_controller_dev *rcdev,
96+
unsigned long id)
97+
{
98+
return jh7100_reset_update(rcdev, id, true);
99+
}
100+
101+
static int jh7100_reset_deassert(struct reset_controller_dev *rcdev,
102+
unsigned long id)
103+
{
104+
return jh7100_reset_update(rcdev, id, false);
105+
}
106+
107+
static int jh7100_reset_reset(struct reset_controller_dev *rcdev,
108+
unsigned long id)
109+
{
110+
int ret;
111+
112+
ret = jh7100_reset_assert(rcdev, id);
113+
if (ret)
114+
return ret;
115+
116+
return jh7100_reset_deassert(rcdev, id);
117+
}
118+
119+
static int jh7100_reset_status(struct reset_controller_dev *rcdev,
120+
unsigned long id)
121+
{
122+
struct jh7100_reset *data = jh7100_reset_from(rcdev);
123+
unsigned long offset = BIT_ULL_WORD(id);
124+
u64 mask = BIT_ULL_MASK(id);
125+
void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
126+
u64 value = readq(reg_status);
127+
128+
return !((value ^ jh7100_reset_asserted[offset]) & mask);
129+
}
130+
131+
static const struct reset_control_ops jh7100_reset_ops = {
132+
.assert = jh7100_reset_assert,
133+
.deassert = jh7100_reset_deassert,
134+
.reset = jh7100_reset_reset,
135+
.status = jh7100_reset_status,
136+
};
137+
138+
static int __init jh7100_reset_probe(struct platform_device *pdev)
139+
{
140+
struct jh7100_reset *data;
141+
142+
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
143+
if (!data)
144+
return -ENOMEM;
145+
146+
data->base = devm_platform_ioremap_resource(pdev, 0);
147+
if (IS_ERR(data->base))
148+
return PTR_ERR(data->base);
149+
150+
data->rcdev.ops = &jh7100_reset_ops;
151+
data->rcdev.owner = THIS_MODULE;
152+
data->rcdev.nr_resets = JH7100_RSTN_END;
153+
data->rcdev.dev = &pdev->dev;
154+
data->rcdev.of_node = pdev->dev.of_node;
155+
spin_lock_init(&data->lock);
156+
157+
return devm_reset_controller_register(&pdev->dev, &data->rcdev);
158+
}
159+
160+
static const struct of_device_id jh7100_reset_dt_ids[] = {
161+
{ .compatible = "starfive,jh7100-reset" },
162+
{ /* sentinel */ }
163+
};
164+
165+
static struct platform_driver jh7100_reset_driver = {
166+
.driver = {
167+
.name = "jh7100-reset",
168+
.of_match_table = jh7100_reset_dt_ids,
169+
.suppress_bind_attrs = true,
170+
},
171+
};
172+
builtin_platform_driver_probe(jh7100_reset_driver, jh7100_reset_probe);

0 commit comments

Comments
 (0)