~funderscore blog cgit wiki get in touch
aboutsummaryrefslogtreecommitdiff
blob: b52d926f3390cbc69efa6f8d1d87e6a77bcd830b (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// SPDX-License-Identifier: GPL-2.0+
/*
 * Main 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-main.c from Linux.
 */

#include <asm/processor.h>
#include <common.h>
#include <clk-uclass.h>
#include <dm.h>
#include <linux/clk-provider.h>
#include <linux/clk/at91_pmc.h>
#include <linux/delay.h>
#include <linux/io.h>
#include "pmc.h"

#define UBOOT_DM_CLK_AT91_MAIN_RC		"at91-main-rc-clk"
#define UBOOT_DM_CLK_AT91_MAIN_OSC		"at91-main-osc-clk"
#define UBOOT_DM_CLK_AT91_RM9200_MAIN		"at91-rm9200-main-clk"
#define UBOOT_DM_CLK_AT91_SAM9X5_MAIN		"at91-sam9x5-main-clk"

#define MOR_KEY_MASK		GENMASK(23, 16)
#define USEC_PER_SEC		1000000UL
#define SLOW_CLOCK_FREQ		32768

#define clk_main_parent_select(s)	(((s) & \
					(AT91_PMC_MOSCEN | \
					AT91_PMC_OSCBYPASS)) ? 1 : 0)

struct clk_main_rc {
	void __iomem	*reg;
	struct clk	clk;
};

#define to_clk_main_rc(_clk) container_of(_clk, struct clk_main_rc, clk)

struct clk_main_osc {
	void __iomem	*reg;
	struct clk	clk;
};

#define to_clk_main_osc(_clk) container_of(_clk, struct clk_main_osc, clk)

struct clk_main {
	void __iomem		*reg;
	const unsigned int	*clk_mux_table;
	const char * const	*parent_names;
	unsigned int		num_parents;
	int			type;
	struct clk		clk;
};

#define to_clk_main(_clk) container_of(_clk, struct clk_main, clk)

static int main_rc_enable(struct clk *clk)
{
	struct clk_main_rc *main_rc = to_clk_main_rc(clk);
	void __iomem *reg = main_rc->reg;
	unsigned int val;

	pmc_read(reg, AT91_CKGR_MOR, &val);

	if (!(val & AT91_PMC_MOSCRCEN)) {
		pmc_update_bits(reg, AT91_CKGR_MOR,
				MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
				AT91_PMC_KEY | AT91_PMC_MOSCRCEN);
	}

	pmc_read(reg, AT91_PMC_SR, &val);
	while (!(val & AT91_PMC_MOSCRCS)) {
		pmc_read(reg, AT91_PMC_SR, &val);
		debug("waiting for main rc...\n");
		cpu_relax();
	}

	return 0;
}

static int main_rc_disable(struct clk *clk)
{
	struct clk_main_rc *main_rc = to_clk_main_rc(clk);
	struct reg *reg = main_rc->reg;
	unsigned int val;

	pmc_read(reg, AT91_CKGR_MOR, &val);

	if (!(val & AT91_PMC_MOSCRCEN))
		return 0;

	pmc_update_bits(reg, AT91_CKGR_MOR, MOR_KEY_MASK | AT91_PMC_MOSCRCEN,
			AT91_PMC_KEY);

	return 0;
}

static const struct clk_ops main_rc_clk_ops = {
	.enable = main_rc_enable,
	.disable = main_rc_disable,
	.get_rate = clk_generic_get_rate,
};

struct clk *at91_clk_main_rc(void __iomem *reg, const char *name,
			     const char *parent_name)
{
	struct clk_main_rc *main_rc;
	struct clk *clk;
	int ret;

	if (!reg || !name || !parent_name)
		return ERR_PTR(-EINVAL);

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

	main_rc->reg = reg;
	clk = &main_rc->clk;

	ret = clk_register(clk, UBOOT_DM_CLK_AT91_MAIN_RC, name,
			   parent_name);
	if (ret) {
		kfree(main_rc);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_main_rc_clk) = {
	.name = UBOOT_DM_CLK_AT91_MAIN_RC,
	.id = UCLASS_CLK,
	.ops = &main_rc_clk_ops,
	.flags = DM_FLAG_PRE_RELOC,
};

static int clk_main_osc_enable(struct clk *clk)
{
	struct clk_main_osc *main = to_clk_main_osc(clk);
	void __iomem *reg = main->reg;
	unsigned int val;

	pmc_read(reg, AT91_CKGR_MOR, &val);
	val &= ~MOR_KEY_MASK;

	if (val & AT91_PMC_OSCBYPASS)
		return 0;

	if (!(val & AT91_PMC_MOSCEN)) {
		val |= AT91_PMC_MOSCEN | AT91_PMC_KEY;
		pmc_write(reg, AT91_CKGR_MOR, val);
	}

	pmc_read(reg, AT91_PMC_SR, &val);
	while (!(val & AT91_PMC_MOSCS)) {
		pmc_read(reg, AT91_PMC_SR, &val);
		debug("waiting for main osc..\n");
		cpu_relax();
	}

	return 0;
}

static int clk_main_osc_disable(struct clk *clk)
{
	struct clk_main_osc *main = to_clk_main_osc(clk);
	void __iomem *reg = main->reg;
	unsigned int val;

	pmc_read(reg, AT91_CKGR_MOR, &val);
	if (val & AT91_PMC_OSCBYPASS)
		return 0;

	if (!(val & AT91_PMC_MOSCEN))
		return 0;

	val &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN);
	pmc_write(reg, AT91_CKGR_MOR, val | AT91_PMC_KEY);

	return 0;
}

