clk-mpll.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (c) 2016 AmLogic, Inc.
  8. * Author: Michael Turquette <mturquette@baylibre.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright (c) 2016 AmLogic, Inc.
  28. * Author: Michael Turquette <mturquette@baylibre.com>
  29. *
  30. * Redistribution and use in source and binary forms, with or without
  31. * modification, are permitted provided that the following conditions
  32. * are met:
  33. *
  34. * * Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * * Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in
  38. * the documentation and/or other materials provided with the
  39. * distribution.
  40. * * Neither the name of Intel Corporation nor the names of its
  41. * contributors may be used to endorse or promote products derived
  42. * from this software without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  45. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  47. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  48. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  49. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  50. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  51. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  52. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  53. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  54. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55. */
  56. /*
  57. * MultiPhase Locked Loops are outputs from a PLL with additional frequency
  58. * scaling capabilities. MPLL rates are calculated as:
  59. *
  60. * f(N2_integer, SDM_IN ) = 2.0G/(N2_integer + SDM_IN/16384)
  61. */
  62. #include <linux/clk-provider.h>
  63. #include "clkc.h"
  64. #define SDM_DEN 16384
  65. #define N2_MIN 4
  66. #define N2_MAX 511
  67. #define to_meson_clk_mpll(_hw) container_of(_hw, struct meson_clk_mpll, hw)
  68. static long rate_from_params(unsigned long parent_rate,
  69. unsigned long sdm,
  70. unsigned long n2)
  71. {
  72. unsigned long divisor = (SDM_DEN * n2) + sdm;
  73. if (n2 < N2_MIN)
  74. return -EINVAL;
  75. return DIV_ROUND_UP_ULL((u64)parent_rate * SDM_DEN, divisor);
  76. }
  77. static void params_from_rate(unsigned long requested_rate,
  78. unsigned long parent_rate,
  79. unsigned long *sdm,
  80. unsigned long *n2)
  81. {
  82. uint64_t div = parent_rate;
  83. unsigned long rem = do_div(div, requested_rate);
  84. if (div < N2_MIN) {
  85. *n2 = N2_MIN;
  86. *sdm = 0;
  87. } else if (div > N2_MAX) {
  88. *n2 = N2_MAX;
  89. *sdm = SDM_DEN - 1;
  90. } else {
  91. *n2 = div;
  92. *sdm = DIV_ROUND_UP(rem * SDM_DEN, requested_rate);
  93. }
  94. }
  95. static unsigned long mpll_recalc_rate(struct clk_hw *hw,
  96. unsigned long parent_rate)
  97. {
  98. struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
  99. struct parm *p;
  100. unsigned long reg, sdm, n2;
  101. long rate;
  102. p = &mpll->sdm;
  103. reg = readl(mpll->base + p->reg_off);
  104. sdm = PARM_GET(p->width, p->shift, reg);
  105. p = &mpll->n2;
  106. reg = readl(mpll->base + p->reg_off);
  107. n2 = PARM_GET(p->width, p->shift, reg);
  108. rate = rate_from_params(parent_rate, sdm, n2);
  109. if (rate < 0)
  110. return 0;
  111. return rate;
  112. }
  113. static long mpll_round_rate(struct clk_hw *hw,
  114. unsigned long rate,
  115. unsigned long *parent_rate)
  116. {
  117. unsigned long sdm, n2;
  118. params_from_rate(rate, *parent_rate, &sdm, &n2);
  119. return rate_from_params(*parent_rate, sdm, n2);
  120. }
  121. static int mpll_set_rate(struct clk_hw *hw,
  122. unsigned long rate,
  123. unsigned long parent_rate)
  124. {
  125. struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
  126. struct parm *p;
  127. unsigned long reg, sdm, n2;
  128. unsigned long flags = 0;
  129. params_from_rate(rate, parent_rate, &sdm, &n2);
  130. if (mpll->lock)
  131. spin_lock_irqsave(mpll->lock, flags);
  132. else
  133. __acquire(mpll->lock);
  134. p = &mpll->sdm;
  135. reg = readl(mpll->base + p->reg_off);
  136. reg = PARM_SET(p->width, p->shift, reg, sdm);
  137. writel(reg, mpll->base + p->reg_off);
  138. p = &mpll->sdm_en;
  139. reg = readl(mpll->base + p->reg_off);
  140. reg = PARM_SET(p->width, p->shift, reg, 1);
  141. writel(reg, mpll->base + p->reg_off);
  142. p = &mpll->n2;
  143. reg = readl(mpll->base + p->reg_off);
  144. reg = PARM_SET(p->width, p->shift, reg, n2);
  145. writel(reg, mpll->base + p->reg_off);
  146. if (mpll->lock)
  147. spin_unlock_irqrestore(mpll->lock, flags);
  148. else
  149. __release(mpll->lock);
  150. return 0;
  151. }
  152. static void mpll_enable_core(struct clk_hw *hw, int enable)
  153. {
  154. struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
  155. struct parm *p;
  156. unsigned long reg;
  157. unsigned long flags = 0;
  158. if (mpll->lock)
  159. spin_lock_irqsave(mpll->lock, flags);
  160. else
  161. __acquire(mpll->lock);
  162. p = &mpll->en;
  163. reg = readl(mpll->base + p->reg_off);
  164. reg = PARM_SET(p->width, p->shift, reg, enable ? 1 : 0);
  165. writel(reg, mpll->base + p->reg_off);
  166. if (mpll->lock)
  167. spin_unlock_irqrestore(mpll->lock, flags);
  168. else
  169. __release(mpll->lock);
  170. }
  171. static int mpll_enable(struct clk_hw *hw)
  172. {
  173. mpll_enable_core(hw, 1);
  174. return 0;
  175. }
  176. static void mpll_disable(struct clk_hw *hw)
  177. {
  178. mpll_enable_core(hw, 0);
  179. }
  180. static int mpll_is_enabled(struct clk_hw *hw)
  181. {
  182. struct meson_clk_mpll *mpll = to_meson_clk_mpll(hw);
  183. struct parm *p;
  184. unsigned long reg;
  185. int en;
  186. p = &mpll->en;
  187. reg = readl(mpll->base + p->reg_off);
  188. en = PARM_GET(p->width, p->shift, reg);
  189. return en;
  190. }
  191. const struct clk_ops meson_clk_mpll_ro_ops = {
  192. .recalc_rate = mpll_recalc_rate,
  193. .round_rate = mpll_round_rate,
  194. .is_enabled = mpll_is_enabled,
  195. };
  196. const struct clk_ops meson_clk_mpll_ops = {
  197. .recalc_rate = mpll_recalc_rate,
  198. .round_rate = mpll_round_rate,
  199. .set_rate = mpll_set_rate,
  200. .enable = mpll_enable,
  201. .disable = mpll_disable,
  202. .is_enabled = mpll_is_enabled,
  203. };