1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2012-2013 Analog Devices Inc.
6 * Author: Lars-Peter Clausen <lars@metafoo.de>
9 #include <linux/platform_device.h>
10 #include <linux/clk-provider.h>
11 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/err.h>
17 #define AXI_CLKGEN_V2_REG_RESET 0x40
18 #define AXI_CLKGEN_V2_REG_CLKSEL 0x44
19 #define AXI_CLKGEN_V2_REG_DRP_CNTRL 0x70
20 #define AXI_CLKGEN_V2_REG_DRP_STATUS 0x74
22 #define AXI_CLKGEN_V2_RESET_MMCM_ENABLE BIT(1)
23 #define AXI_CLKGEN_V2_RESET_ENABLE BIT(0)
25 #define AXI_CLKGEN_V2_DRP_CNTRL_SEL BIT(29)
26 #define AXI_CLKGEN_V2_DRP_CNTRL_READ BIT(28)
28 #define AXI_CLKGEN_V2_DRP_STATUS_BUSY BIT(16)
30 #define MMCM_REG_CLKOUT5_2 0x07
31 #define MMCM_REG_CLKOUT0_1 0x08
32 #define MMCM_REG_CLKOUT0_2 0x09
33 #define MMCM_REG_CLKOUT6_2 0x13
34 #define MMCM_REG_CLK_FB1 0x14
35 #define MMCM_REG_CLK_FB2 0x15
36 #define MMCM_REG_CLK_DIV 0x16
37 #define MMCM_REG_LOCK1 0x18
38 #define MMCM_REG_LOCK2 0x19
39 #define MMCM_REG_LOCK3 0x1a
40 #define MMCM_REG_POWER 0x28
41 #define MMCM_REG_FILTER1 0x4e
42 #define MMCM_REG_FILTER2 0x4f
44 #define MMCM_CLKOUT_NOCOUNT BIT(6)
46 #define MMCM_CLK_DIV_DIVIDE BIT(11)
47 #define MMCM_CLK_DIV_NOCOUNT BIT(12)
49 struct axi_clkgen_limits {
50 unsigned int fpfd_min;
51 unsigned int fpfd_max;
52 unsigned int fvco_min;
53 unsigned int fvco_max;
59 struct axi_clkgen_limits limits;
62 static uint32_t axi_clkgen_lookup_filter(unsigned int m)
92 static const uint32_t axi_clkgen_lock_table[] = {
93 0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8,
94 0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8,
95 0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339,
96 0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271,
97 0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4,
98 0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190,
99 0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e,
100 0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c,
101 0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113,
104 static uint32_t axi_clkgen_lookup_lock(unsigned int m)
106 if (m < ARRAY_SIZE(axi_clkgen_lock_table))
107 return axi_clkgen_lock_table[m];
111 static const struct axi_clkgen_limits axi_clkgen_zynqmp_default_limits = {
118 static const struct axi_clkgen_limits axi_clkgen_zynq_default_limits = {
125 static void axi_clkgen_calc_params(const struct axi_clkgen_limits *limits,
126 unsigned long fin, unsigned long fout,
127 unsigned int *best_d, unsigned int *best_m, unsigned int *best_dout)
129 unsigned long d, d_min, d_max, _d_min, _d_max;
130 unsigned long m, m_min, m_max;
131 unsigned long f, dout, best_f, fvco;
132 unsigned long fract_shift = 0;
133 unsigned long fvco_min_fract, fvco_max_fract;
143 d_min = max_t(unsigned long, DIV_ROUND_UP(fin, limits->fpfd_max), 1);
144 d_max = min_t(unsigned long, fin / limits->fpfd_min, 80);
147 fvco_min_fract = limits->fvco_min << fract_shift;
148 fvco_max_fract = limits->fvco_max << fract_shift;
150 m_min = max_t(unsigned long, DIV_ROUND_UP(fvco_min_fract, fin) * d_min, 1);
151 m_max = min_t(unsigned long, fvco_max_fract * d_max / fin, 64 << fract_shift);
153 for (m = m_min; m <= m_max; m++) {
154 _d_min = max(d_min, DIV_ROUND_UP(fin * m, fvco_max_fract));
155 _d_max = min(d_max, fin * m / fvco_min_fract);
157 for (d = _d_min; d <= _d_max; d++) {
160 dout = DIV_ROUND_CLOSEST(fvco, fout);
161 dout = clamp_t(unsigned long, dout, 1, 128 << fract_shift);
163 if (abs(f - fout) < abs(best_f - fout)) {
166 *best_m = m << (3 - fract_shift);
167 *best_dout = dout << (3 - fract_shift);
174 /* Lets see if we find a better setting in fractional mode */
175 if (fract_shift == 0) {
181 struct axi_clkgen_div_params {
185 unsigned int nocount;
186 unsigned int frac_en;
188 unsigned int frac_wf_f;
189 unsigned int frac_wf_r;
190 unsigned int frac_phase;
193 static void axi_clkgen_calc_clk_params(unsigned int divider,
194 unsigned int frac_divider, struct axi_clkgen_div_params *params)
197 memset(params, 0x0, sizeof(*params));
204 if (frac_divider == 0) {
205 params->high = divider / 2;
206 params->edge = divider % 2;
207 params->low = divider - params->high;
210 params->frac = frac_divider;
212 params->high = divider / 2;
213 params->edge = divider % 2;
214 params->low = params->high;
216 if (params->edge == 0) {
218 params->frac_wf_r = 1;
221 if (params->edge == 0 || frac_divider == 1)
223 if (((params->edge == 0) ^ (frac_divider == 1)) ||
224 (divider == 2 && frac_divider == 1))
225 params->frac_wf_f = 1;
227 params->frac_phase = params->edge * 4 + frac_divider / 2;
231 static void axi_clkgen_write(struct axi_clkgen *axi_clkgen,
232 unsigned int reg, unsigned int val)
234 writel(val, axi_clkgen->base + reg);
237 static void axi_clkgen_read(struct axi_clkgen *axi_clkgen,
238 unsigned int reg, unsigned int *val)
240 *val = readl(axi_clkgen->base + reg);
243 static int axi_clkgen_wait_non_busy(struct axi_clkgen *axi_clkgen)
245 unsigned int timeout = 10000;
249 axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_STATUS, &val);
250 } while ((val & AXI_CLKGEN_V2_DRP_STATUS_BUSY) && --timeout);
252 if (val & AXI_CLKGEN_V2_DRP_STATUS_BUSY)
258 static int axi_clkgen_mmcm_read(struct axi_clkgen *axi_clkgen,
259 unsigned int reg, unsigned int *val)
261 unsigned int reg_val;
264 ret = axi_clkgen_wait_non_busy(axi_clkgen);
268 reg_val = AXI_CLKGEN_V2_DRP_CNTRL_SEL | AXI_CLKGEN_V2_DRP_CNTRL_READ;
269 reg_val |= (reg << 16);
271 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
273 ret = axi_clkgen_wait_non_busy(axi_clkgen);
282 static int axi_clkgen_mmcm_write(struct axi_clkgen *axi_clkgen,
283 unsigned int reg, unsigned int val, unsigned int mask)
285 unsigned int reg_val = 0;
288 ret = axi_clkgen_wait_non_busy(axi_clkgen);
292 if (mask != 0xffff) {
293 axi_clkgen_mmcm_read(axi_clkgen, reg, ®_val);
297 reg_val |= AXI_CLKGEN_V2_DRP_CNTRL_SEL | (reg << 16) | (val & mask);
299 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
304 static void axi_clkgen_mmcm_enable(struct axi_clkgen *axi_clkgen,
307 unsigned int val = AXI_CLKGEN_V2_RESET_ENABLE;
310 val |= AXI_CLKGEN_V2_RESET_MMCM_ENABLE;
312 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_RESET, val);
315 static struct axi_clkgen *clk_hw_to_axi_clkgen(struct clk_hw *clk_hw)
317 return container_of(clk_hw, struct axi_clkgen, clk_hw);
320 static void axi_clkgen_set_div(struct axi_clkgen *axi_clkgen,
321 unsigned int reg1, unsigned int reg2, unsigned int reg3,
322 struct axi_clkgen_div_params *params)
324 axi_clkgen_mmcm_write(axi_clkgen, reg1,
325 (params->high << 6) | params->low, 0xefff);
326 axi_clkgen_mmcm_write(axi_clkgen, reg2,
327 (params->frac << 12) | (params->frac_en << 11) |
328 (params->frac_wf_r << 10) | (params->edge << 7) |
329 (params->nocount << 6), 0x7fff);
331 axi_clkgen_mmcm_write(axi_clkgen, reg3,
332 (params->frac_phase << 11) | (params->frac_wf_f << 10), 0x3c00);
336 static int axi_clkgen_set_rate(struct clk_hw *clk_hw,
337 unsigned long rate, unsigned long parent_rate)
339 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
340 const struct axi_clkgen_limits *limits = &axi_clkgen->limits;
341 unsigned int d, m, dout;
342 struct axi_clkgen_div_params params;
347 if (parent_rate == 0 || rate == 0)
350 axi_clkgen_calc_params(limits, parent_rate, rate, &d, &m, &dout);
352 if (d == 0 || dout == 0 || m == 0)
355 if ((dout & 0x7) != 0 || (m & 0x7) != 0)
358 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_POWER, power, 0x9800);
360 filter = axi_clkgen_lookup_filter(m - 1);
361 lock = axi_clkgen_lookup_lock(m - 1);
363 axi_clkgen_calc_clk_params(dout >> 3, dout & 0x7, ¶ms);
364 axi_clkgen_set_div(axi_clkgen, MMCM_REG_CLKOUT0_1, MMCM_REG_CLKOUT0_2,
365 MMCM_REG_CLKOUT5_2, ¶ms);
367 axi_clkgen_calc_clk_params(d, 0, ¶ms);
368 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_DIV,
369 (params.edge << 13) | (params.nocount << 12) |
370 (params.high << 6) | params.low, 0x3fff);
372 axi_clkgen_calc_clk_params(m >> 3, m & 0x7, ¶ms);
373 axi_clkgen_set_div(axi_clkgen, MMCM_REG_CLK_FB1, MMCM_REG_CLK_FB2,
374 MMCM_REG_CLKOUT6_2, ¶ms);
376 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK1, lock & 0x3ff, 0x3ff);
377 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK2,
378 (((lock >> 16) & 0x1f) << 10) | 0x1, 0x7fff);
379 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK3,
380 (((lock >> 24) & 0x1f) << 10) | 0x3e9, 0x7fff);
381 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER1, filter >> 16, 0x9900);
382 axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER2, filter, 0x9900);
387 static int axi_clkgen_determine_rate(struct clk_hw *hw,
388 struct clk_rate_request *req)
390 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(hw);
391 const struct axi_clkgen_limits *limits = &axi_clkgen->limits;
392 unsigned int d, m, dout;
393 unsigned long long tmp;
395 axi_clkgen_calc_params(limits, req->best_parent_rate, req->rate,
398 if (d == 0 || dout == 0 || m == 0)
401 tmp = (unsigned long long)req->best_parent_rate * m;
402 tmp = DIV_ROUND_CLOSEST_ULL(tmp, dout * d);
404 req->rate = min_t(unsigned long long, tmp, LONG_MAX);
408 static unsigned int axi_clkgen_get_div(struct axi_clkgen *axi_clkgen,
409 unsigned int reg1, unsigned int reg2)
411 unsigned int val1, val2;
414 axi_clkgen_mmcm_read(axi_clkgen, reg2, &val2);
415 if (val2 & MMCM_CLKOUT_NOCOUNT)
418 axi_clkgen_mmcm_read(axi_clkgen, reg1, &val1);
420 div = (val1 & 0x3f) + ((val1 >> 6) & 0x3f);
423 if (val2 & MMCM_CLK_DIV_DIVIDE) {
424 if ((val2 & BIT(7)) && (val2 & 0x7000) != 0x1000)
429 div += (val2 >> 12) & 0x7;
435 static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,
436 unsigned long parent_rate)
438 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
439 unsigned int d, m, dout;
440 unsigned long long tmp;
443 dout = axi_clkgen_get_div(axi_clkgen, MMCM_REG_CLKOUT0_1,
445 m = axi_clkgen_get_div(axi_clkgen, MMCM_REG_CLK_FB1,
448 axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLK_DIV, &val);
449 if (val & MMCM_CLK_DIV_NOCOUNT)
452 d = (val & 0x3f) + ((val >> 6) & 0x3f);
454 if (d == 0 || dout == 0)
457 tmp = (unsigned long long)parent_rate * m;
458 tmp = DIV_ROUND_CLOSEST_ULL(tmp, dout * d);
460 return min_t(unsigned long long, tmp, ULONG_MAX);
463 static int axi_clkgen_enable(struct clk_hw *clk_hw)
465 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
467 axi_clkgen_mmcm_enable(axi_clkgen, true);
472 static void axi_clkgen_disable(struct clk_hw *clk_hw)
474 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
476 axi_clkgen_mmcm_enable(axi_clkgen, false);
479 static int axi_clkgen_set_parent(struct clk_hw *clk_hw, u8 index)
481 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
483 axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, index);
488 static u8 axi_clkgen_get_parent(struct clk_hw *clk_hw)
490 struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
493 axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, &parent);
498 static const struct clk_ops axi_clkgen_ops = {
499 .recalc_rate = axi_clkgen_recalc_rate,
500 .determine_rate = axi_clkgen_determine_rate,
501 .set_rate = axi_clkgen_set_rate,
502 .enable = axi_clkgen_enable,
503 .disable = axi_clkgen_disable,
504 .set_parent = axi_clkgen_set_parent,
505 .get_parent = axi_clkgen_get_parent,
508 static int axi_clkgen_probe(struct platform_device *pdev)
510 const struct axi_clkgen_limits *dflt_limits;
511 struct axi_clkgen *axi_clkgen;
512 struct clk_init_data init;
513 const char *parent_names[2];
514 const char *clk_name;
518 dflt_limits = device_get_match_data(&pdev->dev);
522 axi_clkgen = devm_kzalloc(&pdev->dev, sizeof(*axi_clkgen), GFP_KERNEL);
526 axi_clkgen->base = devm_platform_ioremap_resource(pdev, 0);
527 if (IS_ERR(axi_clkgen->base))
528 return PTR_ERR(axi_clkgen->base);
530 init.num_parents = of_clk_get_parent_count(pdev->dev.of_node);
531 if (init.num_parents < 1 || init.num_parents > 2)
534 for (i = 0; i < init.num_parents; i++) {
535 parent_names[i] = of_clk_get_parent_name(pdev->dev.of_node, i);
536 if (!parent_names[i])
540 memcpy(&axi_clkgen->limits, dflt_limits, sizeof(axi_clkgen->limits));
542 clk_name = pdev->dev.of_node->name;
543 of_property_read_string(pdev->dev.of_node, "clock-output-names",
546 init.name = clk_name;
547 init.ops = &axi_clkgen_ops;
548 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
549 init.parent_names = parent_names;
551 axi_clkgen_mmcm_enable(axi_clkgen, false);
553 axi_clkgen->clk_hw.init = &init;
554 ret = devm_clk_hw_register(&pdev->dev, &axi_clkgen->clk_hw);
558 return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get,
559 &axi_clkgen->clk_hw);
562 static const struct of_device_id axi_clkgen_ids[] = {
564 .compatible = "adi,zynqmp-axi-clkgen-2.00.a",
565 .data = &axi_clkgen_zynqmp_default_limits,
568 .compatible = "adi,axi-clkgen-2.00.a",
569 .data = &axi_clkgen_zynq_default_limits,
573 MODULE_DEVICE_TABLE(of, axi_clkgen_ids);
575 static struct platform_driver axi_clkgen_driver = {
577 .name = "adi-axi-clkgen",
578 .of_match_table = axi_clkgen_ids,
580 .probe = axi_clkgen_probe,
582 module_platform_driver(axi_clkgen_driver);
584 MODULE_LICENSE("GPL v2");
585 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
586 MODULE_DESCRIPTION("Driver for the Analog Devices' AXI clkgen pcore clock generator");