static const struct clk_ops main_osc_clk_ops = {
	.enable = clk_main_osc_enable,
	.disable = clk_main_osc_disable,
	.get_rate = clk_generic_get_rate,
};

struct clk *at91_clk_main_osc(void __iomem *reg, const char *name,
			      const char *parent_name, bool bypass)
{
	struct clk_main_osc *main;
	struct clk *clk;
	int ret;

	if (!reg || !name || !parent_name)
		return ERR_PTR(-EINVAL);

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

	main->reg = reg;
	clk = &main->clk;

	if (bypass) {
		pmc_update_bits(reg, AT91_CKGR_MOR,
				MOR_KEY_MASK | AT91_PMC_OSCBYPASS,
				AT91_PMC_KEY | AT91_PMC_OSCBYPASS);
	}

	ret = clk_register(clk, UBOOT_DM_CLK_AT91_MAIN_OSC, name, parent_name);
	if (ret) {
		kfree(main);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_main_osc_clk) = {
	.name = UBOOT_DM_CLK_AT91_MAIN_OSC,
	.id = UCLASS_CLK,
	.ops = &main_osc_clk_ops,
	.flags = DM_FLAG_PRE_RELOC,
};

static int clk_main_probe_frequency(void __iomem *reg)
{
	unsigned int cycles = 16;
	unsigned int cycle = DIV_ROUND_UP(USEC_PER_SEC, SLOW_CLOCK_FREQ);
	unsigned int mcfr;

	while (cycles--) {
		pmc_read(reg, AT91_CKGR_MCFR, &mcfr);
		if (mcfr & AT91_PMC_MAINRDY)
			return 0;
		udelay(cycle);
	}

	return -ETIMEDOUT;
}

static int clk_rm9200_main_enable(struct clk *clk)
{
	struct clk_main *main = to_clk_main(clk);

	return clk_main_probe_frequency(main->reg);
}

static const struct clk_ops rm9200_main_clk_ops = {
	.enable = clk_rm9200_main_enable,
};

struct clk *at91_clk_rm9200_main(void __iomem *reg, const char *name,
				 const char *parent_name)
{
	struct clk_main *main;
	struct clk *clk;
	int ret;

	if (!reg || !name || !parent_name)
		return ERR_PTR(-EINVAL);

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

	main->reg = reg;
	clk = &main->clk;

	ret = clk_register(clk, UBOOT_DM_CLK_AT91_RM9200_MAIN, name,
			   parent_name);
	if (ret) {
		kfree(main);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_rm9200_main_clk) = {
	.name = UBOOT_DM_CLK_AT91_RM9200_MAIN,
	.id = UCLASS_CLK,
	.ops = &rm9200_main_clk_ops,
	.flags = DM_FLAG_PRE_RELOC,
};

static inline bool clk_sam9x5_main_ready(void __iomem *reg)
{
	unsigned int val;

	pmc_read(reg, AT91_PMC_SR, &val);

	return !!(val & AT91_PMC_MOSCSELS);
}

static int clk_sam9x5_main_enable(struct clk *clk)
{
	struct clk_main *main = to_clk_main(clk);
	void __iomem *reg = main->reg;

	while (!clk_sam9x5_main_ready(reg)) {
		debug("waiting for main...");
		cpu_relax();
	}

	return clk_main_probe_frequency(reg);
}

static int clk_sam9x5_main_set_parent(struct clk *clk, struct clk *parent)
{
	struct clk_main *main = to_clk_main(clk);
	void __iomem *reg = main->reg;
	unsigned int tmp, index;

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

	pmc_read(reg, AT91_CKGR_MOR, &tmp);
	tmp &= ~MOR_KEY_MASK;
	tmp |= AT91_PMC_KEY;

	if (index && !(tmp & AT91_PMC_MOSCSEL))
		pmc_write(reg, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL);
	else if (!index && (tmp & AT91_PMC_MOSCSEL))
		pmc_write(reg, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL);

	while (!clk_sam9x5_main_ready(reg))
		cpu_relax();

	return 0;
}

static const struct clk_ops sam9x5_main_clk_ops = {
	.enable = clk_sam9x5_main_enable,
	.set_parent = clk_sam9x5_main_set_parent,
	.get_rate = clk_generic_get_rate,
};

struct clk *at91_clk_sam9x5_main(void __iomem *reg, const char *name,
				 const char * const *parent_names,
				 int num_parents, const u32 *clk_mux_table,
				 int type)
{
	struct clk *clk = ERR_PTR(-ENOMEM);
	struct clk_main *main = NULL;
	unsigned int val;
	int ret;

	if (!reg || !name || !parent_names || !num_parents || !clk_mux_table)
		return ERR_PTR(-EINVAL);

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

	main->reg = reg;
	main->parent_names = parent_names;
	main->num_parents = num_parents;
	main->clk_mux_table = clk_mux_table;
	main->type = type;
	clk = &main->clk;
	clk->flags = CLK_GET_RATE_NOCACHE;
	pmc_read(reg, AT91_CKGR_MOR, &val);
	ret = clk_register(clk, UBOOT_DM_CLK_AT91_SAM9X5_MAIN, name,
			   main->parent_names[clk_main_parent_select(val)]);
	if (ret) {
		kfree(main);
		clk = ERR_PTR(ret);
	}

	return clk;
}

U_BOOT_DRIVER(at91_sam9x5_main_clk) = {
	.name = UBOOT_DM_CLK_AT91_SAM9X5_MAIN,
	.id = UCLASS_CLK,
	.ops = &sam9x5_main_clk_ops,
	.flags = DM_FLAG_PRE_RELOC,
};