ccu_common.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016 Maxime Ripard
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/device.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include "ccu_common.h"
  14. #include "ccu_gate.h"
  15. #include "ccu_reset.h"
  16. struct sunxi_ccu {
  17. const struct sunxi_ccu_desc *desc;
  18. spinlock_t lock;
  19. struct ccu_reset reset;
  20. };
  21. void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
  22. {
  23. void __iomem *addr;
  24. u32 reg;
  25. if (!lock)
  26. return;
  27. if (common->features & CCU_FEATURE_LOCK_REG)
  28. addr = common->base + common->lock_reg;
  29. else
  30. addr = common->base + common->reg;
  31. WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
  32. }
  33. EXPORT_SYMBOL_NS_GPL(ccu_helper_wait_for_lock, "SUNXI_CCU");
  34. bool ccu_is_better_rate(struct ccu_common *common,
  35. unsigned long target_rate,
  36. unsigned long current_rate,
  37. unsigned long best_rate)
  38. {
  39. unsigned long min_rate, max_rate;
  40. clk_hw_get_rate_range(&common->hw, &min_rate, &max_rate);
  41. if (current_rate > max_rate)
  42. return false;
  43. if (current_rate < min_rate)
  44. return false;
  45. if (common->features & CCU_FEATURE_CLOSEST_RATE)
  46. return abs(current_rate - target_rate) < abs(best_rate - target_rate);
  47. return current_rate <= target_rate && current_rate > best_rate;
  48. }
  49. EXPORT_SYMBOL_NS_GPL(ccu_is_better_rate, "SUNXI_CCU");
  50. /*
  51. * This clock notifier is called when the frequency of a PLL clock is
  52. * changed. In common PLL designs, changes to the dividers take effect
  53. * almost immediately, while changes to the multipliers (implemented
  54. * as dividers in the feedback loop) take a few cycles to work into
  55. * the feedback loop for the PLL to stabilize.
  56. *
  57. * Sometimes when the PLL clock rate is changed, the decrease in the
  58. * divider is too much for the decrease in the multiplier to catch up.
  59. * The PLL clock rate will spike, and in some cases, might lock up
  60. * completely.
  61. *
  62. * This notifier callback will gate and then ungate the clock,
  63. * effectively resetting it, so it proceeds to work. Care must be
  64. * taken to reparent consumers to other temporary clocks during the
  65. * rate change, and that this notifier callback must be the first
  66. * to be registered.
  67. */
  68. static int ccu_pll_notifier_cb(struct notifier_block *nb,
  69. unsigned long event, void *data)
  70. {
  71. struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
  72. int ret = 0;
  73. if (event != POST_RATE_CHANGE)
  74. goto out;
  75. ccu_gate_helper_disable(pll->common, pll->enable);
  76. ret = ccu_gate_helper_enable(pll->common, pll->enable);
  77. if (ret)
  78. goto out;
  79. ccu_helper_wait_for_lock(pll->common, pll->lock);
  80. out:
  81. return notifier_from_errno(ret);
  82. }
  83. int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
  84. {
  85. pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
  86. return clk_notifier_register(pll_nb->common->hw.clk,
  87. &pll_nb->clk_nb);
  88. }
  89. EXPORT_SYMBOL_NS_GPL(ccu_pll_notifier_register, "SUNXI_CCU");
  90. static int sunxi_ccu_probe(struct sunxi_ccu *ccu, struct device *dev,
  91. struct device_node *node, void __iomem *reg,
  92. const struct sunxi_ccu_desc *desc)
  93. {
  94. struct ccu_reset *reset;
  95. int i, ret;
  96. ccu->desc = desc;
  97. spin_lock_init(&ccu->lock);
  98. for (i = 0; i < desc->num_ccu_clks; i++) {
  99. struct ccu_common *cclk = desc->ccu_clks[i];
  100. if (!cclk)
  101. continue;
  102. cclk->base = reg;
  103. cclk->lock = &ccu->lock;
  104. }
  105. for (i = 0; i < desc->hw_clks->num ; i++) {
  106. struct clk_hw *hw = desc->hw_clks->hws[i];
  107. const char *name;
  108. if (!hw)
  109. continue;
  110. name = hw->init->name;
  111. if (dev)
  112. ret = clk_hw_register(dev, hw);
  113. else
  114. ret = of_clk_hw_register(node, hw);
  115. if (ret) {
  116. pr_err("Couldn't register clock %d - %s\n", i, name);
  117. goto err_clk_unreg;
  118. }
  119. }
  120. for (i = 0; i < desc->num_ccu_clks; i++) {
  121. struct ccu_common *cclk = desc->ccu_clks[i];
  122. if (!cclk)
  123. continue;
  124. if (cclk->max_rate)
  125. clk_hw_set_rate_range(&cclk->hw, cclk->min_rate,
  126. cclk->max_rate);
  127. else
  128. WARN(cclk->min_rate,
  129. "No max_rate, ignoring min_rate of clock %d - %s\n",
  130. i, clk_hw_get_name(&cclk->hw));
  131. }
  132. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
  133. desc->hw_clks);
  134. if (ret)
  135. goto err_clk_unreg;
  136. reset = &ccu->reset;
  137. reset->rcdev.of_node = node;
  138. reset->rcdev.ops = &ccu_reset_ops;
  139. reset->rcdev.owner = dev ? dev->driver->owner : THIS_MODULE;
  140. reset->rcdev.nr_resets = desc->num_resets;
  141. reset->base = reg;
  142. reset->lock = &ccu->lock;
  143. reset->reset_map = desc->resets;
  144. ret = reset_controller_register(&reset->rcdev);
  145. if (ret)
  146. goto err_del_provider;
  147. return 0;
  148. err_del_provider:
  149. of_clk_del_provider(node);
  150. err_clk_unreg:
  151. while (--i >= 0) {
  152. struct clk_hw *hw = desc->hw_clks->hws[i];
  153. if (!hw)
  154. continue;
  155. clk_hw_unregister(hw);
  156. }
  157. return ret;
  158. }
  159. static void devm_sunxi_ccu_release(struct device *dev, void *res)
  160. {
  161. struct sunxi_ccu *ccu = res;
  162. const struct sunxi_ccu_desc *desc = ccu->desc;
  163. int i;
  164. reset_controller_unregister(&ccu->reset.rcdev);
  165. of_clk_del_provider(dev->of_node);
  166. for (i = 0; i < desc->hw_clks->num; i++) {
  167. struct clk_hw *hw = desc->hw_clks->hws[i];
  168. if (!hw)
  169. continue;
  170. clk_hw_unregister(hw);
  171. }
  172. }
  173. int devm_sunxi_ccu_probe(struct device *dev, void __iomem *reg,
  174. const struct sunxi_ccu_desc *desc)
  175. {
  176. struct sunxi_ccu *ccu;
  177. int ret;
  178. ccu = devres_alloc(devm_sunxi_ccu_release, sizeof(*ccu), GFP_KERNEL);
  179. if (!ccu)
  180. return -ENOMEM;
  181. ret = sunxi_ccu_probe(ccu, dev, dev->of_node, reg, desc);
  182. if (ret) {
  183. devres_free(ccu);
  184. return ret;
  185. }
  186. devres_add(dev, ccu);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_NS_GPL(devm_sunxi_ccu_probe, "SUNXI_CCU");
  190. void of_sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
  191. const struct sunxi_ccu_desc *desc)
  192. {
  193. struct sunxi_ccu *ccu;
  194. int ret;
  195. ccu = kzalloc_obj(*ccu);
  196. if (!ccu)
  197. return;
  198. ret = sunxi_ccu_probe(ccu, NULL, node, reg, desc);
  199. if (ret) {
  200. pr_err("%pOF: probing clocks failed: %d\n", node, ret);
  201. kfree(ccu);
  202. }
  203. }
  204. MODULE_DESCRIPTION("Common clock support for Allwinner SoCs");
  205. MODULE_LICENSE("GPL");