clk-krait.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2018, The Linux Foundation. All rights reserved.
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/io.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/spinlock.h>
  11. #include <asm/krait-l2-accessors.h>
  12. #include "clk-krait.h"
  13. /* Secondary and primary muxes share the same cp15 register */
  14. static DEFINE_SPINLOCK(krait_clock_reg_lock);
  15. #define LPL_SHIFT 8
  16. #define SECCLKAGD BIT(4)
  17. static void __krait_mux_set_sel(struct krait_mux_clk *mux, int sel)
  18. {
  19. unsigned long flags;
  20. u32 regval;
  21. spin_lock_irqsave(&krait_clock_reg_lock, flags);
  22. regval = krait_get_l2_indirect_reg(mux->offset);
  23. /* apq/ipq8064 Errata: disable sec_src clock gating during switch. */
  24. if (mux->disable_sec_src_gating) {
  25. regval |= SECCLKAGD;
  26. krait_set_l2_indirect_reg(mux->offset, regval);
  27. }
  28. regval &= ~(mux->mask << mux->shift);
  29. regval |= (sel & mux->mask) << mux->shift;
  30. if (mux->lpl) {
  31. regval &= ~(mux->mask << (mux->shift + LPL_SHIFT));
  32. regval |= (sel & mux->mask) << (mux->shift + LPL_SHIFT);
  33. }
  34. krait_set_l2_indirect_reg(mux->offset, regval);
  35. /* apq/ipq8064 Errata: re-enabled sec_src clock gating. */
  36. if (mux->disable_sec_src_gating) {
  37. regval &= ~SECCLKAGD;
  38. krait_set_l2_indirect_reg(mux->offset, regval);
  39. }
  40. /* Wait for switch to complete. */
  41. mb();
  42. udelay(1);
  43. /*
  44. * Unlock now to make sure the mux register is not
  45. * modified while switching to the new parent.
  46. */
  47. spin_unlock_irqrestore(&krait_clock_reg_lock, flags);
  48. }
  49. static int krait_mux_set_parent(struct clk_hw *hw, u8 index)
  50. {
  51. struct krait_mux_clk *mux = to_krait_mux_clk(hw);
  52. u32 sel;
  53. sel = clk_mux_index_to_val(mux->parent_map, 0, index);
  54. mux->en_mask = sel;
  55. /* Don't touch mux if CPU is off as it won't work */
  56. if (__clk_is_enabled(hw->clk))
  57. __krait_mux_set_sel(mux, sel);
  58. mux->reparent = true;
  59. return 0;
  60. }
  61. static u8 krait_mux_get_parent(struct clk_hw *hw)
  62. {
  63. struct krait_mux_clk *mux = to_krait_mux_clk(hw);
  64. u32 sel;
  65. sel = krait_get_l2_indirect_reg(mux->offset);
  66. sel >>= mux->shift;
  67. sel &= mux->mask;
  68. mux->en_mask = sel;
  69. return clk_mux_val_to_index(hw, mux->parent_map, 0, sel);
  70. }
  71. const struct clk_ops krait_mux_clk_ops = {
  72. .set_parent = krait_mux_set_parent,
  73. .get_parent = krait_mux_get_parent,
  74. .determine_rate = __clk_mux_determine_rate_closest,
  75. };
  76. EXPORT_SYMBOL_GPL(krait_mux_clk_ops);
  77. /* The divider can divide by 2, 4, 6 and 8. But we only really need div-2. */
  78. static int krait_div2_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  79. {
  80. req->best_parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), req->rate * 2);
  81. req->rate = DIV_ROUND_UP(req->best_parent_rate, 2);
  82. return 0;
  83. }
  84. static int krait_div2_set_rate(struct clk_hw *hw, unsigned long rate,
  85. unsigned long parent_rate)
  86. {
  87. struct krait_div2_clk *d = to_krait_div2_clk(hw);
  88. unsigned long flags;
  89. u32 val;
  90. u32 mask = BIT(d->width) - 1;
  91. if (d->lpl)
  92. mask = mask << (d->shift + LPL_SHIFT) | mask << d->shift;
  93. else
  94. mask <<= d->shift;
  95. spin_lock_irqsave(&krait_clock_reg_lock, flags);
  96. val = krait_get_l2_indirect_reg(d->offset);
  97. val &= ~mask;
  98. krait_set_l2_indirect_reg(d->offset, val);
  99. spin_unlock_irqrestore(&krait_clock_reg_lock, flags);
  100. return 0;
  101. }
  102. static unsigned long
  103. krait_div2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  104. {
  105. struct krait_div2_clk *d = to_krait_div2_clk(hw);
  106. u32 mask = BIT(d->width) - 1;
  107. u32 div;
  108. div = krait_get_l2_indirect_reg(d->offset);
  109. div >>= d->shift;
  110. div &= mask;
  111. div = (div + 1) * 2;
  112. return DIV_ROUND_UP(parent_rate, div);
  113. }
  114. const struct clk_ops krait_div2_clk_ops = {
  115. .determine_rate = krait_div2_determine_rate,
  116. .set_rate = krait_div2_set_rate,
  117. .recalc_rate = krait_div2_recalc_rate,
  118. };
  119. EXPORT_SYMBOL_GPL(krait_div2_clk_ops);