summaryrefslogtreecommitdiff
path: root/drivers/clk/altera/clk-arria10.c
blob: b7eed948a518164e5695a340818a47c1b39accc0 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018 Marek Vasut <marex@denx.de>
 */

#include <common.h>
#include <malloc.h>
#include <asm/io.h>
#include <clk-uclass.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/devres.h>
#include <dm/lists.h>
#include <dm/util.h>

#include <asm/arch/clock_manager.h>

enum socfpga_a10_clk_type {
	SOCFPGA_A10_CLK_MAIN_PLL,
	SOCFPGA_A10_CLK_PER_PLL,
	SOCFPGA_A10_CLK_PERIP_CLK,
	SOCFPGA_A10_CLK_GATE_CLK,
	SOCFPGA_A10_CLK_UNKNOWN_CLK,
};

struct socfpga_a10_clk_platdata {
	enum socfpga_a10_clk_type type;
	struct clk_bulk	clks;
	u32		regs;
	/* Fixed divider */
	u16		fix_div;
	/* Control register */
	u16		ctl_reg;
	/* Divider register */
	u16		div_reg;
	u8		div_len;
	u8		div_off;
	/* Clock gating register */
	u16		gate_reg;
	u8		gate_bit;
};

static int socfpga_a10_clk_get_upstream(struct clk *clk, struct clk **upclk)
{
	struct socfpga_a10_clk_platdata *plat = dev_get_platdata(clk->dev);
	u32 reg, maxval;

	if (plat->clks.count == 0)
		return 0;

	if (plat->clks.count == 1) {
		*upclk = &plat->clks.clks[0];
		return 0;
	}

	if (!plat->ctl_reg) {
		dev_err(clk->dev, "Invalid control register\n");
		return -EINVAL;
	}

	reg = readl(plat->regs + plat->ctl_reg);

	/* Assume PLLs are ON for now */
	if (plat->type == SOCFPGA_A10_CLK_MAIN_PLL) {
		reg = (reg >> 8) & 0x3;
		maxval = 2;
	} else if (plat->type == SOCFPGA_A10_CLK_PER_PLL) {
		reg = (reg >> 8) & 0x3;
		maxval = 3;
	} else {
		reg = (reg >> 16) & 0x7;
		maxval = 4;
	}

	if (reg > maxval) {
		dev_err(clk->dev, "Invalid clock source\n");
		return -EINVAL;
	}

	*upclk = &plat->clks.clks[reg];
	return 0;
}

static int socfpga_a10_clk_endisable(struct clk *clk, bool enable)
{
	struct socfpga_a10_clk_platdata *plat = dev_get_platdata(clk->dev);
	struct clk *upclk = NULL;
	int ret;

	if (!enable && plat->gate_reg)
		clrbits_le32(plat->regs + plat->gate_reg, BIT(plat->gate_bit));

	ret = socfpga_a10_clk_get_upstream(clk, &upclk);
	if (ret)
		return ret;

	if (upclk) {
		if (enable)
			clk_enable(upclk);
		else
			clk_disable(upclk);
	}

	if (enable && plat->gate_reg)
		setbits_le32(plat->regs + plat->gate_reg, BIT(plat->gate_bit));

	return 0;
}

static int socfpga_a10_clk_enable(struct clk *clk)
{
	return socfpga_a10_clk_endisable(clk, true);
}

static int socfpga_a10_clk_disable(struct clk *clk)
{
	return socfpga_a10_clk_endisable(clk, false);
}

