clk-branch.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/bitops.h>
  7. #include <linux/err.h>
  8. #include <linux/delay.h>
  9. #include <linux/export.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/regmap.h>
  12. #include "clk-branch.h"
  13. static bool clk_branch_in_hwcg_mode(const struct clk_branch *br)
  14. {
  15. u32 val;
  16. if (!br->hwcg_reg)
  17. return false;
  18. regmap_read(br->clkr.regmap, br->hwcg_reg, &val);
  19. return !!(val & BIT(br->hwcg_bit));
  20. }
  21. static bool clk_branch_check_halt(const struct clk_branch *br, bool enabling)
  22. {
  23. bool invert = (br->halt_check == BRANCH_HALT_ENABLE);
  24. u32 val;
  25. regmap_read(br->clkr.regmap, br->halt_reg, &val);
  26. val &= BIT(br->halt_bit);
  27. if (invert)
  28. val = !val;
  29. return !!val == !enabling;
  30. }
  31. #define BRANCH_CLK_OFF BIT(31)
  32. #define BRANCH_NOC_FSM_STATUS_SHIFT 28
  33. #define BRANCH_NOC_FSM_STATUS_MASK 0x7
  34. #define BRANCH_NOC_FSM_STATUS_ON (0x2 << BRANCH_NOC_FSM_STATUS_SHIFT)
  35. static bool clk_branch2_check_halt(const struct clk_branch *br, bool enabling)
  36. {
  37. u32 val;
  38. u32 mask;
  39. mask = BRANCH_NOC_FSM_STATUS_MASK << BRANCH_NOC_FSM_STATUS_SHIFT;
  40. mask |= BRANCH_CLK_OFF;
  41. regmap_read(br->clkr.regmap, br->halt_reg, &val);
  42. if (enabling) {
  43. val &= mask;
  44. return (val & BRANCH_CLK_OFF) == 0 ||
  45. val == BRANCH_NOC_FSM_STATUS_ON;
  46. } else {
  47. return val & BRANCH_CLK_OFF;
  48. }
  49. }
  50. static int clk_branch_wait(const struct clk_branch *br, bool enabling,
  51. bool (check_halt)(const struct clk_branch *, bool))
  52. {
  53. bool voted = br->halt_check & BRANCH_VOTED;
  54. const char *name = clk_hw_get_name(&br->clkr.hw);
  55. /*
  56. * Skip checking halt bit if we're explicitly ignoring the bit or the
  57. * clock is in hardware gated mode
  58. */
  59. if (br->halt_check == BRANCH_HALT_SKIP || clk_branch_in_hwcg_mode(br))
  60. return 0;
  61. if (br->halt_check == BRANCH_HALT_DELAY || (!enabling && voted)) {
  62. udelay(10);
  63. } else if (br->halt_check == BRANCH_HALT_ENABLE ||
  64. br->halt_check == BRANCH_HALT ||
  65. (enabling && voted)) {
  66. int count = 200;
  67. while (count-- > 0) {
  68. if (check_halt(br, enabling))
  69. return 0;
  70. udelay(1);
  71. }
  72. WARN(1, "%s status stuck at 'o%s'", name,
  73. enabling ? "ff" : "n");
  74. return -EBUSY;
  75. }
  76. return 0;
  77. }
  78. static int clk_branch_toggle(struct clk_hw *hw, bool en,
  79. bool (check_halt)(const struct clk_branch *, bool))
  80. {
  81. struct clk_branch *br = to_clk_branch(hw);
  82. int ret;
  83. if (en) {
  84. ret = clk_enable_regmap(hw);
  85. if (ret)
  86. return ret;
  87. } else {
  88. clk_disable_regmap(hw);
  89. }
  90. return clk_branch_wait(br, en, check_halt);
  91. }
  92. static int clk_branch_enable(struct clk_hw *hw)
  93. {
  94. return clk_branch_toggle(hw, true, clk_branch_check_halt);
  95. }
  96. static void clk_branch_disable(struct clk_hw *hw)
  97. {
  98. clk_branch_toggle(hw, false, clk_branch_check_halt);
  99. }
  100. const struct clk_ops clk_branch_ops = {
  101. .enable = clk_branch_enable,
  102. .disable = clk_branch_disable,
  103. .is_enabled = clk_is_enabled_regmap,
  104. };
  105. EXPORT_SYMBOL_GPL(clk_branch_ops);
  106. static int clk_branch2_enable(struct clk_hw *hw)
  107. {
  108. return clk_branch_toggle(hw, true, clk_branch2_check_halt);
  109. }
  110. static void clk_branch2_disable(struct clk_hw *hw)
  111. {
  112. clk_branch_toggle(hw, false, clk_branch2_check_halt);
  113. }
  114. const struct clk_ops clk_branch2_ops = {
  115. .enable = clk_branch2_enable,
  116. .disable = clk_branch2_disable,
  117. .is_enabled = clk_is_enabled_regmap,
  118. };
  119. EXPORT_SYMBOL_GPL(clk_branch2_ops);
  120. const struct clk_ops clk_branch_simple_ops = {
  121. .enable = clk_enable_regmap,
  122. .disable = clk_disable_regmap,
  123. .is_enabled = clk_is_enabled_regmap,
  124. };
  125. EXPORT_SYMBOL_GPL(clk_branch_simple_ops);