ccu_common.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2016 Maxime Ripard
  3. *
  4. * Maxime Ripard <maxime.ripard@free-electrons.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/slab.h>
  20. #include "ccu_common.h"
  21. #include "ccu_gate.h"
  22. #include "ccu_reset.h"
  23. static DEFINE_SPINLOCK(ccu_lock);
  24. void ccu_helper_wait_for_lock(struct ccu_common *common, u32 lock)
  25. {
  26. void __iomem *addr;
  27. u32 reg;
  28. if (!lock)
  29. return;
  30. if (common->features & CCU_FEATURE_LOCK_REG)
  31. addr = common->base + common->lock_reg;
  32. else
  33. addr = common->base + common->reg;
  34. WARN_ON(readl_relaxed_poll_timeout(addr, reg, reg & lock, 100, 70000));
  35. }
  36. /*
  37. * This clock notifier is called when the frequency of a PLL clock is
  38. * changed. In common PLL designs, changes to the dividers take effect
  39. * almost immediately, while changes to the multipliers (implemented
  40. * as dividers in the feedback loop) take a few cycles to work into
  41. * the feedback loop for the PLL to stablize.
  42. *
  43. * Sometimes when the PLL clock rate is changed, the decrease in the
  44. * divider is too much for the decrease in the multiplier to catch up.
  45. * The PLL clock rate will spike, and in some cases, might lock up
  46. * completely.
  47. *
  48. * This notifier callback will gate and then ungate the clock,
  49. * effectively resetting it, so it proceeds to work. Care must be
  50. * taken to reparent consumers to other temporary clocks during the
  51. * rate change, and that this notifier callback must be the first
  52. * to be registered.
  53. */
  54. static int ccu_pll_notifier_cb(struct notifier_block *nb,
  55. unsigned long event, void *data)
  56. {
  57. struct ccu_pll_nb *pll = to_ccu_pll_nb(nb);
  58. int ret = 0;
  59. if (event != POST_RATE_CHANGE)
  60. goto out;
  61. ccu_gate_helper_disable(pll->common, pll->enable);
  62. ret = ccu_gate_helper_enable(pll->common, pll->enable);
  63. if (ret)
  64. goto out;
  65. ccu_helper_wait_for_lock(pll->common, pll->lock);
  66. out:
  67. return notifier_from_errno(ret);
  68. }
  69. int ccu_pll_notifier_register(struct ccu_pll_nb *pll_nb)
  70. {
  71. pll_nb->clk_nb.notifier_call = ccu_pll_notifier_cb;
  72. return clk_notifier_register(pll_nb->common->hw.clk,
  73. &pll_nb->clk_nb);
  74. }
  75. int sunxi_ccu_probe(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. for (i = 0; i < desc->num_ccu_clks; i++) {
  81. struct ccu_common *cclk = desc->ccu_clks[i];
  82. if (!cclk)
  83. continue;
  84. cclk->base = reg;
  85. cclk->lock = &ccu_lock;
  86. }
  87. for (i = 0; i < desc->hw_clks->num ; i++) {
  88. struct clk_hw *hw = desc->hw_clks->hws[i];
  89. if (!hw)
  90. continue;
  91. ret = clk_hw_register(NULL, hw);
  92. if (ret) {
  93. pr_err("Couldn't register clock %d - %s\n",
  94. i, clk_hw_get_name(hw));
  95. goto err_clk_unreg;
  96. }
  97. }
  98. ret = of_clk_add_hw_provider(node, of_clk_hw_onecell_get,
  99. desc->hw_clks);
  100. if (ret)
  101. goto err_clk_unreg;
  102. reset = kzalloc(sizeof(*reset), GFP_KERNEL);
  103. if (!reset) {
  104. ret = -ENOMEM;
  105. goto err_alloc_reset;
  106. }
  107. reset->rcdev.of_node = node;
  108. reset->rcdev.ops = &ccu_reset_ops;
  109. reset->rcdev.owner = THIS_MODULE;
  110. reset->rcdev.nr_resets = desc->num_resets;
  111. reset->base = reg;
  112. reset->lock = &ccu_lock;
  113. reset->reset_map = desc->resets;
  114. ret = reset_controller_register(&reset->rcdev);
  115. if (ret)
  116. goto err_of_clk_unreg;
  117. return 0;
  118. err_of_clk_unreg:
  119. kfree(reset);
  120. err_alloc_reset:
  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. }