summaryrefslogtreecommitdiff
path: root/drivers/clk/microchip/clk-mpfs-ccc.c
blob: 0ddc73e07be429734d32aae75f08534782b3c564 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Author: Conor Dooley <conor.dooley@microchip.com>
 *
 * Copyright (C) 2022 Microchip Technology Inc. and its subsidiaries
 */
#include "asm-generic/errno-base.h"
#include <linux/clk-provider.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <dt-bindings/clock/microchip,mpfs-clock.h>

/* address offset of control registers */
#define MPFS_CCC_PLL_CR			0x04u
#define MPFS_CCC_REF_CR			0x08u
#define MPFS_CCC_SSCG_2_CR		0x2Cu
#define MPFS_CCC_POSTDIV01_CR		0x10u
#define MPFS_CCC_POSTDIV23_CR		0x14u

#define MPFS_CCC_FBDIV_SHIFT		0x00u
#define MPFS_CCC_FBDIV_WIDTH		0x0Cu
#define MPFS_CCC_POSTDIV0_SHIFT		0x08u
#define MPFS_CCC_POSTDIV1_SHIFT		0x18u
#define MPFS_CCC_POSTDIV2_SHIFT		MPFS_CCC_POSTDIV0_SHIFT
#define MPFS_CCC_POSTDIV3_SHIFT		MPFS_CCC_POSTDIV1_SHIFT
#define MPFS_CCC_POSTDIV_WIDTH		0x06u
#define MPFS_CCC_REFCLK_SEL		BIT(6)
#define MPFS_CCC_REFDIV_SHIFT		0x08u
#define MPFS_CCC_REFDIV_WIDTH		0x06u

#define MPFS_CCC_FIXED_DIV		4
#define MPFS_CCC_OUTPUTS_PER_PLL	4
#define MPFS_CCC_REFS_PER_PLL		2

struct mpfs_ccc_data {
	void __iomem **pll_base;
	struct device *dev;
	struct clk_hw_onecell_data hw_data;
};

struct mpfs_ccc_pll_hw_clock {
	void __iomem *base;
	const char *name;
	const struct clk_parent_data *parents;
	unsigned int id;
	u32 reg_offset;
	u32 shift;
	u32 width;
	u32 flags;
	struct clk_hw hw;
	struct clk_init_data init;
};

#define to_mpfs_ccc_clk(_hw) container_of(_hw, struct mpfs_ccc_pll_hw_clock, hw)

/*
 * mpfs_ccc_lock prevents anything else from writing to a fabric ccc
 * while a software locked register is being written.
 */
static DEFINE_SPINLOCK(mpfs_ccc_lock);

static const struct clk_parent_data mpfs_ccc_pll0_refs[] = {
	{ .fw_name = "pll0_ref0" },
	{ .fw_name = "pll0_ref1" },
};

static const struct clk_parent_data mpfs_ccc_pll1_refs[] = {
	{ .fw_name = "pll1_ref0" },
	{ .fw_name = "pll1_ref1" },
};

static unsigned long mpfs_ccc_pll_recalc_rate(struct clk_hw *hw, unsigned long prate)
{
	struct mpfs_ccc_pll_hw_clock *ccc_hw = to_mpfs_ccc_clk(hw);
	void __iomem *mult_addr = ccc_hw->base + ccc_hw->reg_offset;
	void __iomem *ref_div_addr = ccc_hw->base + MPFS_CCC_REF_CR;
	u32 mult, ref_div;

	mult = readl_relaxed(mult_addr) >> MPFS_CCC_FBDIV_SHIFT;
	mult &= clk_div_mask(MPFS_CCC_FBDIV_WIDTH);
	ref_div = readl_relaxed(ref_div_addr) >> MPFS_CCC_REFDIV_SHIFT;
	ref_div &= clk_div_mask(MPFS_CCC_REFDIV_WIDTH);

	return prate * mult / (ref_div * MPFS_CCC_FIXED_DIV);
}

static u8 mpfs_ccc_pll_get_parent(struct clk_hw *hw)
{
	struct mpfs_ccc_pll_hw_clock *ccc_hw = to_mpfs_ccc_clk(hw);
	void __iomem *pll_cr_addr = ccc_hw->base + MPFS_CCC_PLL_CR;

	return !!(readl_relaxed(pll_cr_addr) & MPFS_CCC_REFCLK_SEL);
}

