0001
0002
0003
0004
0005
0006
0007 #include "clk-kona.h"
0008
0009 #include <linux/delay.h>
0010 #include <linux/io.h>
0011 #include <linux/kernel.h>
0012 #include <linux/clk-provider.h>
0013
0014
0015
0016
0017
0018
0019
0020 #define CCU_POLICY_COUNT 4
0021
0022 #define CCU_ACCESS_PASSWORD 0xA5A500
0023 #define CLK_GATE_DELAY_LOOP 2000
0024
0025
0026
0027
0028 static inline u32 bitfield_mask(u32 shift, u32 width)
0029 {
0030 return ((1 << width) - 1) << shift;
0031 }
0032
0033
0034 static inline u32 bitfield_extract(u32 reg_val, u32 shift, u32 width)
0035 {
0036 return (reg_val & bitfield_mask(shift, width)) >> shift;
0037 }
0038
0039
0040 static inline u32 bitfield_replace(u32 reg_val, u32 shift, u32 width, u32 val)
0041 {
0042 u32 mask = bitfield_mask(shift, width);
0043
0044 return (reg_val & ~mask) | (val << shift);
0045 }
0046
0047
0048
0049
0050 static inline u64 scaled_div_value(struct bcm_clk_div *div, u32 reg_div)
0051 {
0052 return (u64)reg_div + ((u64)1 << div->u.s.frac_width);
0053 }
0054
0055
0056
0057
0058
0059
0060 u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, u32 billionths)
0061 {
0062 u64 combined;
0063
0064 BUG_ON(!div_value);
0065 BUG_ON(billionths >= BILLION);
0066
0067 combined = (u64)div_value * BILLION + billionths;
0068 combined <<= div->u.s.frac_width;
0069
0070 return DIV_ROUND_CLOSEST_ULL(combined, BILLION);
0071 }
0072
0073
0074 static inline u64
0075 scaled_div_min(struct bcm_clk_div *div)
0076 {
0077 if (divider_is_fixed(div))
0078 return (u64)div->u.fixed;
0079
0080 return scaled_div_value(div, 0);
0081 }
0082
0083
0084 u64 scaled_div_max(struct bcm_clk_div *div)
0085 {
0086 u32 reg_div;
0087
0088 if (divider_is_fixed(div))
0089 return (u64)div->u.fixed;
0090
0091 reg_div = ((u32)1 << div->u.s.width) - 1;
0092
0093 return scaled_div_value(div, reg_div);
0094 }
0095
0096
0097
0098
0099
0100 static inline u32
0101 divider(struct bcm_clk_div *div, u64 scaled_div)
0102 {
0103 BUG_ON(scaled_div < scaled_div_min(div));
0104 BUG_ON(scaled_div > scaled_div_max(div));
0105
0106 return (u32)(scaled_div - ((u64)1 << div->u.s.frac_width));
0107 }
0108
0109
0110 static inline u64
0111 scale_rate(struct bcm_clk_div *div, u32 rate)
0112 {
0113 if (divider_is_fixed(div))
0114 return (u64)rate;
0115
0116 return (u64)rate << div->u.s.frac_width;
0117 }
0118
0119
0120
0121
0122 static inline u32 __ccu_read(struct ccu_data *ccu, u32 reg_offset)
0123 {
0124 return readl(ccu->base + reg_offset);
0125 }
0126
0127
0128 static inline void
0129 __ccu_write(struct ccu_data *ccu, u32 reg_offset, u32 reg_val)
0130 {
0131 writel(reg_val, ccu->base + reg_offset);
0132 }
0133
0134 static inline unsigned long ccu_lock(struct ccu_data *ccu)
0135 {
0136 unsigned long flags;
0137
0138 spin_lock_irqsave(&ccu->lock, flags);
0139
0140 return flags;
0141 }
0142 static inline void ccu_unlock(struct ccu_data *ccu, unsigned long flags)
0143 {
0144 spin_unlock_irqrestore(&ccu->lock, flags);
0145 }
0146
0147
0148
0149
0150
0151 static inline void __ccu_write_enable(struct ccu_data *ccu)
0152 {
0153 if (ccu->write_enabled) {
0154 pr_err("%s: access already enabled for %s\n", __func__,
0155 ccu->name);
0156 return;
0157 }
0158 ccu->write_enabled = true;
0159 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD | 1);
0160 }
0161
0162 static inline void __ccu_write_disable(struct ccu_data *ccu)
0163 {
0164 if (!ccu->write_enabled) {
0165 pr_err("%s: access wasn't enabled for %s\n", __func__,
0166 ccu->name);
0167 return;
0168 }
0169
0170 __ccu_write(ccu, 0, CCU_ACCESS_PASSWORD);
0171 ccu->write_enabled = false;
0172 }
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 static inline bool
0183 __ccu_wait_bit(struct ccu_data *ccu, u32 reg_offset, u32 bit, bool want)
0184 {
0185 unsigned int tries;
0186 u32 bit_mask = 1 << bit;
0187
0188 for (tries = 0; tries < CLK_GATE_DELAY_LOOP; tries++) {
0189 u32 val;
0190 bool bit_val;
0191
0192 val = __ccu_read(ccu, reg_offset);
0193 bit_val = (val & bit_mask) != 0;
0194 if (bit_val == want)
0195 return true;
0196 udelay(1);
0197 }
0198 pr_warn("%s: %s/0x%04x bit %u was never %s\n", __func__,
0199 ccu->name, reg_offset, bit, want ? "set" : "clear");
0200
0201 return false;
0202 }
0203
0204
0205
0206 static bool __ccu_policy_engine_start(struct ccu_data *ccu, bool sync)
0207 {
0208 struct bcm_policy_ctl *control = &ccu->policy.control;
0209 u32 offset;
0210 u32 go_bit;
0211 u32 mask;
0212 bool ret;
0213
0214
0215 if (!policy_ctl_exists(control))
0216 return true;
0217
0218 offset = control->offset;
0219 go_bit = control->go_bit;
0220
0221
0222 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
0223 if (!ret) {
0224 pr_err("%s: ccu %s policy engine wouldn't go idle\n",
0225 __func__, ccu->name);
0226 return false;
0227 }
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244 mask = (u32)1 << go_bit;
0245 if (sync)
0246 mask |= 1 << control->atl_bit;
0247 else
0248 mask |= 1 << control->ac_bit;
0249 __ccu_write(ccu, offset, mask);
0250
0251
0252 ret = __ccu_wait_bit(ccu, offset, go_bit, false);
0253 if (!ret)
0254 pr_err("%s: ccu %s policy engine never started\n",
0255 __func__, ccu->name);
0256
0257 return ret;
0258 }
0259
0260 static bool __ccu_policy_engine_stop(struct ccu_data *ccu)
0261 {
0262 struct bcm_lvm_en *enable = &ccu->policy.enable;
0263 u32 offset;
0264 u32 enable_bit;
0265 bool ret;
0266
0267
0268 if (!policy_lvm_en_exists(enable))
0269 return true;
0270
0271
0272 offset = enable->offset;
0273 enable_bit = enable->bit;
0274 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
0275 if (!ret) {
0276 pr_err("%s: ccu %s policy engine already stopped\n",
0277 __func__, ccu->name);
0278 return false;
0279 }
0280
0281
0282 __ccu_write(ccu, offset, (u32)1 << enable_bit);
0283
0284
0285 ret = __ccu_wait_bit(ccu, offset, enable_bit, false);
0286 if (!ret)
0287 pr_err("%s: ccu %s policy engine never stopped\n",
0288 __func__, ccu->name);
0289
0290 return ret;
0291 }
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302 static bool policy_init(struct ccu_data *ccu, struct bcm_clk_policy *policy)
0303 {
0304 u32 offset;
0305 u32 mask;
0306 int i;
0307 bool ret;
0308
0309 if (!policy_exists(policy))
0310 return true;
0311
0312
0313
0314
0315
0316 if (!__ccu_policy_engine_stop(ccu)) {
0317 pr_err("%s: unable to stop CCU %s policy engine\n",
0318 __func__, ccu->name);
0319 return false;
0320 }
0321
0322
0323
0324
0325
0326 offset = policy->offset;
0327 mask = (u32)1 << policy->bit;
0328 for (i = 0; i < CCU_POLICY_COUNT; i++) {
0329 u32 reg_val;
0330
0331 reg_val = __ccu_read(ccu, offset);
0332 reg_val |= mask;
0333 __ccu_write(ccu, offset, reg_val);
0334 offset += sizeof(u32);
0335 }
0336
0337
0338 ret = __ccu_policy_engine_start(ccu, true);
0339 if (!ret)
0340 pr_err("%s: unable to restart CCU %s policy engine\n",
0341 __func__, ccu->name);
0342
0343 return ret;
0344 }
0345
0346
0347
0348
0349 static bool
0350 __is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
0351 {
0352 u32 bit_mask;
0353 u32 reg_val;
0354
0355
0356 if (!gate_exists(gate))
0357 return true;
0358
0359 bit_mask = 1 << gate->status_bit;
0360 reg_val = __ccu_read(ccu, gate->offset);
0361
0362 return (reg_val & bit_mask) != 0;
0363 }
0364
0365
0366 static bool
0367 is_clk_gate_enabled(struct ccu_data *ccu, struct bcm_clk_gate *gate)
0368 {
0369 long flags;
0370 bool ret;
0371
0372
0373 if (!gate_exists(gate))
0374 return true;
0375
0376 flags = ccu_lock(ccu);
0377 ret = __is_clk_gate_enabled(ccu, gate);
0378 ccu_unlock(ccu, flags);
0379
0380 return ret;
0381 }
0382
0383
0384
0385
0386
0387 static bool
0388 __gate_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate)
0389 {
0390 u32 reg_val;
0391 u32 mask;
0392 bool enabled = false;
0393
0394 BUG_ON(!gate_exists(gate));
0395 if (!gate_is_sw_controllable(gate))
0396 return true;
0397
0398 reg_val = __ccu_read(ccu, gate->offset);
0399
0400
0401 if (gate_is_hw_controllable(gate)) {
0402 mask = (u32)1 << gate->hw_sw_sel_bit;
0403 if (gate_is_sw_managed(gate))
0404 reg_val |= mask;
0405 else
0406 reg_val &= ~mask;
0407 }
0408
0409
0410
0411
0412
0413
0414
0415
0416 mask = (u32)1 << gate->en_bit;
0417 if (gate_is_sw_managed(gate) && (enabled = gate_is_enabled(gate)) &&
0418 !gate_is_no_disable(gate))
0419 reg_val |= mask;
0420 else
0421 reg_val &= ~mask;
0422
0423 __ccu_write(ccu, gate->offset, reg_val);
0424
0425
0426 if (!gate_is_sw_managed(gate))
0427 return true;
0428
0429
0430 return __ccu_wait_bit(ccu, gate->offset, gate->status_bit, enabled);
0431 }
0432
0433
0434
0435
0436
0437
0438
0439 static bool gate_init(struct ccu_data *ccu, struct bcm_clk_gate *gate)
0440 {
0441 if (!gate_exists(gate))
0442 return true;
0443 return __gate_commit(ccu, gate);
0444 }
0445
0446
0447
0448
0449
0450
0451
0452 static bool
0453 __clk_gate(struct ccu_data *ccu, struct bcm_clk_gate *gate, bool enable)
0454 {
0455 bool ret;
0456
0457 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
0458 return true;
0459
0460 if (!enable && gate_is_no_disable(gate)) {
0461 pr_warn("%s: invalid gate disable request (ignoring)\n",
0462 __func__);
0463 return true;
0464 }
0465
0466 if (enable == gate_is_enabled(gate))
0467 return true;
0468
0469 gate_flip_enabled(gate);
0470 ret = __gate_commit(ccu, gate);
0471 if (!ret)
0472 gate_flip_enabled(gate);
0473
0474 return ret;
0475 }
0476
0477
0478 static int clk_gate(struct ccu_data *ccu, const char *name,
0479 struct bcm_clk_gate *gate, bool enable)
0480 {
0481 unsigned long flags;
0482 bool success;
0483
0484
0485
0486
0487
0488 if (!gate_exists(gate) || !gate_is_sw_managed(gate))
0489 return 0;
0490 if (!enable && gate_is_no_disable(gate))
0491 return 0;
0492
0493 flags = ccu_lock(ccu);
0494 __ccu_write_enable(ccu);
0495
0496 success = __clk_gate(ccu, gate, enable);
0497
0498 __ccu_write_disable(ccu);
0499 ccu_unlock(ccu, flags);
0500
0501 if (success)
0502 return 0;
0503
0504 pr_err("%s: failed to %s gate for %s\n", __func__,
0505 enable ? "enable" : "disable", name);
0506
0507 return -EIO;
0508 }
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519 static bool hyst_init(struct ccu_data *ccu, struct bcm_clk_hyst *hyst)
0520 {
0521 u32 offset;
0522 u32 reg_val;
0523 u32 mask;
0524
0525 if (!hyst_exists(hyst))
0526 return true;
0527
0528 offset = hyst->offset;
0529 mask = (u32)1 << hyst->en_bit;
0530 mask |= (u32)1 << hyst->val_bit;
0531
0532 reg_val = __ccu_read(ccu, offset);
0533 reg_val |= mask;
0534 __ccu_write(ccu, offset, reg_val);
0535
0536 return true;
0537 }
0538
0539
0540
0541
0542
0543
0544
0545 static bool __clk_trigger(struct ccu_data *ccu, struct bcm_clk_trig *trig)
0546 {
0547
0548 __ccu_write(ccu, trig->offset, 1 << trig->bit);
0549
0550 return __ccu_wait_bit(ccu, trig->offset, trig->bit, false);
0551 }
0552
0553
0554
0555
0556 static u64 divider_read_scaled(struct ccu_data *ccu, struct bcm_clk_div *div)
0557 {
0558 unsigned long flags;
0559 u32 reg_val;
0560 u32 reg_div;
0561
0562 if (divider_is_fixed(div))
0563 return (u64)div->u.fixed;
0564
0565 flags = ccu_lock(ccu);
0566 reg_val = __ccu_read(ccu, div->u.s.offset);
0567 ccu_unlock(ccu, flags);
0568
0569
0570 reg_div = bitfield_extract(reg_val, div->u.s.shift, div->u.s.width);
0571
0572
0573 return scaled_div_value(div, reg_div);
0574 }
0575
0576
0577
0578
0579
0580
0581
0582
0583 static int __div_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
0584 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
0585 {
0586 bool enabled;
0587 u32 reg_div;
0588 u32 reg_val;
0589 int ret = 0;
0590
0591 BUG_ON(divider_is_fixed(div));
0592
0593
0594
0595
0596
0597
0598 if (div->u.s.scaled_div == BAD_SCALED_DIV_VALUE) {
0599 reg_val = __ccu_read(ccu, div->u.s.offset);
0600 reg_div = bitfield_extract(reg_val, div->u.s.shift,
0601 div->u.s.width);
0602 div->u.s.scaled_div = scaled_div_value(div, reg_div);
0603
0604 return 0;
0605 }
0606
0607
0608 reg_div = divider(div, div->u.s.scaled_div);
0609
0610
0611 enabled = __is_clk_gate_enabled(ccu, gate);
0612 if (!enabled && !__clk_gate(ccu, gate, true)) {
0613 ret = -ENXIO;
0614 goto out;
0615 }
0616
0617
0618 reg_val = __ccu_read(ccu, div->u.s.offset);
0619 reg_val = bitfield_replace(reg_val, div->u.s.shift, div->u.s.width,
0620 reg_div);
0621 __ccu_write(ccu, div->u.s.offset, reg_val);
0622
0623
0624 if (!__clk_trigger(ccu, trig))
0625 ret = -EIO;
0626
0627
0628 if (!enabled && !__clk_gate(ccu, gate, false))
0629 ret = ret ? ret : -ENXIO;
0630 out:
0631 return ret;
0632 }
0633
0634
0635
0636
0637
0638
0639 static bool div_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
0640 struct bcm_clk_div *div, struct bcm_clk_trig *trig)
0641 {
0642 if (!divider_exists(div) || divider_is_fixed(div))
0643 return true;
0644 return !__div_commit(ccu, gate, div, trig);
0645 }
0646
0647 static int divider_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
0648 struct bcm_clk_div *div, struct bcm_clk_trig *trig,
0649 u64 scaled_div)
0650 {
0651 unsigned long flags;
0652 u64 previous;
0653 int ret;
0654
0655 BUG_ON(divider_is_fixed(div));
0656
0657 previous = div->u.s.scaled_div;
0658 if (previous == scaled_div)
0659 return 0;
0660
0661 div->u.s.scaled_div = scaled_div;
0662
0663 flags = ccu_lock(ccu);
0664 __ccu_write_enable(ccu);
0665
0666 ret = __div_commit(ccu, gate, div, trig);
0667
0668 __ccu_write_disable(ccu);
0669 ccu_unlock(ccu, flags);
0670
0671 if (ret)
0672 div->u.s.scaled_div = previous;
0673
0674 return ret;
0675
0676 }
0677
0678
0679
0680
0681
0682
0683
0684
0685 static unsigned long clk_recalc_rate(struct ccu_data *ccu,
0686 struct bcm_clk_div *div, struct bcm_clk_div *pre_div,
0687 unsigned long parent_rate)
0688 {
0689 u64 scaled_parent_rate;
0690 u64 scaled_div;
0691 u64 result;
0692
0693 if (!divider_exists(div))
0694 return parent_rate;
0695
0696 if (parent_rate > (unsigned long)LONG_MAX)
0697 return 0;
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708 if (pre_div && divider_exists(pre_div)) {
0709 u64 scaled_rate;
0710
0711 scaled_rate = scale_rate(pre_div, parent_rate);
0712 scaled_rate = scale_rate(div, scaled_rate);
0713 scaled_div = divider_read_scaled(ccu, pre_div);
0714 scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
0715 scaled_div);
0716 } else {
0717 scaled_parent_rate = scale_rate(div, parent_rate);
0718 }
0719
0720
0721
0722
0723
0724
0725 scaled_div = divider_read_scaled(ccu, div);
0726 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, scaled_div);
0727
0728 return (unsigned long)result;
0729 }
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740 static long round_rate(struct ccu_data *ccu, struct bcm_clk_div *div,
0741 struct bcm_clk_div *pre_div,
0742 unsigned long rate, unsigned long parent_rate,
0743 u64 *scaled_div)
0744 {
0745 u64 scaled_parent_rate;
0746 u64 min_scaled_div;
0747 u64 max_scaled_div;
0748 u64 best_scaled_div;
0749 u64 result;
0750
0751 BUG_ON(!divider_exists(div));
0752 BUG_ON(!rate);
0753 BUG_ON(parent_rate > (u64)LONG_MAX);
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766 if (divider_exists(pre_div)) {
0767 u64 scaled_rate;
0768 u64 scaled_pre_div;
0769
0770 scaled_rate = scale_rate(pre_div, parent_rate);
0771 scaled_rate = scale_rate(div, scaled_rate);
0772 scaled_pre_div = divider_read_scaled(ccu, pre_div);
0773 scaled_parent_rate = DIV_ROUND_CLOSEST_ULL(scaled_rate,
0774 scaled_pre_div);
0775 } else {
0776 scaled_parent_rate = scale_rate(div, parent_rate);
0777 }
0778
0779
0780
0781
0782
0783
0784 if (!divider_is_fixed(div)) {
0785 best_scaled_div = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate,
0786 rate);
0787 min_scaled_div = scaled_div_min(div);
0788 max_scaled_div = scaled_div_max(div);
0789 if (best_scaled_div > max_scaled_div)
0790 best_scaled_div = max_scaled_div;
0791 else if (best_scaled_div < min_scaled_div)
0792 best_scaled_div = min_scaled_div;
0793 } else {
0794 best_scaled_div = divider_read_scaled(ccu, div);
0795 }
0796
0797
0798 result = DIV_ROUND_CLOSEST_ULL(scaled_parent_rate, best_scaled_div);
0799
0800 if (scaled_div)
0801 *scaled_div = best_scaled_div;
0802
0803 return (long)result;
0804 }
0805
0806
0807
0808
0809
0810
0811
0812
0813 static u8 parent_index(struct bcm_clk_sel *sel, u8 parent_sel)
0814 {
0815 u8 i;
0816
0817 BUG_ON(sel->parent_count > (u32)U8_MAX);
0818 for (i = 0; i < sel->parent_count; i++)
0819 if (sel->parent_sel[i] == parent_sel)
0820 return i;
0821 return BAD_CLK_INDEX;
0822 }
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832 static u8 selector_read_index(struct ccu_data *ccu, struct bcm_clk_sel *sel)
0833 {
0834 unsigned long flags;
0835 u32 reg_val;
0836 u32 parent_sel;
0837 u8 index;
0838
0839
0840 if (!selector_exists(sel))
0841 return 0;
0842
0843
0844 flags = ccu_lock(ccu);
0845 reg_val = __ccu_read(ccu, sel->offset);
0846 ccu_unlock(ccu, flags);
0847
0848 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
0849
0850
0851 index = parent_index(sel, parent_sel);
0852 if (index == BAD_CLK_INDEX)
0853 pr_err("%s: out-of-range parent selector %u (%s 0x%04x)\n",
0854 __func__, parent_sel, ccu->name, sel->offset);
0855
0856 return index;
0857 }
0858
0859
0860
0861
0862
0863
0864
0865 static int
0866 __sel_commit(struct ccu_data *ccu, struct bcm_clk_gate *gate,
0867 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
0868 {
0869 u32 parent_sel;
0870 u32 reg_val;
0871 bool enabled;
0872 int ret = 0;
0873
0874 BUG_ON(!selector_exists(sel));
0875
0876
0877
0878
0879
0880
0881 if (sel->clk_index == BAD_CLK_INDEX) {
0882 u8 index;
0883
0884 reg_val = __ccu_read(ccu, sel->offset);
0885 parent_sel = bitfield_extract(reg_val, sel->shift, sel->width);
0886 index = parent_index(sel, parent_sel);
0887 if (index == BAD_CLK_INDEX)
0888 return -EINVAL;
0889 sel->clk_index = index;
0890
0891 return 0;
0892 }
0893
0894 BUG_ON((u32)sel->clk_index >= sel->parent_count);
0895 parent_sel = sel->parent_sel[sel->clk_index];
0896
0897
0898 enabled = __is_clk_gate_enabled(ccu, gate);
0899 if (!enabled && !__clk_gate(ccu, gate, true))
0900 return -ENXIO;
0901
0902
0903 reg_val = __ccu_read(ccu, sel->offset);
0904 reg_val = bitfield_replace(reg_val, sel->shift, sel->width, parent_sel);
0905 __ccu_write(ccu, sel->offset, reg_val);
0906
0907
0908 if (!__clk_trigger(ccu, trig))
0909 ret = -EIO;
0910
0911
0912 if (!enabled && !__clk_gate(ccu, gate, false))
0913 ret = ret ? ret : -ENXIO;
0914
0915 return ret;
0916 }
0917
0918
0919
0920
0921
0922
0923 static bool sel_init(struct ccu_data *ccu, struct bcm_clk_gate *gate,
0924 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig)
0925 {
0926 if (!selector_exists(sel))
0927 return true;
0928 return !__sel_commit(ccu, gate, sel, trig);
0929 }
0930
0931
0932
0933
0934
0935
0936 static int selector_write(struct ccu_data *ccu, struct bcm_clk_gate *gate,
0937 struct bcm_clk_sel *sel, struct bcm_clk_trig *trig,
0938 u8 index)
0939 {
0940 unsigned long flags;
0941 u8 previous;
0942 int ret;
0943
0944 previous = sel->clk_index;
0945 if (previous == index)
0946 return 0;
0947
0948 sel->clk_index = index;
0949
0950 flags = ccu_lock(ccu);
0951 __ccu_write_enable(ccu);
0952
0953 ret = __sel_commit(ccu, gate, sel, trig);
0954
0955 __ccu_write_disable(ccu);
0956 ccu_unlock(ccu, flags);
0957
0958 if (ret)
0959 sel->clk_index = previous;
0960
0961 return ret;
0962 }
0963
0964
0965
0966 static int kona_peri_clk_enable(struct clk_hw *hw)
0967 {
0968 struct kona_clk *bcm_clk = to_kona_clk(hw);
0969 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
0970
0971 return clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, true);
0972 }
0973
0974 static void kona_peri_clk_disable(struct clk_hw *hw)
0975 {
0976 struct kona_clk *bcm_clk = to_kona_clk(hw);
0977 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
0978
0979 (void)clk_gate(bcm_clk->ccu, bcm_clk->init_data.name, gate, false);
0980 }
0981
0982 static int kona_peri_clk_is_enabled(struct clk_hw *hw)
0983 {
0984 struct kona_clk *bcm_clk = to_kona_clk(hw);
0985 struct bcm_clk_gate *gate = &bcm_clk->u.peri->gate;
0986
0987 return is_clk_gate_enabled(bcm_clk->ccu, gate) ? 1 : 0;
0988 }
0989
0990 static unsigned long kona_peri_clk_recalc_rate(struct clk_hw *hw,
0991 unsigned long parent_rate)
0992 {
0993 struct kona_clk *bcm_clk = to_kona_clk(hw);
0994 struct peri_clk_data *data = bcm_clk->u.peri;
0995
0996 return clk_recalc_rate(bcm_clk->ccu, &data->div, &data->pre_div,
0997 parent_rate);
0998 }
0999
1000 static long kona_peri_clk_round_rate(struct clk_hw *hw, unsigned long rate,
1001 unsigned long *parent_rate)
1002 {
1003 struct kona_clk *bcm_clk = to_kona_clk(hw);
1004 struct bcm_clk_div *div = &bcm_clk->u.peri->div;
1005
1006 if (!divider_exists(div))
1007 return clk_hw_get_rate(hw);
1008
1009
1010 return round_rate(bcm_clk->ccu, div, &bcm_clk->u.peri->pre_div,
1011 rate ? rate : 1, *parent_rate, NULL);
1012 }
1013
1014 static int kona_peri_clk_determine_rate(struct clk_hw *hw,
1015 struct clk_rate_request *req)
1016 {
1017 struct kona_clk *bcm_clk = to_kona_clk(hw);
1018 struct clk_hw *current_parent;
1019 unsigned long parent_rate;
1020 unsigned long best_delta;
1021 unsigned long best_rate;
1022 u32 parent_count;
1023 long rate;
1024 u32 which;
1025
1026
1027
1028
1029
1030 WARN_ON_ONCE(bcm_clk->init_data.flags & CLK_SET_RATE_NO_REPARENT);
1031 parent_count = (u32)bcm_clk->init_data.num_parents;
1032 if (parent_count < 2) {
1033 rate = kona_peri_clk_round_rate(hw, req->rate,
1034 &req->best_parent_rate);
1035 if (rate < 0)
1036 return rate;
1037
1038 req->rate = rate;
1039 return 0;
1040 }
1041
1042
1043 current_parent = clk_hw_get_parent(hw);
1044 parent_rate = clk_hw_get_rate(current_parent);
1045 best_rate = kona_peri_clk_round_rate(hw, req->rate, &parent_rate);
1046 best_delta = abs(best_rate - req->rate);
1047
1048
1049 for (which = 0; which < parent_count; which++) {
1050 struct clk_hw *parent = clk_hw_get_parent_by_index(hw, which);
1051 unsigned long delta;
1052 unsigned long other_rate;
1053
1054 BUG_ON(!parent);
1055 if (parent == current_parent)
1056 continue;
1057
1058
1059 parent_rate = clk_hw_get_rate(parent);
1060 other_rate = kona_peri_clk_round_rate(hw, req->rate,
1061 &parent_rate);
1062 delta = abs(other_rate - req->rate);
1063 if (delta < best_delta) {
1064 best_delta = delta;
1065 best_rate = other_rate;
1066 req->best_parent_hw = parent;
1067 req->best_parent_rate = parent_rate;
1068 }
1069 }
1070
1071 req->rate = best_rate;
1072 return 0;
1073 }
1074
1075 static int kona_peri_clk_set_parent(struct clk_hw *hw, u8 index)
1076 {
1077 struct kona_clk *bcm_clk = to_kona_clk(hw);
1078 struct peri_clk_data *data = bcm_clk->u.peri;
1079 struct bcm_clk_sel *sel = &data->sel;
1080 struct bcm_clk_trig *trig;
1081 int ret;
1082
1083 BUG_ON(index >= sel->parent_count);
1084
1085
1086 if (!selector_exists(sel))
1087 return 0;
1088
1089
1090
1091
1092
1093 trig = trigger_exists(&data->pre_trig) ? &data->pre_trig
1094 : &data->trig;
1095
1096 ret = selector_write(bcm_clk->ccu, &data->gate, sel, trig, index);
1097 if (ret == -ENXIO) {
1098 pr_err("%s: gating failure for %s\n", __func__,
1099 bcm_clk->init_data.name);
1100 ret = -EIO;
1101 } else if (ret == -EIO) {
1102 pr_err("%s: %strigger failed for %s\n", __func__,
1103 trig == &data->pre_trig ? "pre-" : "",
1104 bcm_clk->init_data.name);
1105 }
1106
1107 return ret;
1108 }
1109
1110 static u8 kona_peri_clk_get_parent(struct clk_hw *hw)
1111 {
1112 struct kona_clk *bcm_clk = to_kona_clk(hw);
1113 struct peri_clk_data *data = bcm_clk->u.peri;
1114 u8 index;
1115
1116 index = selector_read_index(bcm_clk->ccu, &data->sel);
1117
1118
1119 return index == BAD_CLK_INDEX ? 0 : index;
1120 }
1121
1122 static int kona_peri_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1123 unsigned long parent_rate)
1124 {
1125 struct kona_clk *bcm_clk = to_kona_clk(hw);
1126 struct peri_clk_data *data = bcm_clk->u.peri;
1127 struct bcm_clk_div *div = &data->div;
1128 u64 scaled_div = 0;
1129 int ret;
1130
1131 if (parent_rate > (unsigned long)LONG_MAX)
1132 return -EINVAL;
1133
1134 if (rate == clk_hw_get_rate(hw))
1135 return 0;
1136
1137 if (!divider_exists(div))
1138 return rate == parent_rate ? 0 : -EINVAL;
1139
1140
1141
1142
1143
1144
1145 if (divider_is_fixed(&data->div))
1146 return rate == parent_rate ? 0 : -EINVAL;
1147
1148
1149
1150
1151
1152
1153 (void)round_rate(bcm_clk->ccu, div, &data->pre_div,
1154 rate ? rate : 1, parent_rate, &scaled_div);
1155
1156
1157
1158
1159
1160 ret = divider_write(bcm_clk->ccu, &data->gate, &data->div,
1161 &data->trig, scaled_div);
1162 if (ret == -ENXIO) {
1163 pr_err("%s: gating failure for %s\n", __func__,
1164 bcm_clk->init_data.name);
1165 ret = -EIO;
1166 } else if (ret == -EIO) {
1167 pr_err("%s: trigger failed for %s\n", __func__,
1168 bcm_clk->init_data.name);
1169 }
1170
1171 return ret;
1172 }
1173
1174 struct clk_ops kona_peri_clk_ops = {
1175 .enable = kona_peri_clk_enable,
1176 .disable = kona_peri_clk_disable,
1177 .is_enabled = kona_peri_clk_is_enabled,
1178 .recalc_rate = kona_peri_clk_recalc_rate,
1179 .determine_rate = kona_peri_clk_determine_rate,
1180 .set_parent = kona_peri_clk_set_parent,
1181 .get_parent = kona_peri_clk_get_parent,
1182 .set_rate = kona_peri_clk_set_rate,
1183 };
1184
1185
1186 static bool __peri_clk_init(struct kona_clk *bcm_clk)
1187 {
1188 struct ccu_data *ccu = bcm_clk->ccu;
1189 struct peri_clk_data *peri = bcm_clk->u.peri;
1190 const char *name = bcm_clk->init_data.name;
1191 struct bcm_clk_trig *trig;
1192
1193 BUG_ON(bcm_clk->type != bcm_clk_peri);
1194
1195 if (!policy_init(ccu, &peri->policy)) {
1196 pr_err("%s: error initializing policy for %s\n",
1197 __func__, name);
1198 return false;
1199 }
1200 if (!gate_init(ccu, &peri->gate)) {
1201 pr_err("%s: error initializing gate for %s\n", __func__, name);
1202 return false;
1203 }
1204 if (!hyst_init(ccu, &peri->hyst)) {
1205 pr_err("%s: error initializing hyst for %s\n", __func__, name);
1206 return false;
1207 }
1208 if (!div_init(ccu, &peri->gate, &peri->div, &peri->trig)) {
1209 pr_err("%s: error initializing divider for %s\n", __func__,
1210 name);
1211 return false;
1212 }
1213
1214
1215
1216
1217
1218 trig = trigger_exists(&peri->pre_trig) ? &peri->pre_trig
1219 : &peri->trig;
1220
1221 if (!div_init(ccu, &peri->gate, &peri->pre_div, trig)) {
1222 pr_err("%s: error initializing pre-divider for %s\n", __func__,
1223 name);
1224 return false;
1225 }
1226
1227 if (!sel_init(ccu, &peri->gate, &peri->sel, trig)) {
1228 pr_err("%s: error initializing selector for %s\n", __func__,
1229 name);
1230 return false;
1231 }
1232
1233 return true;
1234 }
1235
1236 static bool __kona_clk_init(struct kona_clk *bcm_clk)
1237 {
1238 switch (bcm_clk->type) {
1239 case bcm_clk_peri:
1240 return __peri_clk_init(bcm_clk);
1241 default:
1242 BUG();
1243 }
1244 return false;
1245 }
1246
1247
1248 bool __init kona_ccu_init(struct ccu_data *ccu)
1249 {
1250 unsigned long flags;
1251 unsigned int which;
1252 struct kona_clk *kona_clks = ccu->kona_clks;
1253 bool success = true;
1254
1255 flags = ccu_lock(ccu);
1256 __ccu_write_enable(ccu);
1257
1258 for (which = 0; which < ccu->clk_num; which++) {
1259 struct kona_clk *bcm_clk = &kona_clks[which];
1260
1261 if (!bcm_clk->ccu)
1262 continue;
1263
1264 success &= __kona_clk_init(bcm_clk);
1265 }
1266
1267 __ccu_write_disable(ccu);
1268 ccu_unlock(ccu, flags);
1269 return success;
1270 }