1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Broadcom Corporation
4 * Copyright 2013 Linaro Limited
9 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/clk-provider.h>
15 * "Policies" affect the frequencies of bus clocks provided by a
16 * CCU. (I believe these polices are named "Deep Sleep", "Economy",
17 * "Normal", and "Turbo".) A lower policy number has lower power
18 * consumption, and policy 2 is the default.
20 #define CCU_POLICY_COUNT 4
22 #define CCU_ACCESS_PASSWORD 0xA5A500
23 #define CLK_GATE_DELAY_LOOP 2000
25 /* Bitfield operations */
27 /* Produces a mask of set bits covering a range of a 32-bit value */
28 static inline u32
bitfield_mask(u32 shift
, u32 width
)
30 return ((1 << width
) - 1) << shift
;
33 /* Extract the value of a bitfield found within a given register value */
34 static inline u32
bitfield_extract(u32 reg_val
, u32 shift
, u32 width
)
36 return (reg_val
& bitfield_mask(shift
, width
)) >> shift
;
39 /* Replace the value of a bitfield found within a given register value */
40 static inline u32
bitfield_replace(u32 reg_val
, u32 shift
, u32 width
, u32 val
)
42 u32 mask
= bitfield_mask(shift
, width
);
44 return (reg_val
& ~mask
) | (val
<< shift
);
47 /* Divider and scaling helpers */
49 /* Convert a divider into the scaled divisor value it represents. */
50 static inline u64
scaled_div_value(struct bcm_clk_div
*div
, u32 reg_div
)
52 return (u64
)reg_div
+ ((u64
)1 << div
->u
.s
.frac_width
);
56 * Build a scaled divider value as close as possible to the
57 * given whole part (div_value) and fractional part (expressed
60 u64
scaled_div_build(struct bcm_clk_div
*div
, u32 div_value
, u32 billionths
)
65 BUG_ON(billionths
>= BILLION
);
67 combined
= (u64
)div_value
* BILLION
+ billionths
;
68 combined
<<= div
->u
.s
.frac_width
;
70 return DIV_ROUND_CLOSEST_ULL(combined
, BILLION
);
73 /* The scaled minimum divisor representable by a divider */
75 scaled_div_min(struct bcm_clk_div
*div
)
77 if (divider_is_fixed(div
))
78 return (u64
)div
->u
.fixed
;
80 return scaled_div_value(div
, 0);
83 /* The scaled maximum divisor representable by a divider */
84 u64
scaled_div_max(struct bcm_clk_div
*div
)
88 if (divider_is_fixed(div
))
89 return (u64
)div
->u
.fixed
;
91 reg_div
= ((u32
)1 << div
->u
.s
.width
) - 1;
93 return scaled_div_value(div
, reg_div
);
97 * Convert a scaled divisor into its divider representation as
98 * stored in a divider register field.
101 divider(struct bcm_clk_div
*div
, u64 scaled_div
)
103 BUG_ON(scaled_div
< scaled_div_min(div
));
104 BUG_ON(scaled_div
> scaled_div_max(div
));
106 return (u32
)(scaled_div
- ((u64
)1 << div
->u
.s
.frac_width
));
109 /* Return a rate scaled for use when dividing by a scaled divisor. */
111 scale_rate(struct bcm_clk_div
*div
, u32 rate
)
113 if (divider_is_fixed(div
))
116 return (u64
)rate
<< div
->u
.s
.frac_width
;
121 /* Read a 32-bit register value from a CCU's address space. */
122 static inline u32
__ccu_read(struct ccu_data
*ccu
, u32 reg_offset
)
124 return readl(ccu
->base
+ reg_offset
);
127 /* Write a 32-bit register value into a CCU's address space. */
129 __ccu_write(struct ccu_data
*ccu
, u32 reg_offset
, u32 reg_val
)
131 writel(reg_val
, ccu
->base
+ reg_offset
);
134 static inline unsigned long ccu_lock(struct ccu_data
*ccu
)
138 spin_lock_irqsave(&ccu
->lock
, flags
);
142 static inline void ccu_unlock(struct ccu_data
*ccu
, unsigned long flags
)
144 spin_unlock_irqrestore(&ccu
->lock
, flags
);
148 * Enable/disable write access to CCU protected registers. The
149 * WR_ACCESS register for all CCUs is at offset 0.
151 static inline void __ccu_write_enable(struct ccu_data
*ccu
)
153 if (ccu
->write_enabled
) {
154 pr_err("%s: access already enabled for %s\n", __func__
,
158 ccu
->write_enabled
= true;
159 __ccu_write(ccu
, 0, CCU_ACCESS_PASSWORD
| 1);
162 static inline void __ccu_write_disable(struct ccu_data
*ccu
)
164 if (!ccu
->write_enabled
) {
165 pr_err("%s: access wasn't enabled for %s\n", __func__
,
170 __ccu_write(ccu
, 0, CCU_ACCESS_PASSWORD
);
171 ccu
->write_enabled
= false;
175 * Poll a register in a CCU's address space, returning when the
176 * specified bit in that register's value is set (or clear). Delay
177 * a microsecond after each read of the register. Returns true if
178 * successful, or false if we gave up trying.
180 * Caller must ensure the CCU lock is held.
183 __ccu_wait_bit(struct ccu_data
*ccu
, u32 reg_offset
, u32 bit
, bool want
)
186 u32 bit_mask
= 1 << bit
;
188 for (tries
= 0; tries
< CLK_GATE_DELAY_LOOP
; tries
++) {
192 val
= __ccu_read(ccu
, reg_offset
);
193 bit_val
= (val
& bit_mask
) != 0;
198 pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__
,
199 ccu
->name
, reg_offset
, bit
, want
? "set" : "clear");
204 /* Policy operations */
206 static bool __ccu_policy_engine_start(struct ccu_data
*ccu
, bool sync
)
208 struct bcm_policy_ctl
*control
= &ccu
->policy
.control
;
214 /* If we don't need to control policy for this CCU, we're done. */
215 if (!policy_ctl_exists(control
))
218 offset
= control
->offset
;
219 go_bit
= control
->go_bit
;
221 /* Ensure we're not busy before we start */
222 ret
= __ccu_wait_bit(ccu
, offset
, go_bit
, false);
224 pr_err("%s: ccu %s policy engine wouldn't go idle\n",
225 __func__
, ccu
->name
);
230 * If it's a synchronous request, we'll wait for the voltage
231 * and frequency of the active load to stabilize before
232 * returning. To do this we select the active load by
233 * setting the ATL bit.
235 * An asynchronous request instead ramps the voltage in the
236 * background, and when that process stabilizes, the target
237 * load is copied to the active load and the CCU frequency
238 * is switched. We do this by selecting the target load
239 * (ATL bit clear) and setting the request auto-copy (AC bit
242 * Note, we do NOT read-modify-write this register.
244 mask
= (u32
)1 << go_bit
;
246 mask
|= 1 << control
->atl_bit
;
248 mask
|= 1 << control
->ac_bit
;
249 __ccu_write(ccu
, offset
, mask
);
251 /* Wait for indication that operation is complete. */
252 ret
= __ccu_wait_bit(ccu
, offset
, go_bit
, false);
254 pr_err("%s: ccu %s policy engine never started\n",
255 __func__
, ccu
->name
);
260 static bool __ccu_policy_engine_stop(struct ccu_data
*ccu
)
262 struct bcm_lvm_en
*enable
= &ccu
->policy
.enable
;
267 /* If we don't need to control policy for this CCU, we're done. */
268 if (!policy_lvm_en_exists(enable
))
271 /* Ensure we're not busy before we start */
272 offset
= enable
->offset
;
273 enable_bit
= enable
->bit
;
274 ret
= __ccu_wait_bit(ccu
, offset
, enable_bit
, false);
276 pr_err("%s: ccu %s policy engine already stopped\n",
277 __func__
, ccu
->name
);
281 /* Now set the bit to stop the engine (NO read-modify-write) */
282 __ccu_write(ccu
, offset
, (u32
)1 << enable_bit
);
284 /* Wait for indication that it has stopped. */
285 ret
= __ccu_wait_bit(ccu
, offset
, enable_bit
, false);
287 pr_err("%s: ccu %s policy engine never stopped\n",
288 __func__
, ccu
->name
);
294 * A CCU has four operating conditions ("policies"), and some clocks
295 * can be disabled or enabled based on which policy is currently in
296 * effect. Such clocks have a bit in a "policy mask" register for
297 * each policy indicating whether the clock is enabled for that
298 * policy or not. The bit position for a clock is the same for all
299 * four registers, and the 32-bit registers are at consecutive
302 static bool policy_init(struct ccu_data
*ccu
, struct bcm_clk_policy
*policy
)
309 if (!policy_exists(policy
))
313 * We need to stop the CCU policy engine to allow update
314 * of our policy bits.
316 if (!__ccu_policy_engine_stop(ccu
)) {
317 pr_err("%s: unable to stop CCU %s policy engine\n",
318 __func__
, ccu
->name
);
323 * For now, if a clock defines its policy bit we just mark
324 * it "enabled" for all four policies.
326 offset
= policy
->offset
;
327 mask
= (u32
)1 << policy
->bit
;
328 for (i
= 0; i
< CCU_POLICY_COUNT
; i
++) {
331 reg_val
= __ccu_read(ccu
, offset
);
333 __ccu_write(ccu
, offset
, reg_val
);
334 offset
+= sizeof(u32
);
337 /* We're done updating; fire up the policy engine again. */
338 ret
= __ccu_policy_engine_start(ccu
, true);
340 pr_err("%s: unable to restart CCU %s policy engine\n",
341 __func__
, ccu
->name
);
346 /* Gate operations */
348 /* Determine whether a clock is gated. CCU lock must be held. */
350 __is_clk_gate_enabled(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
)
355 /* If there is no gate we can assume it's enabled. */
356 if (!gate_exists(gate
))
359 bit_mask
= 1 << gate
->status_bit
;
360 reg_val
= __ccu_read(ccu
, gate
->offset
);
362 return (reg_val
& bit_mask
) != 0;
365 /* Determine whether a clock is gated. */
367 is_clk_gate_enabled(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
)
372 /* Avoid taking the lock if we can */
373 if (!gate_exists(gate
))
376 flags
= ccu_lock(ccu
);
377 ret
= __is_clk_gate_enabled(ccu
, gate
);
378 ccu_unlock(ccu
, flags
);
384 * Commit our desired gate state to the hardware.
385 * Returns true if successful, false otherwise.
388 __gate_commit(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
)
392 bool enabled
= false;
394 BUG_ON(!gate_exists(gate
));
395 if (!gate_is_sw_controllable(gate
))
396 return true; /* Nothing we can change */
398 reg_val
= __ccu_read(ccu
, gate
->offset
);
400 /* For a hardware/software gate, set which is in control */
401 if (gate_is_hw_controllable(gate
)) {
402 mask
= (u32
)1 << gate
->hw_sw_sel_bit
;
403 if (gate_is_sw_managed(gate
))
410 * If software is in control, enable or disable the gate.
411 * If hardware is, clear the enabled bit for good measure.
412 * If a software controlled gate can't be disabled, we're
413 * required to write a 0 into the enable bit (but the gate
416 mask
= (u32
)1 << gate
->en_bit
;
417 if (gate_is_sw_managed(gate
) && (enabled
= gate_is_enabled(gate
)) &&
418 !gate_is_no_disable(gate
))
423 __ccu_write(ccu
, gate
->offset
, reg_val
);
425 /* For a hardware controlled gate, we're done */
426 if (!gate_is_sw_managed(gate
))
429 /* Otherwise wait for the gate to be in desired state */
430 return __ccu_wait_bit(ccu
, gate
->offset
, gate
->status_bit
, enabled
);
434 * Initialize a gate. Our desired state (hardware/software select,
435 * and if software, its enable state) is committed to hardware
436 * without the usual checks to see if it's already set up that way.
437 * Returns true if successful, false otherwise.
439 static bool gate_init(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
)
441 if (!gate_exists(gate
))
443 return __gate_commit(ccu
, gate
);
447 * Set a gate to enabled or disabled state. Does nothing if the
448 * gate is not currently under software control, or if it is already
449 * in the requested state. Returns true if successful, false
450 * otherwise. CCU lock must be held.
453 __clk_gate(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
, bool enable
)
457 if (!gate_exists(gate
) || !gate_is_sw_managed(gate
))
458 return true; /* Nothing to do */
460 if (!enable
&& gate_is_no_disable(gate
)) {
461 pr_warn("%s: invalid gate disable request (ignoring)\n",
466 if (enable
== gate_is_enabled(gate
))
467 return true; /* No change */
469 gate_flip_enabled(gate
);
470 ret
= __gate_commit(ccu
, gate
);
472 gate_flip_enabled(gate
); /* Revert the change */
477 /* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */
478 static int clk_gate(struct ccu_data
*ccu
, const char *name
,
479 struct bcm_clk_gate
*gate
, bool enable
)
485 * Avoid taking the lock if we can. We quietly ignore
486 * requests to change state that don't make sense.
488 if (!gate_exists(gate
) || !gate_is_sw_managed(gate
))
490 if (!enable
&& gate_is_no_disable(gate
))
493 flags
= ccu_lock(ccu
);
494 __ccu_write_enable(ccu
);
496 success
= __clk_gate(ccu
, gate
, enable
);
498 __ccu_write_disable(ccu
);
499 ccu_unlock(ccu
, flags
);
504 pr_err("%s: failed to %s gate for %s\n", __func__
,
505 enable
? "enable" : "disable", name
);
510 /* Hysteresis operations */
513 * If a clock gate requires a turn-off delay it will have
514 * "hysteresis" register bits defined. The first, if set, enables
515 * the delay; and if enabled, the second bit determines whether the
516 * delay is "low" or "high" (1 means high). For now, if it's
517 * defined for a clock, we set it.
519 static bool hyst_init(struct ccu_data
*ccu
, struct bcm_clk_hyst
*hyst
)
525 if (!hyst_exists(hyst
))
528 offset
= hyst
->offset
;
529 mask
= (u32
)1 << hyst
->en_bit
;
530 mask
|= (u32
)1 << hyst
->val_bit
;
532 reg_val
= __ccu_read(ccu
, offset
);
534 __ccu_write(ccu
, offset
, reg_val
);
539 /* Trigger operations */
542 * Caller must ensure CCU lock is held and access is enabled.
543 * Returns true if successful, false otherwise.
545 static bool __clk_trigger(struct ccu_data
*ccu
, struct bcm_clk_trig
*trig
)
547 /* Trigger the clock and wait for it to finish */
548 __ccu_write(ccu
, trig
->offset
, 1 << trig
->bit
);
550 return __ccu_wait_bit(ccu
, trig
->offset
, trig
->bit
, false);
553 /* Divider operations */
555 /* Read a divider value and return the scaled divisor it represents. */
556 static u64
divider_read_scaled(struct ccu_data
*ccu
, struct bcm_clk_div
*div
)
562 if (divider_is_fixed(div
))
563 return (u64
)div
->u
.fixed
;
565 flags
= ccu_lock(ccu
);
566 reg_val
= __ccu_read(ccu
, div
->u
.s
.offset
);
567 ccu_unlock(ccu
, flags
);
569 /* Extract the full divider field from the register value */
570 reg_div
= bitfield_extract(reg_val
, div
->u
.s
.shift
, div
->u
.s
.width
);
572 /* Return the scaled divisor value it represents */
573 return scaled_div_value(div
, reg_div
);
577 * Convert a divider's scaled divisor value into its recorded form
578 * and commit it into the hardware divider register.
580 * Returns 0 on success. Returns -EINVAL for invalid arguments.
581 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
583 static int __div_commit(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
,
584 struct bcm_clk_div
*div
, struct bcm_clk_trig
*trig
)
591 BUG_ON(divider_is_fixed(div
));
594 * If we're just initializing the divider, and no initial
595 * state was defined in the device tree, we just find out
596 * what its current value is rather than updating it.
598 if (div
->u
.s
.scaled_div
== BAD_SCALED_DIV_VALUE
) {
599 reg_val
= __ccu_read(ccu
, div
->u
.s
.offset
);
600 reg_div
= bitfield_extract(reg_val
, div
->u
.s
.shift
,
602 div
->u
.s
.scaled_div
= scaled_div_value(div
, reg_div
);
607 /* Convert the scaled divisor to the value we need to record */
608 reg_div
= divider(div
, div
->u
.s
.scaled_div
);
610 /* Clock needs to be enabled before changing the rate */
611 enabled
= __is_clk_gate_enabled(ccu
, gate
);
612 if (!enabled
&& !__clk_gate(ccu
, gate
, true)) {
617 /* Replace the divider value and record the result */
618 reg_val
= __ccu_read(ccu
, div
->u
.s
.offset
);
619 reg_val
= bitfield_replace(reg_val
, div
->u
.s
.shift
, div
->u
.s
.width
,
621 __ccu_write(ccu
, div
->u
.s
.offset
, reg_val
);
623 /* If the trigger fails we still want to disable the gate */
624 if (!__clk_trigger(ccu
, trig
))
627 /* Disable the clock again if it was disabled to begin with */
628 if (!enabled
&& !__clk_gate(ccu
, gate
, false))
629 ret
= ret
? ret
: -ENXIO
; /* return first error */
635 * Initialize a divider by committing our desired state to hardware
636 * without the usual checks to see if it's already set up that way.
637 * Returns true if successful, false otherwise.
639 static bool div_init(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
,
640 struct bcm_clk_div
*div
, struct bcm_clk_trig
*trig
)
642 if (!divider_exists(div
) || divider_is_fixed(div
))
644 return !__div_commit(ccu
, gate
, div
, trig
);
647 static int divider_write(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
,
648 struct bcm_clk_div
*div
, struct bcm_clk_trig
*trig
,
655 BUG_ON(divider_is_fixed(div
));
657 previous
= div
->u
.s
.scaled_div
;
658 if (previous
== scaled_div
)
659 return 0; /* No change */
661 div
->u
.s
.scaled_div
= scaled_div
;
663 flags
= ccu_lock(ccu
);
664 __ccu_write_enable(ccu
);
666 ret
= __div_commit(ccu
, gate
, div
, trig
);
668 __ccu_write_disable(ccu
);
669 ccu_unlock(ccu
, flags
);
672 div
->u
.s
.scaled_div
= previous
; /* Revert the change */
678 /* Common clock rate helpers */
681 * Implement the common clock framework recalc_rate method, taking
682 * into account a divider and an optional pre-divider. The
683 * pre-divider register pointer may be NULL.
685 static unsigned long clk_recalc_rate(struct ccu_data
*ccu
,
686 struct bcm_clk_div
*div
, struct bcm_clk_div
*pre_div
,
687 unsigned long parent_rate
)
689 u64 scaled_parent_rate
;
693 if (!divider_exists(div
))
696 if (parent_rate
> (unsigned long)LONG_MAX
)
697 return 0; /* actually this would be a caller bug */
700 * If there is a pre-divider, divide the scaled parent rate
701 * by the pre-divider value first. In this case--to improve
702 * accuracy--scale the parent rate by *both* the pre-divider
703 * value and the divider before actually computing the
704 * result of the pre-divider.
706 * If there's only one divider, just scale the parent rate.
708 if (pre_div
&& divider_exists(pre_div
)) {
711 scaled_rate
= scale_rate(pre_div
, parent_rate
);
712 scaled_rate
= scale_rate(div
, scaled_rate
);
713 scaled_div
= divider_read_scaled(ccu
, pre_div
);
714 scaled_parent_rate
= DIV_ROUND_CLOSEST_ULL(scaled_rate
,
717 scaled_parent_rate
= scale_rate(div
, parent_rate
);
721 * Get the scaled divisor value, and divide the scaled
722 * parent rate by that to determine this clock's resulting
725 scaled_div
= divider_read_scaled(ccu
, div
);
726 result
= DIV_ROUND_CLOSEST_ULL(scaled_parent_rate
, scaled_div
);
728 return (unsigned long)result
;
732 * Compute the output rate produced when a given parent rate is fed
733 * into two dividers. The pre-divider can be NULL, and even if it's
734 * non-null it may be nonexistent. It's also OK for the divider to
735 * be nonexistent, and in that case the pre-divider is also ignored.
737 * If scaled_div is non-null, it is used to return the scaled divisor
738 * value used by the (downstream) divider to produce that rate.
740 static long round_rate(struct ccu_data
*ccu
, struct bcm_clk_div
*div
,
741 struct bcm_clk_div
*pre_div
,
742 unsigned long rate
, unsigned long parent_rate
,
745 u64 scaled_parent_rate
;
751 BUG_ON(!divider_exists(div
));
753 BUG_ON(parent_rate
> (u64
)LONG_MAX
);
756 * If there is a pre-divider, divide the scaled parent rate
757 * by the pre-divider value first. In this case--to improve
758 * accuracy--scale the parent rate by *both* the pre-divider
759 * value and the divider before actually computing the
760 * result of the pre-divider.
762 * If there's only one divider, just scale the parent rate.
764 * For simplicity we treat the pre-divider as fixed (for now).
766 if (divider_exists(pre_div
)) {
770 scaled_rate
= scale_rate(pre_div
, parent_rate
);
771 scaled_rate
= scale_rate(div
, scaled_rate
);
772 scaled_pre_div
= divider_read_scaled(ccu
, pre_div
);
773 scaled_parent_rate
= DIV_ROUND_CLOSEST_ULL(scaled_rate
,
776 scaled_parent_rate
= scale_rate(div
, parent_rate
);
780 * Compute the best possible divider and ensure it is in
781 * range. A fixed divider can't be changed, so just report
782 * the best we can do.
784 if (!divider_is_fixed(div
)) {
785 best_scaled_div
= DIV_ROUND_CLOSEST_ULL(scaled_parent_rate
,
787 min_scaled_div
= scaled_div_min(div
);
788 max_scaled_div
= scaled_div_max(div
);
789 if (best_scaled_div
> max_scaled_div
)
790 best_scaled_div
= max_scaled_div
;
791 else if (best_scaled_div
< min_scaled_div
)
792 best_scaled_div
= min_scaled_div
;
794 best_scaled_div
= divider_read_scaled(ccu
, div
);
797 /* OK, figure out the resulting rate */
798 result
= DIV_ROUND_CLOSEST_ULL(scaled_parent_rate
, best_scaled_div
);
801 *scaled_div
= best_scaled_div
;
806 /* Common clock parent helpers */
809 * For a given parent selector (register field) value, find the
810 * index into a selector's parent_sel array that contains it.
811 * Returns the index, or BAD_CLK_INDEX if it's not found.
813 static u8
parent_index(struct bcm_clk_sel
*sel
, u8 parent_sel
)
817 BUG_ON(sel
->parent_count
> (u32
)U8_MAX
);
818 for (i
= 0; i
< sel
->parent_count
; i
++)
819 if (sel
->parent_sel
[i
] == parent_sel
)
821 return BAD_CLK_INDEX
;
825 * Fetch the current value of the selector, and translate that into
826 * its corresponding index in the parent array we registered with
827 * the clock framework.
829 * Returns parent array index that corresponds with the value found,
830 * or BAD_CLK_INDEX if the found value is out of range.
832 static u8
selector_read_index(struct ccu_data
*ccu
, struct bcm_clk_sel
*sel
)
839 /* If there's no selector, there's only one parent */
840 if (!selector_exists(sel
))
843 /* Get the value in the selector register */
844 flags
= ccu_lock(ccu
);
845 reg_val
= __ccu_read(ccu
, sel
->offset
);
846 ccu_unlock(ccu
, flags
);
848 parent_sel
= bitfield_extract(reg_val
, sel
->shift
, sel
->width
);
850 /* Look up that selector's parent array index and return it */
851 index
= parent_index(sel
, parent_sel
);
852 if (index
== BAD_CLK_INDEX
)
853 pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
854 __func__
, parent_sel
, ccu
->name
, sel
->offset
);
860 * Commit our desired selector value to the hardware.
862 * Returns 0 on success. Returns -EINVAL for invalid arguments.
863 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
866 __sel_commit(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
,
867 struct bcm_clk_sel
*sel
, struct bcm_clk_trig
*trig
)
874 BUG_ON(!selector_exists(sel
));
877 * If we're just initializing the selector, and no initial
878 * state was defined in the device tree, we just find out
879 * what its current value is rather than updating it.
881 if (sel
->clk_index
== BAD_CLK_INDEX
) {
884 reg_val
= __ccu_read(ccu
, sel
->offset
);
885 parent_sel
= bitfield_extract(reg_val
, sel
->shift
, sel
->width
);
886 index
= parent_index(sel
, parent_sel
);
887 if (index
== BAD_CLK_INDEX
)
889 sel
->clk_index
= index
;
894 BUG_ON((u32
)sel
->clk_index
>= sel
->parent_count
);
895 parent_sel
= sel
->parent_sel
[sel
->clk_index
];
897 /* Clock needs to be enabled before changing the parent */
898 enabled
= __is_clk_gate_enabled(ccu
, gate
);
899 if (!enabled
&& !__clk_gate(ccu
, gate
, true))
902 /* Replace the selector value and record the result */
903 reg_val
= __ccu_read(ccu
, sel
->offset
);
904 reg_val
= bitfield_replace(reg_val
, sel
->shift
, sel
->width
, parent_sel
);
905 __ccu_write(ccu
, sel
->offset
, reg_val
);
907 /* If the trigger fails we still want to disable the gate */
908 if (!__clk_trigger(ccu
, trig
))
911 /* Disable the clock again if it was disabled to begin with */
912 if (!enabled
&& !__clk_gate(ccu
, gate
, false))
913 ret
= ret
? ret
: -ENXIO
; /* return first error */
919 * Initialize a selector by committing our desired state to hardware
920 * without the usual checks to see if it's already set up that way.
921 * Returns true if successful, false otherwise.
923 static bool sel_init(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
,
924 struct bcm_clk_sel
*sel
, struct bcm_clk_trig
*trig
)
926 if (!selector_exists(sel
))
928 return !__sel_commit(ccu
, gate
, sel
, trig
);
932 * Write a new value into a selector register to switch to a
933 * different parent clock. Returns 0 on success, or an error code
934 * (from __sel_commit()) otherwise.
936 static int selector_write(struct ccu_data
*ccu
, struct bcm_clk_gate
*gate
,
937 struct bcm_clk_sel
*sel
, struct bcm_clk_trig
*trig
,
944 previous
= sel
->clk_index
;
945 if (previous
== index
)
946 return 0; /* No change */
948 sel
->clk_index
= index
;
950 flags
= ccu_lock(ccu
);
951 __ccu_write_enable(ccu
);
953 ret
= __sel_commit(ccu
, gate
, sel
, trig
);
955 __ccu_write_disable(ccu
);
956 ccu_unlock(ccu
, flags
);
959 sel
->clk_index
= previous
; /* Revert the change */
964 /* Clock operations */
966 static int kona_peri_clk_enable(struct clk_hw
*hw
)
968 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
969 struct bcm_clk_gate
*gate
= &bcm_clk
->u
.peri
->gate
;
971 return clk_gate(bcm_clk
->ccu
, bcm_clk
->init_data
.name
, gate
, true);
974 static void kona_peri_clk_disable(struct clk_hw
*hw
)
976 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
977 struct bcm_clk_gate
*gate
= &bcm_clk
->u
.peri
->gate
;
979 (void)clk_gate(bcm_clk
->ccu
, bcm_clk
->init_data
.name
, gate
, false);
982 static int kona_peri_clk_is_enabled(struct clk_hw
*hw
)
984 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
985 struct bcm_clk_gate
*gate
= &bcm_clk
->u
.peri
->gate
;
987 return is_clk_gate_enabled(bcm_clk
->ccu
, gate
) ? 1 : 0;
990 static unsigned long kona_peri_clk_recalc_rate(struct clk_hw
*hw
,
991 unsigned long parent_rate
)
993 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
994 struct peri_clk_data
*data
= bcm_clk
->u
.peri
;
996 return clk_recalc_rate(bcm_clk
->ccu
, &data
->div
, &data
->pre_div
,
1000 static long kona_peri_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
1001 unsigned long *parent_rate
)
1003 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
1004 struct bcm_clk_div
*div
= &bcm_clk
->u
.peri
->div
;
1006 if (!divider_exists(div
))
1007 return clk_hw_get_rate(hw
);
1009 /* Quietly avoid a zero rate */
1010 return round_rate(bcm_clk
->ccu
, div
, &bcm_clk
->u
.peri
->pre_div
,
1011 rate
? rate
: 1, *parent_rate
, NULL
);
1014 static int kona_peri_clk_determine_rate(struct clk_hw
*hw
,
1015 struct clk_rate_request
*req
)
1017 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
1018 struct clk_hw
*current_parent
;
1019 unsigned long parent_rate
;
1020 unsigned long best_delta
;
1021 unsigned long best_rate
;
1027 * If there is no other parent to choose, use the current one.
1028 * Note: We don't honor (or use) CLK_SET_RATE_NO_REPARENT.
1030 WARN_ON_ONCE(bcm_clk
->init_data
.flags
& CLK_SET_RATE_NO_REPARENT
);
1031 parent_count
= (u32
)bcm_clk
->init_data
.num_parents
;
1032 if (parent_count
< 2) {
1033 rate
= kona_peri_clk_round_rate(hw
, req
->rate
,
1034 &req
->best_parent_rate
);
1042 /* Unless we can do better, stick with current parent */
1043 current_parent
= clk_hw_get_parent(hw
);
1044 parent_rate
= clk_hw_get_rate(current_parent
);
1045 best_rate
= kona_peri_clk_round_rate(hw
, req
->rate
, &parent_rate
);
1046 best_delta
= abs(best_rate
- req
->rate
);
1048 /* Check whether any other parent clock can produce a better result */
1049 for (which
= 0; which
< parent_count
; which
++) {
1050 struct clk_hw
*parent
= clk_hw_get_parent_by_index(hw
, which
);
1051 unsigned long delta
;
1052 unsigned long other_rate
;
1055 if (parent
== current_parent
)
1058 /* We don't support CLK_SET_RATE_PARENT */
1059 parent_rate
= clk_hw_get_rate(parent
);
1060 other_rate
= kona_peri_clk_round_rate(hw
, req
->rate
,
1062 delta
= abs(other_rate
- req
->rate
);
1063 if (delta
< best_delta
) {
1065 best_rate
= other_rate
;
1066 req
->best_parent_hw
= parent
;
1067 req
->best_parent_rate
= parent_rate
;
1071 req
->rate
= best_rate
;
1075 static int kona_peri_clk_set_parent(struct clk_hw
*hw
, u8 index
)
1077 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
1078 struct peri_clk_data
*data
= bcm_clk
->u
.peri
;
1079 struct bcm_clk_sel
*sel
= &data
->sel
;
1080 struct bcm_clk_trig
*trig
;
1083 BUG_ON(index
>= sel
->parent_count
);
1085 /* If there's only one parent we don't require a selector */
1086 if (!selector_exists(sel
))
1090 * The regular trigger is used by default, but if there's a
1091 * pre-trigger we want to use that instead.
1093 trig
= trigger_exists(&data
->pre_trig
) ? &data
->pre_trig
1096 ret
= selector_write(bcm_clk
->ccu
, &data
->gate
, sel
, trig
, index
);
1097 if (ret
== -ENXIO
) {
1098 pr_err("%s: gating failure for %s\n", __func__
,
1099 bcm_clk
->init_data
.name
);
1100 ret
= -EIO
; /* Don't proliferate weird errors */
1101 } else if (ret
== -EIO
) {
1102 pr_err("%s: %strigger failed for %s\n", __func__
,
1103 trig
== &data
->pre_trig
? "pre-" : "",
1104 bcm_clk
->init_data
.name
);
1110 static u8
kona_peri_clk_get_parent(struct clk_hw
*hw
)
1112 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
1113 struct peri_clk_data
*data
= bcm_clk
->u
.peri
;
1116 index
= selector_read_index(bcm_clk
->ccu
, &data
->sel
);
1118 /* Not all callers would handle an out-of-range value gracefully */
1119 return index
== BAD_CLK_INDEX
? 0 : index
;
1122 static int kona_peri_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
1123 unsigned long parent_rate
)
1125 struct kona_clk
*bcm_clk
= to_kona_clk(hw
);
1126 struct peri_clk_data
*data
= bcm_clk
->u
.peri
;
1127 struct bcm_clk_div
*div
= &data
->div
;
1131 if (parent_rate
> (unsigned long)LONG_MAX
)
1134 if (rate
== clk_hw_get_rate(hw
))
1137 if (!divider_exists(div
))
1138 return rate
== parent_rate
? 0 : -EINVAL
;
1141 * A fixed divider can't be changed. (Nor can a fixed
1142 * pre-divider be, but for now we never actually try to
1143 * change that.) Tolerate a request for a no-op change.
1145 if (divider_is_fixed(&data
->div
))
1146 return rate
== parent_rate
? 0 : -EINVAL
;
1149 * Get the scaled divisor value needed to achieve a clock
1150 * rate as close as possible to what was requested, given
1151 * the parent clock rate supplied.
1153 (void)round_rate(bcm_clk
->ccu
, div
, &data
->pre_div
,
1154 rate
? rate
: 1, parent_rate
, &scaled_div
);
1157 * We aren't updating any pre-divider at this point, so
1158 * we'll use the regular trigger.
1160 ret
= divider_write(bcm_clk
->ccu
, &data
->gate
, &data
->div
,
1161 &data
->trig
, scaled_div
);
1162 if (ret
== -ENXIO
) {
1163 pr_err("%s: gating failure for %s\n", __func__
,
1164 bcm_clk
->init_data
.name
);
1165 ret
= -EIO
; /* Don't proliferate weird errors */
1166 } else if (ret
== -EIO
) {
1167 pr_err("%s: trigger failed for %s\n", __func__
,
1168 bcm_clk
->init_data
.name
);
1174 struct clk_ops kona_peri_clk_ops
= {
1175 .enable
= kona_peri_clk_enable
,
1176 .disable
= kona_peri_clk_disable
,
1177 .is_enabled
= kona_peri_clk_is_enabled
,
1178 .recalc_rate
= kona_peri_clk_recalc_rate
,
1179 .determine_rate
= kona_peri_clk_determine_rate
,
1180 .set_parent
= kona_peri_clk_set_parent
,
1181 .get_parent
= kona_peri_clk_get_parent
,
1182 .set_rate
= kona_peri_clk_set_rate
,
1185 /* Put a peripheral clock into its initial state */
1186 static bool __peri_clk_init(struct kona_clk
*bcm_clk
)
1188 struct ccu_data
*ccu
= bcm_clk
->ccu
;
1189 struct peri_clk_data
*peri
= bcm_clk
->u
.peri
;
1190 const char *name
= bcm_clk
->init_data
.name
;
1191 struct bcm_clk_trig
*trig
;
1193 BUG_ON(bcm_clk
->type
!= bcm_clk_peri
);
1195 if (!policy_init(ccu
, &peri
->policy
)) {
1196 pr_err("%s: error initializing policy for %s\n",
1200 if (!gate_init(ccu
, &peri
->gate
)) {
1201 pr_err("%s: error initializing gate for %s\n", __func__
, name
);
1204 if (!hyst_init(ccu
, &peri
->hyst
)) {
1205 pr_err("%s: error initializing hyst for %s\n", __func__
, name
);
1208 if (!div_init(ccu
, &peri
->gate
, &peri
->div
, &peri
->trig
)) {
1209 pr_err("%s: error initializing divider for %s\n", __func__
,
1215 * For the pre-divider and selector, the pre-trigger is used
1216 * if it's present, otherwise we just use the regular trigger.
1218 trig
= trigger_exists(&peri
->pre_trig
) ? &peri
->pre_trig
1221 if (!div_init(ccu
, &peri
->gate
, &peri
->pre_div
, trig
)) {
1222 pr_err("%s: error initializing pre-divider for %s\n", __func__
,
1227 if (!sel_init(ccu
, &peri
->gate
, &peri
->sel
, trig
)) {
1228 pr_err("%s: error initializing selector for %s\n", __func__
,
1236 static bool __kona_clk_init(struct kona_clk
*bcm_clk
)
1238 switch (bcm_clk
->type
) {
1240 return __peri_clk_init(bcm_clk
);
1247 /* Set a CCU and all its clocks into their desired initial state */
1248 bool __init
kona_ccu_init(struct ccu_data
*ccu
)
1250 unsigned long flags
;
1252 struct kona_clk
*kona_clks
= ccu
->kona_clks
;
1253 bool success
= true;
1255 flags
= ccu_lock(ccu
);
1256 __ccu_write_enable(ccu
);
1258 for (which
= 0; which
< ccu
->clk_num
; which
++) {
1259 struct kona_clk
*bcm_clk
= &kona_clks
[which
];
1264 success
&= __kona_clk_init(bcm_clk
);
1267 __ccu_write_disable(ccu
);
1268 ccu_unlock(ccu
, flags
);