WIP FPC-III support
[linux/fpc-iii.git] / drivers / clk / bcm / clk-kona.c
blobcc3b1e1bc0871ba5abd2c896badb7697d014e714
1 /*
2 * Copyright (C) 2013 Broadcom Corporation
3 * Copyright 2013 Linaro Limited
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation version 2.
9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10 * kind, whether express or implied; without even the implied warranty
11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include "clk-kona.h"
17 #include <linux/delay.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/clk-provider.h>
23 * "Policies" affect the frequencies of bus clocks provided by a
24 * CCU. (I believe these polices are named "Deep Sleep", "Economy",
25 * "Normal", and "Turbo".) A lower policy number has lower power
26 * consumption, and policy 2 is the default.
28 #define CCU_POLICY_COUNT 4
30 #define CCU_ACCESS_PASSWORD 0xA5A500
31 #define CLK_GATE_DELAY_LOOP 2000
33 /* Bitfield operations */
35 /* Produces a mask of set bits covering a range of a 32-bit value */
36 static inline u32 bitfield_mask(u32 shift, u32 width)
38 return ((1 << width) - 1) << shift;
41 /* Extract the value of a bitfield found within a given register value */
42 static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
44 return (reg_val & bitfield_mask(shift, width)) >> shift;
47 /* Replace the value of a bitfield found within a given register value */
48 static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
50 u32 mask = bitfield_mask(shift, width);
52 return (reg_val & ~mask) | (val << shift);
55 /* Divider and scaling helpers */
57 /* Convert a divider into the scaled divisor value it represents. */
58 static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
60 return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
64 * Build a scaled divider value as close as possible to the
65 * given whole part (div_value) and fractional part (expressed
66 * in billionths).
68 u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
70 u64 combined;
72 BUG_ON(!div_value);
73 BUG_ON(billionths >= BILLION);
75 combined = (u64)div_value * BILLION + billionths;
76 combined <<= div->u.s.frac_width;
78 return DIV_ROUND_CLOSEST_ULL(combined, BILLION);
81 /* The scaled minimum divisor representable by a divider */
82 static inline u64
83 scaled_div_min(struct bcm_clk_div *div)
85 if (divider_is_fixed(div))
86 return (u64)div->u.fixed;
88 return scaled_div_value(div, 0);
91 /* The scaled maximum divisor representable by a divider */
92 u64 scaled_div_max(struct bcm_clk_div *div)
94 u32 reg_div;
96 if (divider_is_fixed(div))
97 return (u64)div->u.fixed;
99 reg_div = ((u32)1 << div->u.s.width) - 1;
101 return scaled_div_value(div, reg_div);
105 * Convert a scaled divisor into its divider representation as
106 * stored in a divider register field.
108 static inline u32
109 divider(struct bcm_clk_div *div, u64 scaled_div)
111 BUG_ON(scaled_div < scaled_div_min(div));
112 BUG_ON(scaled_div > scaled_div_max(div));
114 return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
117 /* Return a rate scaled for use when dividing by a scaled divisor. */
118 static inline u64
119 scale_rate(struct bcm_clk_div *div, u32 rate)
121 if (divider_is_fixed(div))
122 return (u64)rate;
124 return (u64)rate << div->u.s.frac_width;
127 /* CCU access */
129 /* Read a 32-bit register value from a CCU's address space. */
130 static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
132 return readl(ccu->base + reg_offset);
135 /* Write a 32-bit register value into a CCU's address space. */
136 static inline void
137 __ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
139 writel(reg_val, ccu->base + reg_offset);
142 static inline unsigned long ccu_lock(struct ccu_data *ccu)
144 unsigned long flags;
146 spin_lock_irqsave(&ccu->lock, flags);
148 return flags;
150 static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
152 spin_unlock_irqrestore(&ccu->lock, flags);
156 * Enable/disable write access to CCU protected registers. The
157 * WR_ACCESS register for all CCUs is at offset 0.
159 static inline void __ccu_write_enable(struct ccu_data *ccu)
161 if (ccu->write_enabled) {
162 pr_err("%s: access already enabled for %s\n", __func__,
163 ccu->name);
164 return;
166 ccu->write_enabled = true;
167 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
170 static inline void __ccu_write_disable(struct ccu_data *ccu)
172 if (!ccu->write_enabled) {
173 pr_err("%s: access wasn't enabled for %s\n", __func__,
174 ccu->name);
175 return;
178 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
179 ccu->write_enabled = false;
183 * Poll a register in a CCU's address space, returning when the
184 * specified bit in that register's value is set (or clear). Delay
185 * a microsecond after each read of the register. Returns true if
186 * successful, or false if we gave up trying.
188 * Caller must ensure the CCU lock is held.
190 static inline bool
191 __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
193 unsigned int tries;
194 u32 bit_mask = 1 << bit;
196 for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
197 u32 val;
198 bool bit_val;
200 val = __ccu_read(ccu, reg_offset);
201 bit_val = (val & bit_mask) != 0;
202 if (bit_val == want)
203 return true;
204 udelay(1);
206 pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
207 ccu->name, reg_offset, bit, want ? "set" : "clear");
209 return false;
212 /* Policy operations */
214 static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
216 struct bcm_policy_ctl *control = &ccu->policy.control;
217 u32 offset;
218 u32 go_bit;
219 u32 mask;
220 bool ret;
222 /* If we don't need to control policy for this CCU, we're done. */
223 if (!policy_ctl_exists(control))
224 return true;
226 offset = control->offset;
227 go_bit = control->go_bit;
229 /* Ensure we're not busy before we start */
230 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
231 if (!ret) {
232 pr_err("%s: ccu %s policy engine wouldn't go idle\n",
233 __func__, ccu->name);
234 return false;
238 * If it's a synchronous request, we'll wait for the voltage
239 * and frequency of the active load to stabilize before
240 * returning. To do this we select the active load by
241 * setting the ATL bit.
243 * An asynchronous request instead ramps the voltage in the
244 * background, and when that process stabilizes, the target
245 * load is copied to the active load and the CCU frequency
246 * is switched. We do this by selecting the target load
247 * (ATL bit clear) and setting the request auto-copy (AC bit
248 * set).
250 * Note, we do NOT read-modify-write this register.
252 mask = (u32)1 << go_bit;
253 if (sync)
254 mask |= 1 << control->atl_bit;
255 else
256 mask |= 1 << control->ac_bit;
257 __ccu_write(ccu, offset, mask);
259 /* Wait for indication that operation is complete. */
260 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
261 if (!ret)
262 pr_err("%s: ccu %s policy engine never started\n",
263 __func__, ccu->name);
265 return ret;
268 static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
270 struct bcm_lvm_en *enable = &ccu->policy.enable;
271 u32 offset;
272 u32 enable_bit;
273 bool ret;
275 /* If we don't need to control policy for this CCU, we're done. */
276 if (!policy_lvm_en_exists(enable))
277 return true;
279 /* Ensure we're not busy before we start */
280 offset = enable->offset;
281 enable_bit = enable->bit;
282 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
283 if (!ret) {
284 pr_err("%s: ccu %s policy engine already stopped\n",
285 __func__, ccu->name);
286 return false;
289 /* Now set the bit to stop the engine (NO read-modify-write) */
290 __ccu_write(ccu, offset, (u32)1 << enable_bit);
292 /* Wait for indication that it has stopped. */
293 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
294 if (!ret)
295 pr_err("%s: ccu %s policy engine never stopped\n",
296 __func__, ccu->name);
298 return ret;
302 * A CCU has four operating conditions ("policies"), and some clocks
303 * can be disabled or enabled based on which policy is currently in
304 * effect. Such clocks have a bit in a "policy mask" register for
305 * each policy indicating whether the clock is enabled for that
306 * policy or not. The bit position for a clock is the same for all
307 * four registers, and the 32-bit registers are at consecutive
308 * addresses.
310 static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
312 u32 offset;
313 u32 mask;
314 int i;
315 bool ret;
317 if (!policy_exists(policy))
318 return true;
321 * We need to stop the CCU policy engine to allow update
322 * of our policy bits.
324 if (!__ccu_policy_engine_stop(ccu)) {
325 pr_err("%s: unable to stop CCU %s policy engine\n",
326 __func__, ccu->name);
327 return false;
331 * For now, if a clock defines its policy bit we just mark
332 * it "enabled" for all four policies.
334 offset = policy->offset;
335 mask = (u32)1 << policy->bit;
336 for (i = 0; i < CCU_POLICY_COUNT; i++) {
337 u32 reg_val;
339 reg_val = __ccu_read(ccu, offset);
340 reg_val |= mask;
341 __ccu_write(ccu, offset, reg_val);
342 offset += sizeof(u32);
345 /* We're done updating; fire up the policy engine again. */
346 ret = __ccu_policy_engine_start(ccu, true);
347 if (!ret)
348 pr_err("%s: unable to restart CCU %s policy engine\n",
349 __func__, ccu->name);
351 return ret;
354 /* Gate operations */
356 /* Determine whether a clock is gated. CCU lock must be held. */
357 static bool
358 __is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
360 u32 bit_mask;
361 u32 reg_val;
363 /* If there is no gate we can assume it's enabled. */
364 if (!gate_exists(gate))
365 return true;
367 bit_mask = 1 << gate->status_bit;
368 reg_val = __ccu_read(ccu, gate->offset);
370 return (reg_val & bit_mask) != 0;
373 /* Determine whether a clock is gated. */
374 static bool
375 is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
377 long flags;
378 bool ret;
380 /* Avoid taking the lock if we can */
381 if (!gate_exists(gate))
382 return true;
384 flags = ccu_lock(ccu);
385 ret = __is_clk_gate_enabled(ccu, gate);
386 ccu_unlock(ccu, flags);
388 return ret;
392 * Commit our desired gate state to the hardware.
393 * Returns true if successful, false otherwise.
395 static bool
396 __gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
398 u32 reg_val;
399 u32 mask;
400 bool enabled = false;
402 BUG_ON(!gate_exists(gate));
403 if (!gate_is_sw_controllable(gate))
404 return true; /* Nothing we can change */
406 reg_val = __ccu_read(ccu, gate->offset);
408 /* For a hardware/software gate, set which is in control */
409 if (gate_is_hw_controllable(gate)) {
410 mask = (u32)1 << gate->hw_sw_sel_bit;
411 if (gate_is_sw_managed(gate))
412 reg_val |= mask;
413 else
414 reg_val &= ~mask;
418 * If software is in control, enable or disable the gate.
419 * If hardware is, clear the enabled bit for good measure.
420 * If a software controlled gate can't be disabled, we're
421 * required to write a 0 into the enable bit (but the gate
422 * will be enabled).
424 mask = (u32)1 << gate->en_bit;
425 if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) &&
426 !gate_is_no_disable(gate))
427 reg_val |= mask;
428 else
429 reg_val &= ~mask;
431 __ccu_write(ccu, gate->offset, reg_val);
433 /* For a hardware controlled gate, we're done */
434 if (!gate_is_sw_managed(gate))
435 return true;
437 /* Otherwise wait for the gate to be in desired state */
438 return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
442 * Initialize a gate. Our desired state (hardware/software select,
443 * and if software, its enable state) is committed to hardware
444 * without the usual checks to see if it's already set up that way.
445 * Returns true if successful, false otherwise.
447 static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
449 if (!gate_exists(gate))
450 return true;
451 return __gate_commit(ccu, gate);
455 * Set a gate to enabled or disabled state. Does nothing if the
456 * gate is not currently under software control, or if it is already
457 * in the requested state. Returns true if successful, false
458 * otherwise. CCU lock must be held.
460 static bool
461 __clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
463 bool ret;
465 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
466 return true; /* Nothing to do */
468 if (!enable && gate_is_no_disable(gate)) {
469 pr_warn("%s: invalid gate disable request (ignoring)\n",
470 __func__);
471 return true;
474 if (enable == gate_is_enabled(gate))
475 return true; /* No change */
477 gate_flip_enabled(gate);
478 ret = __gate_commit(ccu, gate);
479 if (!ret)
480 gate_flip_enabled(gate); /* Revert the change */
482 return ret;
485 /* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */
486 static int clk_gate(struct ccu_data *ccu, const char *name,
487 struct bcm_clk_gate *gate, bool enable)
489 unsigned long flags;
490 bool success;
493 * Avoid taking the lock if we can. We quietly ignore
494 * requests to change state that don't make sense.
496 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
497 return 0;
498 if (!enable && gate_is_no_disable(gate))
499 return 0;
501 flags = ccu_lock(ccu);
502 __ccu_write_enable(ccu);
504 success = __clk_gate(ccu, gate, enable);
506 __ccu_write_disable(ccu);
507 ccu_unlock(ccu, flags);
509 if (success)
510 return 0;
512 pr_err("%s: failed to %s gate for %s\n", __func__,
513 enable ? "enable" : "disable", name);
515 return -EIO;
518 /* Hysteresis operations */
521 * If a clock gate requires a turn-off delay it will have
522 * "hysteresis" register bits defined. The first, if set, enables
523 * the delay; and if enabled, the second bit determines whether the
524 * delay is "low" or "high" (1 means high). For now, if it's
525 * defined for a clock, we set it.
527 static bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst)
529 u32 offset;
530 u32 reg_val;
531 u32 mask;
533 if (!hyst_exists(hyst))
534 return true;
536 offset = hyst->offset;
537 mask = (u32)1 << hyst->en_bit;
538 mask |= (u32)1 << hyst->val_bit;
540 reg_val = __ccu_read(ccu, offset);
541 reg_val |= mask;
542 __ccu_write(ccu, offset, reg_val);
544 return true;
547 /* Trigger operations */
550 * Caller must ensure CCU lock is held and access is enabled.
551 * Returns true if successful, false otherwise.
553 static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
555 /* Trigger the clock and wait for it to finish */
556 __ccu_write(ccu, trig->offset, 1 << trig->bit);
558 return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
561 /* Divider operations */
563 /* Read a divider value and return the scaled divisor it represents. */
564 static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
566 unsigned long flags;
567 u32 reg_val;
568 u32 reg_div;
570 if (divider_is_fixed(div))
571 return (u64)div->u.fixed;
573 flags = ccu_lock(ccu);
574 reg_val = __ccu_read(ccu, div->u.s.offset);
575 ccu_unlock(ccu, flags);
577 /* Extract the full divider field from the register value */
578 reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
580 /* Return the scaled divisor value it represents */
581 return scaled_div_value(div, reg_div);
585 * Convert a divider's scaled divisor value into its recorded form
586 * and commit it into the hardware divider register.
588 * Returns 0 on success. Returns -EINVAL for invalid arguments.
589 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
591 static int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
592 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
594 bool enabled;
595 u32 reg_div;
596 u32 reg_val;
597 int ret = 0;
599 BUG_ON(divider_is_fixed(div));
602 * If we're just initializing the divider, and no initial
603 * state was defined in the device tree, we just find out
604 * what its current value is rather than updating it.
606 if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) {
607 reg_val = __ccu_read(ccu, div->u.s.offset);
608 reg_div = bitfield_extract(reg_val, div->u.s.shift,
609 div->u.s.width);
610 div->u.s.scaled_div = scaled_div_value(div, reg_div);
612 return 0;
615 /* Convert the scaled divisor to the value we need to record */
616 reg_div = divider(div, div->u.s.scaled_div);
618 /* Clock needs to be enabled before changing the rate */
619 enabled = __is_clk_gate_enabled(ccu, gate);
620 if (!enabled && !__clk_gate(ccu, gate, true)) {
621 ret = -ENXIO;
622 goto out;
625 /* Replace the divider value and record the result */
626 reg_val = __ccu_read(ccu, div->u.s.offset);
627 reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width,
628 reg_div);
629 __ccu_write(ccu, div->u.s.offset, reg_val);
631 /* If the trigger fails we still want to disable the gate */
632 if (!__clk_trigger(ccu, trig))
633 ret = -EIO;
635 /* Disable the clock again if it was disabled to begin with */
636 if (!enabled && !__clk_gate(ccu, gate, false))
637 ret = ret ? ret : -ENXIO; /* return first error */
638 out:
639 return ret;
643 * Initialize a divider by committing our desired state to hardware
644 * without the usual checks to see if it's already set up that way.
645 * Returns true if successful, false otherwise.
647 static bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
648 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
650 if (!divider_exists(div) || divider_is_fixed(div))
651 return true;
652 return !__div_commit(ccu, gate, div, trig);
655 static int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
656 struct bcm_clk_div *div, struct bcm_clk_trig *trig,
657 u64 scaled_div)
659 unsigned long flags;
660 u64 previous;
661 int ret;
663 BUG_ON(divider_is_fixed(div));
665 previous = div->u.s.scaled_div;
666 if (previous == scaled_div)
667 return 0; /* No change */
669 div->u.s.scaled_div = scaled_div;
671 flags = ccu_lock(ccu);
672 __ccu_write_enable(ccu);
674 ret = __div_commit(ccu, gate, div, trig);
676 __ccu_write_disable(ccu);
677 ccu_unlock(ccu, flags);
679 if (ret)
680 div->u.s.scaled_div = previous; /* Revert the change */
682 return ret;
686 /* Common clock rate helpers */
689 * Implement the common clock framework recalc_rate method, taking
690 * into account a divider and an optional pre-divider. The
691 * pre-divider register pointer may be NULL.
693 static unsigned long clk_recalc_rate(struct ccu_data *ccu,
694 struct bcm_clk_div *div, struct bcm_clk_div *pre_div,
695 unsigned long parent_rate)
697 u64 scaled_parent_rate;
698 u64 scaled_div;
699 u64 result;
701 if (!divider_exists(div))
702 return parent_rate;
704 if (parent_rate > (unsigned long)LONG_MAX)
705 return 0; /* actually this would be a caller bug */
708 * If there is a pre-divider, divide the scaled parent rate
709 * by the pre-divider value first. In this case--to improve
710 * accuracy--scale the parent rate by *both* the pre-divider
711 * value and the divider before actually computing the
712 * result of the pre-divider.
714 * If there's only one divider, just scale the parent rate.
716 if (pre_div && divider_exists(pre_div)) {
717 u64 scaled_rate;
719 scaled_rate = scale_rate(pre_div, parent_rate);
720 scaled_rate = scale_rate(div, scaled_rate);
721 scaled_div = divider_read_scaled(ccu, pre_div);
722 scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
723 scaled_div);
724 } else {
725 scaled_parent_rate = scale_rate(div, parent_rate);
729 * Get the scaled divisor value, and divide the scaled
730 * parent rate by that to determine this clock's resulting
731 * rate.
733 scaled_div = divider_read_scaled(ccu, div);
734 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, scaled_div);
736 return (unsigned long)result;
740 * Compute the output rate produced when a given parent rate is fed
741 * into two dividers. The pre-divider can be NULL, and even if it's
742 * non-null it may be nonexistent. It's also OK for the divider to
743 * be nonexistent, and in that case the pre-divider is also ignored.
745 * If scaled_div is non-null, it is used to return the scaled divisor
746 * value used by the (downstream) divider to produce that rate.
748 static long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div,
749 struct bcm_clk_div *pre_div,
750 unsigned long rate, unsigned long parent_rate,
751 u64 *scaled_div)
753 u64 scaled_parent_rate;
754 u64 min_scaled_div;
755 u64 max_scaled_div;
756 u64 best_scaled_div;
757 u64 result;
759 BUG_ON(!divider_exists(div));
760 BUG_ON(!rate);
761 BUG_ON(parent_rate > (u64)LONG_MAX);
764 * If there is a pre-divider, divide the scaled parent rate
765 * by the pre-divider value first. In this case--to improve
766 * accuracy--scale the parent rate by *both* the pre-divider
767 * value and the divider before actually computing the
768 * result of the pre-divider.
770 * If there's only one divider, just scale the parent rate.
772 * For simplicity we treat the pre-divider as fixed (for now).
774 if (divider_exists(pre_div)) {
775 u64 scaled_rate;
776 u64 scaled_pre_div;
778 scaled_rate = scale_rate(pre_div, parent_rate);
779 scaled_rate = scale_rate(div, scaled_rate);
780 scaled_pre_div = divider_read_scaled(ccu, pre_div);
781 scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
782 scaled_pre_div);
783 } else {
784 scaled_parent_rate = scale_rate(div, parent_rate);
788 * Compute the best possible divider and ensure it is in
789 * range. A fixed divider can't be changed, so just report
790 * the best we can do.
792 if (!divider_is_fixed(div)) {
793 best_scaled_div = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate,
794 rate);
795 min_scaled_div = scaled_div_min(div);
796 max_scaled_div = scaled_div_max(div);
797 if (best_scaled_div > max_scaled_div)
798 best_scaled_div = max_scaled_div;
799 else if (best_scaled_div < min_scaled_div)
800 best_scaled_div = min_scaled_div;
801 } else {
802 best_scaled_div = divider_read_scaled(ccu, div);
805 /* OK, figure out the resulting rate */
806 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, best_scaled_div);
808 if (scaled_div)
809 *scaled_div = best_scaled_div;
811 return (long)result;
814 /* Common clock parent helpers */
817 * For a given parent selector (register field) value, find the
818 * index into a selector's parent_sel array that contains it.
819 * Returns the index, or BAD_CLK_INDEX if it's not found.
821 static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
823 u8 i;
825 BUG_ON(sel->parent_count > (u32)U8_MAX);
826 for (i = 0; i < sel->parent_count; i++)
827 if (sel->parent_sel[i] == parent_sel)
828 return i;
829 return BAD_CLK_INDEX;
833 * Fetch the current value of the selector, and translate that into
834 * its corresponding index in the parent array we registered with
835 * the clock framework.
837 * Returns parent array index that corresponds with the value found,
838 * or BAD_CLK_INDEX if the found value is out of range.
840 static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
842 unsigned long flags;
843 u32 reg_val;
844 u32 parent_sel;
845 u8 index;
847 /* If there's no selector, there's only one parent */
848 if (!selector_exists(sel))
849 return 0;
851 /* Get the value in the selector register */
852 flags = ccu_lock(ccu);
853 reg_val = __ccu_read(ccu, sel->offset);
854 ccu_unlock(ccu, flags);
856 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
858 /* Look up that selector's parent array index and return it */
859 index = parent_index(sel, parent_sel);
860 if (index == BAD_CLK_INDEX)
861 pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
862 __func__, parent_sel, ccu->name, sel->offset);
864 return index;
868 * Commit our desired selector value to the hardware.
870 * Returns 0 on success. Returns -EINVAL for invalid arguments.
871 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
873 static int
874 __sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
875 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
877 u32 parent_sel;
878 u32 reg_val;
879 bool enabled;
880 int ret = 0;
882 BUG_ON(!selector_exists(sel));
885 * If we're just initializing the selector, and no initial
886 * state was defined in the device tree, we just find out
887 * what its current value is rather than updating it.
889 if (sel->clk_index == BAD_CLK_INDEX) {
890 u8 index;
892 reg_val = __ccu_read(ccu, sel->offset);
893 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
894 index = parent_index(sel, parent_sel);
895 if (index == BAD_CLK_INDEX)
896 return -EINVAL;
897 sel->clk_index = index;
899 return 0;
902 BUG_ON((u32)sel->clk_index >= sel->parent_count);
903 parent_sel = sel->parent_sel[sel->clk_index];
905 /* Clock needs to be enabled before changing the parent */
906 enabled = __is_clk_gate_enabled(ccu, gate);
907 if (!enabled && !__clk_gate(ccu, gate, true))
908 return -ENXIO;
910 /* Replace the selector value and record the result */
911 reg_val = __ccu_read(ccu, sel->offset);
912 reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel);
913 __ccu_write(ccu, sel->offset, reg_val);
915 /* If the trigger fails we still want to disable the gate */
916 if (!__clk_trigger(ccu, trig))
917 ret = -EIO;
919 /* Disable the clock again if it was disabled to begin with */
920 if (!enabled && !__clk_gate(ccu, gate, false))
921 ret = ret ? ret : -ENXIO; /* return first error */
923 return ret;
927 * Initialize a selector by committing our desired state to hardware
928 * without the usual checks to see if it's already set up that way.
929 * Returns true if successful, false otherwise.
931 static bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
932 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
934 if (!selector_exists(sel))
935 return true;
936 return !__sel_commit(ccu, gate, sel, trig);
940 * Write a new value into a selector register to switch to a
941 * different parent clock. Returns 0 on success, or an error code
942 * (from __sel_commit()) otherwise.
944 static int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
945 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig,
946 u8 index)
948 unsigned long flags;
949 u8 previous;
950 int ret;
952 previous = sel->clk_index;
953 if (previous == index)
954 return 0; /* No change */
956 sel->clk_index = index;
958 flags = ccu_lock(ccu);
959 __ccu_write_enable(ccu);
961 ret = __sel_commit(ccu, gate, sel, trig);
963 __ccu_write_disable(ccu);
964 ccu_unlock(ccu, flags);
966 if (ret)
967 sel->clk_index = previous; /* Revert the change */
969 return ret;
972 /* Clock operations */
974 static int kona_peri_clk_enable(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 return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
982 static void kona_peri_clk_disable(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 (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
990 static int kona_peri_clk_is_enabled(struct clk_hw *hw)
992 struct kona_clk *bcm_clk = to_kona_clk(hw);
993 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
995 return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
998 static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
999 unsigned long parent_rate)
1001 struct kona_clk *bcm_clk = to_kona_clk(hw);
1002 struct peri_clk_data *data = bcm_clk->u.peri;
1004 return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
1005 parent_rate);
1008 static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1009 unsigned long *parent_rate)
1011 struct kona_clk *bcm_clk = to_kona_clk(hw);
1012 struct bcm_clk_div *div = &bcm_clk->u.peri->div;
1014 if (!divider_exists(div))
1015 return clk_hw_get_rate(hw);
1017 /* Quietly avoid a zero rate */
1018 return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
1019 rate ? rate : 1, *parent_rate, NULL);
1022 static int kona_peri_clk_determine_rate(struct clk_hw *hw,
1023 struct clk_rate_request *req)
1025 struct kona_clk *bcm_clk = to_kona_clk(hw);
1026 struct clk_hw *current_parent;
1027 unsigned long parent_rate;
1028 unsigned long best_delta;
1029 unsigned long best_rate;
1030 u32 parent_count;
1031 long rate;
1032 u32 which;
1035 * If there is no other parent to choose, use the current one.
1036 * Note: We don't honor (or use) CLK_SET_RATE_NO_REPARENT.
1038 WARN_ON_ONCE(bcm_clk->init_data.flags & CLK_SET_RATE_NO_REPARENT);
1039 parent_count = (u32)bcm_clk->init_data.num_parents;
1040 if (parent_count < 2) {
1041 rate = kona_peri_clk_round_rate(hw, req->rate,
1042 &req->best_parent_rate);
1043 if (rate < 0)
1044 return rate;
1046 req->rate = rate;
1047 return 0;
1050 /* Unless we can do better, stick with current parent */
1051 current_parent = clk_hw_get_parent(hw);
1052 parent_rate = clk_hw_get_rate(current_parent);
1053 best_rate = kona_peri_clk_round_rate(hw, req->rate, &parent_rate);
1054 best_delta = abs(best_rate - req->rate);
1056 /* Check whether any other parent clock can produce a better result */
1057 for (which = 0; which < parent_count; which++) {
1058 struct clk_hw *parent = clk_hw_get_parent_by_index(hw, which);
1059 unsigned long delta;
1060 unsigned long other_rate;
1062 BUG_ON(!parent);
1063 if (parent == current_parent)
1064 continue;
1066 /* We don't support CLK_SET_RATE_PARENT */
1067 parent_rate = clk_hw_get_rate(parent);
1068 other_rate = kona_peri_clk_round_rate(hw, req->rate,
1069 &parent_rate);
1070 delta = abs(other_rate - req->rate);
1071 if (delta < best_delta) {
1072 best_delta = delta;
1073 best_rate = other_rate;
1074 req->best_parent_hw = parent;
1075 req->best_parent_rate = parent_rate;
1079 req->rate = best_rate;
1080 return 0;
1083 static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
1085 struct kona_clk *bcm_clk = to_kona_clk(hw);
1086 struct peri_clk_data *data = bcm_clk->u.peri;
1087 struct bcm_clk_sel *sel = &data->sel;
1088 struct bcm_clk_trig *trig;
1089 int ret;
1091 BUG_ON(index >= sel->parent_count);
1093 /* If there's only one parent we don't require a selector */
1094 if (!selector_exists(sel))
1095 return 0;
1098 * The regular trigger is used by default, but if there's a
1099 * pre-trigger we want to use that instead.
1101 trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
1102 : &data->trig;
1104 ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index);
1105 if (ret == -ENXIO) {
1106 pr_err("%s: gating failure for %s\n", __func__,
1107 bcm_clk->init_data.name);
1108 ret = -EIO; /* Don't proliferate weird errors */
1109 } else if (ret == -EIO) {
1110 pr_err("%s: %strigger failed for %s\n", __func__,
1111 trig == &data->pre_trig ? "pre-" : "",
1112 bcm_clk->init_data.name);
1115 return ret;
1118 static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
1120 struct kona_clk *bcm_clk = to_kona_clk(hw);
1121 struct peri_clk_data *data = bcm_clk->u.peri;
1122 u8 index;
1124 index = selector_read_index(bcm_clk->ccu, &data->sel);
1126 /* Not all callers would handle an out-of-range value gracefully */
1127 return index == BAD_CLK_INDEX ? 0 : index;
1130 static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1131 unsigned long parent_rate)
1133 struct kona_clk *bcm_clk = to_kona_clk(hw);
1134 struct peri_clk_data *data = bcm_clk->u.peri;
1135 struct bcm_clk_div *div = &data->div;
1136 u64 scaled_div = 0;
1137 int ret;
1139 if (parent_rate > (unsigned long)LONG_MAX)
1140 return -EINVAL;
1142 if (rate == clk_hw_get_rate(hw))
1143 return 0;
1145 if (!divider_exists(div))
1146 return rate == parent_rate ? 0 : -EINVAL;
1149 * A fixed divider can't be changed. (Nor can a fixed
1150 * pre-divider be, but for now we never actually try to
1151 * change that.) Tolerate a request for a no-op change.
1153 if (divider_is_fixed(&data->div))
1154 return rate == parent_rate ? 0 : -EINVAL;
1157 * Get the scaled divisor value needed to achieve a clock
1158 * rate as close as possible to what was requested, given
1159 * the parent clock rate supplied.
1161 (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
1162 rate ? rate : 1, parent_rate, &scaled_div);
1165 * We aren't updating any pre-divider at this point, so
1166 * we'll use the regular trigger.
1168 ret = divider_write(bcm_clk->ccu, &data->gate, &data->div,
1169 &data->trig, scaled_div);
1170 if (ret == -ENXIO) {
1171 pr_err("%s: gating failure for %s\n", __func__,
1172 bcm_clk->init_data.name);
1173 ret = -EIO; /* Don't proliferate weird errors */
1174 } else if (ret == -EIO) {
1175 pr_err("%s: trigger failed for %s\n", __func__,
1176 bcm_clk->init_data.name);
1179 return ret;
1182 struct clk_ops kona_peri_clk_ops = {
1183 .enable = kona_peri_clk_enable,
1184 .disable = kona_peri_clk_disable,
1185 .is_enabled = kona_peri_clk_is_enabled,
1186 .recalc_rate = kona_peri_clk_recalc_rate,
1187 .determine_rate = kona_peri_clk_determine_rate,
1188 .set_parent = kona_peri_clk_set_parent,
1189 .get_parent = kona_peri_clk_get_parent,
1190 .set_rate = kona_peri_clk_set_rate,
1193 /* Put a peripheral clock into its initial state */
1194 static bool __peri_clk_init(struct kona_clk *bcm_clk)
1196 struct ccu_data *ccu = bcm_clk->ccu;
1197 struct peri_clk_data *peri = bcm_clk->u.peri;
1198 const char *name = bcm_clk->init_data.name;
1199 struct bcm_clk_trig *trig;
1201 BUG_ON(bcm_clk->type != bcm_clk_peri);
1203 if (!policy_init(ccu, &peri->policy)) {
1204 pr_err("%s: error initializing policy for %s\n",
1205 __func__, name);
1206 return false;
1208 if (!gate_init(ccu, &peri->gate)) {
1209 pr_err("%s: error initializing gate for %s\n", __func__, name);
1210 return false;
1212 if (!hyst_init(ccu, &peri->hyst)) {
1213 pr_err("%s: error initializing hyst for %s\n", __func__, name);
1214 return false;
1216 if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) {
1217 pr_err("%s: error initializing divider for %s\n", __func__,
1218 name);
1219 return false;
1223 * For the pre-divider and selector, the pre-trigger is used
1224 * if it's present, otherwise we just use the regular trigger.
1226 trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
1227 : &peri->trig;
1229 if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) {
1230 pr_err("%s: error initializing pre-divider for %s\n", __func__,
1231 name);
1232 return false;
1235 if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) {
1236 pr_err("%s: error initializing selector for %s\n", __func__,
1237 name);
1238 return false;
1241 return true;
1244 static bool __kona_clk_init(struct kona_clk *bcm_clk)
1246 switch (bcm_clk->type) {
1247 case bcm_clk_peri:
1248 return __peri_clk_init(bcm_clk);
1249 default:
1250 BUG();
1252 return false;
1255 /* Set a CCU and all its clocks into their desired initial state */
1256 bool __init kona_ccu_init(struct ccu_data *ccu)
1258 unsigned long flags;
1259 unsigned int which;
1260 struct kona_clk *kona_clks = ccu->kona_clks;
1261 bool success = true;
1263 flags = ccu_lock(ccu);
1264 __ccu_write_enable(ccu);
1266 for (which = 0; which < ccu->clk_num; which++) {
1267 struct kona_clk *bcm_clk = &kona_clks[which];
1269 if (!bcm_clk->ccu)
1270 continue;
1272 success &= __kona_clk_init(bcm_clk);
1275 __ccu_write_disable(ccu);
1276 ccu_unlock(ccu, flags);
1277 return success;