clk-branch.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /* Skip checking halt bit if the clock is in hardware gated mode */
  64. if (clk_branch_in_hwcg_mode(br))
  65. return 0;
  66. if (br->halt_check == BRANCH_HALT_DELAY || (!enabling && voted)) {
  67. udelay(10);
  68. } else if (br->halt_check == BRANCH_HALT_ENABLE ||
  69. br->halt_check == BRANCH_HALT ||
  70. (enabling && voted)) {
  71. int count = 200;
  72. while (count-- > 0) {
  73. if (check_halt(br, enabling))
  74. return 0;
  75. udelay(1);
  76. }
  77. WARN(1, "%s status stuck at 'o%s'", name,
  78. enabling ? "ff" : "n");
  79. return -EBUSY;
  80. }
  81. return 0;
  82. }
  83. static int clk_branch_toggle(struct clk_hw *hw, bool en,
  84. bool (check_halt)(const struct clk_branch *, bool))
  85. {
  86. struct clk_branch *br = to_clk_branch(hw);
  87. int ret;
  88. if (en) {
  89. ret = clk_enable_regmap(hw);
  90. if (ret)
  91. return ret;
  92. } else {
  93. clk_disable_regmap(hw);
  94. }
  95. return clk_branch_wait(br, en, check_halt);
  96. }
  97. static int clk_branch_enable(struct clk_hw *hw)
  98. {
  99. return clk_branch_toggle(hw, true, clk_branch_check_halt);
  100. }
  101. static void clk_branch_disable(struct clk_hw *hw)
  102. {
  103. clk_branch_toggle(hw, false, clk_branch_check_halt);
  104. }
  105. const struct clk_ops clk_branch_ops = {
  106. .enable = clk_branch_enable,
  107. .disable = clk_branch_disable,
  108. .is_enabled = clk_is_enabled_regmap,
  109. };
  110. EXPORT_SYMBOL_GPL(clk_branch_ops);
  111. static int clk_branch2_enable(struct clk_hw *hw)
  112. {
  113. return clk_branch_toggle(hw, true, clk_branch2_check_halt);
  114. }
  115. static void clk_branch2_disable(struct clk_hw *hw)
  116. {
  117. clk_branch_toggle(hw, false, clk_branch2_check_halt);
  118. }
  119. const struct clk_ops clk_branch2_ops = {
  120. .enable = clk_branch2_enable,
  121. .disable = clk_branch2_disable,
  122. .is_enabled = clk_is_enabled_regmap,
  123. };
  124. EXPORT_SYMBOL_GPL(clk_branch2_ops);
  125. const struct clk_ops clk_branch_simple_ops = {
  126. .enable = clk_enable_regmap,
  127. .disable = clk_disable_regmap,
  128. .is_enabled = clk_is_enabled_regmap,
  129. };
  130. EXPORT_SYMBOL_GPL(clk_branch_simple_ops);