pll.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Zynq UltraScale+ MPSoC PLL driver
  4. *
  5. * Copyright (C) 2016-2018 Xilinx
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/slab.h>
  10. #include "clk-zynqmp.h"
  11. /**
  12. * struct zynqmp_pll - PLL clock
  13. * @hw: Handle between common and hardware-specific interfaces
  14. * @clk_id: PLL clock ID
  15. */
  16. struct zynqmp_pll {
  17. struct clk_hw hw;
  18. u32 clk_id;
  19. };
  20. #define to_zynqmp_pll(_hw) container_of(_hw, struct zynqmp_pll, hw)
  21. #define PLL_FBDIV_MIN 25
  22. #define PLL_FBDIV_MAX 125
  23. #define PS_PLL_VCO_MIN 1500000000
  24. #define PS_PLL_VCO_MAX 3000000000UL
  25. enum pll_mode {
  26. PLL_MODE_INT,
  27. PLL_MODE_FRAC,
  28. };
  29. #define FRAC_OFFSET 0x8
  30. #define PLLFCFG_FRAC_EN BIT(31)
  31. #define FRAC_DIV BIT(16) /* 2^16 */
  32. /**
  33. * zynqmp_pll_get_mode() - Get mode of PLL
  34. * @hw: Handle between common and hardware-specific interfaces
  35. *
  36. * Return: Mode of PLL
  37. */
  38. static inline enum pll_mode zynqmp_pll_get_mode(struct clk_hw *hw)
  39. {
  40. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  41. u32 clk_id = clk->clk_id;
  42. const char *clk_name = clk_hw_get_name(hw);
  43. u32 ret_payload[PAYLOAD_ARG_CNT];
  44. int ret;
  45. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  46. ret = eemi_ops->ioctl(0, IOCTL_GET_PLL_FRAC_MODE, clk_id, 0,
  47. ret_payload);
  48. if (ret)
  49. pr_warn_once("%s() PLL get frac mode failed for %s, ret = %d\n",
  50. __func__, clk_name, ret);
  51. return ret_payload[1];
  52. }
  53. /**
  54. * zynqmp_pll_set_mode() - Set the PLL mode
  55. * @hw: Handle between common and hardware-specific interfaces
  56. * @on: Flag to determine the mode
  57. */
  58. static inline void zynqmp_pll_set_mode(struct clk_hw *hw, bool on)
  59. {
  60. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  61. u32 clk_id = clk->clk_id;
  62. const char *clk_name = clk_hw_get_name(hw);
  63. int ret;
  64. u32 mode;
  65. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  66. if (on)
  67. mode = PLL_MODE_FRAC;
  68. else
  69. mode = PLL_MODE_INT;
  70. ret = eemi_ops->ioctl(0, IOCTL_SET_PLL_FRAC_MODE, clk_id, mode, NULL);
  71. if (ret)
  72. pr_warn_once("%s() PLL set frac mode failed for %s, ret = %d\n",
  73. __func__, clk_name, ret);
  74. }
  75. /**
  76. * zynqmp_pll_round_rate() - Round a clock frequency
  77. * @hw: Handle between common and hardware-specific interfaces
  78. * @rate: Desired clock frequency
  79. * @prate: Clock frequency of parent clock
  80. *
  81. * Return: Frequency closest to @rate the hardware can generate
  82. */
  83. static long zynqmp_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  84. unsigned long *prate)
  85. {
  86. u32 fbdiv;
  87. long rate_div, f;
  88. /* Enable the fractional mode if needed */
  89. rate_div = (rate * FRAC_DIV) / *prate;
  90. f = rate_div % FRAC_DIV;
  91. zynqmp_pll_set_mode(hw, !!f);
  92. if (zynqmp_pll_get_mode(hw) == PLL_MODE_FRAC) {
  93. if (rate > PS_PLL_VCO_MAX) {
  94. fbdiv = rate / PS_PLL_VCO_MAX;
  95. rate = rate / (fbdiv + 1);
  96. }
  97. if (rate < PS_PLL_VCO_MIN) {
  98. fbdiv = DIV_ROUND_UP(PS_PLL_VCO_MIN, rate);
  99. rate = rate * fbdiv;
  100. }
  101. return rate;
  102. }
  103. fbdiv = DIV_ROUND_CLOSEST(rate, *prate);
  104. fbdiv = clamp_t(u32, fbdiv, PLL_FBDIV_MIN, PLL_FBDIV_MAX);
  105. return *prate * fbdiv;
  106. }
  107. /**
  108. * zynqmp_pll_recalc_rate() - Recalculate clock frequency
  109. * @hw: Handle between common and hardware-specific interfaces
  110. * @parent_rate: Clock frequency of parent clock
  111. *
  112. * Return: Current clock frequency
  113. */
  114. static unsigned long zynqmp_pll_recalc_rate(struct clk_hw *hw,
  115. unsigned long parent_rate)
  116. {
  117. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  118. u32 clk_id = clk->clk_id;
  119. const char *clk_name = clk_hw_get_name(hw);
  120. u32 fbdiv, data;
  121. unsigned long rate, frac;
  122. u32 ret_payload[PAYLOAD_ARG_CNT];
  123. int ret;
  124. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  125. ret = eemi_ops->clock_getdivider(clk_id, &fbdiv);
  126. if (ret)
  127. pr_warn_once("%s() get divider failed for %s, ret = %d\n",
  128. __func__, clk_name, ret);
  129. rate = parent_rate * fbdiv;
  130. if (zynqmp_pll_get_mode(hw) == PLL_MODE_FRAC) {
  131. eemi_ops->ioctl(0, IOCTL_GET_PLL_FRAC_DATA, clk_id, 0,
  132. ret_payload);
  133. data = ret_payload[1];
  134. frac = (parent_rate * data) / FRAC_DIV;
  135. rate = rate + frac;
  136. }
  137. return rate;
  138. }
  139. /**
  140. * zynqmp_pll_set_rate() - Set rate of PLL
  141. * @hw: Handle between common and hardware-specific interfaces
  142. * @rate: Frequency of clock to be set
  143. * @parent_rate: Clock frequency of parent clock
  144. *
  145. * Set PLL divider to set desired rate.
  146. *
  147. * Returns: rate which is set on success else error code
  148. */
  149. static int zynqmp_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  150. unsigned long parent_rate)
  151. {
  152. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  153. u32 clk_id = clk->clk_id;
  154. const char *clk_name = clk_hw_get_name(hw);
  155. u32 fbdiv;
  156. long rate_div, frac, m, f;
  157. int ret;
  158. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  159. if (zynqmp_pll_get_mode(hw) == PLL_MODE_FRAC) {
  160. rate_div = (rate * FRAC_DIV) / parent_rate;
  161. m = rate_div / FRAC_DIV;
  162. f = rate_div % FRAC_DIV;
  163. m = clamp_t(u32, m, (PLL_FBDIV_MIN), (PLL_FBDIV_MAX));
  164. rate = parent_rate * m;
  165. frac = (parent_rate * f) / FRAC_DIV;
  166. ret = eemi_ops->clock_setdivider(clk_id, m);
  167. if (ret)
  168. pr_warn_once("%s() set divider failed for %s, ret = %d\n",
  169. __func__, clk_name, ret);
  170. eemi_ops->ioctl(0, IOCTL_SET_PLL_FRAC_DATA, clk_id, f, NULL);
  171. return rate + frac;
  172. }
  173. fbdiv = DIV_ROUND_CLOSEST(rate, parent_rate);
  174. fbdiv = clamp_t(u32, fbdiv, PLL_FBDIV_MIN, PLL_FBDIV_MAX);
  175. ret = eemi_ops->clock_setdivider(clk_id, fbdiv);
  176. if (ret)
  177. pr_warn_once("%s() set divider failed for %s, ret = %d\n",
  178. __func__, clk_name, ret);
  179. return parent_rate * fbdiv;
  180. }
  181. /**
  182. * zynqmp_pll_is_enabled() - Check if a clock is enabled
  183. * @hw: Handle between common and hardware-specific interfaces
  184. *
  185. * Return: 1 if the clock is enabled, 0 otherwise
  186. */
  187. static int zynqmp_pll_is_enabled(struct clk_hw *hw)
  188. {
  189. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  190. const char *clk_name = clk_hw_get_name(hw);
  191. u32 clk_id = clk->clk_id;
  192. unsigned int state;
  193. int ret;
  194. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  195. ret = eemi_ops->clock_getstate(clk_id, &state);
  196. if (ret) {
  197. pr_warn_once("%s() clock get state failed for %s, ret = %d\n",
  198. __func__, clk_name, ret);
  199. return -EIO;
  200. }
  201. return state ? 1 : 0;
  202. }
  203. /**
  204. * zynqmp_pll_enable() - Enable clock
  205. * @hw: Handle between common and hardware-specific interfaces
  206. *
  207. * Return: 0 on success else error code
  208. */
  209. static int zynqmp_pll_enable(struct clk_hw *hw)
  210. {
  211. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  212. const char *clk_name = clk_hw_get_name(hw);
  213. u32 clk_id = clk->clk_id;
  214. int ret;
  215. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  216. if (zynqmp_pll_is_enabled(hw))
  217. return 0;
  218. ret = eemi_ops->clock_enable(clk_id);
  219. if (ret)
  220. pr_warn_once("%s() clock enable failed for %s, ret = %d\n",
  221. __func__, clk_name, ret);
  222. return ret;
  223. }
  224. /**
  225. * zynqmp_pll_disable() - Disable clock
  226. * @hw: Handle between common and hardware-specific interfaces
  227. */
  228. static void zynqmp_pll_disable(struct clk_hw *hw)
  229. {
  230. struct zynqmp_pll *clk = to_zynqmp_pll(hw);
  231. const char *clk_name = clk_hw_get_name(hw);
  232. u32 clk_id = clk->clk_id;
  233. int ret;
  234. const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
  235. if (!zynqmp_pll_is_enabled(hw))
  236. return;
  237. ret = eemi_ops->clock_disable(clk_id);
  238. if (ret)
  239. pr_warn_once("%s() clock disable failed for %s, ret = %d\n",
  240. __func__, clk_name, ret);
  241. }
  242. static const struct clk_ops zynqmp_pll_ops = {
  243. .enable = zynqmp_pll_enable,
  244. .disable = zynqmp_pll_disable,
  245. .is_enabled = zynqmp_pll_is_enabled,
  246. .round_rate = zynqmp_pll_round_rate,
  247. .recalc_rate = zynqmp_pll_recalc_rate,
  248. .set_rate = zynqmp_pll_set_rate,
  249. };
  250. /**
  251. * zynqmp_clk_register_pll() - Register PLL with the clock framework
  252. * @name: PLL name
  253. * @clk_id: Clock ID
  254. * @parents: Name of this clock's parents
  255. * @num_parents: Number of parents
  256. * @nodes: Clock topology node
  257. *
  258. * Return: clock hardware to the registered clock
  259. */
  260. struct clk_hw *zynqmp_clk_register_pll(const char *name, u32 clk_id,
  261. const char * const *parents,
  262. u8 num_parents,
  263. const struct clock_topology *nodes)
  264. {
  265. struct zynqmp_pll *pll;
  266. struct clk_hw *hw;
  267. struct clk_init_data init;
  268. int ret;
  269. init.name = name;
  270. init.ops = &zynqmp_pll_ops;
  271. init.flags = nodes->flag;
  272. init.parent_names = parents;
  273. init.num_parents = 1;
  274. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  275. if (!pll)
  276. return ERR_PTR(-ENOMEM);
  277. pll->hw.init = &init;
  278. pll->clk_id = clk_id;
  279. hw = &pll->hw;
  280. ret = clk_hw_register(NULL, hw);
  281. if (ret) {
  282. kfree(pll);
  283. return ERR_PTR(ret);
  284. }
  285. clk_hw_set_rate_range(hw, PS_PLL_VCO_MIN, PS_PLL_VCO_MAX);
  286. if (ret < 0)
  287. pr_err("%s:ERROR clk_set_rate_range failed %d\n", name, ret);
  288. return hw;
  289. }