summaryrefslogtreecommitdiff
path: root/drivers/reset/reset-jh7110.c
blob: d6bdf6bb00c4ac7808be49b2bc966ca42ad054bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2022 StarFive Technology Co., Ltd.
 * Author:	Yanhong Wang <yanhong.wang@starfivetech.com>
 *
 */

#include <common.h>
#include <dm.h>
#include <dm/ofnode.h>
#include <dt-bindings/reset/starfive,jh7110-crg.h>
#include <errno.h>
#include <linux/iopoll.h>
#include <reset-uclass.h>

struct jh7110_reset_priv {
	void __iomem *reg;
	u32	assert;
	u32	status;
	u32	resets;
};

struct reset_info {
	const char *compat;
	const u32 nr_resets;
	const u32 assert_offset;
	const u32 status_offset;
};

static const struct reset_info jh7110_rst_info[] = {
	{
		.compat = "starfive,jh7110-syscrg",
		.nr_resets = JH7110_SYSRST_END,
		.assert_offset = 0x2F8,
		.status_offset = 0x308,
	},
	{
		.compat = "starfive,jh7110-aoncrg",
		.nr_resets = JH7110_AONRST_END,
		.assert_offset = 0x38,
		.status_offset = 0x3C,
	},
	{
		.compat = "starfive,jh7110-stgcrg",
		.nr_resets = JH7110_STGRST_END,
		.assert_offset = 0x74,
		.status_offset = 0x78,
	}
};

static const struct reset_info *jh7110_reset_get_cfg(const char *compat)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(jh7110_rst_info); i++)
		if (!strcmp(compat, jh7110_rst_info[i].compat))
			return &jh7110_rst_info[i];

	return NULL;
}

static int jh7110_reset_trigger(struct jh7110_reset_priv *priv,
				unsigned long id, bool assert)
{
	ulong group;
	u32 mask, value, done = 0;
	ulong addr;

	group = id / 32;
	mask = BIT(id % 32);

	if (!assert)
		done ^= mask;

	addr = (ulong)priv->reg + priv->assert + group * sizeof(u32);
	value = readl((ulong *)addr);

	if (assert)
		value |= mask;
	else
		value &= ~mask;

	writel(value, (ulong *)addr);
	addr = (ulong)priv->reg + priv->status + group * sizeof(u32);

	return readl_poll_timeout((ulong *)addr, value,
						(value & mask) == done, 1000);
}

static int jh7110_reset_assert(struct reset_ctl *rst)
{
	struct jh7110_reset_priv *priv = dev_get_priv(rst->dev);

	jh7110_reset_trigger(priv, rst->id, true);

	return 0;
}

static int jh7110_reset_deassert(struct reset_ctl *rst)
{
	struct jh7110_reset_priv *priv = dev_get_priv(rst->dev);

	jh7110_reset_trigger(priv, rst->id, false);

	return 0;
}

static int jh7110_reset_free(struct reset_ctl *rst)
{
	return 0;
}

static int jh7110_reset_request(struct reset_ctl *rst)
{
	struct jh7110_reset_priv *priv = dev_get_priv(rst->dev);

	if (rst->id >= priv->resets)
		return -EINVAL;

	return 0;
}

static int jh7110_reset_probe(struct udevice *dev)
{
	struct jh7110_reset_priv *priv = dev_get_priv(dev);
	const struct reset_info *cfg;
	const char *compat;

	compat = ofnode_get_property(dev_ofnode(dev), "compatible", NULL);
	if (!compat)
		return -EINVAL;

	cfg = jh7110_reset_get_cfg(compat);
	if (!cfg)
		return -EINVAL;

	priv->assert = cfg->assert_offset;
	priv->status = cfg->status_offset;
	priv->resets = cfg->nr_resets;
	priv->reg = (void __iomem *)dev_read_addr_index(dev, 0);

	return 0;
}

const struct reset_ops jh7110_reset_reset_ops = {
	.rfree = jh7110_reset_free,
	.request = jh7110_reset_request,
	.rst_assert = jh7110_reset_assert,
	.rst_deassert = jh7110_reset_deassert,
};

U_BOOT_DRIVER(jh7110_reset) = {
	.name = "jh7110_reset",
	.id = UCLASS_RESET,
	.ops = &jh7110_reset_reset_ops,
	.probe = jh7110_reset_probe,
	.priv_auto = sizeof(struct jh7110_reset_priv),
};