static ulong socfpga_a10_clk_get_rate(struct clk *clk)
{
	struct socfpga_a10_clk_platdata *plat = dev_get_platdata(clk->dev);
	struct clk *upclk = NULL;
	ulong rate = 0, reg, numer, denom;
	int ret;

	ret = socfpga_a10_clk_get_upstream(clk, &upclk);
	if (ret || !upclk)
		return 0;

	rate = clk_get_rate(upclk);

	if (plat->type == SOCFPGA_A10_CLK_MAIN_PLL) {
		reg = readl(plat->regs + plat->ctl_reg + 4);	/* VCO1 */
		numer = reg & CLKMGR_MAINPLL_VCO1_NUMER_MSK;
		denom = (reg >> CLKMGR_MAINPLL_VCO1_DENOM_LSB) &
			CLKMGR_MAINPLL_VCO1_DENOM_MSK;

		rate /= denom + 1;
		rate *= numer + 1;
	} else if (plat->type == SOCFPGA_A10_CLK_PER_PLL) {
		reg = readl(plat->regs + plat->ctl_reg + 4);	/* VCO1 */
		numer = reg & CLKMGR_PERPLL_VCO1_NUMER_MSK;
		denom = (reg >> CLKMGR_PERPLL_VCO1_DENOM_LSB) &
			CLKMGR_PERPLL_VCO1_DENOM_MSK;

		rate /= denom + 1;
		rate *= numer + 1;
	} else {
		rate /= plat->fix_div;

		if (plat->fix_div == 1 && plat->ctl_reg) {
			reg = readl(plat->regs + plat->ctl_reg);
			reg &= 0x7ff;
			rate /= reg + 1;
		}

		if (plat->div_reg) {
			reg = readl(plat->regs + plat->div_reg);
			reg >>= plat->div_off;
			reg &= (1 << plat->div_len) - 1;
			if (plat->type == SOCFPGA_A10_CLK_PERIP_CLK)
				rate /= reg + 1;
			if (plat->type == SOCFPGA_A10_CLK_GATE_CLK)
				rate /= 1 << reg;
		}
	}

	return rate;
}

static struct clk_ops socfpga_a10_clk_ops = {
	.enable		= socfpga_a10_clk_enable,
	.disable	= socfpga_a10_clk_disable,
	.get_rate	= socfpga_a10_clk_get_rate,
};

/*
 * This workaround tries to fix the massively broken generated "handoff" DT,
 * which contains duplicate clock nodes without any connection to the clock
 * manager DT node. Yet, those "handoff" DT nodes contain configuration of
 * the fixed input clock of the Arria10 which are missing from the base DT
 * for Arria10.
 *
 * This workaround sets up upstream clock for the fixed input clocks of the
 * A10 described in the base DT such that they map to the fixed clock from
 * the "handoff" DT. This does not fully match how the clock look on the
 * A10, but it is the least intrusive way to fix this mess.
 */
static void socfpga_a10_handoff_workaround(struct udevice *dev)
{
	struct socfpga_a10_clk_platdata *plat = dev_get_platdata(dev);
	const void *fdt = gd->fdt_blob;
	struct clk_bulk	*bulk = &plat->clks;
	int i, ret, offset = dev_of_offset(dev);
	static const char * const socfpga_a10_fixedclk_map[] = {
		"osc1", "altera_arria10_hps_eosc1",
		"cb_intosc_ls_clk", "altera_arria10_hps_cb_intosc_ls",
		"f2s_free_clk", "altera_arria10_hps_f2h_free",
	};

	if (fdt_node_check_compatible(fdt, offset, "fixed-clock"))
		return;

	for (i = 0; i < ARRAY_SIZE(socfpga_a10_fixedclk_map); i += 2)
		if (!strcmp(dev->name, socfpga_a10_fixedclk_map[i]))
			break;

	if (i == ARRAY_SIZE(socfpga_a10_fixedclk_map))
		return;

	ret = uclass_get_device_by_name(UCLASS_CLK,
					socfpga_a10_fixedclk_map[i + 1], &dev);
	if (ret)
		return;

	bulk->count = 1;
	bulk->clks = devm_kcalloc(dev, bulk->count,
				  sizeof(struct clk), GFP_KERNEL);
	if (!bulk->clks)
		return;

	ret = clk_request(dev, &bulk->clks[0]);
	if (ret)
		free(bulk->clks);
}

