clk-alpha-pll.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright (c) 2015, 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/export.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/regmap.h>
  17. #include <linux/delay.h>
  18. #include "clk-alpha-pll.h"
  19. #define PLL_MODE 0x00
  20. # define PLL_OUTCTRL BIT(0)
  21. # define PLL_BYPASSNL BIT(1)
  22. # define PLL_RESET_N BIT(2)
  23. # define PLL_LOCK_COUNT_SHIFT 8
  24. # define PLL_LOCK_COUNT_MASK 0x3f
  25. # define PLL_BIAS_COUNT_SHIFT 14
  26. # define PLL_BIAS_COUNT_MASK 0x3f
  27. # define PLL_VOTE_FSM_ENA BIT(20)
  28. # define PLL_VOTE_FSM_RESET BIT(21)
  29. # define PLL_ACTIVE_FLAG BIT(30)
  30. # define PLL_LOCK_DET BIT(31)
  31. #define PLL_L_VAL 0x04
  32. #define PLL_ALPHA_VAL 0x08
  33. #define PLL_ALPHA_VAL_U 0x0c
  34. #define PLL_USER_CTL 0x10
  35. # define PLL_POST_DIV_SHIFT 8
  36. # define PLL_POST_DIV_MASK 0xf
  37. # define PLL_ALPHA_EN BIT(24)
  38. # define PLL_VCO_SHIFT 20
  39. # define PLL_VCO_MASK 0x3
  40. #define PLL_USER_CTL_U 0x14
  41. #define PLL_CONFIG_CTL 0x18
  42. #define PLL_TEST_CTL 0x1c
  43. #define PLL_TEST_CTL_U 0x20
  44. #define PLL_STATUS 0x24
  45. /*
  46. * Even though 40 bits are present, use only 32 for ease of calculation.
  47. */
  48. #define ALPHA_REG_BITWIDTH 40
  49. #define ALPHA_BITWIDTH 32
  50. #define to_clk_alpha_pll(_hw) container_of(to_clk_regmap(_hw), \
  51. struct clk_alpha_pll, clkr)
  52. #define to_clk_alpha_pll_postdiv(_hw) container_of(to_clk_regmap(_hw), \
  53. struct clk_alpha_pll_postdiv, clkr)
  54. static int wait_for_pll(struct clk_alpha_pll *pll)
  55. {
  56. u32 val, mask, off;
  57. int count;
  58. int ret;
  59. const char *name = clk_hw_get_name(&pll->clkr.hw);
  60. off = pll->offset;
  61. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  62. if (ret)
  63. return ret;
  64. if (val & PLL_VOTE_FSM_ENA)
  65. mask = PLL_ACTIVE_FLAG;
  66. else
  67. mask = PLL_LOCK_DET;
  68. /* Wait for pll to enable. */
  69. for (count = 100; count > 0; count--) {
  70. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  71. if (ret)
  72. return ret;
  73. if ((val & mask) == mask)
  74. return 0;
  75. udelay(1);
  76. }
  77. WARN(1, "%s didn't enable after voting for it!\n", name);
  78. return -ETIMEDOUT;
  79. }
  80. static int clk_alpha_pll_enable(struct clk_hw *hw)
  81. {
  82. int ret;
  83. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  84. u32 val, mask, off;
  85. off = pll->offset;
  86. mask = PLL_OUTCTRL | PLL_RESET_N | PLL_BYPASSNL;
  87. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  88. if (ret)
  89. return ret;
  90. /* If in FSM mode, just vote for it */
  91. if (val & PLL_VOTE_FSM_ENA) {
  92. ret = clk_enable_regmap(hw);
  93. if (ret)
  94. return ret;
  95. return wait_for_pll(pll);
  96. }
  97. /* Skip if already enabled */
  98. if ((val & mask) == mask)
  99. return 0;
  100. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  101. PLL_BYPASSNL, PLL_BYPASSNL);
  102. if (ret)
  103. return ret;
  104. /*
  105. * H/W requires a 5us delay between disabling the bypass and
  106. * de-asserting the reset.
  107. */
  108. mb();
  109. udelay(5);
  110. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  111. PLL_RESET_N, PLL_RESET_N);
  112. if (ret)
  113. return ret;
  114. ret = wait_for_pll(pll);
  115. if (ret)
  116. return ret;
  117. ret = regmap_update_bits(pll->clkr.regmap, off + PLL_MODE,
  118. PLL_OUTCTRL, PLL_OUTCTRL);
  119. /* Ensure that the write above goes through before returning. */
  120. mb();
  121. return ret;
  122. }
  123. static void clk_alpha_pll_disable(struct clk_hw *hw)
  124. {
  125. int ret;
  126. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  127. u32 val, mask, off;
  128. off = pll->offset;
  129. ret = regmap_read(pll->clkr.regmap, off + PLL_MODE, &val);
  130. if (ret)
  131. return;
  132. /* If in FSM mode, just unvote it */
  133. if (val & PLL_VOTE_FSM_ENA) {
  134. clk_disable_regmap(hw);
  135. return;
  136. }
  137. mask = PLL_OUTCTRL;
  138. regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
  139. /* Delay of 2 output clock ticks required until output is disabled */
  140. mb();
  141. udelay(1);
  142. mask = PLL_RESET_N | PLL_BYPASSNL;
  143. regmap_update_bits(pll->clkr.regmap, off + PLL_MODE, mask, 0);
  144. }
  145. static unsigned long alpha_pll_calc_rate(u64 prate, u32 l, u32 a)
  146. {
  147. return (prate * l) + ((prate * a) >> ALPHA_BITWIDTH);
  148. }
  149. static unsigned long
  150. alpha_pll_round_rate(unsigned long rate, unsigned long prate, u32 *l, u64 *a)
  151. {
  152. u64 remainder;
  153. u64 quotient;
  154. quotient = rate;
  155. remainder = do_div(quotient, prate);
  156. *l = quotient;
  157. if (!remainder) {
  158. *a = 0;
  159. return rate;
  160. }
  161. /* Upper ALPHA_BITWIDTH bits of Alpha */
  162. quotient = remainder << ALPHA_BITWIDTH;
  163. remainder = do_div(quotient, prate);
  164. if (remainder)
  165. quotient++;
  166. *a = quotient;
  167. return alpha_pll_calc_rate(prate, *l, *a);
  168. }
  169. static const struct pll_vco *
  170. alpha_pll_find_vco(const struct clk_alpha_pll *pll, unsigned long rate)
  171. {
  172. const struct pll_vco *v = pll->vco_table;
  173. const struct pll_vco *end = v + pll->num_vco;
  174. for (; v < end; v++)
  175. if (rate >= v->min_freq && rate <= v->max_freq)
  176. return v;
  177. return NULL;
  178. }
  179. static unsigned long
  180. clk_alpha_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  181. {
  182. u32 l, low, high, ctl;
  183. u64 a = 0, prate = parent_rate;
  184. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  185. u32 off = pll->offset;
  186. regmap_read(pll->clkr.regmap, off + PLL_L_VAL, &l);
  187. regmap_read(pll->clkr.regmap, off + PLL_USER_CTL, &ctl);
  188. if (ctl & PLL_ALPHA_EN) {
  189. regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL, &low);
  190. regmap_read(pll->clkr.regmap, off + PLL_ALPHA_VAL_U, &high);
  191. a = (u64)high << 32 | low;
  192. a >>= ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH;
  193. }
  194. return alpha_pll_calc_rate(prate, l, a);
  195. }
  196. static int clk_alpha_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  197. unsigned long prate)
  198. {
  199. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  200. const struct pll_vco *vco;
  201. u32 l, off = pll->offset;
  202. u64 a;
  203. rate = alpha_pll_round_rate(rate, prate, &l, &a);
  204. vco = alpha_pll_find_vco(pll, rate);
  205. if (!vco) {
  206. pr_err("alpha pll not in a valid vco range\n");
  207. return -EINVAL;
  208. }
  209. a <<= (ALPHA_REG_BITWIDTH - ALPHA_BITWIDTH);
  210. regmap_write(pll->clkr.regmap, off + PLL_L_VAL, l);
  211. regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL, a);
  212. regmap_write(pll->clkr.regmap, off + PLL_ALPHA_VAL_U, a >> 32);
  213. regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL,
  214. PLL_VCO_MASK << PLL_VCO_SHIFT,
  215. vco->val << PLL_VCO_SHIFT);
  216. regmap_update_bits(pll->clkr.regmap, off + PLL_USER_CTL, PLL_ALPHA_EN,
  217. PLL_ALPHA_EN);
  218. return 0;
  219. }
  220. static long clk_alpha_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  221. unsigned long *prate)
  222. {
  223. struct clk_alpha_pll *pll = to_clk_alpha_pll(hw);
  224. u32 l;
  225. u64 a;
  226. unsigned long min_freq, max_freq;
  227. rate = alpha_pll_round_rate(rate, *prate, &l, &a);
  228. if (alpha_pll_find_vco(pll, rate))
  229. return rate;
  230. min_freq = pll->vco_table[0].min_freq;
  231. max_freq = pll->vco_table[pll->num_vco - 1].max_freq;
  232. return clamp(rate, min_freq, max_freq);
  233. }
  234. const struct clk_ops clk_alpha_pll_ops = {
  235. .enable = clk_alpha_pll_enable,
  236. .disable = clk_alpha_pll_disable,
  237. .recalc_rate = clk_alpha_pll_recalc_rate,
  238. .round_rate = clk_alpha_pll_round_rate,
  239. .set_rate = clk_alpha_pll_set_rate,
  240. };
  241. EXPORT_SYMBOL_GPL(clk_alpha_pll_ops);
  242. static unsigned long
  243. clk_alpha_pll_postdiv_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  244. {
  245. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  246. u32 ctl;
  247. regmap_read(pll->clkr.regmap, pll->offset + PLL_USER_CTL, &ctl);
  248. ctl >>= PLL_POST_DIV_SHIFT;
  249. ctl &= PLL_POST_DIV_MASK;
  250. return parent_rate >> fls(ctl);
  251. }
  252. static const struct clk_div_table clk_alpha_div_table[] = {
  253. { 0x0, 1 },
  254. { 0x1, 2 },
  255. { 0x3, 4 },
  256. { 0x7, 8 },
  257. { 0xf, 16 },
  258. { }
  259. };
  260. static long
  261. clk_alpha_pll_postdiv_round_rate(struct clk_hw *hw, unsigned long rate,
  262. unsigned long *prate)
  263. {
  264. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  265. return divider_round_rate(hw, rate, prate, clk_alpha_div_table,
  266. pll->width, CLK_DIVIDER_POWER_OF_TWO);
  267. }
  268. static int clk_alpha_pll_postdiv_set_rate(struct clk_hw *hw, unsigned long rate,
  269. unsigned long parent_rate)
  270. {
  271. struct clk_alpha_pll_postdiv *pll = to_clk_alpha_pll_postdiv(hw);
  272. int div;
  273. /* 16 -> 0xf, 8 -> 0x7, 4 -> 0x3, 2 -> 0x1, 1 -> 0x0 */
  274. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate) - 1;
  275. return regmap_update_bits(pll->clkr.regmap, pll->offset + PLL_USER_CTL,
  276. PLL_POST_DIV_MASK << PLL_POST_DIV_SHIFT,
  277. div << PLL_POST_DIV_SHIFT);
  278. }
  279. const struct clk_ops clk_alpha_pll_postdiv_ops = {
  280. .recalc_rate = clk_alpha_pll_postdiv_recalc_rate,
  281. .round_rate = clk_alpha_pll_postdiv_round_rate,
  282. .set_rate = clk_alpha_pll_postdiv_set_rate,
  283. };
  284. EXPORT_SYMBOL_GPL(clk_alpha_pll_postdiv_ops);