clk-branch.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/bitops.h>
  8. #include <linux/err.h>
  9. #include <linux/delay.h>
  10. #include <linux/export.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/regmap.h>
  13. #include "clk-branch.h"
  14. static bool clk_branch_in_hwcg_mode(const struct clk_branch *br)
  15. {
  16. u32 val;
  17. if (!br->hwcg_reg)
  18. return false;
  19. regmap_read(br->clkr.regmap, br->hwcg_reg, &val);
  20. return !!(val & BIT(br->hwcg_bit));
  21. }
  22. static bool clk_branch_check_halt(const struct clk_branch *br, bool enabling)
  23. {
  24. bool invert = (br->halt_check == BRANCH_HALT_ENABLE);
  25. u32 val;
  26. regmap_read(br->clkr.regmap, br->halt_reg, &val);
  27. val &= BIT(br->halt_bit);
  28. if (invert)
  29. val = !val;
  30. return !!val == !enabling;
  31. }
  32. static bool clk_branch2_check_halt(const struct clk_branch *br, bool enabling)
  33. {
  34. u32 val;
  35. u32 mask;
  36. bool invert = (br->halt_check == BRANCH_HALT_ENABLE);
  37. mask = CBCR_NOC_FSM_STATUS;
  38. mask |= CBCR_CLK_OFF;
  39. regmap_read(br->clkr.regmap, br->halt_reg, &val);
  40. if (enabling) {
  41. val &= mask;
  42. return (val & CBCR_CLK_OFF) == (invert ? CBCR_CLK_OFF : 0) ||
  43. FIELD_GET(CBCR_NOC_FSM_STATUS, val) == FSM_STATUS_ON;
  44. }
  45. return (val & CBCR_CLK_OFF) == (invert ? 0 : CBCR_CLK_OFF);
  46. }
  47. static int clk_branch_wait(const struct clk_branch *br, bool enabling,
  48. bool (check_halt)(const struct clk_branch *, bool))
  49. {
  50. bool voted = br->halt_check & BRANCH_VOTED;
  51. const char *name = clk_hw_get_name(&br->clkr.hw);
  52. /*
  53. * Skip checking halt bit if we're explicitly ignoring the bit or the
  54. * clock is in hardware gated mode
  55. */
  56. if (br->halt_check == BRANCH_HALT_SKIP || clk_branch_in_hwcg_mode(br))
  57. return 0;
  58. if (br->halt_check == BRANCH_HALT_DELAY || (!enabling && voted)) {
  59. udelay(10);
  60. } else if (br->halt_check == BRANCH_HALT_ENABLE ||
  61. br->halt_check == BRANCH_HALT ||
  62. (enabling && voted)) {
  63. int count = 200;
  64. while (count-- > 0) {
  65. if (check_halt(br, enabling))
  66. return 0;
  67. udelay(1);
  68. }
  69. WARN(1, "%s status stuck at 'o%s'", name,
  70. enabling ? "ff" : "n");
  71. return -EBUSY;
  72. }
  73. return 0;
  74. }
  75. static int clk_branch_toggle(struct clk_hw *hw, bool en,
  76. bool (check_halt)(const struct clk_branch *, bool))
  77. {
  78. struct clk_branch *br = to_clk_branch(hw);
  79. int ret;
  80. if (en) {
  81. ret = clk_enable_regmap(hw);
  82. if (ret)
  83. return ret;
  84. } else {
  85. clk_disable_regmap(hw);
  86. }
  87. return clk_branch_wait(br, en, check_halt);
  88. }
  89. static int clk_branch_enable(struct clk_hw *hw)
  90. {
  91. return clk_branch_toggle(hw, true, clk_branch_check_halt);
  92. }
  93. static void clk_branch_disable(struct clk_hw *hw)
  94. {
  95. clk_branch_toggle(hw, false, clk_branch_check_halt);
  96. }
  97. const struct clk_ops clk_branch_ops = {
  98. .enable = clk_branch_enable,
  99. .disable = clk_branch_disable,
  100. .is_enabled = clk_is_enabled_regmap,
  101. };
  102. EXPORT_SYMBOL_GPL(clk_branch_ops);
  103. static int clk_branch2_enable(struct clk_hw *hw)
  104. {
  105. return clk_branch_toggle(hw, true, clk_branch2_check_halt);
  106. }
  107. static void clk_branch2_disable(struct clk_hw *hw)
  108. {
  109. clk_branch_toggle(hw, false, clk_branch2_check_halt);
  110. }
  111. static int clk_branch2_mem_enable(struct clk_hw *hw)
  112. {
  113. struct clk_mem_branch *mem_br = to_clk_mem_branch(hw);
  114. struct clk_branch branch = mem_br->branch;
  115. u32 val;
  116. int ret;
  117. regmap_update_bits(branch.clkr.regmap, mem_br->mem_enable_reg,
  118. mem_br->mem_enable_ack_mask, mem_br->mem_enable_ack_mask);
  119. ret = regmap_read_poll_timeout(branch.clkr.regmap, mem_br->mem_ack_reg,
  120. val, val & mem_br->mem_enable_ack_mask, 0, 200);
  121. if (ret) {
  122. WARN(1, "%s mem enable failed\n", clk_hw_get_name(&branch.clkr.hw));
  123. return ret;
  124. }
  125. return clk_branch2_enable(hw);
  126. }
  127. static void clk_branch2_mem_disable(struct clk_hw *hw)
  128. {
  129. struct clk_mem_branch *mem_br = to_clk_mem_branch(hw);
  130. regmap_update_bits(mem_br->branch.clkr.regmap, mem_br->mem_enable_reg,
  131. mem_br->mem_enable_ack_mask, 0);
  132. return clk_branch2_disable(hw);
  133. }
  134. const struct clk_ops clk_branch2_mem_ops = {
  135. .enable = clk_branch2_mem_enable,
  136. .disable = clk_branch2_mem_disable,
  137. .is_enabled = clk_is_enabled_regmap,
  138. };
  139. EXPORT_SYMBOL_GPL(clk_branch2_mem_ops);
  140. const struct clk_ops clk_branch2_ops = {
  141. .enable = clk_branch2_enable,
  142. .disable = clk_branch2_disable,
  143. .is_enabled = clk_is_enabled_regmap,
  144. };
  145. EXPORT_SYMBOL_GPL(clk_branch2_ops);
  146. const struct clk_ops clk_branch2_aon_ops = {
  147. .enable = clk_branch2_enable,
  148. .is_enabled = clk_is_enabled_regmap,
  149. };
  150. EXPORT_SYMBOL_GPL(clk_branch2_aon_ops);
  151. const struct clk_ops clk_branch_simple_ops = {
  152. .enable = clk_enable_regmap,
  153. .disable = clk_disable_regmap,
  154. .is_enabled = clk_is_enabled_regmap,
  155. };
  156. EXPORT_SYMBOL_GPL(clk_branch_simple_ops);
  157. const struct clk_ops clk_branch2_prepare_ops = {
  158. .prepare = clk_branch2_enable,
  159. .unprepare = clk_branch2_disable,
  160. .is_prepared = clk_is_enabled_regmap,
  161. };
  162. EXPORT_SYMBOL_GPL(clk_branch2_prepare_ops);