summaryrefslogtreecommitdiff
path: root/drivers/clk/at91/clk-peripheral.c
blob: 08d7e7dddc9e5f769bc1200fd41214bc6daec130 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: GPL-2.0+
/*
 * Peripheral 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-peripheral.c from Linux.
 */
#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_PERIPH		"at91-periph-clk"
#define UBOOT_DM_CLK_AT91_SAM9X5_PERIPH		"at91-sam9x5-periph-clk"

#define PERIPHERAL_ID_MIN	2
#define PERIPHERAL_ID_MAX	31
#define PERIPHERAL_MASK(id)	(1 << ((id) & PERIPHERAL_ID_MAX))

#define PERIPHERAL_MAX_SHIFT	3

struct clk_peripheral {
	void __iomem *base;
	struct clk clk;
	u32 id;
};

#define to_clk_peripheral(_c) container_of(_c, struct clk_peripheral, clk)

struct clk_sam9x5_peripheral {
	const struct clk_pcr_layout *layout;
	void __iomem *base;
	struct clk clk;
	struct clk_range range;
	u32 id;
	u32 div;
	bool auto_div;
};

#define to_clk_sam9x5_peripheral(_c) \
	container_of(_c, struct clk_sam9x5_peripheral, clk)

static int clk_peripheral_enable(struct clk *clk)
{
	struct clk_peripheral *periph = to_clk_peripheral(clk);
	int offset = AT91_PMC_PCER;
	u32 id = periph->id;

	if (id < PERIPHERAL_ID_MIN)
		return 0;
	if (id > PERIPHERAL_ID_MAX)
		offset = AT91_PMC_PCER1;
	pmc_write(periph->base, offset, PERIPHERAL_MASK(id));

	return 0;
}

static int clk_peripheral_disable(struct clk *clk)
{
	struct clk_peripheral *periph = to_clk_peripheral(clk);
	int offset = AT91_PMC_PCDR;
	u32 id = periph->id;

	if (id < PERIPHERAL_ID_MIN)
		return -EINVAL;

	if (id > PERIPHERAL_ID_MAX)
		offset = AT91_PMC_PCDR1;
	pmc_write(periph->base, offset, PERIPHERAL_MASK(id));

	return 0;
}

static const struct clk_ops peripheral_ops = {
	.enable = clk_peripheral_enable,
	.disable = clk_peripheral_disable,
	.get_rate = clk_generic_get_rate,
};

struct clk *
at91_clk_register_peripheral(void __iomem *base, const char *name,
			     const char *parent_name, u32 id)
{
	struct clk_peripheral *periph;
	struct clk *clk;
	int ret;

	if (!base || !name || !parent_name || id > PERIPHERAL_ID_MAX)
		return ERR_PTR(-EINVAL);

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

	periph->id = id;
	periph->base = base;

	clk = &periph->clk;
	clk->flags = CLK_GET_RATE_NOCACHE;
	ret = clk_register(clk, UBOOT_DM_CLK_AT91_PERIPH, name, parent_name);
	if (ret) {
		kfree(periph);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_periph_clk) = {
	.name = UBOOT_DM_CLK_AT91_PERIPH,
	.id = UCLASS_CLK,
	.ops = &peripheral_ops,
	.flags = DM_FLAG_PRE_RELOC,
};

static int clk_sam9x5_peripheral_enable(struct clk *clk)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(clk);

	if (periph->id < PERIPHERAL_ID_MIN)
		return 0;

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

	return 0;
}

static int clk_sam9x5_peripheral_disable(struct clk *clk)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(clk);

	if (periph->id < PERIPHERAL_ID_MIN)
		return -EINVAL;

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

	return 0;
}

static ulong clk_sam9x5_peripheral_get_rate(struct clk *clk)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(clk);
	ulong parent_rate = clk_get_parent_rate(clk);
	u32 val, shift = ffs(periph->layout->div_mask) - 1;

	if (!parent_rate)
		return 0;

	pmc_write(periph->base, periph->layout->offset,
		  (periph->id & periph->layout->pid_mask));
	pmc_read(periph->base, periph->layout->offset, &val);
	shift = (val & periph->layout->div_mask) >> shift;

	return parent_rate >> shift;
}

static ulong clk_sam9x5_peripheral_set_rate(struct clk *clk, ulong rate)
{
	struct clk_sam9x5_peripheral *periph = to_clk_sam9x5_peripheral(clk);
	ulong parent_rate = clk_get_parent_rate(clk);
	int shift;

	if (!parent_rate)
		return 0;

	if (periph->id < PERIPHERAL_ID_MIN || !periph->range.max) {
		if (parent_rate == rate)
			return rate;
		else
			return 0;
	}

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

	for (shift = 0; shift <= PERIPHERAL_MAX_SHIFT; shift++) {
		if (parent_rate >> shift <= rate)
			break;
	}
	if (shift == PERIPHERAL_MAX_SHIFT + 1)
		return 0;

	pmc_write(periph->base, periph->layout->offset,
		  (periph->id & periph->layout->pid_mask));
	pmc_update_bits(periph->base, periph->layout->offset,
			periph->layout->div_mask | periph->layout->cmd,
			(shift << (ffs(periph->layout->div_mask) - 1)) |
			periph->layout->cmd);

	return parent_rate >> shift;
}

static const struct clk_ops sam9x5_peripheral_ops = {
	.enable = clk_sam9x5_peripheral_enable,
	.disable = clk_sam9x5_peripheral_disable,
	.get_rate = clk_sam9x5_peripheral_get_rate,
	.set_rate = clk_sam9x5_peripheral_set_rate,
};

struct clk *
at91_clk_register_sam9x5_peripheral(void __iomem *base,
				    const struct clk_pcr_layout *layout,
				    const char *name, const char *parent_name,
				    u32 id, const struct clk_range *range)
{
	struct clk_sam9x5_peripheral *periph;
	struct clk *clk;
	int ret;

	if (!base || !layout || !name || !parent_name || !range)
		return ERR_PTR(-EINVAL);

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

	periph->id = id;
	periph->base = base;
	periph->layout = layout;
	periph->range = *range;

	clk = &periph->clk;
	clk->flags = CLK_GET_RATE_NOCACHE;
	ret = clk_register(clk, UBOOT_DM_CLK_AT91_SAM9X5_PERIPH, name,
			   parent_name);
	if (ret) {
		kfree(periph);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_sam9x5_periph_clk) = {
	.name = UBOOT_DM_CLK_AT91_SAM9X5_PERIPH,
	.id = UCLASS_CLK,
	.ops = &sam9x5_peripheral_ops,
	.flags = DM_FLAG_PRE_RELOC,
};