static int socfpga_a10_clk_bind(struct udevice *dev)
{
	const void *fdt = gd->fdt_blob;
	int offset = dev_of_offset(dev);
	bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
	const char *name;
	int ret;

	for (offset = fdt_first_subnode(fdt, offset);
	     offset > 0;
	     offset = fdt_next_subnode(fdt, offset)) {
		name = fdt_get_name(fdt, offset, NULL);
		if (!name)
			return -EINVAL;

		if (!strcmp(name, "clocks")) {
			offset = fdt_first_subnode(fdt, offset);
			name = fdt_get_name(fdt, offset, NULL);
			if (!name)
				return -EINVAL;
		}

		/* Filter out supported sub-clock */
		if (fdt_node_check_compatible(fdt, offset,
					      "altr,socfpga-a10-pll-clock") &&
		    fdt_node_check_compatible(fdt, offset,
					      "altr,socfpga-a10-perip-clk") &&
		    fdt_node_check_compatible(fdt, offset,
					      "altr,socfpga-a10-gate-clk") &&
		    fdt_node_check_compatible(fdt, offset, "fixed-clock"))
			continue;

		if (pre_reloc_only &&
		    !dm_ofnode_pre_reloc(offset_to_ofnode(offset)))
			continue;

		ret = device_bind_driver_to_node(dev, "clk-a10", name,
						 offset_to_ofnode(offset),
						 NULL);
		if (ret)
			return ret;
	}

	return 0;
}

static int socfpga_a10_clk_probe(struct udevice *dev)
{
	struct socfpga_a10_clk_platdata *plat = dev_get_platdata(dev);
	struct socfpga_a10_clk_platdata *pplat;
	struct udevice *pdev;
	const void *fdt = gd->fdt_blob;
	int offset = dev_of_offset(dev);

	clk_get_bulk(dev, &plat->clks);

	socfpga_a10_handoff_workaround(dev);

	if (!fdt_node_check_compatible(fdt, offset, "altr,clk-mgr")) {
		plat->regs = devfdt_get_addr(dev);
	} else {
		pdev = dev_get_parent(dev);
		if (!pdev)
			return -ENODEV;

		pplat = dev_get_platdata(pdev);
		if (!pplat)
			return -EINVAL;

		plat->ctl_reg = dev_read_u32_default(dev, "reg", 0x0);
		plat->regs = pplat->regs;
	}

	if (!fdt_node_check_compatible(fdt, offset,
				       "altr,socfpga-a10-pll-clock")) {
		/* Main PLL has 3 upstream clock */
		if (plat->clks.count == 3)
			plat->type = SOCFPGA_A10_CLK_MAIN_PLL;
		else
			plat->type = SOCFPGA_A10_CLK_PER_PLL;
	} else if (!fdt_node_check_compatible(fdt, offset,
					      "altr,socfpga-a10-perip-clk")) {
		plat->type = SOCFPGA_A10_CLK_PERIP_CLK;
	} else if (!fdt_node_check_compatible(fdt, offset,
					      "altr,socfpga-a10-gate-clk")) {
		plat->type = SOCFPGA_A10_CLK_GATE_CLK;
	} else {
		plat->type = SOCFPGA_A10_CLK_UNKNOWN_CLK;
	}

	return 0;
}

static int socfpga_a10_ofdata_to_platdata(struct udevice *dev)
{
	struct socfpga_a10_clk_platdata *plat = dev_get_platdata(dev);
	unsigned int divreg[3], gatereg[2];
	int ret;

	plat->type = SOCFPGA_A10_CLK_UNKNOWN_CLK;

	plat->fix_div = dev_read_u32_default(dev, "fixed-divider", 1);

	ret = dev_read_u32_array(dev, "div-reg", divreg, ARRAY_SIZE(divreg));
	if (!ret) {
		plat->div_reg = divreg[0];
		plat->div_len = divreg[2];
		plat->div_off = divreg[1];
	}

	ret = dev_read_u32_array(dev, "clk-gate", gatereg, ARRAY_SIZE(gatereg));
	if (!ret) {
		plat->gate_reg = gatereg[0];
		plat->gate_bit = gatereg[1];
	}

	return 0;
}

static const struct udevice_id socfpga_a10_clk_match[] = {
	{ .compatible = "altr,clk-mgr" },
	{}
};

U_BOOT_DRIVER(socfpga_a10_clk) = {
	.name		= "clk-a10",
	.id		= UCLASS_CLK,
	.of_match	= socfpga_a10_clk_match,
	.ops		= &socfpga_a10_clk_ops,
	.bind		= socfpga_a10_clk_bind,
	.probe		= socfpga_a10_clk_probe,
	.ofdata_to_platdata = socfpga_a10_ofdata_to_platdata,

	.platdata_auto_alloc_size = sizeof(struct socfpga_a10_clk_platdata),
};