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>
9 #include <linux/device.h>
11 #include <linux/io-64-nonatomic-lo-hi.h>
12 #include <linux/iopoll.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset-controller.h>
15 #include <linux/spinlock.h>
17 #include "reset-starfive-jh71x0.h"
19 #include <dt-bindings/reset/starfive-jh7100.h>
21 /* register offsets */
22 #define JH7100_RESET_ASSERT0 0x00
23 #define JH7100_RESET_ASSERT1 0x04
24 #define JH7100_RESET_ASSERT2 0x08
25 #define JH7100_RESET_ASSERT3 0x0c
26 #define JH7100_RESET_STATUS0 0x10
27 #define JH7100_RESET_STATUS1 0x14
28 #define JH7100_RESET_STATUS2 0x18
29 #define JH7100_RESET_STATUS3 0x1c
32 * Writing a 1 to the n'th bit of the m'th ASSERT register asserts
33 * line 32m + n, and writing a 0 deasserts the same line.
34 * Most reset lines have their status inverted so a 0 bit in the STATUS
35 * register means the line is asserted and a 1 means it's deasserted. A few
36 * lines don't though, so store the expected value of the status registers when
37 * all lines are asserted.
39 static const u64 jh7100_reset_asserted[2] = {
41 BIT_ULL_MASK(JH7100_RST_U74) |
42 BIT_ULL_MASK(JH7100_RST_VP6_DRESET) |
43 BIT_ULL_MASK(JH7100_RST_VP6_BRESET) |
45 BIT_ULL_MASK(JH7100_RST_HIFI4_DRESET) |
46 BIT_ULL_MASK(JH7100_RST_HIFI4_BRESET),
48 BIT_ULL_MASK(JH7100_RST_E24) |
54 struct reset_controller_dev rcdev;
55 /* protect registers against concurrent read-modify-write */
60 static inline struct jh7100_reset *
61 jh7100_reset_from(struct reset_controller_dev *rcdev)
63 return container_of(rcdev, struct jh7100_reset, rcdev);
66 static int jh7100_reset_update(struct reset_controller_dev *rcdev,
67 unsigned long id, bool assert)
69 struct jh7100_reset *data = jh7100_reset_from(rcdev);
70 unsigned long offset = BIT_ULL_WORD(id);
71 u64 mask = BIT_ULL_MASK(id);
72 void __iomem *reg_assert = data->base + JH7100_RESET_ASSERT0 + offset * sizeof(u64);
73 void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
74 u64 done = jh7100_reset_asserted[offset] & mask;
82 spin_lock_irqsave(&data->lock, flags);
84 value = readq(reg_assert);
89 writeq(value, reg_assert);
91 /* if the associated clock is gated, deasserting might otherwise hang forever */
92 ret = readq_poll_timeout_atomic(reg_status, value, (value & mask) == done, 0, 1000);
94 spin_unlock_irqrestore(&data->lock, flags);
98 static int jh7100_reset_assert(struct reset_controller_dev *rcdev,
101 return jh7100_reset_update(rcdev, id, true);
104 static int jh7100_reset_deassert(struct reset_controller_dev *rcdev,
107 return jh7100_reset_update(rcdev, id, false);
110 static int jh7100_reset_reset(struct reset_controller_dev *rcdev,
115 ret = jh7100_reset_assert(rcdev, id);
119 return jh7100_reset_deassert(rcdev, id);
122 static int jh7100_reset_status(struct reset_controller_dev *rcdev,
125 struct jh7100_reset *data = jh7100_reset_from(rcdev);
126 unsigned long offset = BIT_ULL_WORD(id);
127 u64 mask = BIT_ULL_MASK(id);
128 void __iomem *reg_status = data->base + JH7100_RESET_STATUS0 + offset * sizeof(u64);
129 u64 value = readq(reg_status);
131 return !((value ^ jh7100_reset_asserted[offset]) & mask);
134 static const struct reset_control_ops jh7100_reset_ops = {
135 .assert = jh7100_reset_assert,
136 .deassert = jh7100_reset_deassert,
137 .reset = jh7100_reset_reset,
138 .status = jh7100_reset_status,
141 int jh7100_reset_probe(struct platform_device *pdev)
143 struct jh7100_reset *data;
145 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
149 data->base = devm_platform_ioremap_resource(pdev, 0);
150 if (IS_ERR(data->base))
151 return PTR_ERR(data->base);
153 data->rcdev.ops = &jh7100_reset_ops;
154 data->rcdev.owner = THIS_MODULE;
155 data->rcdev.nr_resets = JH7100_RSTN_END;
156 data->rcdev.dev = &pdev->dev;
157 data->rcdev.of_node = pdev->dev.of_node;
158 spin_lock_init(&data->lock);
160 return devm_reset_controller_register(&pdev->dev, &data->rcdev);
162 EXPORT_SYMBOL_GPL(jh7100_reset_probe);