clk-branch.c 3.9 KB

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