clk-pll.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. struct meson_clk_pll {
  43. struct clk_hw hw;
  44. void __iomem *base;
  45. struct pll_conf *conf;
  46. unsigned int rate_count;
  47. spinlock_t *lock;
  48. };
  49. #define to_meson_clk_pll(_hw) container_of(_hw, struct meson_clk_pll, hw)
  50. static unsigned long meson_clk_pll_recalc_rate(struct clk_hw *hw,
  51. unsigned long parent_rate)
  52. {
  53. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  54. struct parm *p;
  55. unsigned long parent_rate_mhz = parent_rate / 1000000;
  56. unsigned long rate_mhz;
  57. u16 n, m, od;
  58. u32 reg;
  59. p = &pll->conf->n;
  60. reg = readl(pll->base + p->reg_off);
  61. n = PARM_GET(p->width, p->shift, reg);
  62. p = &pll->conf->m;
  63. reg = readl(pll->base + p->reg_off);
  64. m = PARM_GET(p->width, p->shift, reg);
  65. p = &pll->conf->od;
  66. reg = readl(pll->base + p->reg_off);
  67. od = PARM_GET(p->width, p->shift, reg);
  68. rate_mhz = (parent_rate_mhz * m / n) >> od;
  69. return rate_mhz * 1000000;
  70. }
  71. static long meson_clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  72. unsigned long *parent_rate)
  73. {
  74. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  75. const struct pll_rate_table *rate_table = pll->conf->rate_table;
  76. int i;
  77. for (i = 0; i < pll->rate_count; i++) {
  78. if (rate <= rate_table[i].rate)
  79. return rate_table[i].rate;
  80. }
  81. /* else return the smallest value */
  82. return rate_table[0].rate;
  83. }
  84. static const struct pll_rate_table *meson_clk_get_pll_settings(struct meson_clk_pll *pll,
  85. unsigned long rate)
  86. {
  87. const struct pll_rate_table *rate_table = pll->conf->rate_table;
  88. int i;
  89. for (i = 0; i < pll->rate_count; i++) {
  90. if (rate == rate_table[i].rate)
  91. return &rate_table[i];
  92. }
  93. return NULL;
  94. }
  95. static int meson_clk_pll_wait_lock(struct meson_clk_pll *pll,
  96. struct parm *p_n)
  97. {
  98. int delay = 24000000;
  99. u32 reg;
  100. while (delay > 0) {
  101. reg = readl(pll->base + p_n->reg_off);
  102. if (reg & MESON_PLL_LOCK)
  103. return 0;
  104. delay--;
  105. }
  106. return -ETIMEDOUT;
  107. }
  108. static int meson_clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  109. unsigned long parent_rate)
  110. {
  111. struct meson_clk_pll *pll = to_meson_clk_pll(hw);
  112. struct parm *p;
  113. const struct pll_rate_table *rate_set;
  114. unsigned long old_rate;
  115. int ret = 0;
  116. u32 reg;
  117. if (parent_rate == 0 || rate == 0)
  118. return -EINVAL;
  119. old_rate = rate;
  120. rate_set = meson_clk_get_pll_settings(pll, rate);
  121. if (!rate_set)
  122. return -EINVAL;
  123. /* PLL reset */
  124. p = &pll->conf->n;
  125. reg = readl(pll->base + p->reg_off);
  126. writel(reg | MESON_PLL_RESET, pll->base + p->reg_off);
  127. reg = PARM_SET(p->width, p->shift, reg, rate_set->n);
  128. writel(reg, pll->base + p->reg_off);
  129. p = &pll->conf->m;
  130. reg = readl(pll->base + p->reg_off);
  131. reg = PARM_SET(p->width, p->shift, reg, rate_set->m);
  132. writel(reg, pll->base + p->reg_off);
  133. p = &pll->conf->od;
  134. reg = readl(pll->base + p->reg_off);
  135. reg = PARM_SET(p->width, p->shift, reg, rate_set->od);
  136. writel(reg, pll->base + p->reg_off);
  137. p = &pll->conf->n;
  138. ret = meson_clk_pll_wait_lock(pll, p);
  139. if (ret) {
  140. pr_warn("%s: pll did not lock, trying to restore old rate %lu\n",
  141. __func__, old_rate);
  142. meson_clk_pll_set_rate(hw, old_rate, parent_rate);
  143. }
  144. return ret;
  145. }
  146. static const struct clk_ops meson_clk_pll_ops = {
  147. .recalc_rate = meson_clk_pll_recalc_rate,
  148. .round_rate = meson_clk_pll_round_rate,
  149. .set_rate = meson_clk_pll_set_rate,
  150. };
  151. static const struct clk_ops meson_clk_pll_ro_ops = {
  152. .recalc_rate = meson_clk_pll_recalc_rate,
  153. };
  154. struct clk *meson_clk_register_pll(const struct clk_conf *clk_conf,
  155. void __iomem *reg_base,
  156. spinlock_t *lock)
  157. {
  158. struct clk *clk;
  159. struct meson_clk_pll *clk_pll;
  160. struct clk_init_data init;
  161. clk_pll = kzalloc(sizeof(*clk_pll), GFP_KERNEL);
  162. if (!clk_pll)
  163. return ERR_PTR(-ENOMEM);
  164. clk_pll->base = reg_base + clk_conf->reg_off;
  165. clk_pll->lock = lock;
  166. clk_pll->conf = clk_conf->conf.pll;
  167. init.name = clk_conf->clk_name;
  168. init.flags = clk_conf->flags | CLK_GET_RATE_NOCACHE;
  169. init.parent_names = &clk_conf->clks_parent[0];
  170. init.num_parents = 1;
  171. init.ops = &meson_clk_pll_ro_ops;
  172. /* If no rate_table is specified we assume the PLL is read-only */
  173. if (clk_pll->conf->rate_table) {
  174. int len;
  175. for (len = 0; clk_pll->conf->rate_table[len].rate != 0; )
  176. len++;
  177. clk_pll->rate_count = len;
  178. init.ops = &meson_clk_pll_ops;
  179. }
  180. clk_pll->hw.init = &init;
  181. clk = clk_register(NULL, &clk_pll->hw);
  182. if (IS_ERR(clk))
  183. kfree(clk_pll);
  184. return clk;
  185. }