clk-pll.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. return regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_OUTCTRL,
  63. PLL_OUTCTRL);
  64. }
  65. static void clk_pll_disable(struct clk_hw *hw)
  66. {
  67. struct clk_pll *pll = to_clk_pll(hw);
  68. u32 mask;
  69. u32 val;
  70. regmap_read(pll->clkr.regmap, pll->mode_reg, &val);
  71. /* Skip if in FSM mode */
  72. if (val & PLL_VOTE_FSM_ENA)
  73. return;
  74. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  75. regmap_update_bits(pll->clkr.regmap, pll->mode_reg, mask, 0);
  76. }
  77. static unsigned long
  78. clk_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  79. {
  80. struct clk_pll *pll = to_clk_pll(hw);
  81. u32 l, m, n, config;
  82. unsigned long rate;
  83. u64 tmp;
  84. regmap_read(pll->clkr.regmap, pll->l_reg, &l);
  85. regmap_read(pll->clkr.regmap, pll->m_reg, &m);
  86. regmap_read(pll->clkr.regmap, pll->n_reg, &n);
  87. l &= 0x3ff;
  88. m &= 0x7ffff;
  89. n &= 0x7ffff;
  90. rate = parent_rate * l;
  91. if (n) {
  92. tmp = parent_rate;
  93. tmp *= m;
  94. do_div(tmp, n);
  95. rate += tmp;
  96. }
  97. if (pll->post_div_width) {
  98. regmap_read(pll->clkr.regmap, pll->config_reg, &config);
  99. config >>= pll->post_div_shift;
  100. config &= BIT(pll->post_div_width) - 1;
  101. rate /= config + 1;
  102. }
  103. return rate;
  104. }
  105. static const
  106. struct pll_freq_tbl *find_freq(const struct pll_freq_tbl *f, unsigned long rate)
  107. {
  108. if (!f)
  109. return NULL;
  110. for (; f->freq; f++)
  111. if (rate <= f->freq)
  112. return f;
  113. return NULL;
  114. }
  115. static int
  116. clk_pll_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  117. {
  118. struct clk_pll *pll = to_clk_pll(hw);
  119. const struct pll_freq_tbl *f;
  120. f = find_freq(pll->freq_tbl, req->rate);
  121. if (!f)
  122. req->rate = clk_pll_recalc_rate(hw, req->best_parent_rate);
  123. else
  124. req->rate = f->freq;
  125. return 0;
  126. }
  127. static int
  128. clk_pll_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long p_rate)
  129. {
  130. struct clk_pll *pll = to_clk_pll(hw);
  131. const struct pll_freq_tbl *f;
  132. bool enabled;
  133. u32 mode;
  134. u32 enable_mask = PLL_OUTCTRL | PLL_BYPASSNL | PLL_RESET_N;
  135. f = find_freq(pll->freq_tbl, rate);
  136. if (!f)
  137. return -EINVAL;
  138. regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  139. enabled = (mode & enable_mask) == enable_mask;
  140. if (enabled)
  141. clk_pll_disable(hw);
  142. regmap_update_bits(pll->clkr.regmap, pll->l_reg, 0x3ff, f->l);
  143. regmap_update_bits(pll->clkr.regmap, pll->m_reg, 0x7ffff, f->m);
  144. regmap_update_bits(pll->clkr.regmap, pll->n_reg, 0x7ffff, f->n);
  145. regmap_write(pll->clkr.regmap, pll->config_reg, f->ibits);
  146. if (enabled)
  147. clk_pll_enable(hw);
  148. return 0;
  149. }
  150. const struct clk_ops clk_pll_ops = {
  151. .enable = clk_pll_enable,
  152. .disable = clk_pll_disable,
  153. .recalc_rate = clk_pll_recalc_rate,
  154. .determine_rate = clk_pll_determine_rate,
  155. .set_rate = clk_pll_set_rate,
  156. };
  157. EXPORT_SYMBOL_GPL(clk_pll_ops);
  158. static int wait_for_pll(struct clk_pll *pll)
  159. {
  160. u32 val;
  161. int count;
  162. int ret;
  163. const char *name = clk_hw_get_name(&pll->clkr.hw);
  164. /* Wait for pll to enable. */
  165. for (count = 200; count > 0; count--) {
  166. ret = regmap_read(pll->clkr.regmap, pll->status_reg, &val);
  167. if (ret)
  168. return ret;
  169. if (val & BIT(pll->status_bit))
  170. return 0;
  171. udelay(1);
  172. }
  173. WARN(1, "%s didn't enable after voting for it!\n", name);
  174. return -ETIMEDOUT;
  175. }
  176. static int clk_pll_vote_enable(struct clk_hw *hw)
  177. {
  178. int ret;
  179. struct clk_pll *p = to_clk_pll(clk_hw_get_parent(hw));
  180. ret = clk_enable_regmap(hw);
  181. if (ret)
  182. return ret;
  183. return wait_for_pll(p);
  184. }
  185. const struct clk_ops clk_pll_vote_ops = {
  186. .enable = clk_pll_vote_enable,
  187. .disable = clk_disable_regmap,
  188. };
  189. EXPORT_SYMBOL_GPL(clk_pll_vote_ops);
  190. static void
  191. clk_pll_set_fsm_mode(struct clk_pll *pll, struct regmap *regmap, u8 lock_count)
  192. {
  193. u32 val;
  194. u32 mask;
  195. /* De-assert reset to FSM */
  196. regmap_update_bits(regmap, pll->mode_reg, PLL_VOTE_FSM_RESET, 0);
  197. /* Program bias count and lock count */
  198. val = 1 << PLL_BIAS_COUNT_SHIFT | lock_count << PLL_LOCK_COUNT_SHIFT;
  199. mask = PLL_BIAS_COUNT_MASK << PLL_BIAS_COUNT_SHIFT;
  200. mask |= PLL_LOCK_COUNT_MASK << PLL_LOCK_COUNT_SHIFT;
  201. regmap_update_bits(regmap, pll->mode_reg, mask, val);
  202. /* Enable PLL FSM voting */
  203. regmap_update_bits(regmap, pll->mode_reg, PLL_VOTE_FSM_ENA,
  204. PLL_VOTE_FSM_ENA);
  205. }
  206. static void clk_pll_configure(struct clk_pll *pll, struct regmap *regmap,
  207. const struct pll_config *config)
  208. {
  209. u32 val;
  210. u32 mask;
  211. regmap_write(regmap, pll->l_reg, config->l);
  212. regmap_write(regmap, pll->m_reg, config->m);
  213. regmap_write(regmap, pll->n_reg, config->n);
  214. val = config->vco_val;
  215. val |= config->pre_div_val;
  216. val |= config->post_div_val;
  217. val |= config->mn_ena_mask;
  218. val |= config->main_output_mask;
  219. val |= config->aux_output_mask;
  220. mask = config->vco_mask;
  221. mask |= config->pre_div_mask;
  222. mask |= config->post_div_mask;
  223. mask |= config->mn_ena_mask;
  224. mask |= config->main_output_mask;
  225. mask |= config->aux_output_mask;
  226. regmap_update_bits(regmap, pll->config_reg, mask, val);
  227. }
  228. void clk_pll_configure_sr(struct clk_pll *pll, struct regmap *regmap,
  229. const struct pll_config *config, bool fsm_mode)
  230. {
  231. clk_pll_configure(pll, regmap, config);
  232. if (fsm_mode)
  233. clk_pll_set_fsm_mode(pll, regmap, 8);
  234. }
  235. EXPORT_SYMBOL_GPL(clk_pll_configure_sr);
  236. void clk_pll_configure_sr_hpm_lp(struct clk_pll *pll, struct regmap *regmap,
  237. const struct pll_config *config, bool fsm_mode)
  238. {
  239. clk_pll_configure(pll, regmap, config);
  240. if (fsm_mode)
  241. clk_pll_set_fsm_mode(pll, regmap, 0);
  242. }
  243. EXPORT_SYMBOL_GPL(clk_pll_configure_sr_hpm_lp);
  244. static int clk_pll_sr2_enable(struct clk_hw *hw)
  245. {
  246. struct clk_pll *pll = to_clk_pll(hw);
  247. int ret;
  248. u32 mode;
  249. ret = regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  250. if (ret)
  251. return ret;
  252. /* Disable PLL bypass mode. */
  253. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_BYPASSNL,
  254. PLL_BYPASSNL);
  255. if (ret)
  256. return ret;
  257. /*
  258. * H/W requires a 5us delay between disabling the bypass and
  259. * de-asserting the reset. Delay 10us just to be safe.
  260. */
  261. udelay(10);
  262. /* De-assert active-low PLL reset. */
  263. ret = regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_RESET_N,
  264. PLL_RESET_N);
  265. if (ret)
  266. return ret;
  267. ret = wait_for_pll(pll);
  268. if (ret)
  269. return ret;
  270. /* Enable PLL output. */
  271. return regmap_update_bits(pll->clkr.regmap, pll->mode_reg, PLL_OUTCTRL,
  272. PLL_OUTCTRL);
  273. }
  274. static int
  275. clk_pll_sr2_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long prate)
  276. {
  277. struct clk_pll *pll = to_clk_pll(hw);
  278. const struct pll_freq_tbl *f;
  279. bool enabled;
  280. u32 mode;
  281. u32 enable_mask = PLL_OUTCTRL | PLL_BYPASSNL | PLL_RESET_N;
  282. f = find_freq(pll->freq_tbl, rate);
  283. if (!f)
  284. return -EINVAL;
  285. regmap_read(pll->clkr.regmap, pll->mode_reg, &mode);
  286. enabled = (mode & enable_mask) == enable_mask;
  287. if (enabled)
  288. clk_pll_disable(hw);
  289. regmap_update_bits(pll->clkr.regmap, pll->l_reg, 0x3ff, f->l);
  290. regmap_update_bits(pll->clkr.regmap, pll->m_reg, 0x7ffff, f->m);
  291. regmap_update_bits(pll->clkr.regmap, pll->n_reg, 0x7ffff, f->n);
  292. if (enabled)
  293. clk_pll_sr2_enable(hw);
  294. return 0;
  295. }
  296. const struct clk_ops clk_pll_sr2_ops = {
  297. .enable = clk_pll_sr2_enable,
  298. .disable = clk_pll_disable,
  299. .set_rate = clk_pll_sr2_set_rate,
  300. .recalc_rate = clk_pll_recalc_rate,
  301. .determine_rate = clk_pll_determine_rate,
  302. };
  303. EXPORT_SYMBOL_GPL(clk_pll_sr2_ops);