static const struct clk_ops mpfs_ccc_pll_ops = {
	.recalc_rate = mpfs_ccc_pll_recalc_rate,
	.get_parent = mpfs_ccc_pll_get_parent,
};

#define CLK_CCC_PLL(_id, _parents, _shift, _width, _flags, _offset) {	\
	.id = _id,							\
	.shift = _shift,						\
	.width = _width,						\
	.reg_offset = _offset,						\
	.flags = _flags,						\
	.parents = _parents,						\
}

static struct mpfs_ccc_pll_hw_clock mpfs_ccc_pll_clks[] = {
	CLK_CCC_PLL(CLK_CCC_PLL0, mpfs_ccc_pll0_refs, MPFS_CCC_FBDIV_SHIFT,
		    MPFS_CCC_FBDIV_WIDTH, 0, MPFS_CCC_SSCG_2_CR),
	CLK_CCC_PLL(CLK_CCC_PLL1, mpfs_ccc_pll1_refs, MPFS_CCC_FBDIV_SHIFT,
		    MPFS_CCC_FBDIV_WIDTH, 0, MPFS_CCC_SSCG_2_CR),
};

struct mpfs_ccc_out_hw_clock {
	struct clk_divider divider;
	struct clk_init_data init;
	unsigned int id;
	u32 reg_offset;
};

#define CLK_CCC_OUT(_id, _shift, _width, _flags, _offset) {	\
	.id = _id,						\
	.divider.shift = _shift,				\
	.divider.width = _width,				\
	.reg_offset = _offset,					\
	.divider.flags = _flags,				\
	.divider.lock = &mpfs_ccc_lock,				\
}

static struct mpfs_ccc_out_hw_clock mpfs_ccc_pll0out_clks[] = {
	CLK_CCC_OUT(CLK_CCC_PLL0_OUT0, MPFS_CCC_POSTDIV0_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV01_CR),
	CLK_CCC_OUT(CLK_CCC_PLL0_OUT1, MPFS_CCC_POSTDIV1_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV01_CR),
	CLK_CCC_OUT(CLK_CCC_PLL0_OUT2, MPFS_CCC_POSTDIV2_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV23_CR),
	CLK_CCC_OUT(CLK_CCC_PLL0_OUT3, MPFS_CCC_POSTDIV3_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV23_CR),
};

static struct mpfs_ccc_out_hw_clock mpfs_ccc_pll1out_clks[] = {
	CLK_CCC_OUT(CLK_CCC_PLL1_OUT0, MPFS_CCC_POSTDIV0_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV01_CR),
	CLK_CCC_OUT(CLK_CCC_PLL1_OUT1, MPFS_CCC_POSTDIV1_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV01_CR),
	CLK_CCC_OUT(CLK_CCC_PLL1_OUT2, MPFS_CCC_POSTDIV2_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV23_CR),
	CLK_CCC_OUT(CLK_CCC_PLL1_OUT3, MPFS_CCC_POSTDIV3_SHIFT, MPFS_CCC_POSTDIV_WIDTH,
		    CLK_DIVIDER_ONE_BASED, MPFS_CCC_POSTDIV23_CR),
};

static struct mpfs_ccc_out_hw_clock *mpfs_ccc_pllout_clks[] = {
	mpfs_ccc_pll0out_clks, mpfs_ccc_pll1out_clks
};

static int mpfs_ccc_register_outputs(struct device *dev, struct mpfs_ccc_out_hw_clock *out_hws,
				     unsigned int num_clks, struct mpfs_ccc_data *data,
				     struct mpfs_ccc_pll_hw_clock *parent)
{
	int ret;

	for (unsigned int i = 0; i < num_clks; i++) {
		struct mpfs_ccc_out_hw_clock *out_hw = &out_hws[i];
		char *name = devm_kasprintf(dev, GFP_KERNEL, "%s_out%u", parent->name, i);

		if (!name)
			return -ENOMEM;

		out_hw->divider.hw.init = CLK_HW_INIT_HW(name, &parent->hw, &clk_divider_ops, 0);
		out_hw->divider.reg = data->pll_base[i / MPFS_CCC_OUTPUTS_PER_PLL] +
			out_hw->reg_offset;

		ret = devm_clk_hw_register(dev, &out_hw->divider.hw);
		if (ret)
			return dev_err_probe(dev, ret, "failed to register clock id: %d\n",
					     out_hw->id);

		data->hw_data.hws[out_hw->id] = &out_hw->divider.hw;
	}

