clk-pll.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2015 Endless Mobile, Inc.
  3. * Author: Carlo Caione <carlo@endlessm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * In the most basic form, a Meson PLL is composed as follows:
  19. *
  20. * PLL
  21. * +------------------------------+
  22. * | |
  23. * in -----[ /N ]---[ *M ]---[ >>OD ]----->> out
  24. * | ^ ^ |
  25. * +------------------------------+
  26. * | |
  27. * FREF VCO
  28. *
  29. * out = (in * M / N) >> OD
  30. */
  31. #include <linux/clk-provider.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/io.h>
  35. #include <linux/module.h>
  36. #include <linux/of_address.h>
  37. #include <linux/slab.h>
  38. #include <linux/string.h>
  39. #include "clkc.h"
  40. #define MESON_PLL_RESET BIT(29)
  41. #define MESON_PLL_LOCK BIT(31)
  42. #define to_meson_clk_pll(_hw) container_of(_hw, struct meson_clk_pll, hw)
  43. static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  47. struct parm *p;
  48. unsigned long parent_rate_mhz = parent_rate / 1000000;
  49. unsigned long rate_mhz;
  50. u16 n, m, frac = 0, od, od2 = 0;
  51. u32 reg;
  52. p = &pll->n;
  53. reg = readl(pll->base + p->reg_off);
  54. n = PARM_GET(p->width, p->shift, reg);
  55. p = &pll->m;
  56. reg = readl(pll->base + p->reg_off);
  57. m = PARM_GET(p->width, p->shift, reg);
  58. p = &pll->od;
  59. reg = readl(pll->base + p->reg_off);
  60. od = PARM_GET(p->width, p->shift, reg);
  61. p = &pll->od2;
  62. if (p->width) {
  63. reg = readl(pll->base + p->reg_off);
  64. od2 = PARM_GET(p->width, p->shift, reg);
  65. }
  66. p = &pll->frac;
  67. if (p->width) {
  68. reg = readl(pll->base + p->reg_off);
  69. frac = PARM_GET(p->width, p->shift, reg);
  70. rate_mhz = (parent_rate_mhz * m + \
  71. (parent_rate_mhz * frac >> 12)) * 2 / n;
  72. rate_mhz = rate_mhz >> od >> od2;
  73. } else
  74. rate_mhz = (parent_rate_mhz * m / n) >> od >> od2;
  75. return rate_mhz * 1000000;
  76. }
  77. static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  78. unsigned long *parent_rate)
  79. {
  80. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  81. const struct pll_rate_table *rate_table = pll->rate_table;
  82. int i;
  83. for (i = 0; i < pll->rate_count; i++) {
  84. if (rate <= rate_table[i].rate)
  85. return rate_table[i].rate;
  86. }
  87. /* else return the smallest value */
  88. return rate_table[0].rate;
  89. }
  90. static const struct pll_rate_table *meson_clk_get_pll_settings(struct meson_clk_pll *pll,
  91. unsigned long rate)
  92. {
  93. const struct pll_rate_table *rate_table = pll->rate_table;
  94. int i;
  95. for (i = 0; i < pll->rate_count; i++) {
  96. if (rate == rate_table[i].rate)
  97. return &rate_table[i];
  98. }
  99. return NULL;
  100. }
  101. /* Specific wait loop for GXL/GXM GP0 PLL */
  102. static int meson_clk_pll_wait_lock_reset(struct meson_clk_pll *pll,
  103. struct parm *p_n)
  104. {
  105. int delay = 100;
  106. u32 reg;
  107. while (delay > 0) {
  108. reg = readl(pll->base + p_n->reg_off);
  109. writel(reg | MESON_PLL_RESET, pll->base + p_n->reg_off);
  110. udelay(10);
  111. writel(reg & ~MESON_PLL_RESET, pll->base + p_n->reg_off);
  112. /* This delay comes from AMLogic tree clk-gp0-gxl driver */
  113. mdelay(1);
  114. reg = readl(pll->base + p_n->reg_off);
  115. if (reg & MESON_PLL_LOCK)
  116. return 0;
  117. delay--;
  118. }
  119. return -ETIMEDOUT;
  120. }
  121. static int meson_clk_pll_wait_lock(struct meson_clk_pll *pll,
  122. struct parm *p_n)
  123. {
  124. int delay = 24000000;
  125. u32 reg;
  126. while (delay > 0) {
  127. reg = readl(pll->base + p_n->reg_off);
  128. if (reg & MESON_PLL_LOCK)
  129. return 0;
  130. delay--;
  131. }
  132. return -ETIMEDOUT;
  133. }
  134. static void meson_clk_pll_init_params(struct meson_clk_pll *pll)
  135. {
  136. int i;
  137. for (i = 0 ; i < pll->params.params_count ; ++i)
  138. writel(pll->params.params_table[i].value,
  139. pll->base + pll->params.params_table[i].reg_off);
  140. }
  141. static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  142. unsigned long parent_rate)
  143. {
  144. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  145. struct parm *p;
  146. const struct pll_rate_table *rate_set;
  147. unsigned long old_rate;
  148. int ret = 0;
  149. u32 reg;
  150. if (parent_rate == 0 || rate == 0)
  151. return -EINVAL;
  152. old_rate = clk_hw_get_rate(hw);
  153. rate_set = meson_clk_get_pll_settings(pll, rate);
  154. if (!rate_set)
  155. return -EINVAL;
  156. /* Initialize the PLL in a clean state if specified */
  157. if (pll->params.params_count)
  158. meson_clk_pll_init_params(pll);
  159. /* PLL reset */
  160. p = &pll->n;
  161. reg = readl(pll->base + p->reg_off);
  162. /* If no_init_reset is provided, avoid resetting at this point */
  163. if (!pll->params.no_init_reset)
  164. writel(reg | MESON_PLL_RESET, pll->base + p->reg_off);
  165. reg = PARM_SET(p->width, p->shift, reg, rate_set->n);
  166. writel(reg, pll->base + p->reg_off);
  167. p = &pll->m;
  168. reg = readl(pll->base + p->reg_off);
  169. reg = PARM_SET(p->width, p->shift, reg, rate_set->m);
  170. writel(reg, pll->base + p->reg_off);
  171. p = &pll->od;
  172. reg = readl(pll->base + p->reg_off);
  173. reg = PARM_SET(p->width, p->shift, reg, rate_set->od);
  174. writel(reg, pll->base + p->reg_off);
  175. p = &pll->od2;
  176. if (p->width) {
  177. reg = readl(pll->base + p->reg_off);
  178. reg = PARM_SET(p->width, p->shift, reg, rate_set->od2);
  179. writel(reg, pll->base + p->reg_off);
  180. }
  181. p = &pll->frac;
  182. if (p->width) {
  183. reg = readl(pll->base + p->reg_off);
  184. reg = PARM_SET(p->width, p->shift, reg, rate_set->frac);
  185. writel(reg, pll->base + p->reg_off);
  186. }
  187. p = &pll->n;
  188. /* If clear_reset_for_lock is provided, remove the reset bit here */
  189. if (pll->params.clear_reset_for_lock) {
  190. reg = readl(pll->base + p->reg_off);
  191. writel(reg & ~MESON_PLL_RESET, pll->base + p->reg_off);
  192. }
  193. /* If reset_lock_loop, use a special loop including resetting */
  194. if (pll->params.reset_lock_loop)
  195. ret = meson_clk_pll_wait_lock_reset(pll, p);
  196. else
  197. ret = meson_clk_pll_wait_lock(pll, p);
  198. if (ret) {
  199. pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
  200. __func__, old_rate);
  201. meson_clk_pll_set_rate(hw, old_rate, parent_rate);
  202. }
  203. return ret;
  204. }
  205. const struct clk_ops meson_clk_pll_ops = {
  206. .recalc_rate = meson_clk_pll_recalc_rate,
  207. .round_rate = meson_clk_pll_round_rate,
  208. .set_rate = meson_clk_pll_set_rate,
  209. };
  210. const struct clk_ops meson_clk_pll_ro_ops = {
  211. .recalc_rate = meson_clk_pll_recalc_rate,
  212. };