1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2013 Broadcom Corporation
4 * Copyright 2013 Linaro Limited
5 */
6
7 #include "clk-kona.h"
8
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/clk-provider.h>
13 #include <linux/string_choices.h>
14
15 /*
16 * "Policies" affect the frequencies of bus clocks provided by a
17 * CCU. (I believe these polices are named "Deep Sleep", "Economy",
18 * "Normal", and "Turbo".) A lower policy number has lower power
19 * consumption, and policy 2 is the default.
20 */
21 #define CCU_POLICY_COUNT 4
22
23 #define CCU_ACCESS_PASSWORD 0xA5A500
24 #define CLK_GATE_DELAY_LOOP 2000
25
26 /* Bitfield operations */
27
28 /* Produces a mask of set bits covering a range of a 32-bit value */
bitfield_mask(u32 shift,u32 width)29 static inline u32 bitfield_mask(u32 shift, u32 width)
30 {
31 return ((1 << width) - 1) << shift;
32 }
33
34 /* Extract the value of a bitfield found within a given register value */
bitfield_extract(u32 reg_val,u32 shift,u32 width)35 static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
36 {
37 return (reg_val & bitfield_mask(shift, width)) >> shift;
38 }
39
40 /* Replace the value of a bitfield found within a given register value */
bitfield_replace(u32 reg_val,u32 shift,u32 width,u32 val)41 static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
42 {
43 u32 mask = bitfield_mask(shift, width);
44
45 return (reg_val & ~mask) | (val << shift);
46 }
47
48 /* Divider and scaling helpers */
49
50 /* Convert a divider into the scaled divisor value it represents. */
scaled_div_value(struct bcm_clk_div * div,u32 reg_div)51 static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
52 {
53 return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
54 }
55
56 /*
57 * Build a scaled divider value as close as possible to the
58 * given whole part (div_value) and fractional part (expressed
59 * in billionths).
60 */
scaled_div_build(struct bcm_clk_div * div,u32 div_value,u32 billionths)61 u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
62 {
63 u64 combined;
64
65 BUG_ON(!div_value);
66 BUG_ON(billionths >= BILLION);
67
68 combined = (u64)div_value * BILLION + billionths;
69 combined <<= div->u.s.frac_width;
70
71 return DIV_ROUND_CLOSEST_ULL(combined, BILLION);
72 }
73
74 /* The scaled minimum divisor representable by a divider */
75 static inline u64
scaled_div_min(struct bcm_clk_div * div)76 scaled_div_min(struct bcm_clk_div *div)
77 {
78 if (divider_is_fixed(div))
79 return (u64)div->u.fixed;
80
81 return scaled_div_value(div, 0);
82 }
83
84 /* The scaled maximum divisor representable by a divider */
scaled_div_max(struct bcm_clk_div * div)85 u64 scaled_div_max(struct bcm_clk_div *div)
86 {
87 u32 reg_div;
88
89 if (divider_is_fixed(div))
90 return (u64)div->u.fixed;
91
92 reg_div = ((u32)1 << div->u.s.width) - 1;
93
94 return scaled_div_value(div, reg_div);
95 }
96
97 /*
98 * Convert a scaled divisor into its divider representation as
99 * stored in a divider register field.
100 */
101 static inline u32
divider(struct bcm_clk_div * div,u64 scaled_div)102 divider(struct bcm_clk_div *div, u64 scaled_div)
103 {
104 BUG_ON(scaled_div < scaled_div_min(div));
105 BUG_ON(scaled_div > scaled_div_max(div));
106
107 return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
108 }
109
110 /* Return a rate scaled for use when dividing by a scaled divisor. */
111 static inline u64
scale_rate(struct bcm_clk_div * div,u32 rate)112 scale_rate(struct bcm_clk_div *div, u32 rate)
113 {
114 if (divider_is_fixed(div))
115 return (u64)rate;
116
117 return (u64)rate << div->u.s.frac_width;
118 }
119
120 /* CCU access */
121
122 /* Read a 32-bit register value from a CCU's address space. */
__ccu_read(struct ccu_data * ccu,u32 reg_offset)123 static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
124 {
125 return readl(ccu->base + reg_offset);
126 }
127
128 /* Write a 32-bit register value into a CCU's address space. */
129 static inline void
__ccu_write(struct ccu_data * ccu,u32 reg_offset,u32 reg_val)130 __ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
131 {
132 writel(reg_val, ccu->base + reg_offset);
133 }
134
ccu_lock(struct ccu_data * ccu)135 static inline unsigned long ccu_lock(struct ccu_data *ccu)
136 {
137 unsigned long flags;
138
139 spin_lock_irqsave(&ccu->lock, flags);
140
141 return flags;
142 }
ccu_unlock(struct ccu_data * ccu,unsigned long flags)143 static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
144 {
145 spin_unlock_irqrestore(&ccu->lock, flags);
146 }
147
148 /*
149 * Enable/disable write access to CCU protected registers. The
150 * WR_ACCESS register for all CCUs is at offset 0.
151 */
__ccu_write_enable(struct ccu_data * ccu)152 static inline void __ccu_write_enable(struct ccu_data *ccu)
153 {
154 if (ccu->write_enabled) {
155 pr_err("%s: access already enabled for %s\n", __func__,
156 ccu->name);
157 return;
158 }
159 ccu->write_enabled = true;
160 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
161 }
162
__ccu_write_disable(struct ccu_data * ccu)163 static inline void __ccu_write_disable(struct ccu_data *ccu)
164 {
165 if (!ccu->write_enabled) {
166 pr_err("%s: access wasn't enabled for %s\n", __func__,
167 ccu->name);
168 return;
169 }
170
171 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
172 ccu->write_enabled = false;
173 }
174
175 /*
176 * Poll a register in a CCU's address space, returning when the
177 * specified bit in that register's value is set (or clear). Delay
178 * a microsecond after each read of the register. Returns true if
179 * successful, or false if we gave up trying.
180 *
181 * Caller must ensure the CCU lock is held.
182 */
183 static inline bool
__ccu_wait_bit(struct ccu_data * ccu,u32 reg_offset,u32 bit,bool want)184 __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
185 {
186 unsigned int tries;
187 u32 bit_mask = 1 << bit;
188
189 for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
190 u32 val;
191 bool bit_val;
192
193 val = __ccu_read(ccu, reg_offset);
194 bit_val = (val & bit_mask) != 0;
195 if (bit_val == want)
196 return true;
197 udelay(1);
198 }
199 pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
200 ccu->name, reg_offset, bit, want ? "set" : "clear");
201
202 return false;
203 }
204
205 /* Policy operations */
206
__ccu_policy_engine_start(struct ccu_data * ccu,bool sync)207 static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
208 {
209 struct bcm_policy_ctl *control = &ccu->policy.control;
210 u32 offset;
211 u32 go_bit;
212 u32 mask;
213 bool ret;
214
215 /* If we don't need to control policy for this CCU, we're done. */
216 if (!policy_ctl_exists(control))
217 return true;
218
219 offset = control->offset;
220 go_bit = control->go_bit;
221
222 /* Ensure we're not busy before we start */
223 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
224 if (!ret) {
225 pr_err("%s: ccu %s policy engine wouldn't go idle\n",
226 __func__, ccu->name);
227 return false;
228 }
229
230 /*
231 * If it's a synchronous request, we'll wait for the voltage
232 * and frequency of the active load to stabilize before
233 * returning. To do this we select the active load by
234 * setting the ATL bit.
235 *
236 * An asynchronous request instead ramps the voltage in the
237 * background, and when that process stabilizes, the target
238 * load is copied to the active load and the CCU frequency
239 * is switched. We do this by selecting the target load
240 * (ATL bit clear) and setting the request auto-copy (AC bit
241 * set).
242 *
243 * Note, we do NOT read-modify-write this register.
244 */
245 mask = (u32)1 << go_bit;
246 if (sync)
247 mask |= 1 << control->atl_bit;
248 else
249 mask |= 1 << control->ac_bit;
250 __ccu_write(ccu, offset, mask);
251
252 /* Wait for indication that operation is complete. */
253 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
254 if (!ret)
255 pr_err("%s: ccu %s policy engine never started\n",
256 __func__, ccu->name);
257
258 return ret;
259 }
260
__ccu_policy_engine_stop(struct ccu_data * ccu)261 static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
262 {
263 struct bcm_lvm_en *enable = &ccu->policy.enable;
264 u32 offset;
265 u32 enable_bit;
266 bool ret;
267
268 /* If we don't need to control policy for this CCU, we're done. */
269 if (!policy_lvm_en_exists(enable))
270 return true;
271
272 /* Ensure we're not busy before we start */
273 offset = enable->offset;
274 enable_bit = enable->bit;
275 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
276 if (!ret) {
277 pr_err("%s: ccu %s policy engine already stopped\n",
278 __func__, ccu->name);
279 return false;
280 }
281
282 /* Now set the bit to stop the engine (NO read-modify-write) */
283 __ccu_write(ccu, offset, (u32)1 << enable_bit);
284
285 /* Wait for indication that it has stopped. */
286 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
287 if (!ret)
288 pr_err("%s: ccu %s policy engine never stopped\n",
289 __func__, ccu->name);
290
291 return ret;
292 }
293
294 /*
295 * A CCU has four operating conditions ("policies"), and some clocks
296 * can be disabled or enabled based on which policy is currently in
297 * effect. Such clocks have a bit in a "policy mask" register for
298 * each policy indicating whether the clock is enabled for that
299 * policy or not. The bit position for a clock is the same for all
300 * four registers, and the 32-bit registers are at consecutive
301 * addresses.
302 */
policy_init(struct ccu_data * ccu,struct bcm_clk_policy * policy)303 static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
304 {
305 u32 offset;
306 u32 mask;
307 int i;
308 bool ret;
309
310 if (!policy_exists(policy))
311 return true;
312
313 /*
314 * We need to stop the CCU policy engine to allow update
315 * of our policy bits.
316 */
317 if (!__ccu_policy_engine_stop(ccu)) {
318 pr_err("%s: unable to stop CCU %s policy engine\n",
319 __func__, ccu->name);
320 return false;
321 }
322
323 /*
324 * For now, if a clock defines its policy bit we just mark
325 * it "enabled" for all four policies.
326 */
327 offset = policy->offset;
328 mask = (u32)1 << policy->bit;
329 for (i = 0; i < CCU_POLICY_COUNT; i++) {
330 u32 reg_val;
331
332 reg_val = __ccu_read(ccu, offset);
333 reg_val |= mask;
334 __ccu_write(ccu, offset, reg_val);
335 offset += sizeof(u32);
336 }
337
338 /* We're done updating; fire up the policy engine again. */
339 ret = __ccu_policy_engine_start(ccu, true);
340 if (!ret)
341 pr_err("%s: unable to restart CCU %s policy engine\n",
342 __func__, ccu->name);
343
344 return ret;
345 }
346
347 /* Gate operations */
348
349 /* Determine whether a clock is gated. CCU lock must be held. */
350 static bool
__is_clk_gate_enabled(struct ccu_data * ccu,struct bcm_clk_gate * gate)351 __is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
352 {
353 u32 bit_mask;
354 u32 reg_val;
355
356 /* If there is no gate we can assume it's enabled. */
357 if (!gate_exists(gate))
358 return true;
359
360 bit_mask = 1 << gate->status_bit;
361 reg_val = __ccu_read(ccu, gate->offset);
362
363 return (reg_val & bit_mask) != 0;
364 }
365
366 /* Determine whether a clock is gated. */
367 static bool
is_clk_gate_enabled(struct ccu_data * ccu,struct bcm_clk_gate * gate)368 is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
369 {
370 long flags;
371 bool ret;
372
373 /* Avoid taking the lock if we can */
374 if (!gate_exists(gate))
375 return true;
376
377 flags = ccu_lock(ccu);
378 ret = __is_clk_gate_enabled(ccu, gate);
379 ccu_unlock(ccu, flags);
380
381 return ret;
382 }
383
384 /*
385 * Commit our desired gate state to the hardware.
386 * Returns true if successful, false otherwise.
387 */
388 static bool
__gate_commit(struct ccu_data * ccu,struct bcm_clk_gate * gate)389 __gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
390 {
391 u32 reg_val;
392 u32 mask;
393 bool enabled = false;
394
395 BUG_ON(!gate_exists(gate));
396 if (!gate_is_sw_controllable(gate))
397 return true; /* Nothing we can change */
398
399 reg_val = __ccu_read(ccu, gate->offset);
400
401 /* For a hardware/software gate, set which is in control */
402 if (gate_is_hw_controllable(gate)) {
403 mask = (u32)1 << gate->hw_sw_sel_bit;
404 if (gate_is_sw_managed(gate))
405 reg_val |= mask;
406 else
407 reg_val &= ~mask;
408 }
409
410 /*
411 * If software is in control, enable or disable the gate.
412 * If hardware is, clear the enabled bit for good measure.
413 * If a software controlled gate can't be disabled, we're
414 * required to write a 0 into the enable bit (but the gate
415 * will be enabled).
416 */
417 mask = (u32)1 << gate->en_bit;
418 if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) &&
419 !gate_is_no_disable(gate))
420 reg_val |= mask;
421 else
422 reg_val &= ~mask;
423
424 __ccu_write(ccu, gate->offset, reg_val);
425
426 /* For a hardware controlled gate, we're done */
427 if (!gate_is_sw_managed(gate))
428 return true;
429
430 /* Otherwise wait for the gate to be in desired state */
431 return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
432 }
433
434 /*
435 * Initialize a gate. Our desired state (hardware/software select,
436 * and if software, its enable state) is committed to hardware
437 * without the usual checks to see if it's already set up that way.
438 * Returns true if successful, false otherwise.
439 */
gate_init(struct ccu_data * ccu,struct bcm_clk_gate * gate)440 static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
441 {
442 if (!gate_exists(gate))
443 return true;
444 return __gate_commit(ccu, gate);
445 }
446
447 /*
448 * Set a gate to enabled or disabled state. Does nothing if the
449 * gate is not currently under software control, or if it is already
450 * in the requested state. Returns true if successful, false
451 * otherwise. CCU lock must be held.
452 */
453 static bool
__clk_gate(struct ccu_data * ccu,struct bcm_clk_gate * gate,bool enable)454 __clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
455 {
456 bool ret;
457
458 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
459 return true; /* Nothing to do */
460
461 if (!enable && gate_is_no_disable(gate)) {
462 pr_warn("%s: invalid gate disable request (ignoring)\n",
463 __func__);
464 return true;
465 }
466
467 if (enable == gate_is_enabled(gate))
468 return true; /* No change */
469
470 gate_flip_enabled(gate);
471 ret = __gate_commit(ccu, gate);
472 if (!ret)
473 gate_flip_enabled(gate); /* Revert the change */
474
475 return ret;
476 }
477
478 /* Enable or disable a gate. Returns 0 if successful, -EIO otherwise */
clk_gate(struct ccu_data * ccu,const char * name,struct bcm_clk_gate * gate,bool enable)479 static int clk_gate(struct ccu_data *ccu, const char *name,
480 struct bcm_clk_gate *gate, bool enable)
481 {
482 unsigned long flags;
483 bool success;
484
485 /*
486 * Avoid taking the lock if we can. We quietly ignore
487 * requests to change state that don't make sense.
488 */
489 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
490 return 0;
491 if (!enable && gate_is_no_disable(gate))
492 return 0;
493
494 flags = ccu_lock(ccu);
495 __ccu_write_enable(ccu);
496
497 success = __clk_gate(ccu, gate, enable);
498
499 __ccu_write_disable(ccu);
500 ccu_unlock(ccu, flags);
501
502 if (success)
503 return 0;
504
505 pr_err("%s: failed to %s gate for %s\n", __func__,
506 str_enable_disable(enable), name);
507
508 return -EIO;
509 }
510
511 /* Hysteresis operations */
512
513 /*
514 * If a clock gate requires a turn-off delay it will have
515 * "hysteresis" register bits defined. The first, if set, enables
516 * the delay; and if enabled, the second bit determines whether the
517 * delay is "low" or "high" (1 means high). For now, if it's
518 * defined for a clock, we set it.
519 */
hyst_init(struct ccu_data * ccu,struct bcm_clk_hyst * hyst)520 static bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst)
521 {
522 u32 offset;
523 u32 reg_val;
524 u32 mask;
525
526 if (!hyst_exists(hyst))
527 return true;
528
529 offset = hyst->offset;
530 mask = (u32)1 << hyst->en_bit;
531 mask |= (u32)1 << hyst->val_bit;
532
533 reg_val = __ccu_read(ccu, offset);
534 reg_val |= mask;
535 __ccu_write(ccu, offset, reg_val);
536
537 return true;
538 }
539
540 /* Trigger operations */
541
542 /*
543 * Caller must ensure CCU lock is held and access is enabled.
544 * Returns true if successful, false otherwise.
545 */
__clk_trigger(struct ccu_data * ccu,struct bcm_clk_trig * trig)546 static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
547 {
548 /* Trigger the clock and wait for it to finish */
549 __ccu_write(ccu, trig->offset, 1 << trig->bit);
550
551 return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
552 }
553
554 /* Divider operations */
555
556 /* Read a divider value and return the scaled divisor it represents. */
divider_read_scaled(struct ccu_data * ccu,struct bcm_clk_div * div)557 static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
558 {
559 unsigned long flags;
560 u32 reg_val;
561 u32 reg_div;
562
563 if (divider_is_fixed(div))
564 return (u64)div->u.fixed;
565
566 flags = ccu_lock(ccu);
567 reg_val = __ccu_read(ccu, div->u.s.offset);
568 ccu_unlock(ccu, flags);
569
570 /* Extract the full divider field from the register value */
571 reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
572
573 /* Return the scaled divisor value it represents */
574 return scaled_div_value(div, reg_div);
575 }
576
577 /*
578 * Convert a divider's scaled divisor value into its recorded form
579 * and commit it into the hardware divider register.
580 *
581 * Returns 0 on success. Returns -EINVAL for invalid arguments.
582 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
583 */
__div_commit(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_div * div,struct bcm_clk_trig * trig)584 static int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
585 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
586 {
587 bool enabled;
588 u32 reg_div;
589 u32 reg_val;
590 int ret = 0;
591
592 BUG_ON(divider_is_fixed(div));
593
594 /*
595 * If we're just initializing the divider, and no initial
596 * state was defined in the device tree, we just find out
597 * what its current value is rather than updating it.
598 */
599 if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) {
600 reg_val = __ccu_read(ccu, div->u.s.offset);
601 reg_div = bitfield_extract(reg_val, div->u.s.shift,
602 div->u.s.width);
603 div->u.s.scaled_div = scaled_div_value(div, reg_div);
604
605 return 0;
606 }
607
608 /* Convert the scaled divisor to the value we need to record */
609 reg_div = divider(div, div->u.s.scaled_div);
610
611 /* Clock needs to be enabled before changing the rate */
612 enabled = __is_clk_gate_enabled(ccu, gate);
613 if (!enabled && !__clk_gate(ccu, gate, true)) {
614 ret = -ENXIO;
615 goto out;
616 }
617
618 /* Replace the divider value and record the result */
619 reg_val = __ccu_read(ccu, div->u.s.offset);
620 reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width,
621 reg_div);
622 __ccu_write(ccu, div->u.s.offset, reg_val);
623
624 /* If the trigger fails we still want to disable the gate */
625 if (!__clk_trigger(ccu, trig))
626 ret = -EIO;
627
628 /* Disable the clock again if it was disabled to begin with */
629 if (!enabled && !__clk_gate(ccu, gate, false))
630 ret = ret ? ret : -ENXIO; /* return first error */
631 out:
632 return ret;
633 }
634
635 /*
636 * Initialize a divider by committing our desired state to hardware
637 * without the usual checks to see if it's already set up that way.
638 * Returns true if successful, false otherwise.
639 */
div_init(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_div * div,struct bcm_clk_trig * trig)640 static bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
641 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
642 {
643 if (!divider_exists(div) || divider_is_fixed(div))
644 return true;
645 return !__div_commit(ccu, gate, div, trig);
646 }
647
divider_write(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_div * div,struct bcm_clk_trig * trig,u64 scaled_div)648 static int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
649 struct bcm_clk_div *div, struct bcm_clk_trig *trig,
650 u64 scaled_div)
651 {
652 unsigned long flags;
653 u64 previous;
654 int ret;
655
656 BUG_ON(divider_is_fixed(div));
657
658 previous = div->u.s.scaled_div;
659 if (previous == scaled_div)
660 return 0; /* No change */
661
662 div->u.s.scaled_div = scaled_div;
663
664 flags = ccu_lock(ccu);
665 __ccu_write_enable(ccu);
666
667 ret = __div_commit(ccu, gate, div, trig);
668
669 __ccu_write_disable(ccu);
670 ccu_unlock(ccu, flags);
671
672 if (ret)
673 div->u.s.scaled_div = previous; /* Revert the change */
674
675 return ret;
676
677 }
678
679 /* Common clock rate helpers */
680
681 /*
682 * Implement the common clock framework recalc_rate method, taking
683 * into account a divider and an optional pre-divider. The
684 * pre-divider register pointer may be NULL.
685 */
clk_recalc_rate(struct ccu_data * ccu,struct bcm_clk_div * div,struct bcm_clk_div * pre_div,unsigned long parent_rate)686 static unsigned long clk_recalc_rate(struct ccu_data *ccu,
687 struct bcm_clk_div *div, struct bcm_clk_div *pre_div,
688 unsigned long parent_rate)
689 {
690 u64 scaled_parent_rate;
691 u64 scaled_div;
692 u64 result;
693
694 if (!divider_exists(div))
695 return parent_rate;
696
697 if (parent_rate > (unsigned long)LONG_MAX)
698 return 0; /* actually this would be a caller bug */
699
700 /*
701 * If there is a pre-divider, divide the scaled parent rate
702 * by the pre-divider value first. In this case--to improve
703 * accuracy--scale the parent rate by *both* the pre-divider
704 * value and the divider before actually computing the
705 * result of the pre-divider.
706 *
707 * If there's only one divider, just scale the parent rate.
708 */
709 if (pre_div && divider_exists(pre_div)) {
710 u64 scaled_rate;
711
712 scaled_rate = scale_rate(pre_div, parent_rate);
713 scaled_rate = scale_rate(div, scaled_rate);
714 scaled_div = divider_read_scaled(ccu, pre_div);
715 scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
716 scaled_div);
717 } else {
718 scaled_parent_rate = scale_rate(div, parent_rate);
719 }
720
721 /*
722 * Get the scaled divisor value, and divide the scaled
723 * parent rate by that to determine this clock's resulting
724 * rate.
725 */
726 scaled_div = divider_read_scaled(ccu, div);
727 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, scaled_div);
728
729 return (unsigned long)result;
730 }
731
732 /*
733 * Compute the output rate produced when a given parent rate is fed
734 * into two dividers. The pre-divider can be NULL, and even if it's
735 * non-null it may be nonexistent. It's also OK for the divider to
736 * be nonexistent, and in that case the pre-divider is also ignored.
737 *
738 * If scaled_div is non-null, it is used to return the scaled divisor
739 * value used by the (downstream) divider to produce that rate.
740 */
round_rate(struct ccu_data * ccu,struct bcm_clk_div * div,struct bcm_clk_div * pre_div,unsigned long rate,unsigned long parent_rate,u64 * scaled_div)741 static long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div,
742 struct bcm_clk_div *pre_div,
743 unsigned long rate, unsigned long parent_rate,
744 u64 *scaled_div)
745 {
746 u64 scaled_parent_rate;
747 u64 min_scaled_div;
748 u64 max_scaled_div;
749 u64 best_scaled_div;
750 u64 result;
751
752 BUG_ON(!divider_exists(div));
753 BUG_ON(!rate);
754 BUG_ON(parent_rate > (u64)LONG_MAX);
755
756 /*
757 * If there is a pre-divider, divide the scaled parent rate
758 * by the pre-divider value first. In this case--to improve
759 * accuracy--scale the parent rate by *both* the pre-divider
760 * value and the divider before actually computing the
761 * result of the pre-divider.
762 *
763 * If there's only one divider, just scale the parent rate.
764 *
765 * For simplicity we treat the pre-divider as fixed (for now).
766 */
767 if (divider_exists(pre_div)) {
768 u64 scaled_rate;
769 u64 scaled_pre_div;
770
771 scaled_rate = scale_rate(pre_div, parent_rate);
772 scaled_rate = scale_rate(div, scaled_rate);
773 scaled_pre_div = divider_read_scaled(ccu, pre_div);
774 scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
775 scaled_pre_div);
776 } else {
777 scaled_parent_rate = scale_rate(div, parent_rate);
778 }
779
780 /*
781 * Compute the best possible divider and ensure it is in
782 * range. A fixed divider can't be changed, so just report
783 * the best we can do.
784 */
785 if (!divider_is_fixed(div)) {
786 best_scaled_div = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate,
787 rate);
788 min_scaled_div = scaled_div_min(div);
789 max_scaled_div = scaled_div_max(div);
790 if (best_scaled_div > max_scaled_div)
791 best_scaled_div = max_scaled_div;
792 else if (best_scaled_div < min_scaled_div)
793 best_scaled_div = min_scaled_div;
794 } else {
795 best_scaled_div = divider_read_scaled(ccu, div);
796 }
797
798 /* OK, figure out the resulting rate */
799 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, best_scaled_div);
800
801 if (scaled_div)
802 *scaled_div = best_scaled_div;
803
804 return (long)result;
805 }
806
807 /* Common clock parent helpers */
808
809 /*
810 * For a given parent selector (register field) value, find the
811 * index into a selector's parent_sel array that contains it.
812 * Returns the index, or BAD_CLK_INDEX if it's not found.
813 */
parent_index(struct bcm_clk_sel * sel,u8 parent_sel)814 static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
815 {
816 u8 i;
817
818 BUG_ON(sel->parent_count > (u32)U8_MAX);
819 for (i = 0; i < sel->parent_count; i++)
820 if (sel->parent_sel[i] == parent_sel)
821 return i;
822 return BAD_CLK_INDEX;
823 }
824
825 /*
826 * Fetch the current value of the selector, and translate that into
827 * its corresponding index in the parent array we registered with
828 * the clock framework.
829 *
830 * Returns parent array index that corresponds with the value found,
831 * or BAD_CLK_INDEX if the found value is out of range.
832 */
selector_read_index(struct ccu_data * ccu,struct bcm_clk_sel * sel)833 static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
834 {
835 unsigned long flags;
836 u32 reg_val;
837 u32 parent_sel;
838 u8 index;
839
840 /* If there's no selector, there's only one parent */
841 if (!selector_exists(sel))
842 return 0;
843
844 /* Get the value in the selector register */
845 flags = ccu_lock(ccu);
846 reg_val = __ccu_read(ccu, sel->offset);
847 ccu_unlock(ccu, flags);
848
849 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
850
851 /* Look up that selector's parent array index and return it */
852 index = parent_index(sel, parent_sel);
853 if (index == BAD_CLK_INDEX)
854 pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
855 __func__, parent_sel, ccu->name, sel->offset);
856
857 return index;
858 }
859
860 /*
861 * Commit our desired selector value to the hardware.
862 *
863 * Returns 0 on success. Returns -EINVAL for invalid arguments.
864 * Returns -ENXIO if gating failed, and -EIO if a trigger failed.
865 */
866 static int
__sel_commit(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_sel * sel,struct bcm_clk_trig * trig)867 __sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
868 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
869 {
870 u32 parent_sel;
871 u32 reg_val;
872 bool enabled;
873 int ret = 0;
874
875 BUG_ON(!selector_exists(sel));
876
877 /*
878 * If we're just initializing the selector, and no initial
879 * state was defined in the device tree, we just find out
880 * what its current value is rather than updating it.
881 */
882 if (sel->clk_index == BAD_CLK_INDEX) {
883 u8 index;
884
885 reg_val = __ccu_read(ccu, sel->offset);
886 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
887 index = parent_index(sel, parent_sel);
888 if (index == BAD_CLK_INDEX)
889 return -EINVAL;
890 sel->clk_index = index;
891
892 return 0;
893 }
894
895 BUG_ON((u32)sel->clk_index >= sel->parent_count);
896 parent_sel = sel->parent_sel[sel->clk_index];
897
898 /* Clock needs to be enabled before changing the parent */
899 enabled = __is_clk_gate_enabled(ccu, gate);
900 if (!enabled && !__clk_gate(ccu, gate, true))
901 return -ENXIO;
902
903 /* Replace the selector value and record the result */
904 reg_val = __ccu_read(ccu, sel->offset);
905 reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel);
906 __ccu_write(ccu, sel->offset, reg_val);
907
908 /* If the trigger fails we still want to disable the gate */
909 if (!__clk_trigger(ccu, trig))
910 ret = -EIO;
911
912 /* Disable the clock again if it was disabled to begin with */
913 if (!enabled && !__clk_gate(ccu, gate, false))
914 ret = ret ? ret : -ENXIO; /* return first error */
915
916 return ret;
917 }
918
919 /*
920 * Initialize a selector by committing our desired state to hardware
921 * without the usual checks to see if it's already set up that way.
922 * Returns true if successful, false otherwise.
923 */
sel_init(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_sel * sel,struct bcm_clk_trig * trig)924 static bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
925 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
926 {
927 if (!selector_exists(sel))
928 return true;
929 return !__sel_commit(ccu, gate, sel, trig);
930 }
931
932 /*
933 * Write a new value into a selector register to switch to a
934 * different parent clock. Returns 0 on success, or an error code
935 * (from __sel_commit()) otherwise.
936 */
selector_write(struct ccu_data * ccu,struct bcm_clk_gate * gate,struct bcm_clk_sel * sel,struct bcm_clk_trig * trig,u8 index)937 static int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
938 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig,
939 u8 index)
940 {
941 unsigned long flags;
942 u8 previous;
943 int ret;
944
945 previous = sel->clk_index;
946 if (previous == index)
947 return 0; /* No change */
948
949 sel->clk_index = index;
950
951 flags = ccu_lock(ccu);
952 __ccu_write_enable(ccu);
953
954 ret = __sel_commit(ccu, gate, sel, trig);
955
956 __ccu_write_disable(ccu);
957 ccu_unlock(ccu, flags);
958
959 if (ret)
960 sel->clk_index = previous; /* Revert the change */
961
962 return ret;
963 }
964
965 /* Clock operations */
966
kona_peri_clk_enable(struct clk_hw * hw)967 static int kona_peri_clk_enable(struct clk_hw *hw)
968 {
969 struct kona_clk *bcm_clk = to_kona_clk(hw);
970 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
971
972 return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
973 }
974
kona_peri_clk_disable(struct clk_hw * hw)975 static void kona_peri_clk_disable(struct clk_hw *hw)
976 {
977 struct kona_clk *bcm_clk = to_kona_clk(hw);
978 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
979
980 (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
981 }
982
kona_peri_clk_is_enabled(struct clk_hw * hw)983 static int kona_peri_clk_is_enabled(struct clk_hw *hw)
984 {
985 struct kona_clk *bcm_clk = to_kona_clk(hw);
986 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
987
988 return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
989 }
990
kona_peri_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)991 static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
992 unsigned long parent_rate)
993 {
994 struct kona_clk *bcm_clk = to_kona_clk(hw);
995 struct peri_clk_data *data = bcm_clk->u.peri;
996
997 return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
998 parent_rate);
999 }
1000
kona_peri_clk_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)1001 static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1002 unsigned long *parent_rate)
1003 {
1004 struct kona_clk *bcm_clk = to_kona_clk(hw);
1005 struct bcm_clk_div *div = &bcm_clk->u.peri->div;
1006
1007 if (!divider_exists(div))
1008 return clk_hw_get_rate(hw);
1009
1010 /* Quietly avoid a zero rate */
1011 return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
1012 rate ? rate : 1, *parent_rate, NULL);
1013 }
1014
kona_peri_clk_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)1015 static int kona_peri_clk_determine_rate(struct clk_hw *hw,
1016 struct clk_rate_request *req)
1017 {
1018 struct kona_clk *bcm_clk = to_kona_clk(hw);
1019 struct clk_hw *current_parent;
1020 unsigned long parent_rate;
1021 unsigned long best_delta;
1022 unsigned long best_rate;
1023 u32 parent_count;
1024 long rate;
1025 u32 which;
1026
1027 /*
1028 * If there is no other parent to choose, use the current one.
1029 * Note: We don't honor (or use) CLK_SET_RATE_NO_REPARENT.
1030 */
1031 WARN_ON_ONCE(bcm_clk->init_data.flags & CLK_SET_RATE_NO_REPARENT);
1032 parent_count = (u32)bcm_clk->init_data.num_parents;
1033 if (parent_count < 2) {
1034 rate = kona_peri_clk_round_rate(hw, req->rate,
1035 &req->best_parent_rate);
1036 if (rate < 0)
1037 return rate;
1038
1039 req->rate = rate;
1040 return 0;
1041 }
1042
1043 /* Unless we can do better, stick with current parent */
1044 current_parent = clk_hw_get_parent(hw);
1045 parent_rate = clk_hw_get_rate(current_parent);
1046 best_rate = kona_peri_clk_round_rate(hw, req->rate, &parent_rate);
1047 best_delta = abs(best_rate - req->rate);
1048
1049 /* Check whether any other parent clock can produce a better result */
1050 for (which = 0; which < parent_count; which++) {
1051 struct clk_hw *parent = clk_hw_get_parent_by_index(hw, which);
1052 unsigned long delta;
1053 unsigned long other_rate;
1054
1055 BUG_ON(!parent);
1056 if (parent == current_parent)
1057 continue;
1058
1059 /* We don't support CLK_SET_RATE_PARENT */
1060 parent_rate = clk_hw_get_rate(parent);
1061 other_rate = kona_peri_clk_round_rate(hw, req->rate,
1062 &parent_rate);
1063 delta = abs(other_rate - req->rate);
1064 if (delta < best_delta) {
1065 best_delta = delta;
1066 best_rate = other_rate;
1067 req->best_parent_hw = parent;
1068 req->best_parent_rate = parent_rate;
1069 }
1070 }
1071
1072 req->rate = best_rate;
1073 return 0;
1074 }
1075
kona_peri_clk_set_parent(struct clk_hw * hw,u8 index)1076 static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
1077 {
1078 struct kona_clk *bcm_clk = to_kona_clk(hw);
1079 struct peri_clk_data *data = bcm_clk->u.peri;
1080 struct bcm_clk_sel *sel = &data->sel;
1081 struct bcm_clk_trig *trig;
1082 int ret;
1083
1084 BUG_ON(index >= sel->parent_count);
1085
1086 /* If there's only one parent we don't require a selector */
1087 if (!selector_exists(sel))
1088 return 0;
1089
1090 /*
1091 * The regular trigger is used by default, but if there's a
1092 * pre-trigger we want to use that instead.
1093 */
1094 trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
1095 : &data->trig;
1096
1097 ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index);
1098 if (ret == -ENXIO) {
1099 pr_err("%s: gating failure for %s\n", __func__,
1100 bcm_clk->init_data.name);
1101 ret = -EIO; /* Don't proliferate weird errors */
1102 } else if (ret == -EIO) {
1103 pr_err("%s: %strigger failed for %s\n", __func__,
1104 trig == &data->pre_trig ? "pre-" : "",
1105 bcm_clk->init_data.name);
1106 }
1107
1108 return ret;
1109 }
1110
kona_peri_clk_get_parent(struct clk_hw * hw)1111 static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
1112 {
1113 struct kona_clk *bcm_clk = to_kona_clk(hw);
1114 struct peri_clk_data *data = bcm_clk->u.peri;
1115 u8 index;
1116
1117 index = selector_read_index(bcm_clk->ccu, &data->sel);
1118
1119 /* Not all callers would handle an out-of-range value gracefully */
1120 return index == BAD_CLK_INDEX ? 0 : index;
1121 }
1122
kona_peri_clk_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1123 static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1124 unsigned long parent_rate)
1125 {
1126 struct kona_clk *bcm_clk = to_kona_clk(hw);
1127 struct peri_clk_data *data = bcm_clk->u.peri;
1128 struct bcm_clk_div *div = &data->div;
1129 u64 scaled_div = 0;
1130 int ret;
1131
1132 if (parent_rate > (unsigned long)LONG_MAX)
1133 return -EINVAL;
1134
1135 if (rate == clk_hw_get_rate(hw))
1136 return 0;
1137
1138 if (!divider_exists(div))
1139 return rate == parent_rate ? 0 : -EINVAL;
1140
1141 /*
1142 * A fixed divider can't be changed. (Nor can a fixed
1143 * pre-divider be, but for now we never actually try to
1144 * change that.) Tolerate a request for a no-op change.
1145 */
1146 if (divider_is_fixed(&data->div))
1147 return rate == parent_rate ? 0 : -EINVAL;
1148
1149 /*
1150 * Get the scaled divisor value needed to achieve a clock
1151 * rate as close as possible to what was requested, given
1152 * the parent clock rate supplied.
1153 */
1154 (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
1155 rate ? rate : 1, parent_rate, &scaled_div);
1156
1157 /*
1158 * We aren't updating any pre-divider at this point, so
1159 * we'll use the regular trigger.
1160 */
1161 ret = divider_write(bcm_clk->ccu, &data->gate, &data->div,
1162 &data->trig, scaled_div);
1163 if (ret == -ENXIO) {
1164 pr_err("%s: gating failure for %s\n", __func__,
1165 bcm_clk->init_data.name);
1166 ret = -EIO; /* Don't proliferate weird errors */
1167 } else if (ret == -EIO) {
1168 pr_err("%s: trigger failed for %s\n", __func__,
1169 bcm_clk->init_data.name);
1170 }
1171
1172 return ret;
1173 }
1174
1175 struct clk_ops kona_peri_clk_ops = {
1176 .enable = kona_peri_clk_enable,
1177 .disable = kona_peri_clk_disable,
1178 .is_enabled = kona_peri_clk_is_enabled,
1179 .recalc_rate = kona_peri_clk_recalc_rate,
1180 .determine_rate = kona_peri_clk_determine_rate,
1181 .set_parent = kona_peri_clk_set_parent,
1182 .get_parent = kona_peri_clk_get_parent,
1183 .set_rate = kona_peri_clk_set_rate,
1184 };
1185
1186 /* Put a peripheral clock into its initial state */
__peri_clk_init(struct kona_clk * bcm_clk)1187 static bool __peri_clk_init(struct kona_clk *bcm_clk)
1188 {
1189 struct ccu_data *ccu = bcm_clk->ccu;
1190 struct peri_clk_data *peri = bcm_clk->u.peri;
1191 const char *name = bcm_clk->init_data.name;
1192 struct bcm_clk_trig *trig;
1193
1194 BUG_ON(bcm_clk->type != bcm_clk_peri);
1195
1196 if (!policy_init(ccu, &peri->policy)) {
1197 pr_err("%s: error initializing policy for %s\n",
1198 __func__, name);
1199 return false;
1200 }
1201 if (!gate_init(ccu, &peri->gate)) {
1202 pr_err("%s: error initializing gate for %s\n", __func__, name);
1203 return false;
1204 }
1205 if (!hyst_init(ccu, &peri->hyst)) {
1206 pr_err("%s: error initializing hyst for %s\n", __func__, name);
1207 return false;
1208 }
1209 if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) {
1210 pr_err("%s: error initializing divider for %s\n", __func__,
1211 name);
1212 return false;
1213 }
1214
1215 /*
1216 * For the pre-divider and selector, the pre-trigger is used
1217 * if it's present, otherwise we just use the regular trigger.
1218 */
1219 trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
1220 : &peri->trig;
1221
1222 if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) {
1223 pr_err("%s: error initializing pre-divider for %s\n", __func__,
1224 name);
1225 return false;
1226 }
1227
1228 if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) {
1229 pr_err("%s: error initializing selector for %s\n", __func__,
1230 name);
1231 return false;
1232 }
1233
1234 return true;
1235 }
1236
__kona_clk_init(struct kona_clk * bcm_clk)1237 static bool __kona_clk_init(struct kona_clk *bcm_clk)
1238 {
1239 switch (bcm_clk->type) {
1240 case bcm_clk_peri:
1241 return __peri_clk_init(bcm_clk);
1242 default:
1243 BUG();
1244 }
1245 return false;
1246 }
1247
1248 /* Set a CCU and all its clocks into their desired initial state */
kona_ccu_init(struct ccu_data * ccu)1249 bool __init kona_ccu_init(struct ccu_data *ccu)
1250 {
1251 unsigned long flags;
1252 unsigned int which;
1253 struct kona_clk *kona_clks = ccu->kona_clks;
1254 bool success = true;
1255
1256 flags = ccu_lock(ccu);
1257 __ccu_write_enable(ccu);
1258
1259 for (which = 0; which < ccu->clk_num; which++) {
1260 struct kona_clk *bcm_clk = &kona_clks[which];
1261
1262 if (!bcm_clk->ccu)
1263 continue;
1264
1265 success &= __kona_clk_init(bcm_clk);
1266 }
1267
1268 __ccu_write_disable(ccu);
1269 ccu_unlock(ccu, flags);
1270 return success;
1271 }
1272