	return 0;
}

#define CLK_HW_INIT_PARENTS_DATA_FIXED_SIZE(_name, _parents, _ops, _flags)	\
	(&(struct clk_init_data) {						\
		.flags		= _flags,					\
		.name		= _name,					\
		.parent_data	= _parents,					\
		.num_parents	= MPFS_CCC_REFS_PER_PLL,			\
		.ops		= _ops,						\
	})

static int mpfs_ccc_register_plls(struct device *dev, struct mpfs_ccc_pll_hw_clock *pll_hws,
				  unsigned int num_clks, struct mpfs_ccc_data *data)
{
	int ret;

	for (unsigned int i = 0; i < num_clks; i++) {
		struct mpfs_ccc_pll_hw_clock *pll_hw = &pll_hws[i];

		pll_hw->name = devm_kasprintf(dev, GFP_KERNEL, "ccc%s_pll%u",
					      strchrnul(dev->of_node->full_name, '@'), i);
		if (!pll_hw->name)
			return -ENOMEM;

		pll_hw->base = data->pll_base[i];
		pll_hw->hw.init = CLK_HW_INIT_PARENTS_DATA_FIXED_SIZE(pll_hw->name,
								      pll_hw->parents,
								      &mpfs_ccc_pll_ops, 0);

		ret = devm_clk_hw_register(dev, &pll_hw->hw);
		if (ret)
			return dev_err_probe(dev, ret, "failed to register ccc id: %d\n",
					     pll_hw->id);

		data->hw_data.hws[pll_hw->id] = &pll_hw->hw;

		ret = mpfs_ccc_register_outputs(dev, mpfs_ccc_pllout_clks[i],
						MPFS_CCC_OUTPUTS_PER_PLL, data, pll_hw);
		if (ret)
			return ret;
	}

	return 0;
}

static int mpfs_ccc_probe(struct platform_device *pdev)
{
	struct mpfs_ccc_data *clk_data;
	void __iomem *pll_base[ARRAY_SIZE(mpfs_ccc_pll_clks)];
	unsigned int num_clks;
	int ret;

	num_clks = ARRAY_SIZE(mpfs_ccc_pll_clks) + ARRAY_SIZE(mpfs_ccc_pll0out_clks) +
		   ARRAY_SIZE(mpfs_ccc_pll1out_clks);

	clk_data = devm_kzalloc(&pdev->dev, struct_size(clk_data, hw_data.hws, num_clks),
				GFP_KERNEL);
	if (!clk_data)
		return -ENOMEM;

	pll_base[0] = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(pll_base[0]))
		return PTR_ERR(pll_base[0]);

	pll_base[1] = devm_platform_ioremap_resource(pdev, 1);
	if (IS_ERR(pll_base[1]))
		return PTR_ERR(pll_base[1]);

	clk_data->pll_base = pll_base;
	clk_data->hw_data.num = num_clks;
	clk_data->dev = &pdev->dev;

	ret = mpfs_ccc_register_plls(clk_data->dev, mpfs_ccc_pll_clks,
				     ARRAY_SIZE(mpfs_ccc_pll_clks), clk_data);
	if (ret)
		return ret;

	return devm_of_clk_add_hw_provider(clk_data->dev, of_clk_hw_onecell_get,
					   &clk_data->hw_data);
}

static const struct of_device_id mpfs_ccc_of_match_table[] = {
	{ .compatible = "microchip,mpfs-ccc", },
	{}
};
MODULE_DEVICE_TABLE(of, mpfs_ccc_of_match_table);

static struct platform_driver mpfs_ccc_driver = {
	.probe = mpfs_ccc_probe,
	.driver	= {
		.name = "microchip-mpfs-ccc",
		.of_match_table = mpfs_ccc_of_match_table,
	},
};

static int __init clk_ccc_init(void)
{
	return platform_driver_register(&mpfs_ccc_driver);
}
core_initcall(clk_ccc_init);

static void __exit clk_ccc_exit(void)
{
	platform_driver_unregister(&mpfs_ccc_driver);
}
module_exit(clk_ccc_exit);

MODULE_DESCRIPTION("Microchip PolarFire SoC Clock Conditioning Circuitry Driver");
MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
MODULE_LICENSE("GPL");