ccu_common.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016 Maxime Ripard
  4. *
  5. * Maxime Ripard <[email protected]>
  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. /*
  35. * This clock notifier is called when the frequency of a PLL clock is
  36. * changed. In common PLL designs, changes to the dividers take effect
  37. * almost immediately, while changes to the multipliers (implemented
  38. * as dividers in the feedback loop) take a few cycles to work into
  39. * the feedback loop for the PLL to stablize.
  40. *
  41. * Sometimes when the PLL clock rate is changed, the decrease in the
  42. * divider is too much for the decrease in the multiplier to catch up.
  43. * The PLL clock rate will spike, and in some cases, might lock up
  44. * completely.
  45. *
  46. * This notifier callback will gate and then ungate the clock,
  47. * effectively resetting it, so it proceeds to work. Care must be
  48. * taken to reparent consumers to other temporary clocks during the
  49. * rate change, and that this notifier callback must be the first
  50. * to be registered.
  51. */
  52. static int ccu_pll_notifier_cb(struct notifier_block *nb,
  53. unsigned long event, void *data)
  54. {
  55. struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
  56. int ret = 0;
  57. if (event != POST_RATE_CHANGE)
  58. goto out;
  59. ccu_gate_helper_disable(pll->common, pll->enable);
  60. ret = ccu_gate_helper_enable(pll->common, pll->enable);
  61. if (ret)
  62. goto out;
  63. ccu_helper_wait_for_lock(pll->common, pll->lock);
  64. out:
  65. return notifier_from_errno(ret);
  66. }
  67. int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
  68. {
  69. pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
  70. return clk_notifier_register(pll_nb->common->hw.clk,
  71. &pll_nb->clk_nb);
  72. }
  73. EXPORT_SYMBOL_NS_GPL(ccu_pll_notifier_register, SUNXI_CCU);
  74. static int sunxi_ccu_probe(struct sunxi_ccu *ccu, struct device *dev,
  75. struct device_node *node, void __iomem *reg,
  76. const struct sunxi_ccu_desc *desc)
  77. {
  78. struct ccu_reset *reset;
  79. int i, ret;
  80. ccu->desc = desc;
  81. spin_lock_init(&ccu->lock);
  82. for (i = 0; i < desc->num_ccu_clks; i++) {
  83. struct ccu_common *cclk = desc->ccu_clks[i];
  84. if (!cclk)
  85. continue;
  86. cclk->base = reg;
  87. cclk->lock = &ccu->lock;
  88. }
  89. for (i = 0; i < desc->hw_clks->num ; i++) {
  90. struct clk_hw *hw = desc->hw_clks->hws[i];
  91. const char *name;
  92. if (!hw)
  93. continue;
  94. name = hw->init->name;
  95. if (dev)
  96. ret = clk_hw_register(dev, hw);
  97. else
  98. ret = of_clk_hw_register(node, hw);
  99. if (ret) {
  100. pr_err("Couldn't register clock %d - %s\n", i, name);
  101. goto err_clk_unreg;
  102. }
  103. }
  104. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
  105. desc->hw_clks);
  106. if (ret)
  107. goto err_clk_unreg;
  108. reset = &ccu->reset;
  109. reset->rcdev.of_node = node;
  110. reset->rcdev.ops = &ccu_reset_ops;
  111. reset->rcdev.owner = dev ? dev->driver->owner : THIS_MODULE;
  112. reset->rcdev.nr_resets = desc->num_resets;
  113. reset->base = reg;
  114. reset->lock = &ccu->lock;
  115. reset->reset_map = desc->resets;
  116. ret = reset_controller_register(&reset->rcdev);
  117. if (ret)
  118. goto err_del_provider;
  119. return 0;
  120. err_del_provider:
  121. of_clk_del_provider(node);
  122. err_clk_unreg:
  123. while (--i >= 0) {
  124. struct clk_hw *hw = desc->hw_clks->hws[i];
  125. if (!hw)
  126. continue;
  127. clk_hw_unregister(hw);
  128. }
  129. return ret;
  130. }
  131. static void devm_sunxi_ccu_release(struct device *dev, void *res)
  132. {
  133. struct sunxi_ccu *ccu = res;
  134. const struct sunxi_ccu_desc *desc = ccu->desc;
  135. int i;
  136. reset_controller_unregister(&ccu->reset.rcdev);
  137. of_clk_del_provider(dev->of_node);
  138. for (i = 0; i < desc->hw_clks->num; i++) {
  139. struct clk_hw *hw = desc->hw_clks->hws[i];
  140. if (!hw)
  141. continue;
  142. clk_hw_unregister(hw);
  143. }
  144. }
  145. int devm_sunxi_ccu_probe(struct device *dev, void __iomem *reg,
  146. const struct sunxi_ccu_desc *desc)
  147. {
  148. struct sunxi_ccu *ccu;
  149. int ret;
  150. ccu = devres_alloc(devm_sunxi_ccu_release, sizeof(*ccu), GFP_KERNEL);
  151. if (!ccu)
  152. return -ENOMEM;
  153. ret = sunxi_ccu_probe(ccu, dev, dev->of_node, reg, desc);
  154. if (ret) {
  155. devres_free(ccu);
  156. return ret;
  157. }
  158. devres_add(dev, ccu);
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_NS_GPL(devm_sunxi_ccu_probe, SUNXI_CCU);
  162. void of_sunxi_ccu_probe(struct device_node *node, void __iomem *reg,
  163. const struct sunxi_ccu_desc *desc)
  164. {
  165. struct sunxi_ccu *ccu;
  166. int ret;
  167. ccu = kzalloc(sizeof(*ccu), GFP_KERNEL);
  168. if (!ccu)
  169. return;
  170. ret = sunxi_ccu_probe(ccu, NULL, node, reg, desc);
  171. if (ret) {
  172. pr_err("%pOF: probing clocks failed: %d\n", node, ret);
  173. kfree(ccu);
  174. }
  175. }
  176. MODULE_LICENSE("GPL");