~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: 87738b7b5bffba9b3a9201d2a6476d1d3a046514 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: GPL-2.0+
/*
 * Generic clock support for AT91 architectures.
 *
 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
 *
 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
 *
 * Based on drivers/clk/at91/clk-generated.c from Linux.
 */
#include <common.h>
#include <clk-uclass.h>
#include <dm.h>
#include <linux/io.h>
#include <linux/clk-provider.h>
#include <linux/clk/at91_pmc.h>

#include "pmc.h"

#define UBOOT_DM_CLK_AT91_GCK		"at91-gck-clk"

#define GENERATED_MAX_DIV	255

struct clk_gck {
	void __iomem *base;
	const u32 *clk_mux_table;
	const u32 *mux_table;
	const struct clk_pcr_layout *layout;
	struct clk_range range;
	struct clk clk;
	u32 num_parents;
	u32 id;
};

#define to_clk_gck(_c) container_of(_c, struct clk_gck, clk)

static int clk_gck_enable(struct clk *clk)
{
	struct clk_gck *gck = to_clk_gck(clk);

	pmc_write(gck->base, gck->layout->offset,
		  (gck->id & gck->layout->pid_mask));
	pmc_update_bits(gck->base, gck->layout->offset,
			gck->layout->cmd | AT91_PMC_PCR_GCKEN,
			gck->layout->cmd | AT91_PMC_PCR_GCKEN);

	return 0;
}

static int clk_gck_disable(struct clk *clk)
{
	struct clk_gck *gck = to_clk_gck(clk);

	pmc_write(gck->base, gck->layout->offset,
		  (gck->id & gck->layout->pid_mask));
	pmc_update_bits(gck->base, gck->layout->offset,
			gck->layout->cmd | AT91_PMC_PCR_GCKEN,
			gck->layout->cmd);

	return 0;
}

static int clk_gck_set_parent(struct clk *clk, struct clk *parent)
{
	struct clk_gck *gck = to_clk_gck(clk);
	int index;

	index = at91_clk_mux_val_to_index(gck->clk_mux_table, gck->num_parents,
					  parent->id);
	if (index < 0)
		return index;

	index = at91_clk_mux_index_to_val(gck->mux_table, gck->num_parents,
					  index);
	if (index < 0)
		return index;

	pmc_write(gck->base, gck->layout->offset,
		  (gck->id & gck->layout->pid_mask));
	pmc_update_bits(gck->base, gck->layout->offset,
			gck->layout->gckcss_mask | gck->layout->cmd,
			(index << (ffs(gck->layout->gckcss_mask) - 1)) |
			gck->layout->cmd);

	return 0;
}

static ulong clk_gck_set_rate(struct clk *clk, ulong rate)
{
	struct clk_gck *gck = to_clk_gck(clk);
	ulong parent_rate = clk_get_parent_rate(clk);
	u32 div;

	if (!rate || !parent_rate)
		return 0;

	if (gck->range.max && rate > gck->range.max)
		return 0;

	div = DIV_ROUND_CLOSEST(parent_rate, rate);
	if (div > GENERATED_MAX_DIV + 1 || !div)
		return 0;

	pmc_write(gck->base, gck->layout->offset,
		  (gck->id & gck->layout->pid_mask));
	pmc_update_bits(gck->base, gck->layout->offset,
			AT91_PMC_PCR_GCKDIV_MASK | gck->layout->cmd,
			((div - 1) << (ffs(AT91_PMC_PCR_GCKDIV_MASK) - 1)) |
			gck->layout->cmd);

	return parent_rate / div;
}

static ulong clk_gck_get_rate(struct clk *clk)
{
	struct clk_gck *gck = to_clk_gck(clk);
	ulong parent_rate = clk_get_parent_rate(clk);
	u32 val, div;

	if (!parent_rate)
		return 0;

	pmc_write(gck->base, gck->layout->offset,
		  (gck->id & gck->layout->pid_mask));
	pmc_read(gck->base, gck->layout->offset, &val);

	div = (val & AT91_PMC_PCR_GCKDIV_MASK) >>
		(ffs(AT91_PMC_PCR_GCKDIV_MASK) - 1);

	return parent_rate / (div + 1);
}

static const struct clk_ops gck_ops = {
	.enable = clk_gck_enable,
	.disable = clk_gck_disable,
	.set_parent = clk_gck_set_parent,
	.set_rate = clk_gck_set_rate,
	.get_rate = clk_gck_get_rate,
};

struct clk *
at91_clk_register_generic(void __iomem *base,
			  const struct clk_pcr_layout *layout,
			  const char *name, const char * const *parent_names,
			  const u32 *clk_mux_table, const u32 *mux_table,
			  u8 num_parents, u8 id,
			  const struct clk_range *range)
{
	struct clk_gck *gck;
	struct clk *clk;
	int ret, index;
	u32 val;

	if (!base || !layout || !name || !parent_names || !num_parents ||
	    !clk_mux_table || !mux_table || !range)
		return ERR_PTR(-EINVAL);

	gck = kzalloc(sizeof(*gck), GFP_KERNEL);
	if (!gck)
		return ERR_PTR(-ENOMEM);

	gck->id = id;
	gck->base = base;
	gck->range = *range;
	gck->layout = layout;
	gck->clk_mux_table = clk_mux_table;
	gck->mux_table = mux_table;
	gck->num_parents = num_parents;

	clk = &gck->clk;
	clk->flags = CLK_GET_RATE_NOCACHE;

	pmc_write(gck->base, gck->layout->offset,
		  (gck->id & gck->layout->pid_mask));
	pmc_read(gck->base, gck->layout->offset, &val);

	val = (val & gck->layout->gckcss_mask) >>
		(ffs(gck->layout->gckcss_mask) - 1);

	index = at91_clk_mux_val_to_index(gck->mux_table, gck->num_parents,
					  val);
	if (index < 0) {
		kfree(gck);
		return ERR_PTR(index);
	}

	ret = clk_register(clk, UBOOT_DM_CLK_AT91_GCK, name,
			   parent_names[index]);
	if (ret) {
		kfree(gck);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_gck_clk) = {
	.name = UBOOT_DM_CLK_AT91_GCK,
	.id = UCLASS_CLK,
	.ops = &gck_ops,
	.flags = DM_FLAG_PRE_RELOC,
};