1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Reset driver for the StarFive JH7100 SoC
5 * Copyright (C) 2021 Emil Renner Berthing <kernel@esmil.dk>
8 #include <linux/bitmap.h>
10 #include <linux/io-64-nonatomic-lo-hi.h>
11 #include <linux/iopoll.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/spinlock.h>
17 #include <dt-bindings/reset/starfive-jh7100.h>
19 /* register offsets */
20 #define JH7100_RESET_ASSERT0 0x00
21 #define JH7100_RESET_ASSERT1 0x04
22 #define JH7100_RESET_ASSERT2 0x08
23 #define JH7100_RESET_ASSERT3 0x0c
24 #define JH7100_RESET_STATUS0 0x10
25 #define JH7100_RESET_STATUS1 0x14
26 #define JH7100_RESET_STATUS2 0x18
27 #define JH7100_RESET_STATUS3 0x1c
30 * Writing a 1 to the n'th bit of the m'th ASSERT register asserts
31 * line 32m + n, and writing a 0 deasserts the same line.
32 * Most reset lines have their status inverted so a 0 bit in the STATUS
33 * register means the line is asserted and a 1 means it's deasserted. A few
34 * lines don't though, so store the expected value of the status registers when
35 * all lines are asserted.
37 static const u64 jh7100_reset_asserted[2] = {
39 BIT_ULL_MASK(JH7100_RST_U74) |
40 BIT_ULL_MASK(JH7100_RST_VP6_DRESET) |
41 BIT_ULL_MASK(JH7100_RST_VP6_BRESET) |
43 BIT_ULL_MASK(JH7100_RST_HIFI4_DRESET) |
44 BIT_ULL_MASK(JH7100_RST_HIFI4_BRESET),
46 BIT_ULL_MASK(JH7100_RST_E24) |
52 struct reset_controller_dev rcdev;
53 /* protect registers against concurrent read-modify-write */
58 static inline struct jh7100_reset *
59 jh7100_reset_from(struct reset_controller_dev *rcdev)
61 return container_of(rcdev, struct jh7100_reset, rcdev);
64 static int jh7100_reset_update(struct reset_controller_dev *rcdev,
65 unsigned long id, bool assert)
67 struct jh7100_reset *data = jh7100_reset_from(rcdev);
68 unsigned long offset = BIT_ULL_WORD(id);
69 u64 mask = BIT_ULL_MASK(id);
70 void __iomem *reg_assert = data->base + JH7100_RESET_ASSERT0 + offset * sizeof(u64);
71 void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
72 u64 done = jh7100_reset_asserted[offset] & mask;
80 spin_lock_irqsave(&data->lock, flags);
82 value = readq(reg_assert);
87 writeq(value, reg_assert);
89 /* if the associated clock is gated, deasserting might otherwise hang forever */
90 ret = readq_poll_timeout_atomic(reg_status, value, (value & mask) == done, 0, 1000);
92 spin_unlock_irqrestore(&data->lock, flags);
96 static int jh7100_reset_assert(struct reset_controller_dev *rcdev,
99 return jh7100_reset_update(rcdev, id, true);
102 static int jh7100_reset_deassert(struct reset_controller_dev *rcdev,
105 return jh7100_reset_update(rcdev, id, false);
108 static int jh7100_reset_reset(struct reset_controller_dev *rcdev,
113 ret = jh7100_reset_assert(rcdev, id);
117 return jh7100_reset_deassert(rcdev, id);
120 static int jh7100_reset_status(struct reset_controller_dev *rcdev,
123 struct jh7100_reset *data = jh7100_reset_from(rcdev);
124 unsigned long offset = BIT_ULL_WORD(id);
125 u64 mask = BIT_ULL_MASK(id);
126 void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
127 u64 value = readq(reg_status);
129 return !((value ^ jh7100_reset_asserted[offset]) & mask);
132 static const struct reset_control_ops jh7100_reset_ops = {
133 .assert = jh7100_reset_assert,
134 .deassert = jh7100_reset_deassert,
135 .reset = jh7100_reset_reset,
136 .status = jh7100_reset_status,
139 static int __init jh7100_reset_probe(struct platform_device *pdev)
141 struct jh7100_reset *data;
143 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
147 data->base = devm_platform_ioremap_resource(pdev, 0);
148 if (IS_ERR(data->base))
149 return PTR_ERR(data->base);
151 data->rcdev.ops = &jh7100_reset_ops;
152 data->rcdev.owner = THIS_MODULE;
153 data->rcdev.nr_resets = JH7100_RSTN_END;
154 data->rcdev.dev = &pdev->dev;
155 data->rcdev.of_node = pdev->dev.of_node;
156 spin_lock_init(&data->lock);
158 return devm_reset_controller_register(&pdev->dev, &data->rcdev);
161 static const struct of_device_id jh7100_reset_dt_ids[] = {
162 { .compatible = "starfive,jh7100-reset" },
166 static struct platform_driver jh7100_reset_driver = {
168 .name = "jh7100-reset",
169 .of_match_table = jh7100_reset_dt_ids,
170 .suppress_bind_attrs = true,
173 builtin_platform_driver_probe(jh7100_reset_driver, jh7100_reset_probe);