This source file includes following definitions.
- bitfield_mask
- bitfield_extract
- bitfield_replace
- scaled_div_value
- scaled_div_build
- scaled_div_min
- scaled_div_max
- divider
- scale_rate
- __ccu_read
- __ccu_write
- ccu_lock
- ccu_unlock
- __ccu_write_enable
- __ccu_write_disable
- __ccu_wait_bit
- __ccu_policy_engine_start
- __ccu_policy_engine_stop
- policy_init
- __is_clk_gate_enabled
- is_clk_gate_enabled
- __gate_commit
- gate_init
- __clk_gate
- clk_gate
- hyst_init
- __clk_trigger
- divider_read_scaled
- __div_commit
- div_init
- divider_write
- clk_recalc_rate
- round_rate
- parent_index
- selector_read_index
- __sel_commit
- sel_init
- selector_write
- kona_peri_clk_enable
- kona_peri_clk_disable
- kona_peri_clk_is_enabled
- kona_peri_clk_recalc_rate
- kona_peri_clk_round_rate
- kona_peri_clk_determine_rate
- kona_peri_clk_set_parent
- kona_peri_clk_get_parent
- kona_peri_clk_set_rate
- __peri_clk_init
- __kona_clk_init
- kona_ccu_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include "clk-kona.h"
16
17 #include <linux/delay.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/clk-provider.h>
21
22
23
24
25
26
27
28 #define CCU_POLICY_COUNT 4
29
30 #define CCU_ACCESS_PASSWORD 0xA5A500
31 #define CLK_GATE_DELAY_LOOP 2000
32
33
34
35
36 static inline u32 bitfield_mask(u32 shift, u32 width)
37 {
38 return ((1 << width) - 1) << shift;
39 }
40
41
42 static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
43 {
44 return (reg_val & bitfield_mask(shift, width)) >> shift;
45 }
46
47
48 static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
49 {
50 u32 mask = bitfield_mask(shift, width);
51
52 return (reg_val & ~mask) | (val << shift);
53 }
54
55
56
57
58 static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
59 {
60 return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
61 }
62
63
64
65
66
67
68 u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
69 {
70 u64 combined;
71
72 BUG_ON(!div_value);
73 BUG_ON(billionths >= BILLION);
74
75 combined = (u64)div_value * BILLION + billionths;
76 combined <<= div->u.s.frac_width;
77
78 return DIV_ROUND_CLOSEST_ULL(combined, BILLION);
79 }
80
81
82 static inline u64
83 scaled_div_min(struct bcm_clk_div *div)
84 {
85 if (divider_is_fixed(div))
86 return (u64)div->u.fixed;
87
88 return scaled_div_value(div, 0);
89 }
90
91
92 u64 scaled_div_max(struct bcm_clk_div *div)
93 {
94 u32 reg_div;
95
96 if (divider_is_fixed(div))
97 return (u64)div->u.fixed;
98
99 reg_div = ((u32)1 << div->u.s.width) - 1;
100
101 return scaled_div_value(div, reg_div);
102 }
103
104
105
106
107
108 static inline u32
109 divider(struct bcm_clk_div *div, u64 scaled_div)
110 {
111 BUG_ON(scaled_div < scaled_div_min(div));
112 BUG_ON(scaled_div > scaled_div_max(div));
113
114 return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
115 }
116
117
118 static inline u64
119 scale_rate(struct bcm_clk_div *div, u32 rate)
120 {
121 if (divider_is_fixed(div))
122 return (u64)rate;
123
124 return (u64)rate << div->u.s.frac_width;
125 }
126
127
128
129
130 static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
131 {
132 return readl(ccu->base + reg_offset);
133 }
134
135
136 static inline void
137 __ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
138 {
139 writel(reg_val, ccu->base + reg_offset);
140 }
141
142 static inline unsigned long ccu_lock(struct ccu_data *ccu)
143 {
144 unsigned long flags;
145
146 spin_lock_irqsave(&ccu->lock, flags);
147
148 return flags;
149 }
150 static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
151 {
152 spin_unlock_irqrestore(&ccu->lock, flags);
153 }
154
155
156
157
158
159 static inline void __ccu_write_enable(struct ccu_data *ccu)
160 {
161 if (ccu->write_enabled) {
162 pr_err("%s: access already enabled for %s\n", __func__,
163 ccu->name);
164 return;
165 }
166 ccu->write_enabled = true;
167 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
168 }
169
170 static inline void __ccu_write_disable(struct ccu_data *ccu)
171 {
172 if (!ccu->write_enabled) {
173 pr_err("%s: access wasn't enabled for %s\n", __func__,
174 ccu->name);
175 return;
176 }
177
178 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
179 ccu->write_enabled = false;
180 }
181
182
183
184
185
186
187
188
189
190 static inline bool
191 __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
192 {
193 unsigned int tries;
194 u32 bit_mask = 1 << bit;
195
196 for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
197 u32 val;
198 bool bit_val;
199
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);
205 }
206 pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
207 ccu->name, reg_offset, bit, want ? "set" : "clear");
208
209 return false;
210 }
211
212
213
214 static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
215 {
216 struct bcm_policy_ctl *control = &ccu->policy.control;
217 u32 offset;
218 u32 go_bit;
219 u32 mask;
220 bool ret;
221
222
223 if (!policy_ctl_exists(control))
224 return true;
225
226 offset = control->offset;
227 go_bit = control->go_bit;
228
229
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;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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);
258
259
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);
264
265 return ret;
266 }
267
268 static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
269 {
270 struct bcm_lvm_en *enable = &ccu->policy.enable;
271 u32 offset;
272 u32 enable_bit;
273 bool ret;
274
275
276 if (!policy_lvm_en_exists(enable))
277 return true;
278
279
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;
287 }
288
289
290 __ccu_write(ccu, offset, (u32)1 << enable_bit);
291
292
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);
297
298 return ret;
299 }
300
301
302
303
304
305
306
307
308
309
310 static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
311 {
312 u32 offset;
313 u32 mask;
314 int i;
315 bool ret;
316
317 if (!policy_exists(policy))
318 return true;
319
320
321
322
323
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;
328 }
329
330
331
332
333
334 offset = policy->offset;
335 mask = (u32)1 << policy->bit;
336 for (i = 0; i < CCU_POLICY_COUNT; i++) {
337 u32 reg_val;
338
339 reg_val = __ccu_read(ccu, offset);
340 reg_val |= mask;
341 __ccu_write(ccu, offset, reg_val);
342 offset += sizeof(u32);
343 }
344
345
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);
350
351 return ret;
352 }
353
354
355
356
357 static bool
358 __is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
359 {
360 u32 bit_mask;
361 u32 reg_val;
362
363
364 if (!gate_exists(gate))
365 return true;
366
367 bit_mask = 1 << gate->status_bit;
368 reg_val = __ccu_read(ccu, gate->offset);
369
370 return (reg_val & bit_mask) != 0;
371 }
372
373
374 static bool
375 is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
376 {
377 long flags;
378 bool ret;
379
380
381 if (!gate_exists(gate))
382 return true;
383
384 flags = ccu_lock(ccu);
385 ret = __is_clk_gate_enabled(ccu, gate);
386 ccu_unlock(ccu, flags);
387
388 return ret;
389 }
390
391
392
393
394
395 static bool
396 __gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
397 {
398 u32 reg_val;
399 u32 mask;
400 bool enabled = false;
401
402 BUG_ON(!gate_exists(gate));
403 if (!gate_is_sw_controllable(gate))
404 return true;
405
406 reg_val = __ccu_read(ccu, gate->offset);
407
408
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;
415 }
416
417
418
419
420
421
422
423
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;
430
431 __ccu_write(ccu, gate->offset, reg_val);
432
433
434 if (!gate_is_sw_managed(gate))
435 return true;
436
437
438 return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
439 }
440
441
442
443
444
445
446
447 static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
448 {
449 if (!gate_exists(gate))
450 return true;
451 return __gate_commit(ccu, gate);
452 }
453
454
455
456
457
458
459
460 static bool
461 __clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
462 {
463 bool ret;
464
465 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
466 return true;
467
468 if (!enable && gate_is_no_disable(gate)) {
469 pr_warn("%s: invalid gate disable request (ignoring)\n",
470 __func__);
471 return true;
472 }
473
474 if (enable == gate_is_enabled(gate))
475 return true;
476
477 gate_flip_enabled(gate);
478 ret = __gate_commit(ccu, gate);
479 if (!ret)
480 gate_flip_enabled(gate);
481
482 return ret;
483 }
484
485
486 static int clk_gate(struct ccu_data *ccu, const char *name,
487 struct bcm_clk_gate *gate, bool enable)
488 {
489 unsigned long flags;
490 bool success;
491
492
493
494
495
496 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
497 return 0;
498 if (!enable && gate_is_no_disable(gate))
499 return 0;
500
501 flags = ccu_lock(ccu);
502 __ccu_write_enable(ccu);
503
504 success = __clk_gate(ccu, gate, enable);
505
506 __ccu_write_disable(ccu);
507 ccu_unlock(ccu, flags);
508
509 if (success)
510 return 0;
511
512 pr_err("%s: failed to %s gate for %s\n", __func__,
513 enable ? "enable" : "disable", name);
514
515 return -EIO;
516 }
517
518
519
520
521
522
523
524
525
526
527 static bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst)
528 {
529 u32 offset;
530 u32 reg_val;
531 u32 mask;
532
533 if (!hyst_exists(hyst))
534 return true;
535
536 offset = hyst->offset;
537 mask = (u32)1 << hyst->en_bit;
538 mask |= (u32)1 << hyst->val_bit;
539
540 reg_val = __ccu_read(ccu, offset);
541 reg_val |= mask;
542 __ccu_write(ccu, offset, reg_val);
543
544 return true;
545 }
546
547
548
549
550
551
552
553 static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
554 {
555
556 __ccu_write(ccu, trig->offset, 1 << trig->bit);
557
558 return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
559 }
560
561
562
563
564 static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
565 {
566 unsigned long flags;
567 u32 reg_val;
568 u32 reg_div;
569
570 if (divider_is_fixed(div))
571 return (u64)div->u.fixed;
572
573 flags = ccu_lock(ccu);
574 reg_val = __ccu_read(ccu, div->u.s.offset);
575 ccu_unlock(ccu, flags);
576
577
578 reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
579
580
581 return scaled_div_value(div, reg_div);
582 }
583
584
585
586
587
588
589
590
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)
593 {
594 bool enabled;
595 u32 reg_div;
596 u32 reg_val;
597 int ret = 0;
598
599 BUG_ON(divider_is_fixed(div));
600
601
602
603
604
605
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);
611
612 return 0;
613 }
614
615
616 reg_div = divider(div, div->u.s.scaled_div);
617
618
619 enabled = __is_clk_gate_enabled(ccu, gate);
620 if (!enabled && !__clk_gate(ccu, gate, true)) {
621 ret = -ENXIO;
622 goto out;
623 }
624
625
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);
630
631
632 if (!__clk_trigger(ccu, trig))
633 ret = -EIO;
634
635
636 if (!enabled && !__clk_gate(ccu, gate, false))
637 ret = ret ? ret : -ENXIO;
638 out:
639 return ret;
640 }
641
642
643
644
645
646
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)
649 {
650 if (!divider_exists(div) || divider_is_fixed(div))
651 return true;
652 return !__div_commit(ccu, gate, div, trig);
653 }
654
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)
658 {
659 unsigned long flags;
660 u64 previous;
661 int ret;
662
663 BUG_ON(divider_is_fixed(div));
664
665 previous = div->u.s.scaled_div;
666 if (previous == scaled_div)
667 return 0;
668
669 div->u.s.scaled_div = scaled_div;
670
671 flags = ccu_lock(ccu);
672 __ccu_write_enable(ccu);
673
674 ret = __div_commit(ccu, gate, div, trig);
675
676 __ccu_write_disable(ccu);
677 ccu_unlock(ccu, flags);
678
679 if (ret)
680 div->u.s.scaled_div = previous;
681
682 return ret;
683
684 }
685
686
687
688
689
690
691
692
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)
696 {
697 u64 scaled_parent_rate;
698 u64 scaled_div;
699 u64 result;
700
701 if (!divider_exists(div))
702 return parent_rate;
703
704 if (parent_rate > (unsigned long)LONG_MAX)
705 return 0;
706
707
708
709
710
711
712
713
714
715
716 if (pre_div && divider_exists(pre_div)) {
717 u64 scaled_rate;
718
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);
726 }
727
728
729
730
731
732
733 scaled_div = divider_read_scaled(ccu, div);
734 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, scaled_div);
735
736 return (unsigned long)result;
737 }
738
739
740
741
742
743
744
745
746
747
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)
752 {
753 u64 scaled_parent_rate;
754 u64 min_scaled_div;
755 u64 max_scaled_div;
756 u64 best_scaled_div;
757 u64 result;
758
759 BUG_ON(!divider_exists(div));
760 BUG_ON(!rate);
761 BUG_ON(parent_rate > (u64)LONG_MAX);
762
763
764
765
766
767
768
769
770
771
772
773
774 if (divider_exists(pre_div)) {
775 u64 scaled_rate;
776 u64 scaled_pre_div;
777
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);
785 }
786
787
788
789
790
791
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);
803 }
804
805
806 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, best_scaled_div);
807
808 if (scaled_div)
809 *scaled_div = best_scaled_div;
810
811 return (long)result;
812 }
813
814
815
816
817
818
819
820
821 static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
822 {
823 u8 i;
824
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;
830 }
831
832
833
834
835
836
837
838
839
840 static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
841 {
842 unsigned long flags;
843 u32 reg_val;
844 u32 parent_sel;
845 u8 index;
846
847
848 if (!selector_exists(sel))
849 return 0;
850
851
852 flags = ccu_lock(ccu);
853 reg_val = __ccu_read(ccu, sel->offset);
854 ccu_unlock(ccu, flags);
855
856 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
857
858
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);
863
864 return index;
865 }
866
867
868
869
870
871
872
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)
876 {
877 u32 parent_sel;
878 u32 reg_val;
879 bool enabled;
880 int ret = 0;
881
882 BUG_ON(!selector_exists(sel));
883
884
885
886
887
888
889 if (sel->clk_index == BAD_CLK_INDEX) {
890 u8 index;
891
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;
898
899 return 0;
900 }
901
902 BUG_ON((u32)sel->clk_index >= sel->parent_count);
903 parent_sel = sel->parent_sel[sel->clk_index];
904
905
906 enabled = __is_clk_gate_enabled(ccu, gate);
907 if (!enabled && !__clk_gate(ccu, gate, true))
908 return -ENXIO;
909
910
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);
914
915
916 if (!__clk_trigger(ccu, trig))
917 ret = -EIO;
918
919
920 if (!enabled && !__clk_gate(ccu, gate, false))
921 ret = ret ? ret : -ENXIO;
922
923 return ret;
924 }
925
926
927
928
929
930
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)
933 {
934 if (!selector_exists(sel))
935 return true;
936 return !__sel_commit(ccu, gate, sel, trig);
937 }
938
939
940
941
942
943
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)
947 {
948 unsigned long flags;
949 u8 previous;
950 int ret;
951
952 previous = sel->clk_index;
953 if (previous == index)
954 return 0;
955
956 sel->clk_index = index;
957
958 flags = ccu_lock(ccu);
959 __ccu_write_enable(ccu);
960
961 ret = __sel_commit(ccu, gate, sel, trig);
962
963 __ccu_write_disable(ccu);
964 ccu_unlock(ccu, flags);
965
966 if (ret)
967 sel->clk_index = previous;
968
969 return ret;
970 }
971
972
973
974 static int kona_peri_clk_enable(struct clk_hw *hw)
975 {
976 struct kona_clk *bcm_clk = to_kona_clk(hw);
977 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
978
979 return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
980 }
981
982 static void kona_peri_clk_disable(struct clk_hw *hw)
983 {
984 struct kona_clk *bcm_clk = to_kona_clk(hw);
985 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
986
987 (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
988 }
989
990 static int kona_peri_clk_is_enabled(struct clk_hw *hw)
991 {
992 struct kona_clk *bcm_clk = to_kona_clk(hw);
993 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
994
995 return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
996 }
997
998 static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
999 unsigned long parent_rate)
1000 {
1001 struct kona_clk *bcm_clk = to_kona_clk(hw);
1002 struct peri_clk_data *data = bcm_clk->u.peri;
1003
1004 return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
1005 parent_rate);
1006 }
1007
1008 static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1009 unsigned long *parent_rate)
1010 {
1011 struct kona_clk *bcm_clk = to_kona_clk(hw);
1012 struct bcm_clk_div *div = &bcm_clk->u.peri->div;
1013
1014 if (!divider_exists(div))
1015 return clk_hw_get_rate(hw);
1016
1017
1018 return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
1019 rate ? rate : 1, *parent_rate, NULL);
1020 }
1021
1022 static int kona_peri_clk_determine_rate(struct clk_hw *hw,
1023 struct clk_rate_request *req)
1024 {
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;
1033
1034
1035
1036
1037
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;
1045
1046 req->rate = rate;
1047 return 0;
1048 }
1049
1050
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);
1055
1056
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;
1061
1062 BUG_ON(!parent);
1063 if (parent == current_parent)
1064 continue;
1065
1066
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;
1076 }
1077 }
1078
1079 req->rate = best_rate;
1080 return 0;
1081 }
1082
1083 static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
1084 {
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;
1090
1091 BUG_ON(index >= sel->parent_count);
1092
1093
1094 if (!selector_exists(sel))
1095 return 0;
1096
1097
1098
1099
1100
1101 trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
1102 : &data->trig;
1103
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;
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);
1113 }
1114
1115 return ret;
1116 }
1117
1118 static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
1119 {
1120 struct kona_clk *bcm_clk = to_kona_clk(hw);
1121 struct peri_clk_data *data = bcm_clk->u.peri;
1122 u8 index;
1123
1124 index = selector_read_index(bcm_clk->ccu, &data->sel);
1125
1126
1127 return index == BAD_CLK_INDEX ? 0 : index;
1128 }
1129
1130 static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1131 unsigned long parent_rate)
1132 {
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;
1138
1139 if (parent_rate > (unsigned long)LONG_MAX)
1140 return -EINVAL;
1141
1142 if (rate == clk_hw_get_rate(hw))
1143 return 0;
1144
1145 if (!divider_exists(div))
1146 return rate == parent_rate ? 0 : -EINVAL;
1147
1148
1149
1150
1151
1152
1153 if (divider_is_fixed(&data->div))
1154 return rate == parent_rate ? 0 : -EINVAL;
1155
1156
1157
1158
1159
1160
1161 (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
1162 rate ? rate : 1, parent_rate, &scaled_div);
1163
1164
1165
1166
1167
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;
1174 } else if (ret == -EIO) {
1175 pr_err("%s: trigger failed for %s\n", __func__,
1176 bcm_clk->init_data.name);
1177 }
1178
1179 return ret;
1180 }
1181
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,
1191 };
1192
1193
1194 static bool __peri_clk_init(struct kona_clk *bcm_clk)
1195 {
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;
1200
1201 BUG_ON(bcm_clk->type != bcm_clk_peri);
1202
1203 if (!policy_init(ccu, &peri->policy)) {
1204 pr_err("%s: error initializing policy for %s\n",
1205 __func__, name);
1206 return false;
1207 }
1208 if (!gate_init(ccu, &peri->gate)) {
1209 pr_err("%s: error initializing gate for %s\n", __func__, name);
1210 return false;
1211 }
1212 if (!hyst_init(ccu, &peri->hyst)) {
1213 pr_err("%s: error initializing hyst for %s\n", __func__, name);
1214 return false;
1215 }
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;
1220 }
1221
1222
1223
1224
1225
1226 trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
1227 : &peri->trig;
1228
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;
1233 }
1234
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;
1239 }
1240
1241 return true;
1242 }
1243
1244 static bool __kona_clk_init(struct kona_clk *bcm_clk)
1245 {
1246 switch (bcm_clk->type) {
1247 case bcm_clk_peri:
1248 return __peri_clk_init(bcm_clk);
1249 default:
1250 BUG();
1251 }
1252 return false;
1253 }
1254
1255
1256 bool __init kona_ccu_init(struct ccu_data *ccu)
1257 {
1258 unsigned long flags;
1259 unsigned int which;
1260 struct kona_clk *kona_clks = ccu->kona_clks;
1261 bool success = true;
1262
1263 flags = ccu_lock(ccu);
1264 __ccu_write_enable(ccu);
1265
1266 for (which = 0; which < ccu->clk_num; which++) {
1267 struct kona_clk *bcm_clk = &kona_clks[which];
1268
1269 if (!bcm_clk->ccu)
1270 continue;
1271
1272 success &= __kona_clk_init(bcm_clk);
1273 }
1274
1275 __ccu_write_disable(ccu);
1276 ccu_unlock(ccu, flags);
1277 return success;
1278 }