clk-pllv4.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Freescale Semiconductor, Inc.
  4. * Copyright 2017~2018 NXP
  5. *
  6. * Author: Dong Aisheng <[email protected]>
  7. *
  8. */
  9. #include <linux/bits.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/slab.h>
  15. #include "clk.h"
  16. /* PLL Control Status Register (xPLLCSR) */
  17. #define PLL_CSR_OFFSET 0x0
  18. #define PLL_VLD BIT(24)
  19. #define PLL_EN BIT(0)
  20. /* PLL Configuration Register (xPLLCFG) */
  21. #define PLL_CFG_OFFSET 0x08
  22. #define IMX8ULP_PLL_CFG_OFFSET 0x10
  23. #define BP_PLL_MULT 16
  24. #define BM_PLL_MULT (0x7f << 16)
  25. /* PLL Numerator Register (xPLLNUM) */
  26. #define PLL_NUM_OFFSET 0x10
  27. #define IMX8ULP_PLL_NUM_OFFSET 0x1c
  28. /* PLL Denominator Register (xPLLDENOM) */
  29. #define PLL_DENOM_OFFSET 0x14
  30. #define IMX8ULP_PLL_DENOM_OFFSET 0x18
  31. #define MAX_MFD 0x3fffffff
  32. #define DEFAULT_MFD 1000000
  33. struct clk_pllv4 {
  34. struct clk_hw hw;
  35. void __iomem *base;
  36. u32 cfg_offset;
  37. u32 num_offset;
  38. u32 denom_offset;
  39. bool use_mult_range;
  40. };
  41. /* Valid PLL MULT Table */
  42. static const int pllv4_mult_table[] = {33, 27, 22, 20, 17, 16};
  43. /* Valid PLL MULT range, (max, min) */
  44. static const int pllv4_mult_range[] = {54, 27};
  45. #define to_clk_pllv4(__hw) container_of(__hw, struct clk_pllv4, hw)
  46. #define LOCK_TIMEOUT_US USEC_PER_MSEC
  47. static inline int clk_pllv4_wait_lock(struct clk_pllv4 *pll)
  48. {
  49. u32 csr;
  50. return readl_poll_timeout(pll->base + PLL_CSR_OFFSET,
  51. csr, csr & PLL_VLD, 0, LOCK_TIMEOUT_US);
  52. }
  53. static int clk_pllv4_is_prepared(struct clk_hw *hw)
  54. {
  55. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  56. if (readl_relaxed(pll->base) & PLL_EN)
  57. return 1;
  58. return 0;
  59. }
  60. static unsigned long clk_pllv4_recalc_rate(struct clk_hw *hw,
  61. unsigned long parent_rate)
  62. {
  63. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  64. u32 mult, mfn, mfd;
  65. u64 temp64;
  66. mult = readl_relaxed(pll->base + pll->cfg_offset);
  67. mult &= BM_PLL_MULT;
  68. mult >>= BP_PLL_MULT;
  69. mfn = readl_relaxed(pll->base + pll->num_offset);
  70. mfd = readl_relaxed(pll->base + pll->denom_offset);
  71. temp64 = parent_rate;
  72. temp64 *= mfn;
  73. do_div(temp64, mfd);
  74. return (parent_rate * mult) + (u32)temp64;
  75. }
  76. static long clk_pllv4_round_rate(struct clk_hw *hw, unsigned long rate,
  77. unsigned long *prate)
  78. {
  79. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  80. unsigned long parent_rate = *prate;
  81. unsigned long round_rate, i;
  82. u32 mfn, mfd = DEFAULT_MFD;
  83. bool found = false;
  84. u64 temp64;
  85. u32 mult;
  86. if (pll->use_mult_range) {
  87. temp64 = (u64)rate;
  88. do_div(temp64, parent_rate);
  89. mult = temp64;
  90. if (mult >= pllv4_mult_range[1] &&
  91. mult <= pllv4_mult_range[0]) {
  92. round_rate = parent_rate * mult;
  93. found = true;
  94. }
  95. } else {
  96. for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
  97. round_rate = parent_rate * pllv4_mult_table[i];
  98. if (rate >= round_rate) {
  99. found = true;
  100. break;
  101. }
  102. }
  103. }
  104. if (!found) {
  105. pr_warn("%s: unable to round rate %lu, parent rate %lu\n",
  106. clk_hw_get_name(hw), rate, parent_rate);
  107. return 0;
  108. }
  109. if (parent_rate <= MAX_MFD)
  110. mfd = parent_rate;
  111. temp64 = (u64)(rate - round_rate);
  112. temp64 *= mfd;
  113. do_div(temp64, parent_rate);
  114. mfn = temp64;
  115. /*
  116. * NOTE: The value of numerator must always be configured to be
  117. * less than the value of the denominator. If we can't get a proper
  118. * pair of mfn/mfd, we simply return the round_rate without using
  119. * the frac part.
  120. */
  121. if (mfn >= mfd)
  122. return round_rate;
  123. temp64 = (u64)parent_rate;
  124. temp64 *= mfn;
  125. do_div(temp64, mfd);
  126. return round_rate + (u32)temp64;
  127. }
  128. static bool clk_pllv4_is_valid_mult(struct clk_pllv4 *pll, unsigned int mult)
  129. {
  130. int i;
  131. /* check if mult is in valid MULT table */
  132. if (pll->use_mult_range) {
  133. if (mult >= pllv4_mult_range[1] &&
  134. mult <= pllv4_mult_range[0])
  135. return true;
  136. } else {
  137. for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
  138. if (pllv4_mult_table[i] == mult)
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. static int clk_pllv4_set_rate(struct clk_hw *hw, unsigned long rate,
  145. unsigned long parent_rate)
  146. {
  147. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  148. u32 val, mult, mfn, mfd = DEFAULT_MFD;
  149. u64 temp64;
  150. mult = rate / parent_rate;
  151. if (!clk_pllv4_is_valid_mult(pll, mult))
  152. return -EINVAL;
  153. if (parent_rate <= MAX_MFD)
  154. mfd = parent_rate;
  155. temp64 = (u64)(rate - mult * parent_rate);
  156. temp64 *= mfd;
  157. do_div(temp64, parent_rate);
  158. mfn = temp64;
  159. val = readl_relaxed(pll->base + pll->cfg_offset);
  160. val &= ~BM_PLL_MULT;
  161. val |= mult << BP_PLL_MULT;
  162. writel_relaxed(val, pll->base + pll->cfg_offset);
  163. writel_relaxed(mfn, pll->base + pll->num_offset);
  164. writel_relaxed(mfd, pll->base + pll->denom_offset);
  165. return 0;
  166. }
  167. static int clk_pllv4_prepare(struct clk_hw *hw)
  168. {
  169. u32 val;
  170. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  171. val = readl_relaxed(pll->base);
  172. val |= PLL_EN;
  173. writel_relaxed(val, pll->base);
  174. return clk_pllv4_wait_lock(pll);
  175. }
  176. static void clk_pllv4_unprepare(struct clk_hw *hw)
  177. {
  178. u32 val;
  179. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  180. val = readl_relaxed(pll->base);
  181. val &= ~PLL_EN;
  182. writel_relaxed(val, pll->base);
  183. }
  184. static const struct clk_ops clk_pllv4_ops = {
  185. .recalc_rate = clk_pllv4_recalc_rate,
  186. .round_rate = clk_pllv4_round_rate,
  187. .set_rate = clk_pllv4_set_rate,
  188. .prepare = clk_pllv4_prepare,
  189. .unprepare = clk_pllv4_unprepare,
  190. .is_prepared = clk_pllv4_is_prepared,
  191. };
  192. struct clk_hw *imx_clk_hw_pllv4(enum imx_pllv4_type type, const char *name,
  193. const char *parent_name, void __iomem *base)
  194. {
  195. struct clk_pllv4 *pll;
  196. struct clk_hw *hw;
  197. struct clk_init_data init;
  198. int ret;
  199. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  200. if (!pll)
  201. return ERR_PTR(-ENOMEM);
  202. pll->base = base;
  203. if (type == IMX_PLLV4_IMX8ULP ||
  204. type == IMX_PLLV4_IMX8ULP_1GHZ) {
  205. pll->cfg_offset = IMX8ULP_PLL_CFG_OFFSET;
  206. pll->num_offset = IMX8ULP_PLL_NUM_OFFSET;
  207. pll->denom_offset = IMX8ULP_PLL_DENOM_OFFSET;
  208. if (type == IMX_PLLV4_IMX8ULP_1GHZ)
  209. pll->use_mult_range = true;
  210. } else {
  211. pll->cfg_offset = PLL_CFG_OFFSET;
  212. pll->num_offset = PLL_NUM_OFFSET;
  213. pll->denom_offset = PLL_DENOM_OFFSET;
  214. }
  215. init.name = name;
  216. init.ops = &clk_pllv4_ops;
  217. init.parent_names = &parent_name;
  218. init.num_parents = 1;
  219. init.flags = CLK_SET_RATE_GATE;
  220. pll->hw.init = &init;
  221. hw = &pll->hw;
  222. ret = clk_hw_register(NULL, hw);
  223. if (ret) {
  224. kfree(pll);
  225. hw = ERR_PTR(ret);
  226. }
  227. return hw;
  228. }
  229. EXPORT_SYMBOL_GPL(imx_clk_hw_pllv4);