1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2020 Antmicro Ltd. <www.antmicro.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
10 #include <linux/litex.h>
11 #include <linux/platform_device.h>
12 #include <linux/module.h>
13 #include <linux/clk-provider.h>
14 #include <linux/delay.h>
16 struct litex_drp_reg
{
21 struct litex_drp_reg drp
[] = {
22 {DRP_OF_RESET
, DRP_SIZE_RESET
},
23 {DRP_OF_READ
, DRP_SIZE_READ
},
24 {DRP_OF_WRITE
, DRP_SIZE_WRITE
},
25 {DRP_OF_DRDY
, DRP_SIZE_DRDY
},
26 {DRP_OF_ADR
, DRP_SIZE_ADR
},
27 {DRP_OF_DAT_W
, DRP_SIZE_DAT_W
},
28 {DRP_OF_DAT_R
, DRP_SIZE_DAT_R
},
29 {DRP_OF_LOCKED
, DRP_SIZE_LOCKED
},
32 struct litex_clk_device
{
36 struct litex_clk_clkout
*clkouts
;
43 struct litex_clk_clkout_addr
{
48 struct litex_clk_regs_addr
{
49 struct litex_clk_clkout_addr clkout
[CLKOUT_MAX
];
52 struct litex_clk_clkout
{
66 struct litex_clk_regs_addr
litex_clk_regs_addr_init(void)
68 struct litex_clk_regs_addr m
;
72 for (i
= 0; i
<= CLKOUT_MAX
; i
++) {
75 *special case because CLKOUT5 have its reg addresses
76 *placed lower than other CLKOUTs
78 m
.clkout
[5].reg1
= CLKOUT5_REG1
;
79 m
.clkout
[5].reg2
= CLKOUT5_REG2
;
81 m
.clkout
[i
].reg1
= addr
;
83 m
.clkout
[i
].reg2
= addr
;
91 * This code is taken from:
92 * https://github.com/torvalds/linux/blob/master/drivers/clk/clk-axi-clkgen.c
94 * Copyright 2012-2013 Analog Devices Inc.
95 * Author: Lars-Peter Clausen <lars@metafoo.de>
97 * FIXME: make this code common
100 /* MMCM loop filter lookup table */
101 static u32
litex_clk_lookup_filter(u32 glob_mul
)
131 /* MMCM lock detection lookup table */
132 static const u32 litex_clk_lock_table
[] = {
133 0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8,
134 0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8,
135 0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339,
136 0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271,
137 0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4,
138 0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190,
139 0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e,
140 0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c,
141 0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113,
144 /* Helper function for lock lookup table */
145 static u32
litex_clk_lookup_lock(u32 glob_mul
)
147 if (glob_mul
< ARRAY_SIZE(litex_clk_lock_table
))
148 return litex_clk_lock_table
[glob_mul
];
151 /* End of copied code */
153 static inline struct litex_clk_device
*clk_hw_to_litex_clk_device
154 (struct clk_hw
*clk_hw
)
156 return container_of(clk_hw
, struct litex_clk_device
, clk_hw
);
159 static inline struct litex_clk_clkout
*clk_hw_to_litex_clk_clkout
160 (struct clk_hw
*clk_hw
)
162 return container_of(clk_hw
, struct litex_clk_clkout
, clk_hw
);
165 /* Clock control driver functions */
166 static inline void litex_clk_set_reg(struct clk_hw
*clk_hw
, u32 reg
, u32 val
)
168 struct litex_clk_clkout
*l_clk
= clk_hw_to_litex_clk_clkout(clk_hw
);
170 _litex_set_reg(l_clk
->base
+ drp
[reg
].offset
, drp
[reg
].size
, val
);
173 static inline u32
litex_clk_get_reg(struct clk_hw
*clk_hw
, u32 reg
)
175 struct litex_clk_clkout
*l_clk
= clk_hw_to_litex_clk_clkout(clk_hw
);
177 return _litex_get_reg(l_clk
->base
+ drp
[reg
].offset
, drp
[reg
].size
);
180 static inline void litex_clk_assert_reg(struct clk_hw
*clk_hw
, u32 reg
)
182 int assert = (1 << (drp
[reg
].size
* BITS_PER_BYTE
)) - 1;
184 litex_clk_set_reg(clk_hw
, reg
, assert);
187 static inline void litex_clk_deassert_reg(struct clk_hw
*clk_hw
, u32 reg
)
189 litex_clk_set_reg(clk_hw
, reg
, ZERO_REG
);
192 static int litex_clk_wait_lock(struct clk_hw
*clk_hw
)
194 struct litex_clk_clkout
*l_clk
;
197 l_clk
= clk_hw_to_litex_clk_clkout(clk_hw
);
199 /* Check if clk_hw is global or clkout specific */
200 if (l_clk
->is_clkout
) {
201 timeout
= l_clk
->lock_timeout
;
203 /* clk hw is global */
204 struct litex_clk_device
*l_dev
;
206 l_dev
= clk_hw_to_litex_clk_device(clk_hw
);
207 timeout
= l_dev
->lock_timeout
;
210 /*Waiting for LOCK signal to assert in MMCM*/
211 while (!litex_clk_get_reg(clk_hw
, DRP_LOCKED
) && timeout
) {
213 usleep_range(900, 1000);
216 pr_err("CLKOUT%d: %s timeout", l_clk
->id
, __func__
);
222 static int litex_clk_wait_drdy(struct clk_hw
*clk_hw
)
224 struct litex_clk_clkout
*l_clk
;
227 l_clk
= clk_hw_to_litex_clk_clkout(clk_hw
);
229 /* Check if clk_hw is global or clkout specific */
230 if (l_clk
->is_clkout
) {
231 timeout
= l_clk
->drdy_timeout
;
233 /* clk hw is global */
234 struct litex_clk_device
*l_dev
;
236 l_dev
= clk_hw_to_litex_clk_device(clk_hw
);
237 timeout
= l_dev
->drdy_timeout
;
240 /*Waiting for DRDY signal to assert in MMCM*/
241 while (!litex_clk_get_reg(clk_hw
, DRP_DRDY
) && timeout
) {
243 usleep_range(900, 1000);
246 pr_err("CLKOUT%d: %s timeout", l_clk
->id
, __func__
);
252 /* Read value written in given internal MMCM register*/
253 static int litex_clk_get_DO(struct clk_hw
*clk_hw
, u8 clk_reg_addr
, u16
*res
)
257 litex_clk_set_reg(clk_hw
, DRP_ADR
, clk_reg_addr
);
258 litex_clk_assert_reg(clk_hw
, DRP_READ
);
260 litex_clk_deassert_reg(clk_hw
, DRP_READ
);
261 ret
= litex_clk_wait_drdy(clk_hw
);
265 *res
= litex_clk_get_reg(clk_hw
, DRP_DAT_R
);
270 /* Return global divider and multiplier values */
271 static int litex_clk_get_global_divider(struct clk_hw
*hw
, u32
*divider
,
276 u8 low_time
, high_time
;
278 ret
= litex_clk_get_DO(hw
, CLKFBOUT_REG1
, &mult
);
281 ret
= litex_clk_get_DO(hw
, DIV_REG
, &divreg
);
285 low_time
= mult
& HL_TIME_MASK
;
286 high_time
= (mult
>> HIGH_TIME_POS
) & HL_TIME_MASK
;
287 *multiplier
= low_time
+ high_time
;
289 low_time
= divreg
& HL_TIME_MASK
;
290 high_time
= (divreg
>> HIGH_TIME_POS
) & HL_TIME_MASK
;
291 *divider
= low_time
+ high_time
;
296 /* Calculate frequency after global divider and multiplier */
297 static u32
litex_clk_get_real_global_frequency(struct clk_hw
*hw
)
299 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
300 u32 g_divider
, g_multiplier
;
302 litex_clk_get_global_divider(hw
, &g_divider
, &g_multiplier
);
303 return l_clkout
->sys_clk_freq
* g_multiplier
/ g_divider
;
306 /* Calculate frequency after global divider and multiplier without clk_hw */
307 static u32
litex_clk_get_expctd_global_frequency(u32 sys_clk_freq
)
309 u32 g_divider
, g_multiplier
;
311 g_divider
= (GLOB_DIV_VAL
& HL_TIME_MASK
) +
312 ((GLOB_DIV_VAL
>> HIGH_TIME_POS
) & HL_TIME_MASK
);
314 g_multiplier
= (GLOB_MUL_VAL
& HL_TIME_MASK
) +
315 ((GLOB_MUL_VAL
>> HIGH_TIME_POS
) & HL_TIME_MASK
);
317 return sys_clk_freq
* g_multiplier
/ g_divider
;
319 /* Return dividers of given CLKOUT */
320 static int litex_clk_get_clkout_divider(struct clk_hw
*hw
, u32
*divider
,
323 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
324 struct litex_clk_regs_addr drp_addr
= litex_clk_regs_addr_init();
327 u8 clkout_nr
= l_clkout
->id
;
328 u8 low_time
, high_time
;
330 ret
= litex_clk_get_DO(hw
, drp_addr
.clkout
[clkout_nr
].reg1
, &div
);
333 ret
= litex_clk_get_DO(hw
, drp_addr
.clkout
[clkout_nr
].reg2
, &frac
);
337 low_time
= div
& HL_TIME_MASK
;
338 high_time
= (div
>> HIGH_TIME_POS
) & HL_TIME_MASK
;
339 *divider
= low_time
+ high_time
;
340 *fract_cnt
= (frac
>> FRAC_POS
) & FRAC_MASK
;
345 /* Debug functions */
347 static void litex_clk_print_general_regs(struct clk_hw
*clk_hw
)
349 u16 power_reg
, div_reg
, clkfbout_reg
, lock_reg1
,
350 lock_reg2
, lock_reg3
, filt_reg1
, filt_reg2
;
352 litex_clk_get_DO(clk_hw
, POWER_REG
, &power_reg
);
353 litex_clk_get_DO(clk_hw
, DIV_REG
, &div_reg
);
354 litex_clk_get_DO(clk_hw
, CLKFBOUT_REG1
, &clkfbout_reg
);
355 litex_clk_get_DO(clk_hw
, LOCK_REG1
, &lock_reg1
);
356 litex_clk_get_DO(clk_hw
, LOCK_REG2
, &lock_reg2
);
357 litex_clk_get_DO(clk_hw
, LOCK_REG3
, &lock_reg3
);
358 litex_clk_get_DO(clk_hw
, FILT_REG1
, &filt_reg1
);
359 litex_clk_get_DO(clk_hw
, FILT_REG2
, &filt_reg2
);
361 pr_debug("POWER REG: 0x%x\n", power_reg
);
362 pr_debug("DIV REG: 0x%x\n", div_reg
);
363 pr_debug("MUL REG: 0x%x\n", clkfbout_reg
);
364 pr_debug("LOCK_REG1: 0x%x\n", lock_reg1
);
365 pr_debug("LOCK_REG2: 0x%x\n", lock_reg2
);
366 pr_debug("LOCK_REG3: 0x%x\n", lock_reg3
);
367 pr_debug("FILT_REG1: 0x%x\n", filt_reg1
);
368 pr_debug("FILT_REG2: 0x%x\n", filt_reg2
);
371 static void litex_clk_print_clkout_regs(struct clk_hw
*clk_hw
, u8 clkout
,
374 u16 clkout_reg1
, clkout_reg2
;
376 litex_clk_get_DO(clk_hw
, reg1
, &clkout_reg1
);
377 pr_debug("CLKOUT%d REG1: 0x%x\n", clkout
, clkout_reg1
);
379 litex_clk_get_DO(clk_hw
, reg1
, &clkout_reg2
);
380 pr_debug("CLKOUT%d REG2: 0x%x\n", clkout
, clkout_reg2
);
383 static void litex_clk_print_all_regs(struct clk_hw
*clk_hw
)
385 struct litex_clk_regs_addr drp_addr
= litex_clk_regs_addr_init();
388 pr_debug("General regs:\n");
389 litex_clk_print_general_regs(clk_hw
);
390 for (i
= 0; i
< CLKOUT_MAX
; i
++)
391 litex_clk_print_clkout_regs(clk_hw
, i
, drp_addr
.clkout
[i
].reg1
,
392 drp_addr
.clkout
[i
].reg2
);
394 #endif /* #ifdef DEBUG */
396 /* Return raw value ready to be written into DRP */
397 static inline u16
litex_clk_calc_DI(u16 DO_val
, u16 mask
, u16 bitset
)
401 DI_val
= DO_val
& mask
;
407 /* Sets calculated DI value into DI DRP register */
408 static int litex_clk_set_DI(struct clk_hw
*clk_hw
, u16 DI_val
)
412 litex_clk_set_reg(clk_hw
, DRP_DAT_W
, DI_val
);
413 litex_clk_assert_reg(clk_hw
, DRP_WRITE
);
414 litex_clk_deassert_reg(clk_hw
, DRP_WRITE
);
415 ret
= litex_clk_wait_drdy(clk_hw
);
422 * Change register value as specified in arguments
424 * clk_hw: hardware specific clock structure for selecting CLKOUT
425 * mask: preserve or zero MMCM register bits
426 * by selecting 1 or 0 on desired specific mask positions
427 * bitset: set those bits in MMCM register which are 1 in bitset
428 * clk_reg_addr: internal MMCM address of control register
431 static int litex_clk_change_value(struct clk_hw
*clk_hw
, u16 mask
, u16 bitset
,
437 litex_clk_assert_reg(clk_hw
, DRP_RESET
);
439 ret
= litex_clk_get_DO(clk_hw
, clk_reg_addr
, &DO_val
);
442 DI_val
= litex_clk_calc_DI(DO_val
, mask
, bitset
);
443 ret
= litex_clk_set_DI(clk_hw
, DI_val
);
447 DI_val
= litex_clk_get_reg(clk_hw
, DRP_DAT_W
);
449 litex_clk_deassert_reg(clk_hw
, DRP_DAT_W
);
450 litex_clk_deassert_reg(clk_hw
, DRP_RESET
);
451 ret
= litex_clk_wait_lock(clk_hw
);
455 pr_debug("set 0x%x under addr: 0x%x", DI_val
, clk_reg_addr
);
460 * Set register values for given CLKOUT
462 * clk_hw: hardware specific clock structure for selecting CLKOUT
463 * clkout_nr: clock output number
464 * mask_regX: preserve or zero MMCM register X bits
465 * by selecting 1 or 0 on desired specific mask positions
466 * bitset_regX: set those bits in MMCM register X which are 1 in bitset
469 static int litex_clk_set_clock(struct clk_hw
*clk_hw
, u8 clkout_nr
,
470 u16 mask_reg1
, u16 bitset_reg1
,
471 u16 mask_reg2
, u16 bitset_reg2
)
473 struct litex_clk_regs_addr drp_addr
= litex_clk_regs_addr_init();
476 if (!(mask_reg2
== KEEP_REG_16
&& bitset_reg2
== ZERO_REG
)) {
477 ret
= litex_clk_change_value(clk_hw
, mask_reg2
, bitset_reg2
,
478 drp_addr
.clkout
[clkout_nr
].reg2
);
482 if (!(mask_reg1
== KEEP_REG_16
&& bitset_reg1
== ZERO_REG
)) {
483 ret
= litex_clk_change_value(clk_hw
, mask_reg1
, bitset_reg1
,
484 drp_addr
.clkout
[clkout_nr
].reg1
);
492 * Set global divider for all CLKOUTs
494 * clk_hw: hardware specific clock structure
495 * mask: preserve or zero MMCM register bits
496 * by selecting 1 or 0 on desired specific mask positions
497 * bitset: set those bits in MMCM register which are 1 in bitset
500 static int litex_clk_set_divreg(struct clk_hw
*clk_hw
, u16 mask
, u16 bitset
)
504 ret
= litex_clk_change_value(clk_hw
, mask
, bitset
, DIV_REG
);
507 pr_debug("Global divider set to %d", (bitset
% (1 << HIGH_TIME_POS
)) +
508 ((bitset
>> HIGH_TIME_POS
) % (1 << HIGH_TIME_POS
)));
513 * Set global multiplier for all CLKOUTs
515 * clk_hw: hardware specific clock structure
516 * mask: preserve or zero MMCM register bits
517 * by selecting 1 or 0 on desired specific mask positions
518 * bitset: set those bits in MMCM register which are 1 in bitset
521 static int litex_clk_set_mulreg(struct clk_hw
*clk_hw
, u16 mask
, u16 bitset
)
525 ret
= litex_clk_change_value(clk_hw
, mask
, bitset
, CLKFBOUT_REG1
);
528 pr_debug("Global multiplier set to %d",
529 (bitset
% (1 << HIGH_TIME_POS
)) + ((bitset
>> HIGH_TIME_POS
) %
530 (1 << HIGH_TIME_POS
)));
534 static int litex_clk_set_filt(struct clk_hw
*hw
)
539 mul
= GLOB_MUL_VAL
&& HL_TIME_MASK
;
540 mul
+= (GLOB_MUL_VAL
>> HIGH_TIME_POS
) && HL_TIME_MASK
;
542 filt
= litex_clk_lookup_filter(mul
- 1);
544 ret
= litex_clk_change_value(hw
, FILT_MASK
, filt
>> 16, FILT_REG1
);
547 ret
= litex_clk_change_value(hw
, FILT_MASK
, filt
, FILT_REG2
);
553 static int litex_clk_set_lock(struct clk_hw
*hw
)
558 mul
= GLOB_MUL_VAL
&& HL_TIME_MASK
;
559 mul
+= (GLOB_MUL_VAL
>> HIGH_TIME_POS
) && HL_TIME_MASK
;
561 lock
= litex_clk_lookup_lock(mul
- 1);
563 ret
= litex_clk_change_value(hw
, LOCK1_MASK
, lock
& 0x3ff, LOCK_REG1
);
566 ret
= litex_clk_change_value(hw
, LOCK23_MASK
,
567 (((lock
>> 16) & 0x1f) << 10) | 0x1, LOCK_REG2
);
570 ret
= litex_clk_change_value(hw
, LOCK23_MASK
,
571 (((lock
>> 24) & 0x1f) << 10) | 0x3e9, LOCK_REG3
);
578 * Load default global divider and multiplier values
581 static int litex_clk_set_dm(struct clk_hw
*clk_hw
)
585 ret
= litex_clk_set_divreg(clk_hw
, KEEP_IN_DIV
, GLOB_DIV_VAL
);
588 ret
= litex_clk_set_mulreg(clk_hw
, KEEP_IN_MUL
, GLOB_MUL_VAL
);
591 ret
= litex_clk_set_filt(clk_hw
);
594 ret
= litex_clk_set_lock(clk_hw
);
600 /* Round scaled value*/
601 static inline u32
litex_round(u32 val
, u32 mod
)
603 if (val
% mod
> mod
/ 2)
604 return val
/ mod
+ 1;
614 * Calculate necessary values for setting duty cycle in fractional mode
616 * divider: frequency divider value for calculating the phase
617 * high_duty: desired duty cycle in percent
618 * fract_cnt: fractional frequency divider value, if not for
620 * pointers: function return values, used for setting desired duty
622 * This function is based on Xilinx Unified Simulation Library Component
623 * Advanced Mixed Mode Clock Manager (MMCM) taken from Xilinx Vivado
624 * found in: <Vivado-dir>/data/verilog/src/unisims/MMCME2_ADV.v
627 static int litex_clk_calc_duty_fract(int divider
, int high_duty
, int fract_cnt
,
628 u8
*edge
, u8
*high_time
, u8
*low_time
,
629 u8
*frac_wf_r
, u8
*frac_wf_f
)
631 int even_part_high
, even_part_low
, odd
, odd_and_frac
;
634 even_part_high
= divider
>> 1;
635 even_part_low
= even_part_high
;
636 odd
= divider
- even_part_high
- even_part_low
;
637 odd_and_frac
= (8 * odd
) + fract_cnt
;
639 *low_time
= even_part_high
;
640 if (odd_and_frac
<= ODD_AND_FRAC
)
643 *high_time
= even_part_low
;
644 if (odd_and_frac
<= EVEN_AND_FRAC
)
649 if (((odd_and_frac
>= 2) && (odd_and_frac
<= ODD_AND_FRAC
)) ||
650 ((fract_cnt
== 1) && (divider
== 2)))
655 if ((odd_and_frac
>= 1) && (odd_and_frac
<= EVEN_AND_FRAC
))
662 /* End of Vivado based code */
665 * Calculate necessary values for setting duty cycle in normal mode
667 * divider: frequency divider value for calculating the phase
668 * high_duty: desired duty cycle in percent
669 * pointers: function return values, used for setting desired duty
672 static int litex_clk_calc_duty_normal(int divider
, int high_duty
, u8
*edge
,
673 u8
*high_time
, u8
*low_time
,
674 u8
*frac_wf_r
, u8
*frac_wf_f
)
676 int synthetic_duty
, delta_d
, min_d
;
677 u32 high_time_it
, ht_aprox
, edge_it
;
680 /* check if duty is available to set */
681 ht_aprox
= high_duty
* divider
;
682 if (ht_aprox
> ((HIGH_LOW_TIME_REG_MAX
* 100) + 50) ||
683 (divider
* 100) - ht_aprox
>
684 ((HIGH_LOW_TIME_REG_MAX
* 100) + 50))
687 /* to prevent high_time == 0 or low_time == 0 */
688 for (high_time_it
= 1; high_time_it
< divider
; high_time_it
++) {
689 for (edge_it
= 0; edge_it
< 2; edge_it
++) {
690 synthetic_duty
= (high_time_it
* 100 + 50 * edge_it
) /
692 delta_d
= abs(synthetic_duty
- high_duty
);
693 /* check if low_time won't be above acceptable range */
694 if (delta_d
< min_d
&& (divider
- high_time_it
) <=
695 HIGH_LOW_TIME_REG_MAX
) {
697 *high_time
= high_time_it
;
698 *low_time
= divider
- *high_time
;
704 * Calculating values in normal mode,
705 * clear control bits of fractional part
714 /* Calculates duty cycle for given ratio in percent, 1% accuracy */
715 static inline int litex_clk_calc_duty_percent(struct clk_hw
*hw
,
716 struct clk_duty
*duty
)
718 u32 div
, duty_ratio
, ht
;
722 duty_ratio
= ht
* 10000 / div
;
724 return litex_round(duty_ratio
, 100);
727 /* Calculates duty high_time for given divider and ratio */
728 static inline int litex_clk_calc_duty_high_time(struct clk_hw
*hw
,
729 struct clk_duty
*duty
,
734 high_duty
= litex_clk_calc_duty_percent(hw
, duty
) * divider
;
736 return litex_round(high_duty
, 100);
739 /* Returns accurate duty ratio of given clkout*/
740 int litex_clk_get_duty_cycle(struct clk_hw
*hw
, struct clk_duty
*duty
)
742 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
743 struct litex_clk_regs_addr drp_addr
= litex_clk_regs_addr_init();
746 u16 clkout_reg1
, clkout_reg2
;
747 u8 clkout_nr
, high_time
, edge
, no_cnt
, frac_en
, frac_cnt
;
749 clkout_nr
= l_clkout
->id
;
750 /* Check if divider is off */
751 ret
= litex_clk_get_DO(hw
, drp_addr
.clkout
[clkout_nr
].reg2
,
755 edge
= (clkout_reg2
>> EDGE_POS
) & EDGE_MASK
;
756 no_cnt
= (clkout_reg2
>> NO_CNT_POS
) & NO_CNT_MASK
;
757 frac_en
= (clkout_reg2
>> FRAC_EN_POS
) & FRAC_EN_MASK
;
758 frac_cnt
= (clkout_reg2
>> FRAC_POS
) & FRAC_MASK
;
760 /* get duty 50% when divider is off or fractional is enabled */
761 if (no_cnt
|| (frac_en
&& frac_cnt
)) {
767 ret
= litex_clk_get_DO(hw
, drp_addr
.clkout
[clkout_nr
].reg1
,
771 divider
= clkout_reg1
& HL_TIME_MASK
;
772 high_time
= (clkout_reg1
>> HIGH_TIME_POS
) & HL_TIME_MASK
;
773 divider
+= high_time
;
775 /* Scaling to consider edge control bit */
776 duty
->num
= high_time
* 10 + edge
* 5;
777 duty
->den
= (divider
+ edge
) * 10;
782 /* Set duty cycle with given ratio */
783 int litex_clk_set_duty_cycle(struct clk_hw
*hw
, struct clk_duty
*duty
)
785 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
786 u32 divider
, fract_cnt
, high_duty
;
787 u16 bitset1
, bitset2
;
788 u8 frac_wf_r
, frac_wf_f
, frac_en
, no_cnt
,
789 edge
, high_time
, low_time
, clkout_nr
;
791 clkout_nr
= l_clkout
->id
;
792 edge
= 0, high_time
= 0, low_time
= 0, frac_en
= 0,
793 no_cnt
= 0, frac_wf_r
= 0, frac_wf_f
= 0;
794 high_duty
= litex_clk_calc_duty_percent(hw
, duty
);
796 litex_clk_get_clkout_divider(hw
, ÷r
, &fract_cnt
);
798 if (fract_cnt
== 0) {
801 ret
= litex_clk_calc_duty_normal(divider
, high_duty
, &edge
,
802 &high_time
, &low_time
,
803 &frac_wf_r
, &frac_wf_f
);
805 pr_err("CLKOUT%d: cannot set %d%% duty cycle for that frequency",
806 clkout_nr
, high_duty
);
809 } else if (high_duty
== 50) {
812 pr_err("CLKOUT%d: cannot set duty cycle when fractional divider enabled!",
817 bitset1
= (high_time
<< HIGH_TIME_POS
) | low_time
;
818 bitset2
= (edge
<< EDGE_POS
);
820 pr_debug("SET DUTY CYCLE: divider:%d fract_cnt:%d frac_wf_r:%d frac_wf_f:%d frac_en:%d no_cnt:%d edge:%d high_time:%d low_time:%d bitset1: 0x%x bitset2: 0x%x",
821 divider
, fract_cnt
, frac_wf_r
, frac_wf_f
, frac_en
,
822 no_cnt
, edge
, high_time
, low_time
, bitset1
, bitset2
);
824 return litex_clk_set_clock(hw
, clkout_nr
, REG1_DUTY_MASK
, bitset1
,
825 REG2_DUTY_MASK
, bitset2
);
833 * Calculate necessary values for setting phase in fractional mode
835 * clk_hw: hardware specific clock structure
836 * divider: frequency divider value for calculating the phase
837 * fract_cnt: fractional frequency divider value, if not for
839 * phase_offset: desired phase in angle degrees
840 * pointers: function return values, used for setting desired phase
842 * This function is based on Xilinx Unified Simulation Library Component
843 * Advanced Mixed Mode Clock Manager (MMCM) taken from Xilinx Vivado
844 * found in: <Vivado-dir>/data/verilog/src/unisims/MMCME2_ADV.v
847 static int litex_clk_calc_phase_fract(struct clk_hw
*hw
, u32 divider
,
848 u32 fract_cnt
, u32 phase_offset
,
849 u32
*phase_mux
, u32
*delay_time
,
850 u32
*phase_mux_f
, u32
*period_offset
)
852 int phase
, dt_calc
, dt
,
853 a_per_in_octets
, a_phase_in_cycles
,
854 pm_rise_frac
, pm_rise_frac_filtered
,
855 pm_fall
, pm_fall_frac
, pm_fall_frac_filtered
;
857 phase
= phase_offset
* 1000;
858 a_per_in_octets
= (8 * divider
) + fract_cnt
;
859 a_phase_in_cycles
= (phase
+ 10) * a_per_in_octets
/ 360000;
861 if ((a_phase_in_cycles
& FULL_BYTE
) != 0)
862 pm_rise_frac
= (a_phase_in_cycles
& PHASE_MUX_MAX
);
866 dt_calc
= a_phase_in_cycles
/ 8;
867 dt
= dt_calc
& FULL_BYTE
;
869 pm_rise_frac_filtered
= pm_rise_frac
;
870 if (pm_rise_frac
>= 8)
871 pm_rise_frac_filtered
-= 8;
873 pm_fall
= ((divider
% 2) << 2) + ((fract_cnt
>> 1) & TWO_LSBITS
);
874 pm_fall_frac
= pm_fall
+ pm_rise_frac
;
875 pm_fall_frac_filtered
= pm_fall
+ pm_rise_frac
-
876 (pm_fall_frac
& F_FRAC_MASK
);
878 /* keep only the lowest bits to fit in control registers bit groups */
879 *delay_time
= dt
% (1 << 6);
880 *phase_mux
= pm_rise_frac_filtered
% (1 << 3);
881 *phase_mux_f
= pm_fall_frac_filtered
% (1 << 3);
885 /* End of Vivado based code */
888 * Calculate necessary values for setting phase in normal mode
890 * clk_hw: hardware specific clock structure
891 * divider: frequency divider value for calculating the phase
892 * fract_cnt: fractional frequency divider value, if not for
894 * phase_offset: desired phase in angle degrees
895 * pointers: function return values, used for setting desired phase
898 static int litex_clk_calc_phase_normal(struct clk_hw
*hw
, u32 divider
,
899 u32 fract_cnt
, u32 phase_offset
,
900 u32
*phase_mux
, u32
*delay_time
,
901 u32
*phase_mux_f
, u32
*period_offset
)
904 u32 global_period
, clkout_period
, post_glob_div_f
;
906 post_glob_div_f
= litex_clk_get_real_global_frequency(hw
);
907 global_period
= NS_IN_SEC
/ post_glob_div_f
;
908 clkout_period
= global_period
* divider
;
910 if (phase_offset
!= 0) {
911 int synthetic_phase
, delta_p
, min_p
;
914 *period_offset
= litex_round(clkout_period
* (*period_offset
),
917 if (*period_offset
/ global_period
> DELAY_TIME_MAX
)
921 /* Delay_time: (0-63) */
922 for (d_t
= 0; d_t
<= DELAY_TIME_MAX
; d_t
++) {
923 /* phase_mux: (0-7) */
924 for (p_m
= 0; p_m
<= PHASE_MUX_MAX
; p_m
++) {
925 synthetic_phase
= (d_t
* global_period
) +
926 ((p_m
* ((global_period
* 100) / 8) / 100));
928 delta_p
= abs(synthetic_phase
- *period_offset
);
929 if (delta_p
< min_p
) {
937 /* Don't change phase offset*/
941 /* Calculating values in normal mode, fractional control bits need to be zero*/
948 * Convert phase offset to positive lower than 360 deg.
949 * and calculate period in nanoseconds
952 static int litex_clk_prepare_phase(int *phase_offset
, u32
*period_offset
)
954 *phase_offset
= *phase_offset
% 360;
956 if (*phase_offset
< 0)
957 *phase_offset
+= 360;
959 *period_offset
= ((*phase_offset
* 10000) / 360);
965 * Calculate necessary values for setting phase
967 * clk_hw: hardware specific clock structure
968 * divider: frequency divider value for calculating the phase
969 * fract_cnt: fractional frequency divider value, if not for
971 * clkout_nr: clock output number
972 * phase_offset: desired phase in angle degrees
973 * pointers: function return values, used for setting desired phase
976 static int litex_clk_calc_phase(struct clk_hw
*clk_hw
,
977 u32 divider
, u32 fract_cnt
,
978 u32 clkout_nr
, int phase_offset
,
979 u32
*phase_mux
, u32
*delay_time
,
980 u32
*phase_mux_f
, u32
*period_offset
)
984 litex_clk_prepare_phase(&phase_offset
, period_offset
);
986 if (clkout_nr
== 0 && fract_cnt
> 0)
987 ret
= litex_clk_calc_phase_fract(clk_hw
, divider
, fract_cnt
,
989 phase_mux
, delay_time
,
990 phase_mux_f
, period_offset
);
992 ret
= litex_clk_calc_phase_normal(clk_hw
, divider
, fract_cnt
,
994 phase_mux
, delay_time
,
995 phase_mux_f
, period_offset
);
999 /* Returns phase-specific values of given clock output */
1000 static int litex_clk_get_phase_data(struct clk_hw
*hw
, u8
*phase_mux
,
1001 u8
*delay_time
, u8
*phase_mux_f
)
1003 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
1004 struct litex_clk_regs_addr drp_addr
= litex_clk_regs_addr_init();
1007 u8 clkout_nr
= l_clkout
->id
;
1009 ret
= litex_clk_get_DO(hw
, drp_addr
.clkout
[clkout_nr
].reg1
, &r1
);
1012 ret
= litex_clk_get_DO(hw
, drp_addr
.clkout
[clkout_nr
].reg2
, &r2
);
1015 *phase_mux
= (r1
>> PHASE_MUX_POS
) & PHASE_MUX_MASK
;
1016 *delay_time
= (r2
>> DELAY_TIME_POS
) & HL_TIME_MASK
;
1018 if (clkout_nr
== 0) {
1019 ret
= litex_clk_get_DO(hw
, CLKOUT5_REG2
, &r2
);
1022 *phase_mux_f
= (r2
>> PHASE_MUX_F_POS
) & PHASE_MUX_F_MASK
;
1030 /* Returns phase of given clock output in degrees */
1031 int litex_clk_get_phase(struct clk_hw
*hw
)
1033 u8 phase_mux
= 0, delay_time
= 0, phase_mux_f
;
1034 u32 divider
, fract_cnt
, post_glob_div_f
, pm
;
1036 u32 global_period
, clkout_period
, period
;
1038 litex_clk_get_phase_data(hw
, &phase_mux
, &delay_time
, &phase_mux_f
);
1039 litex_clk_get_clkout_divider(hw
, ÷r
, &fract_cnt
);
1041 post_glob_div_f
= litex_clk_get_real_global_frequency(hw
);
1042 global_period
= NS_IN_SEC
/ post_glob_div_f
;
1043 clkout_period
= global_period
* divider
;
1045 pm
= (phase_mux
* global_period
* 1000) / PHASE_MUX_RES_FACTOR
;
1046 pm
= litex_round(pm
, 1000);
1048 period
= delay_time
* global_period
+ pm
;
1050 period
= period
* 1000 / clkout_period
;
1051 period
= period
* 360;
1053 return litex_round(period
, 1000);
1056 /* Sets phase given in degrees on given clock output */
1057 int litex_clk_set_phase(struct clk_hw
*hw
, int degrees
)
1059 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
1060 u32 phase_mux
, divider
, fract_cnt
,
1061 phase_mux_f
, delay_time
, period_offset
;
1062 u16 bitset1
, bitset2
, reg2_mask
;
1063 u8 clkout_nr
, edge
, high_time
, low_time
, frac_wf_r
, frac_wf_f
;
1066 reg2_mask
= REG2_PHASE_MASK
;
1069 clkout_nr
= l_clkout
->id
;
1070 litex_clk_get_clkout_divider(hw
, ÷r
, &fract_cnt
);
1071 if (fract_cnt
> 0 && degrees
!= 0) {
1072 pr_err("CLKOUT%d: cannot set phase on that frequency",
1076 ret
= litex_clk_calc_phase(hw
, divider
, fract_cnt
, clkout_nr
, degrees
,
1077 &phase_mux
, &delay_time
,
1078 &phase_mux_f
, &period_offset
);
1080 pr_err("CLKOUT%d: phase offset %d deg is too high for given frequency",
1081 clkout_nr
, degrees
);
1085 bitset1
= (phase_mux
<< PHASE_MUX_POS
);
1086 bitset2
= (delay_time
<< DELAY_TIME_POS
);
1088 if (clkout_nr
== 0 && fract_cnt
> 0) {
1089 u16 clkout5_reg2_bitset
;
1091 litex_clk_calc_duty_fract(divider
, 50, fract_cnt
,
1092 &edge
, &high_time
, &low_time
,
1093 &frac_wf_r
, &frac_wf_f
);
1094 bitset2
|= (fract_cnt
<< FRAC_POS
) |
1095 (1 << FRAC_EN_POS
) |
1096 (frac_wf_r
<< FRAC_WF_R_POS
);
1098 clkout5_reg2_bitset
= (phase_mux_f
<< PHASE_MUX_F_POS
) |
1099 (frac_wf_f
<< FRAC_WF_F_POS
);
1101 ret
= litex_clk_change_value(hw
, CLKOUT5_FRAC_MASK
,
1102 clkout5_reg2_bitset
,
1106 reg2_mask
= REG2_PHASE_F_MASK
;
1109 pr_debug("SET PHASE: phase_mux:%d phase_mux_f:%d delay_time:%d bitset1: 0x%x bitset2: 0x%x",
1110 phase_mux
, phase_mux_f
, delay_time
, bitset1
, bitset2
);
1112 return litex_clk_set_clock(hw
, clkout_nr
, REG1_PHASE_MASK
, bitset1
,
1113 reg2_mask
, bitset2
);
1121 * Calculate necessary values for setting frequency in fractional mode
1123 * clk_hw: hardware specific clock structure
1124 * freq: desired frequency in Hz
1125 * pointers: function return values, used for setting
1128 static int litex_clk_calc_freq_fract(struct clk_hw
*clk_hw
, u32 freq
,
1129 u32
*divider
, u8
*frac_en
,
1130 u8
*no_cnt
, u32
*fract_cnt
)
1132 int delta_f
, synthetic_freq
, min_f
;
1133 u32 post_glob_div_f
, div
, f_cnt
, fract
;
1135 post_glob_div_f
= litex_clk_get_real_global_frequency(clk_hw
);
1138 synthetic_freq
= post_glob_div_f
;
1139 min_f
= abs(synthetic_freq
- freq
);
1141 /* div is integer divider * 1000 to keep precision */
1142 for (div
= MIN_DIVIDER_F
; div
<= MAX_DIVIDER_F
; div
+= 1000) {
1144 * f_cnt is fractional part of divider * 1000 incremented
1145 * by minimal fractional divider step to keep precision
1146 * and find best divider
1148 for (f_cnt
= 0, fract
= 0; fract
< 1000;
1149 fract
+= FRACT_DIV_RES
, f_cnt
++) {
1151 synthetic_freq
= (post_glob_div_f
/ (div
+ fract
)) *
1153 delta_f
= abs(synthetic_freq
- freq
);
1155 if (delta_f
< min_f
) {
1157 *divider
= div
/ 1000;
1162 if (*fract_cnt
== 0) {
1177 * Calculate necessary values for setting frequency in normal mode
1179 * clk_hw: hardware specific clock structure
1180 * freq: desired frequency in Hz
1181 * pointers: function return values, used for setting
1185 static int litex_clk_calc_freq_normal(struct clk_hw
*clk_hw
, u32 freq
,
1186 u32
*divider
, u8
*frac_en
,
1187 u8
*no_cnt
, u32
*fract_cnt
)
1189 int delta_f
, synthetic_freq
, min_f
;
1190 u32 post_glob_div_f
, div
;
1192 post_glob_div_f
= litex_clk_get_real_global_frequency(clk_hw
);
1193 synthetic_freq
= post_glob_div_f
;
1194 min_f
= abs(synthetic_freq
- freq
);
1198 for (div
= MIN_DIVIDER
; div
<= MAX_DIVIDER
; div
++) {
1199 synthetic_freq
= post_glob_div_f
/ div
;
1200 delta_f
= abs(synthetic_freq
- freq
);
1201 if (delta_f
< min_f
) {
1217 /* Returns rate in Hz */
1218 static inline unsigned long litex_clk_calc_rate(struct clk_hw
*hw
, u32 g_mul
,
1222 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
1224 return (l_clkout
->sys_clk_freq
* g_mul
/
1225 ((div
* 1000 + (fract_cnt
* FRACT_DIV_RES
)) * g_div
)) * 1000;
1229 * Calculate necessary values for setting frequency
1231 * clk_hw: hardware specific clock structure
1232 * rate: desired frequency in Hz
1233 * clkout_nr: clock output number
1234 * pointers: function return values, used for setting
1238 static void litex_clk_calc_dividers(struct clk_hw
*hw
, u32 rate
, u8 clkout_nr
,
1239 u32
*divider
, u32
*fract_cnt
,
1240 u8
*frac_wf_r
, u8
*frac_wf_f
,
1241 u8
*frac_en
, u8
*no_cnt
, u8
*edge
,
1242 u8
*high_time
, u8
*low_time
)
1245 struct clk_duty duty
;
1247 litex_clk_get_duty_cycle(hw
, &duty
);
1248 high_duty
= litex_clk_calc_duty_percent(hw
, &duty
);
1250 if (clkout_nr
== 0) {
1251 litex_clk_calc_freq_fract(hw
, rate
, divider
, frac_en
, no_cnt
,
1254 litex_clk_calc_duty_fract(*divider
, high_duty
,
1256 high_time
, low_time
,
1257 frac_wf_r
, frac_wf_f
);
1259 litex_clk_calc_duty_normal(*divider
, high_duty
, edge
,
1260 high_time
, low_time
,
1261 frac_wf_r
, frac_wf_f
);
1264 litex_clk_calc_freq_normal(hw
, rate
, divider
, frac_en
,
1267 litex_clk_calc_duty_normal(*divider
, high_duty
, edge
,
1268 high_time
, low_time
,
1269 frac_wf_r
, frac_wf_f
);
1273 /* Returns rate of given CLKOUT, parent_rate ignored */
1274 unsigned long litex_clk_recalc_rate(struct clk_hw
*hw
,
1275 unsigned long parent_rate
)
1277 u32 multiplier
, divider
, clkout_div
, fract_cnt
;
1279 LITEX_UNUSED(parent_rate
);
1281 litex_clk_get_global_divider(hw
, ÷r
, &multiplier
);
1282 litex_clk_get_clkout_divider(hw
, &clkout_div
, &fract_cnt
);
1283 return litex_clk_calc_rate(hw
, multiplier
, divider
, clkout_div
,
1287 int litex_clk_check_rate_range(struct clk_hw
*hw
, unsigned long rate
)
1289 u32 f_max
, f_min
, gf
;
1291 gf
= litex_clk_get_real_global_frequency(hw
);
1293 f_min
= gf
/ MAX_DIVIDER
;
1294 /* margin of 1kHz */
1295 if ((rate
> f_max
+ KHZ
) | (rate
< f_min
- KHZ
))
1301 /* Returns closest available clock rate in Hz, parent_rate ignored */
1302 long litex_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
1303 unsigned long *parent_rate
)
1305 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
1306 u32 divider
, fract_cnt
, g_divider
, g_multiplier
;
1307 u8 frac_wf_r
, frac_wf_f
, frac_en
, no_cnt
,
1308 edge
, high_time
, low_time
, clkout_nr
;
1310 if (litex_clk_check_rate_range(hw
, rate
))
1313 LITEX_UNUSED(parent_rate
);
1314 clkout_nr
= l_clkout
->id
;
1316 litex_clk_calc_dividers(hw
, rate
, clkout_nr
, ÷r
, &fract_cnt
,
1317 &frac_wf_r
, &frac_wf_f
, &frac_en
,
1318 &no_cnt
, &edge
, &high_time
, &low_time
);
1319 litex_clk_get_global_divider(hw
, &g_divider
, &g_multiplier
);
1321 return litex_clk_calc_rate(hw
, g_multiplier
, g_divider
, divider
,
1325 /* Set closest available clock rate in Hz, parent_rate ignored */
1326 int litex_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
1327 unsigned long parent_rate
)
1329 struct litex_clk_clkout
*l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
1330 u16 bitset1
, bitset2
;
1331 u32 divider
, fract_cnt
;
1332 u8 frac_wf_r
, frac_wf_f
, frac_en
, no_cnt
,
1333 edge
, high_time
, low_time
, clkout_nr
;
1336 LITEX_UNUSED(parent_rate
);
1337 clkout_nr
= l_clkout
->id
;
1339 pr_debug("set_rate: %lu", rate
);
1340 if (litex_clk_check_rate_range(hw
, rate
)) {
1341 pr_err("CLKOUT%d: cannot set %luHz, frequency not in available range",
1346 litex_clk_calc_dividers(hw
, rate
, clkout_nr
, ÷r
, &fract_cnt
,
1347 &frac_wf_r
, &frac_wf_f
, &frac_en
, &no_cnt
,
1348 &edge
, &high_time
, &low_time
);
1350 bitset1
= (high_time
<< HIGH_TIME_POS
) |
1351 (low_time
<< LOW_TIME_POS
);
1353 bitset2
= (fract_cnt
<< FRAC_POS
) |
1354 (frac_en
<< FRAC_EN_POS
) |
1355 (frac_wf_r
<< FRAC_WF_R_POS
) |
1356 (edge
<< EDGE_POS
) |
1357 (no_cnt
<< NO_CNT_POS
);
1359 u32 phase_mux
, delay_time
, phase_mux_f
,
1360 phase_offset
, period_offset
;
1363 litex_clk_calc_phase_fract(hw
, divider
, fract_cnt
, phase_offset
,
1364 &phase_mux
, &delay_time
,
1365 &phase_mux_f
, &period_offset
);
1367 bitset1
|= (phase_mux
<< PHASE_MUX_POS
);
1368 bitset2
|= (delay_time
<< DELAY_TIME_POS
);
1370 if (frac_wf_f
> 0 || phase_mux_f
> 0) {
1371 u16 clkout5_reg2_bitset
= 0;
1373 clkout5_reg2_bitset
= (phase_mux_f
<< PHASE_MUX_F_POS
) |
1374 (frac_wf_f
<< FRAC_WF_F_POS
);
1376 ret
= litex_clk_change_value(hw
, CLKOUT5_FRAC_MASK
,
1377 clkout5_reg2_bitset
, CLKOUT5_REG2
);
1383 pr_debug("SET RATE: divider:%d fract_cnt:%d frac_wf_r:%d frac_wf_f:%d frac_en:%d no_cnt:%d edge:%d high_time:%d low_time:%d bitset1: 0x%x bitset2: 0x%x",
1384 divider
, fract_cnt
, frac_wf_r
, frac_wf_f
, frac_en
,
1385 no_cnt
, edge
, high_time
, low_time
, bitset1
, bitset2
);
1387 return litex_clk_set_clock(hw
, clkout_nr
, REG1_FREQ_MASK
, bitset1
,
1388 REG2_FREQ_MASK
, bitset2
);
1391 /* Prepare clock output */
1392 int litex_clk_prepare(struct clk_hw
*hw
)
1394 struct litex_clk_clkout
*l_clkout
;
1395 struct clk_duty duty
;
1398 l_clkout
= clk_hw_to_litex_clk_clkout(hw
);
1399 freq
= l_clkout
->default_freq
;
1400 phase
= l_clkout
->default_phase
;
1401 duty
.num
= l_clkout
->default_duty_num
;
1402 duty
.den
= l_clkout
->default_duty_den
;
1404 litex_clk_set_rate(hw
, freq
, 0);
1405 litex_clk_set_duty_cycle(hw
, &duty
);
1406 litex_clk_set_phase(hw
, phase
);
1410 /* Unrepare clock output */
1411 void litex_clk_unprepare(struct clk_hw
*hw
)
1413 struct clk_duty duty
;
1417 litex_clk_set_rate(hw
, 100000000, 0);
1418 litex_clk_set_duty_cycle(hw
, &duty
);
1419 litex_clk_set_phase(hw
, 0);
1422 /* Enable Clock Control MMCM module */
1423 int litex_clk_enable(struct clk_hw
*hw
)
1425 litex_clk_assert_reg(hw
, DRP_RESET
);
1429 /* Disable Clock Control MMCM module */
1430 void litex_clk_disable(struct clk_hw
*hw
)
1432 litex_clk_deassert_reg(hw
, DRP_RESET
);
1435 /* Set default clock value from device tree for given clkout*/
1436 static int litex_clk_set_def_clkout(struct clk_hw
*hw
, u32 freq
, u32 phase
,
1437 struct clk_duty
*duty
)
1441 ret
= litex_clk_set_rate(hw
, freq
, 0);
1444 ret
= litex_clk_set_duty_cycle(hw
, duty
);
1447 ret
= litex_clk_set_phase(hw
, phase
);
1453 static const struct clk_ops litex_clk_ops
= {
1454 .prepare
= litex_clk_prepare
,
1455 .unprepare
= litex_clk_unprepare
,
1456 .enable
= litex_clk_enable
,
1457 .disable
= litex_clk_disable
,
1458 .recalc_rate
= litex_clk_recalc_rate
,
1459 .round_rate
= litex_clk_round_rate
,
1460 .set_rate
= litex_clk_set_rate
,
1461 .get_phase
= litex_clk_get_phase
,
1462 .set_phase
= litex_clk_set_phase
,
1463 .get_duty_cycle
= litex_clk_get_duty_cycle
,
1464 .set_duty_cycle
= litex_clk_set_duty_cycle
,
1467 static const struct of_device_id litex_of_match
[] = {
1468 {.compatible
= "litex,clk"},
1472 MODULE_DEVICE_TABLE(of
, litex_of_match
);
1474 static int litex_clk_read_clkout_dts(struct device_node
*child
,
1475 struct litex_clk_clkout
*clkout
)
1478 u32 dt_num
, dt_freq
, dt_phase
, dt_duty_num
,
1479 dt_duty_den
, f_max
, f_min
, gf
;
1481 gf
= litex_clk_get_expctd_global_frequency(clkout
->sys_clk_freq
);
1483 f_min
= gf
/ MAX_DIVIDER
;
1485 ret
= of_property_read_u32(child
, "reg", &dt_num
);
1489 if (dt_num
> CLKOUT_MAX
) {
1490 pr_err("%s: Invalid CLKOUT index!", child
->name
);
1494 ret
= of_property_read_u32(child
, "litex,clock-frequency", &dt_freq
);
1498 if (dt_freq
> f_max
|| dt_freq
< f_min
) {
1499 pr_err("%s: Invalid default frequency!(%d), MIN/MAX (%d/%d)Hz",
1500 child
->name
, dt_freq
, f_min
, f_max
);
1504 ret
= of_property_read_u32(child
, "litex,clock-phase", &dt_phase
);
1508 ret
= of_property_read_u32(child
, "litex,clock-duty-den", &dt_duty_den
);
1512 ret
= of_property_read_u32(child
, "litex,clock-duty-num", &dt_duty_num
);
1516 if (dt_duty_den
<= 0 || dt_duty_num
> dt_duty_den
) {
1517 pr_err("%s: Invalid default duty! %d/%d", child
->name
,
1518 dt_duty_num
, dt_duty_den
);
1522 clkout
->id
= dt_num
;
1523 clkout
->default_freq
= dt_freq
;
1524 clkout
->default_phase
= dt_phase
;
1525 clkout
->default_duty_num
= dt_duty_num
;
1526 clkout
->default_duty_den
= dt_duty_den
;
1531 static int litex_clk_get_timeouts(struct device_node
*node
, int *dt_lock_timeout
,
1532 int *dt_drdy_timeout
)
1536 /* Read wait_lock timeout from device property*/
1537 ret
= of_property_read_u32(node
, "litex,lock-timeout", dt_lock_timeout
);
1539 pr_err("No litex,lock_timeout entry in the dts file\n");
1542 if (*dt_lock_timeout
< 1) {
1543 pr_err("LiteX CLK driver cannot wait for time bellow ca. 1ms\n");
1547 /* Read wait_drdy timeout from device property*/
1548 ret
= of_property_read_u32(node
, "litex,drdy-timeout", dt_drdy_timeout
);
1550 pr_err("No litex,lock_drdy entry in the dts file\n");
1553 if (*dt_drdy_timeout
< 1) {
1554 pr_err("LiteX CLK driver cannot wait for time bellow ca. 1ms\n");
1561 static int litex_clk_init_clkouts(struct device
*dev
,
1562 struct device_node
*node
,
1563 struct litex_clk_device
*l_dev
)
1565 struct device_node
*child_node
;
1567 struct clk_duty duty
;
1568 u32 dt_lock_timeout
, dt_drdy_timeout
;
1571 ret
= litex_clk_get_timeouts(node
, &dt_lock_timeout
, &dt_drdy_timeout
);
1575 for_each_child_of_node(node
, child_node
) {
1576 struct litex_clk_clkout
*clkout
;
1577 struct clk_init_data clkout_init
;
1579 clkout
= &l_dev
->clkouts
[i
];
1580 clkout
->is_clkout
= true;
1582 clkout
->lock_timeout
= dt_lock_timeout
;
1583 clkout
->drdy_timeout
= dt_drdy_timeout
;
1584 clkout
->sys_clk_freq
= l_dev
->sys_clk_freq
;
1586 ret
= litex_clk_read_clkout_dts(child_node
, clkout
);
1590 clkout_init
.name
= child_node
->name
;
1591 clkout_init
.ops
= &litex_clk_ops
;
1592 clkout_init
.num_parents
= 0;
1593 clkout_init
.flags
= CLK_SET_RATE_UNGATE
| CLK_GET_RATE_NOCACHE
;
1594 clkout
->clk_hw
.init
= &clkout_init
;
1595 clkout
->base
= l_dev
->base
;
1597 duty
.num
= clkout
->default_duty_num
;
1598 duty
.den
= clkout
->default_duty_den
;
1600 /* set global divider and multiplier once */
1602 hw
= &l_dev
->clkouts
[i
].clk_hw
;
1603 litex_clk_enable(hw
);
1604 ret
= litex_clk_set_dm(hw
);
1605 litex_clk_disable(hw
);
1610 ret
= litex_clk_set_def_clkout(&clkout
->clk_hw
,
1611 clkout
->default_freq
,
1612 clkout
->default_phase
, &duty
);
1613 ret
= devm_clk_hw_register(dev
, &clkout
->clk_hw
);
1615 pr_err("devm_clk_hw_register failure, %s ret: %d\n",
1616 child_node
->name
, ret
);
1620 ret
= of_clk_add_hw_provider(child_node
, of_clk_hw_simple_get
,
1629 static int litex_clk_read_global_dts(struct device
*dev
,
1630 struct device_node
*node
,
1631 struct litex_clk_device
*l_dev
)
1633 struct device_node
*child_node
;
1634 struct litex_clk_clkout
*l_clkout
;
1635 u32 dt_nclkout
, dt_lock_timeout
, dt_drdy_timeout
, dt_sys_clk_freq
;
1638 ret
= of_property_read_u32(node
, "litex,sys-clock-frequency",
1641 pr_err("No clock-frequency entry in the dts file\n");
1644 l_dev
->sys_clk_freq
= dt_sys_clk_freq
;
1646 /* Read cnt of CLKOUTs from device property*/
1647 ret
= of_property_read_u32(node
, "litex,nclkout", &dt_nclkout
);
1649 pr_err("No litex,nclkout entry in the dts file\n");
1652 if (dt_nclkout
> CLKOUT_MAX
) {
1653 pr_err("LiteX CLK driver cannot use more than %d clock outputs\n",
1657 l_dev
->nclkout
= dt_nclkout
;
1659 /* count existing CLKOUTs */
1660 for_each_child_of_node(node
, child_node
)
1662 if (i
!= dt_nclkout
) {
1663 pr_err("nclkout(%d) not matching actual CLKOUT count(%d)!",
1668 l_dev
->clkouts
= devm_kzalloc(dev
, sizeof(*l_clkout
) * i
, GFP_KERNEL
);
1669 if (!l_dev
->clkouts
) {
1670 pr_err("CLKOUT memory allocation failure!");
1674 ret
= litex_clk_get_timeouts(node
, &dt_lock_timeout
, &dt_drdy_timeout
);
1677 l_dev
->lock_timeout
= dt_lock_timeout
;
1678 l_dev
->drdy_timeout
= dt_drdy_timeout
;
1683 static int litex_clk_init_glob_clk(struct device
*dev
,
1684 struct device_node
*node
,
1685 struct litex_clk_device
*l_dev
)
1687 struct clk_init_data init
;
1691 init
.name
= node
->name
;
1692 init
.ops
= &litex_clk_ops
;
1693 init
.flags
= CLK_SET_RATE_UNGATE
;
1695 hw
= &l_dev
->clk_hw
;
1698 ret
= devm_clk_hw_register(dev
, hw
);
1700 pr_err("devm_clk_hw_register failure, ret: %d\n", ret
);
1704 ret
= of_clk_add_hw_provider(node
, of_clk_hw_simple_get
, hw
);
1706 pr_err("of_clk_add_hw_provider failure, ret: %d", ret
);
1710 /* Power on MMCM module */
1711 ret
= litex_clk_change_value(hw
, FULL_REG_16
, FULL_REG_16
, POWER_REG
);
1713 pr_err("MMCM initialization failure, ret: %d", ret
);
1720 static int litex_clk_probe(struct platform_device
*pdev
)
1722 const struct of_device_id
*id
;
1724 struct device_node
*node
;
1725 struct litex_clk_device
*litex_clk_device
;
1726 struct resource
*res
;
1730 node
= dev
->of_node
;
1734 id
= of_match_node(litex_of_match
, node
);
1738 litex_clk_device
= devm_kzalloc(dev
, sizeof(*litex_clk_device
),
1740 if (IS_ERR_OR_NULL(litex_clk_device
))
1742 litex_clk_device
->is_clkout
= false;
1744 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1745 if (IS_ERR_OR_NULL(res
))
1748 litex_clk_device
->base
= devm_of_iomap(dev
, node
, 0, &res
->end
);
1749 if (IS_ERR_OR_NULL(litex_clk_device
->base
))
1752 ret
= litex_clk_read_global_dts(dev
, node
, litex_clk_device
);
1756 ret
= litex_clk_init_clkouts(dev
, node
, litex_clk_device
);
1760 ret
= litex_clk_init_glob_clk(dev
, node
, litex_clk_device
);
1764 pr_info("litex clk control driver initialized");
1768 static int litex_clk_remove(struct platform_device
*pdev
)
1770 of_clk_del_provider(pdev
->dev
.of_node
);
1774 static struct platform_driver litex_clk_driver
= {
1776 .name
= "litex-clk",
1777 .of_match_table
= of_match_ptr(litex_of_match
),
1779 .probe
= litex_clk_probe
,
1780 .remove
= litex_clk_remove
,
1783 module_platform_driver(litex_clk_driver
);
1784 MODULE_DESCRIPTION("LiteX clock driver");
1785 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
1786 MODULE_LICENSE("GPL v2");