clk-krait.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. static void __krait_mux_set_sel(struct krait_mux_clk *mux, int sel)
  17. {
  18. unsigned long flags;
  19. u32 regval;
  20. spin_lock_irqsave(&krait_clock_reg_lock, flags);
  21. regval = krait_get_l2_indirect_reg(mux->offset);
  22. regval &= ~(mux->mask << mux->shift);
  23. regval |= (sel & mux->mask) << mux->shift;
  24. if (mux->lpl) {
  25. regval &= ~(mux->mask << (mux->shift + LPL_SHIFT));
  26. regval |= (sel & mux->mask) << (mux->shift + LPL_SHIFT);
  27. }
  28. krait_set_l2_indirect_reg(mux->offset, regval);
  29. spin_unlock_irqrestore(&krait_clock_reg_lock, flags);
  30. /* Wait for switch to complete. */
  31. mb();
  32. udelay(1);
  33. }
  34. static int krait_mux_set_parent(struct clk_hw *hw, u8 index)
  35. {
  36. struct krait_mux_clk *mux = to_krait_mux_clk(hw);
  37. u32 sel;
  38. sel = clk_mux_index_to_val(mux->parent_map, 0, index);
  39. mux->en_mask = sel;
  40. /* Don't touch mux if CPU is off as it won't work */
  41. if (__clk_is_enabled(hw->clk))
  42. __krait_mux_set_sel(mux, sel);
  43. mux->reparent = true;
  44. return 0;
  45. }
  46. static u8 krait_mux_get_parent(struct clk_hw *hw)
  47. {
  48. struct krait_mux_clk *mux = to_krait_mux_clk(hw);
  49. u32 sel;
  50. sel = krait_get_l2_indirect_reg(mux->offset);
  51. sel >>= mux->shift;
  52. sel &= mux->mask;
  53. mux->en_mask = sel;
  54. return clk_mux_val_to_index(hw, mux->parent_map, 0, sel);
  55. }
  56. const struct clk_ops krait_mux_clk_ops = {
  57. .set_parent = krait_mux_set_parent,
  58. .get_parent = krait_mux_get_parent,
  59. .determine_rate = __clk_mux_determine_rate_closest,
  60. };
  61. EXPORT_SYMBOL_GPL(krait_mux_clk_ops);
  62. /* The divider can divide by 2, 4, 6 and 8. But we only really need div-2. */
  63. static long krait_div2_round_rate(struct clk_hw *hw, unsigned long rate,
  64. unsigned long *parent_rate)
  65. {
  66. *parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), rate * 2);
  67. return DIV_ROUND_UP(*parent_rate, 2);
  68. }
  69. static int krait_div2_set_rate(struct clk_hw *hw, unsigned long rate,
  70. unsigned long parent_rate)
  71. {
  72. struct krait_div2_clk *d = to_krait_div2_clk(hw);
  73. unsigned long flags;
  74. u32 val;
  75. u32 mask = BIT(d->width) - 1;
  76. if (d->lpl)
  77. mask = mask << (d->shift + LPL_SHIFT) | mask << d->shift;
  78. spin_lock_irqsave(&krait_clock_reg_lock, flags);
  79. val = krait_get_l2_indirect_reg(d->offset);
  80. val &= ~mask;
  81. krait_set_l2_indirect_reg(d->offset, val);
  82. spin_unlock_irqrestore(&krait_clock_reg_lock, flags);
  83. return 0;
  84. }
  85. static unsigned long
  86. krait_div2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  87. {
  88. struct krait_div2_clk *d = to_krait_div2_clk(hw);
  89. u32 mask = BIT(d->width) - 1;
  90. u32 div;
  91. div = krait_get_l2_indirect_reg(d->offset);
  92. div >>= d->shift;
  93. div &= mask;
  94. div = (div + 1) * 2;
  95. return DIV_ROUND_UP(parent_rate, div);
  96. }
  97. const struct clk_ops krait_div2_clk_ops = {
  98. .round_rate = krait_div2_round_rate,
  99. .set_rate = krait_div2_set_rate,
  100. .recalc_rate = krait_div2_recalc_rate,
  101. };
  102. EXPORT_SYMBOL_GPL(krait_div2_clk_ops);