clk-pll.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/bitops.h>
  15. #include <linux/err.h>
  16. #include <linux/bug.h>
  17. #include <linux/delay.h>
  18. #include <linux/export.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/regmap.h>
  21. #include <asm/div64.h>
  22. #include "clk-pll.h"
  23. #define PLL_OUTCTRL BIT(0)
  24. #define PLL_BYPASSNL BIT(1)
  25. #define PLL_RESET_N BIT(2)
  26. #define PLL_LOCK_COUNT_SHIFT 8
  27. #define PLL_LOCK_COUNT_MASK 0x3f
  28. #define PLL_BIAS_COUNT_SHIFT 14
  29. #define PLL_BIAS_COUNT_MASK 0x3f
  30. #define PLL_VOTE_FSM_ENA BIT(20)
  31. #define PLL_VOTE_FSM_RESET BIT(21)
  32. static int clk_pll_enable(struct clk_hw *hw)
  33. {
  34. struct clk_pll *pll = to_clk_pll(hw);
  35. int ret;
  36. u32 mask, val;
  37. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  38. ret = regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  39. if (ret)
  40. return ret;
  41. /* Skip if already enabled or in FSM mode */
  42. if ((val & mask) == mask || val & PLL_VOTE_FSM_ENA)
  43. return 0;
  44. /* Disable PLL bypass mode. */
  45. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_BYPASSNL,
  46. PLL_BYPASSNL);
  47. if (ret)
  48. return ret;
  49. /*
  50. * H/W requires a 5us delay between disabling the bypass and
  51. * de-asserting the reset. Delay 10us just to be safe.
  52. */
  53. udelay(10);
  54. /* De-assert active-low PLL reset. */
  55. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_RESET_N,
  56. PLL_RESET_N);
  57. if (ret)
  58. return ret;
  59. /* Wait until PLL is locked. */
  60. udelay(50);
  61. /* Enable PLL output. */
  62. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_OUTCTRL,
  63. PLL_OUTCTRL);
  64. if (ret)
  65. return ret;
  66. return 0;
  67. }
  68. static void clk_pll_disable(struct clk_hw *hw)
  69. {
  70. struct clk_pll *pll = to_clk_pll(hw);
  71. u32 mask;
  72. u32 val;
  73. regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  74. /* Skip if in FSM mode */
  75. if (val & PLL_VOTE_FSM_ENA)
  76. return;
  77. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  78. regmap_update_bits(pll->clkr.regmap, pll->mode_reg, mask, 0);
  79. }
  80. static unsigned long
  81. clk_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  82. {
  83. struct clk_pll *pll = to_clk_pll(hw);
  84. u32 l, m, n, config;
  85. unsigned long rate;
  86. u64 tmp;
  87. regmap_read(pll->clkr.regmap, pll->l_reg, &l);
  88. regmap_read(pll->clkr.regmap, pll->m_reg, &m);
  89. regmap_read(pll->clkr.regmap, pll->n_reg, &n);
  90. l &= 0x3ff;
  91. m &= 0x7ffff;
  92. n &= 0x7ffff;
  93. rate = parent_rate * l;
  94. if (n) {
  95. tmp = parent_rate;
  96. tmp *= m;
  97. do_div(tmp, n);
  98. rate += tmp;
  99. }
  100. if (pll->post_div_width) {
  101. regmap_read(pll->clkr.regmap, pll->config_reg, &config);
  102. config >>= pll->post_div_shift;
  103. config &= BIT(pll->post_div_width) - 1;
  104. rate /= config + 1;
  105. }
  106. return rate;
  107. }
  108. static const
  109. struct pll_freq_tbl *find_freq(const struct pll_freq_tbl *f, unsigned long rate)
  110. {
  111. if (!f)
  112. return NULL;
  113. for (; f->freq; f++)
  114. if (rate <= f->freq)
  115. return f;
  116. return NULL;
  117. }
  118. static long
  119. clk_pll_determine_rate(struct clk_hw *hw, unsigned long rate,
  120. unsigned long *p_rate, struct clk_hw **p)
  121. {
  122. struct clk_pll *pll = to_clk_pll(hw);
  123. const struct pll_freq_tbl *f;
  124. f = find_freq(pll->freq_tbl, rate);
  125. if (!f)
  126. return clk_pll_recalc_rate(hw, *p_rate);
  127. return f->freq;
  128. }
  129. static int
  130. clk_pll_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long p_rate)
  131. {
  132. struct clk_pll *pll = to_clk_pll(hw);
  133. const struct pll_freq_tbl *f;
  134. bool enabled;
  135. u32 mode;
  136. u32 enable_mask = PLL_OUTCTRL | PLL_BYPASSNL | PLL_RESET_N;
  137. f = find_freq(pll->freq_tbl, rate);
  138. if (!f)
  139. return -EINVAL;
  140. regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  141. enabled = (mode & enable_mask) == enable_mask;
  142. if (enabled)
  143. clk_pll_disable(hw);
  144. regmap_update_bits(pll->clkr.regmap, pll->l_reg, 0x3ff, f->l);
  145. regmap_update_bits(pll->clkr.regmap, pll->m_reg, 0x7ffff, f->m);
  146. regmap_update_bits(pll->clkr.regmap, pll->n_reg, 0x7ffff, f->n);
  147. regmap_write(pll->clkr.regmap, pll->config_reg, f->ibits);
  148. if (enabled)
  149. clk_pll_enable(hw);
  150. return 0;
  151. }
  152. const struct clk_ops clk_pll_ops = {
  153. .enable = clk_pll_enable,
  154. .disable = clk_pll_disable,
  155. .recalc_rate = clk_pll_recalc_rate,
  156. .determine_rate = clk_pll_determine_rate,
  157. .set_rate = clk_pll_set_rate,
  158. };
  159. EXPORT_SYMBOL_GPL(clk_pll_ops);
  160. static int wait_for_pll(struct clk_pll *pll)
  161. {
  162. u32 val;
  163. int count;
  164. int ret;
  165. const char *name = __clk_get_name(pll->clkr.hw.clk);
  166. /* Wait for pll to enable. */
  167. for (count = 200; count > 0; count--) {
  168. ret = regmap_read(pll->clkr.regmap, pll->status_reg, &val);
  169. if (ret)
  170. return ret;
  171. if (val & BIT(pll->status_bit))
  172. return 0;
  173. udelay(1);
  174. }
  175. WARN(1, "%s didn't enable after voting for it!\n", name);
  176. return -ETIMEDOUT;
  177. }
  178. static int clk_pll_vote_enable(struct clk_hw *hw)
  179. {
  180. int ret;
  181. struct clk_pll *p = to_clk_pll(__clk_get_hw(__clk_get_parent(hw->clk)));
  182. ret = clk_enable_regmap(hw);
  183. if (ret)
  184. return ret;
  185. return wait_for_pll(p);
  186. }
  187. const struct clk_ops clk_pll_vote_ops = {
  188. .enable = clk_pll_vote_enable,
  189. .disable = clk_disable_regmap,
  190. };
  191. EXPORT_SYMBOL_GPL(clk_pll_vote_ops);
  192. static void
  193. clk_pll_set_fsm_mode(struct clk_pll *pll, struct regmap *regmap, u8 lock_count)
  194. {
  195. u32 val;
  196. u32 mask;
  197. /* De-assert reset to FSM */
  198. regmap_update_bits(regmap, pll->mode_reg, PLL_VOTE_FSM_RESET, 0);
  199. /* Program bias count and lock count */
  200. val = 1 << PLL_BIAS_COUNT_SHIFT | lock_count << PLL_LOCK_COUNT_SHIFT;
  201. mask = PLL_BIAS_COUNT_MASK << PLL_BIAS_COUNT_SHIFT;
  202. mask |= PLL_LOCK_COUNT_MASK << PLL_LOCK_COUNT_SHIFT;
  203. regmap_update_bits(regmap, pll->mode_reg, mask, val);
  204. /* Enable PLL FSM voting */
  205. regmap_update_bits(regmap, pll->mode_reg, PLL_VOTE_FSM_ENA,
  206. PLL_VOTE_FSM_ENA);
  207. }
  208. static void clk_pll_configure(struct clk_pll *pll, struct regmap *regmap,
  209. const struct pll_config *config)
  210. {
  211. u32 val;
  212. u32 mask;
  213. regmap_write(regmap, pll->l_reg, config->l);
  214. regmap_write(regmap, pll->m_reg, config->m);
  215. regmap_write(regmap, pll->n_reg, config->n);
  216. val = config->vco_val;
  217. val |= config->pre_div_val;
  218. val |= config->post_div_val;
  219. val |= config->mn_ena_mask;
  220. val |= config->main_output_mask;
  221. val |= config->aux_output_mask;
  222. mask = config->vco_mask;
  223. mask |= config->pre_div_mask;
  224. mask |= config->post_div_mask;
  225. mask |= config->mn_ena_mask;
  226. mask |= config->main_output_mask;
  227. mask |= config->aux_output_mask;
  228. regmap_update_bits(regmap, pll->config_reg, mask, val);
  229. }
  230. void clk_pll_configure_sr(struct clk_pll *pll, struct regmap *regmap,
  231. const struct pll_config *config, bool fsm_mode)
  232. {
  233. clk_pll_configure(pll, regmap, config);
  234. if (fsm_mode)
  235. clk_pll_set_fsm_mode(pll, regmap, 8);
  236. }
  237. EXPORT_SYMBOL_GPL(clk_pll_configure_sr);
  238. void clk_pll_configure_sr_hpm_lp(struct clk_pll *pll, struct regmap *regmap,
  239. const struct pll_config *config, bool fsm_mode)
  240. {
  241. clk_pll_configure(pll, regmap, config);
  242. if (fsm_mode)
  243. clk_pll_set_fsm_mode(pll, regmap, 0);
  244. }
  245. EXPORT_SYMBOL_GPL(clk_pll_configure_sr_hpm_lp);