1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
6 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
7 * Dmitry Dunaev <dmitry.dunaev@baikalelectronics.ru>
9 * Baikal-T1 CCU Dividers interface driver
12 #define pr_fmt(fmt) "bt1-ccu-div: " fmt
14 #include <linux/kernel.h>
15 #include <linux/printk.h>
16 #include <linux/bits.h>
17 #include <linux/bitfield.h>
18 #include <linux/slab.h>
19 #include <linux/clk-provider.h>
21 #include <linux/spinlock.h>
22 #include <linux/regmap.h>
23 #include <linux/delay.h>
24 #include <linux/time64.h>
25 #include <linux/debugfs.h>
29 #define CCU_DIV_CTL 0x00
30 #define CCU_DIV_CTL_EN BIT(0)
31 #define CCU_DIV_CTL_RST BIT(1)
32 #define CCU_DIV_CTL_SET_CLKDIV BIT(2)
33 #define CCU_DIV_CTL_CLKDIV_FLD 4
34 #define CCU_DIV_CTL_CLKDIV_MASK(_width) \
35 GENMASK((_width) + CCU_DIV_CTL_CLKDIV_FLD - 1, CCU_DIV_CTL_CLKDIV_FLD)
36 #define CCU_DIV_CTL_LOCK_SHIFTED BIT(27)
37 #define CCU_DIV_CTL_GATE_REF_BUF BIT(28)
38 #define CCU_DIV_CTL_LOCK_NORMAL BIT(31)
40 #define CCU_DIV_LOCK_CHECK_RETRIES 50
42 #define CCU_DIV_CLKDIV_MIN 0
43 #define CCU_DIV_CLKDIV_MAX(_mask) \
44 ((_mask) >> CCU_DIV_CTL_CLKDIV_FLD)
47 * Use the next two methods until there are generic field setter and
48 * getter available with non-constant mask support.
50 static inline u32
ccu_div_get(u32 mask
, u32 val
)
52 return (val
& mask
) >> CCU_DIV_CTL_CLKDIV_FLD
;
55 static inline u32
ccu_div_prep(u32 mask
, u32 val
)
57 return (val
<< CCU_DIV_CTL_CLKDIV_FLD
) & mask
;
60 static inline unsigned long ccu_div_lock_delay_ns(unsigned long ref_clk
,
63 u64 ns
= 4ULL * (div
?: 1) * NSEC_PER_SEC
;
70 static inline unsigned long ccu_div_calc_freq(unsigned long ref_clk
,
73 return ref_clk
/ (div
?: 1);
76 static int ccu_div_var_update_clkdiv(struct ccu_div
*div
,
77 unsigned long parent_rate
,
78 unsigned long divider
)
85 nd
= ccu_div_lock_delay_ns(parent_rate
, divider
);
87 if (div
->features
& CCU_DIV_LOCK_SHIFTED
)
88 lock
= CCU_DIV_CTL_LOCK_SHIFTED
;
90 lock
= CCU_DIV_CTL_LOCK_NORMAL
;
92 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
93 CCU_DIV_CTL_SET_CLKDIV
, CCU_DIV_CTL_SET_CLKDIV
);
96 * Until there is nsec-version of readl_poll_timeout() is available
97 * we have to implement the next polling loop.
99 count
= CCU_DIV_LOCK_CHECK_RETRIES
;
102 regmap_read(div
->sys_regs
, div
->reg_ctl
, &val
);
110 static int ccu_div_var_enable(struct clk_hw
*hw
)
112 struct clk_hw
*parent_hw
= clk_hw_get_parent(hw
);
113 struct ccu_div
*div
= to_ccu_div(hw
);
119 pr_err("Can't enable '%s' with no parent", clk_hw_get_name(hw
));
123 regmap_read(div
->sys_regs
, div
->reg_ctl
, &val
);
124 if (val
& CCU_DIV_CTL_EN
)
127 spin_lock_irqsave(&div
->lock
, flags
);
128 ret
= ccu_div_var_update_clkdiv(div
, clk_hw_get_rate(parent_hw
),
129 ccu_div_get(div
->mask
, val
));
131 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
132 CCU_DIV_CTL_EN
, CCU_DIV_CTL_EN
);
133 spin_unlock_irqrestore(&div
->lock
, flags
);
135 pr_err("Divider '%s' lock timed out\n", clk_hw_get_name(hw
));
140 static int ccu_div_gate_enable(struct clk_hw
*hw
)
142 struct ccu_div
*div
= to_ccu_div(hw
);
145 spin_lock_irqsave(&div
->lock
, flags
);
146 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
147 CCU_DIV_CTL_EN
, CCU_DIV_CTL_EN
);
148 spin_unlock_irqrestore(&div
->lock
, flags
);
153 static void ccu_div_gate_disable(struct clk_hw
*hw
)
155 struct ccu_div
*div
= to_ccu_div(hw
);
158 spin_lock_irqsave(&div
->lock
, flags
);
159 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
, CCU_DIV_CTL_EN
, 0);
160 spin_unlock_irqrestore(&div
->lock
, flags
);
163 static int ccu_div_gate_is_enabled(struct clk_hw
*hw
)
165 struct ccu_div
*div
= to_ccu_div(hw
);
168 regmap_read(div
->sys_regs
, div
->reg_ctl
, &val
);
170 return !!(val
& CCU_DIV_CTL_EN
);
173 static int ccu_div_buf_enable(struct clk_hw
*hw
)
175 struct ccu_div
*div
= to_ccu_div(hw
);
178 spin_lock_irqsave(&div
->lock
, flags
);
179 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
180 CCU_DIV_CTL_GATE_REF_BUF
, 0);
181 spin_unlock_irqrestore(&div
->lock
, flags
);
186 static void ccu_div_buf_disable(struct clk_hw
*hw
)
188 struct ccu_div
*div
= to_ccu_div(hw
);
191 spin_lock_irqsave(&div
->lock
, flags
);
192 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
193 CCU_DIV_CTL_GATE_REF_BUF
, CCU_DIV_CTL_GATE_REF_BUF
);
194 spin_unlock_irqrestore(&div
->lock
, flags
);
197 static int ccu_div_buf_is_enabled(struct clk_hw
*hw
)
199 struct ccu_div
*div
= to_ccu_div(hw
);
202 regmap_read(div
->sys_regs
, div
->reg_ctl
, &val
);
204 return !(val
& CCU_DIV_CTL_GATE_REF_BUF
);
207 static unsigned long ccu_div_var_recalc_rate(struct clk_hw
*hw
,
208 unsigned long parent_rate
)
210 struct ccu_div
*div
= to_ccu_div(hw
);
211 unsigned long divider
;
214 regmap_read(div
->sys_regs
, div
->reg_ctl
, &val
);
215 divider
= ccu_div_get(div
->mask
, val
);
217 return ccu_div_calc_freq(parent_rate
, divider
);
220 static inline unsigned long ccu_div_var_calc_divider(unsigned long rate
,
221 unsigned long parent_rate
,
224 unsigned long divider
;
226 divider
= parent_rate
/ rate
;
227 return clamp_t(unsigned long, divider
, CCU_DIV_CLKDIV_MIN
,
228 CCU_DIV_CLKDIV_MAX(mask
));
231 static long ccu_div_var_round_rate(struct clk_hw
*hw
, unsigned long rate
,
232 unsigned long *parent_rate
)
234 struct ccu_div
*div
= to_ccu_div(hw
);
235 unsigned long divider
;
237 divider
= ccu_div_var_calc_divider(rate
, *parent_rate
, div
->mask
);
239 return ccu_div_calc_freq(*parent_rate
, divider
);
243 * This method is used for the clock divider blocks, which support the
244 * on-the-fly rate change. So due to lacking the EN bit functionality
245 * they can't be gated before the rate adjustment.
247 static int ccu_div_var_set_rate_slow(struct clk_hw
*hw
, unsigned long rate
,
248 unsigned long parent_rate
)
250 struct ccu_div
*div
= to_ccu_div(hw
);
251 unsigned long flags
, divider
;
255 divider
= ccu_div_var_calc_divider(rate
, parent_rate
, div
->mask
);
256 if (divider
== 1 && div
->features
& CCU_DIV_SKIP_ONE
) {
258 } else if (div
->features
& CCU_DIV_SKIP_ONE_TO_THREE
) {
259 if (divider
== 1 || divider
== 2)
261 else if (divider
== 3)
265 val
= ccu_div_prep(div
->mask
, divider
);
267 spin_lock_irqsave(&div
->lock
, flags
);
268 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
, div
->mask
, val
);
269 ret
= ccu_div_var_update_clkdiv(div
, parent_rate
, divider
);
270 spin_unlock_irqrestore(&div
->lock
, flags
);
272 pr_err("Divider '%s' lock timed out\n", clk_hw_get_name(hw
));
278 * This method is used for the clock divider blocks, which don't support
279 * the on-the-fly rate change.
281 static int ccu_div_var_set_rate_fast(struct clk_hw
*hw
, unsigned long rate
,
282 unsigned long parent_rate
)
284 struct ccu_div
*div
= to_ccu_div(hw
);
285 unsigned long flags
, divider
;
288 divider
= ccu_div_var_calc_divider(rate
, parent_rate
, div
->mask
);
289 val
= ccu_div_prep(div
->mask
, divider
);
292 * Also disable the clock divider block if it was enabled by default
293 * or by the bootloader.
295 spin_lock_irqsave(&div
->lock
, flags
);
296 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
297 div
->mask
| CCU_DIV_CTL_EN
, val
);
298 spin_unlock_irqrestore(&div
->lock
, flags
);
303 static unsigned long ccu_div_fixed_recalc_rate(struct clk_hw
*hw
,
304 unsigned long parent_rate
)
306 struct ccu_div
*div
= to_ccu_div(hw
);
308 return ccu_div_calc_freq(parent_rate
, div
->divider
);
311 static long ccu_div_fixed_round_rate(struct clk_hw
*hw
, unsigned long rate
,
312 unsigned long *parent_rate
)
314 struct ccu_div
*div
= to_ccu_div(hw
);
316 return ccu_div_calc_freq(*parent_rate
, div
->divider
);
319 static int ccu_div_fixed_set_rate(struct clk_hw
*hw
, unsigned long rate
,
320 unsigned long parent_rate
)
325 #ifdef CONFIG_DEBUG_FS
327 struct ccu_div_dbgfs_bit
{
333 #define CCU_DIV_DBGFS_BIT_ATTR(_name, _mask) { \
338 static const struct ccu_div_dbgfs_bit ccu_div_bits
[] = {
339 CCU_DIV_DBGFS_BIT_ATTR("div_en", CCU_DIV_CTL_EN
),
340 CCU_DIV_DBGFS_BIT_ATTR("div_rst", CCU_DIV_CTL_RST
),
341 CCU_DIV_DBGFS_BIT_ATTR("div_bypass", CCU_DIV_CTL_SET_CLKDIV
),
342 CCU_DIV_DBGFS_BIT_ATTR("div_buf", CCU_DIV_CTL_GATE_REF_BUF
),
343 CCU_DIV_DBGFS_BIT_ATTR("div_lock", CCU_DIV_CTL_LOCK_NORMAL
)
346 #define CCU_DIV_DBGFS_BIT_NUM ARRAY_SIZE(ccu_div_bits)
349 * It can be dangerous to change the Divider settings behind clock framework
350 * back, therefore we don't provide any kernel config based compile time option
351 * for this feature to enable.
353 #undef CCU_DIV_ALLOW_WRITE_DEBUGFS
354 #ifdef CCU_DIV_ALLOW_WRITE_DEBUGFS
356 static int ccu_div_dbgfs_bit_set(void *priv
, u64 val
)
358 const struct ccu_div_dbgfs_bit
*bit
= priv
;
359 struct ccu_div
*div
= bit
->div
;
362 spin_lock_irqsave(&div
->lock
, flags
);
363 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
,
364 bit
->mask
, val
? bit
->mask
: 0);
365 spin_unlock_irqrestore(&div
->lock
, flags
);
370 static int ccu_div_dbgfs_var_clkdiv_set(void *priv
, u64 val
)
372 struct ccu_div
*div
= priv
;
376 val
= clamp_t(u64
, val
, CCU_DIV_CLKDIV_MIN
,
377 CCU_DIV_CLKDIV_MAX(div
->mask
));
378 data
= ccu_div_prep(div
->mask
, val
);
380 spin_lock_irqsave(&div
->lock
, flags
);
381 regmap_update_bits(div
->sys_regs
, div
->reg_ctl
, div
->mask
, data
);
382 spin_unlock_irqrestore(&div
->lock
, flags
);
387 #define ccu_div_dbgfs_mode 0644
389 #else /* !CCU_DIV_ALLOW_WRITE_DEBUGFS */
391 #define ccu_div_dbgfs_bit_set NULL
392 #define ccu_div_dbgfs_var_clkdiv_set NULL
393 #define ccu_div_dbgfs_mode 0444
395 #endif /* !CCU_DIV_ALLOW_WRITE_DEBUGFS */
397 static int ccu_div_dbgfs_bit_get(void *priv
, u64
*val
)
399 const struct ccu_div_dbgfs_bit
*bit
= priv
;
400 struct ccu_div
*div
= bit
->div
;
403 regmap_read(div
->sys_regs
, div
->reg_ctl
, &data
);
404 *val
= !!(data
& bit
->mask
);
408 DEFINE_DEBUGFS_ATTRIBUTE(ccu_div_dbgfs_bit_fops
,
409 ccu_div_dbgfs_bit_get
, ccu_div_dbgfs_bit_set
, "%llu\n");
411 static int ccu_div_dbgfs_var_clkdiv_get(void *priv
, u64
*val
)
413 struct ccu_div
*div
= priv
;
416 regmap_read(div
->sys_regs
, div
->reg_ctl
, &data
);
417 *val
= ccu_div_get(div
->mask
, data
);
421 DEFINE_DEBUGFS_ATTRIBUTE(ccu_div_dbgfs_var_clkdiv_fops
,
422 ccu_div_dbgfs_var_clkdiv_get
, ccu_div_dbgfs_var_clkdiv_set
, "%llu\n");
424 static int ccu_div_dbgfs_fixed_clkdiv_get(void *priv
, u64
*val
)
426 struct ccu_div
*div
= priv
;
432 DEFINE_DEBUGFS_ATTRIBUTE(ccu_div_dbgfs_fixed_clkdiv_fops
,
433 ccu_div_dbgfs_fixed_clkdiv_get
, NULL
, "%llu\n");
435 static void ccu_div_var_debug_init(struct clk_hw
*hw
, struct dentry
*dentry
)
437 struct ccu_div
*div
= to_ccu_div(hw
);
438 struct ccu_div_dbgfs_bit
*bits
;
439 int didx
, bidx
, num
= 2;
442 num
+= !!(div
->flags
& CLK_SET_RATE_GATE
) +
443 !!(div
->features
& CCU_DIV_RESET_DOMAIN
);
445 bits
= kcalloc(num
, sizeof(*bits
), GFP_KERNEL
);
449 for (didx
= 0, bidx
= 0; bidx
< CCU_DIV_DBGFS_BIT_NUM
; ++bidx
) {
450 name
= ccu_div_bits
[bidx
].name
;
451 if (!(div
->flags
& CLK_SET_RATE_GATE
) &&
452 !strcmp("div_en", name
)) {
456 if (!(div
->features
& CCU_DIV_RESET_DOMAIN
) &&
457 !strcmp("div_rst", name
)) {
461 if (!strcmp("div_buf", name
))
464 bits
[didx
] = ccu_div_bits
[bidx
];
465 bits
[didx
].div
= div
;
467 if (div
->features
& CCU_DIV_LOCK_SHIFTED
&&
468 !strcmp("div_lock", name
)) {
469 bits
[didx
].mask
= CCU_DIV_CTL_LOCK_SHIFTED
;
472 debugfs_create_file_unsafe(bits
[didx
].name
, ccu_div_dbgfs_mode
,
474 &ccu_div_dbgfs_bit_fops
);
478 debugfs_create_file_unsafe("div_clkdiv", ccu_div_dbgfs_mode
, dentry
,
479 div
, &ccu_div_dbgfs_var_clkdiv_fops
);
482 static void ccu_div_gate_debug_init(struct clk_hw
*hw
, struct dentry
*dentry
)
484 struct ccu_div
*div
= to_ccu_div(hw
);
485 struct ccu_div_dbgfs_bit
*bit
;
487 bit
= kmalloc(sizeof(*bit
), GFP_KERNEL
);
491 *bit
= ccu_div_bits
[0];
493 debugfs_create_file_unsafe(bit
->name
, ccu_div_dbgfs_mode
, dentry
, bit
,
494 &ccu_div_dbgfs_bit_fops
);
496 debugfs_create_file_unsafe("div_clkdiv", 0400, dentry
, div
,
497 &ccu_div_dbgfs_fixed_clkdiv_fops
);
500 static void ccu_div_buf_debug_init(struct clk_hw
*hw
, struct dentry
*dentry
)
502 struct ccu_div
*div
= to_ccu_div(hw
);
503 struct ccu_div_dbgfs_bit
*bit
;
505 bit
= kmalloc(sizeof(*bit
), GFP_KERNEL
);
509 *bit
= ccu_div_bits
[3];
511 debugfs_create_file_unsafe(bit
->name
, ccu_div_dbgfs_mode
, dentry
, bit
,
512 &ccu_div_dbgfs_bit_fops
);
515 static void ccu_div_fixed_debug_init(struct clk_hw
*hw
, struct dentry
*dentry
)
517 struct ccu_div
*div
= to_ccu_div(hw
);
519 debugfs_create_file_unsafe("div_clkdiv", 0400, dentry
, div
,
520 &ccu_div_dbgfs_fixed_clkdiv_fops
);
523 #else /* !CONFIG_DEBUG_FS */
525 #define ccu_div_var_debug_init NULL
526 #define ccu_div_gate_debug_init NULL
527 #define ccu_div_buf_debug_init NULL
528 #define ccu_div_fixed_debug_init NULL
530 #endif /* !CONFIG_DEBUG_FS */
532 static const struct clk_ops ccu_div_var_gate_to_set_ops
= {
533 .enable
= ccu_div_var_enable
,
534 .disable
= ccu_div_gate_disable
,
535 .is_enabled
= ccu_div_gate_is_enabled
,
536 .recalc_rate
= ccu_div_var_recalc_rate
,
537 .round_rate
= ccu_div_var_round_rate
,
538 .set_rate
= ccu_div_var_set_rate_fast
,
539 .debug_init
= ccu_div_var_debug_init
542 static const struct clk_ops ccu_div_var_nogate_ops
= {
543 .recalc_rate
= ccu_div_var_recalc_rate
,
544 .round_rate
= ccu_div_var_round_rate
,
545 .set_rate
= ccu_div_var_set_rate_slow
,
546 .debug_init
= ccu_div_var_debug_init
549 static const struct clk_ops ccu_div_gate_ops
= {
550 .enable
= ccu_div_gate_enable
,
551 .disable
= ccu_div_gate_disable
,
552 .is_enabled
= ccu_div_gate_is_enabled
,
553 .recalc_rate
= ccu_div_fixed_recalc_rate
,
554 .round_rate
= ccu_div_fixed_round_rate
,
555 .set_rate
= ccu_div_fixed_set_rate
,
556 .debug_init
= ccu_div_gate_debug_init
559 static const struct clk_ops ccu_div_buf_ops
= {
560 .enable
= ccu_div_buf_enable
,
561 .disable
= ccu_div_buf_disable
,
562 .is_enabled
= ccu_div_buf_is_enabled
,
563 .debug_init
= ccu_div_buf_debug_init
566 static const struct clk_ops ccu_div_fixed_ops
= {
567 .recalc_rate
= ccu_div_fixed_recalc_rate
,
568 .round_rate
= ccu_div_fixed_round_rate
,
569 .set_rate
= ccu_div_fixed_set_rate
,
570 .debug_init
= ccu_div_fixed_debug_init
573 struct ccu_div
*ccu_div_hw_register(const struct ccu_div_init_data
*div_init
)
575 struct clk_parent_data parent_data
= { };
576 struct clk_init_data hw_init
= { };
581 return ERR_PTR(-EINVAL
);
583 div
= kzalloc(sizeof(*div
), GFP_KERNEL
);
585 return ERR_PTR(-ENOMEM
);
588 * Note since Baikal-T1 System Controller registers are MMIO-backed
589 * we won't check the regmap IO operations return status, because it
590 * must be zero anyway.
592 div
->hw
.init
= &hw_init
;
593 div
->id
= div_init
->id
;
594 div
->reg_ctl
= div_init
->base
+ CCU_DIV_CTL
;
595 div
->sys_regs
= div_init
->sys_regs
;
596 div
->flags
= div_init
->flags
;
597 div
->features
= div_init
->features
;
598 spin_lock_init(&div
->lock
);
600 hw_init
.name
= div_init
->name
;
601 hw_init
.flags
= div_init
->flags
;
603 if (div_init
->type
== CCU_DIV_VAR
) {
604 if (hw_init
.flags
& CLK_SET_RATE_GATE
)
605 hw_init
.ops
= &ccu_div_var_gate_to_set_ops
;
607 hw_init
.ops
= &ccu_div_var_nogate_ops
;
608 div
->mask
= CCU_DIV_CTL_CLKDIV_MASK(div_init
->width
);
609 } else if (div_init
->type
== CCU_DIV_GATE
) {
610 hw_init
.ops
= &ccu_div_gate_ops
;
611 div
->divider
= div_init
->divider
;
612 } else if (div_init
->type
== CCU_DIV_BUF
) {
613 hw_init
.ops
= &ccu_div_buf_ops
;
614 } else if (div_init
->type
== CCU_DIV_FIXED
) {
615 hw_init
.ops
= &ccu_div_fixed_ops
;
616 div
->divider
= div_init
->divider
;
622 if (!div_init
->parent_name
) {
626 parent_data
.fw_name
= div_init
->parent_name
;
627 parent_data
.name
= div_init
->parent_name
;
628 hw_init
.parent_data
= &parent_data
;
629 hw_init
.num_parents
= 1;
631 ret
= of_clk_hw_register(div_init
->np
, &div
->hw
);
643 void ccu_div_hw_unregister(struct ccu_div
*div
)
645 clk_hw_unregister(&div
->hw
);