clk-pllv4.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <aisheng.dong@nxp.com>
  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 int clk_pllv4_determine_rate(struct clk_hw *hw,
  77. struct clk_rate_request *req)
  78. {
  79. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  80. unsigned long parent_rate = req->best_parent_rate;
  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) req->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 (req->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), req->rate, parent_rate);
  107. req->rate = 0;
  108. return 0;
  109. }
  110. if (parent_rate <= MAX_MFD)
  111. mfd = parent_rate;
  112. temp64 = (u64)(req->rate - round_rate);
  113. temp64 *= mfd;
  114. do_div(temp64, parent_rate);
  115. mfn = temp64;
  116. /*
  117. * NOTE: The value of numerator must always be configured to be
  118. * less than the value of the denominator. If we can't get a proper
  119. * pair of mfn/mfd, we simply return the round_rate without using
  120. * the frac part.
  121. */
  122. if (mfn >= mfd) {
  123. req->rate = round_rate;
  124. return 0;
  125. }
  126. temp64 = (u64)parent_rate;
  127. temp64 *= mfn;
  128. do_div(temp64, mfd);
  129. req->rate = round_rate + (u32)temp64;
  130. return 0;
  131. }
  132. static bool clk_pllv4_is_valid_mult(struct clk_pllv4 *pll, unsigned int mult)
  133. {
  134. int i;
  135. /* check if mult is in valid MULT table */
  136. if (pll->use_mult_range) {
  137. if (mult >= pllv4_mult_range[1] &&
  138. mult <= pllv4_mult_range[0])
  139. return true;
  140. } else {
  141. for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
  142. if (pllv4_mult_table[i] == mult)
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. static int clk_pllv4_set_rate(struct clk_hw *hw, unsigned long rate,
  149. unsigned long parent_rate)
  150. {
  151. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  152. u32 val, mult, mfn, mfd = DEFAULT_MFD;
  153. u64 temp64;
  154. mult = rate / parent_rate;
  155. if (!clk_pllv4_is_valid_mult(pll, mult))
  156. return -EINVAL;
  157. if (parent_rate <= MAX_MFD)
  158. mfd = parent_rate;
  159. temp64 = (u64)(rate - mult * parent_rate);
  160. temp64 *= mfd;
  161. do_div(temp64, parent_rate);
  162. mfn = temp64;
  163. val = readl_relaxed(pll->base + pll->cfg_offset);
  164. val &= ~BM_PLL_MULT;
  165. val |= mult << BP_PLL_MULT;
  166. writel_relaxed(val, pll->base + pll->cfg_offset);
  167. writel_relaxed(mfn, pll->base + pll->num_offset);
  168. writel_relaxed(mfd, pll->base + pll->denom_offset);
  169. return 0;
  170. }
  171. static int clk_pllv4_prepare(struct clk_hw *hw)
  172. {
  173. u32 val;
  174. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  175. val = readl_relaxed(pll->base);
  176. val |= PLL_EN;
  177. writel_relaxed(val, pll->base);
  178. return clk_pllv4_wait_lock(pll);
  179. }
  180. static void clk_pllv4_unprepare(struct clk_hw *hw)
  181. {
  182. u32 val;
  183. struct clk_pllv4 *pll = to_clk_pllv4(hw);
  184. val = readl_relaxed(pll->base);
  185. val &= ~PLL_EN;
  186. writel_relaxed(val, pll->base);
  187. }
  188. static const struct clk_ops clk_pllv4_ops = {
  189. .recalc_rate = clk_pllv4_recalc_rate,
  190. .determine_rate = clk_pllv4_determine_rate,
  191. .set_rate = clk_pllv4_set_rate,
  192. .prepare = clk_pllv4_prepare,
  193. .unprepare = clk_pllv4_unprepare,
  194. .is_prepared = clk_pllv4_is_prepared,
  195. };
  196. struct clk_hw *imx_clk_hw_pllv4(enum imx_pllv4_type type, const char *name,
  197. const char *parent_name, void __iomem *base)
  198. {
  199. struct clk_pllv4 *pll;
  200. struct clk_hw *hw;
  201. struct clk_init_data init;
  202. int ret;
  203. pll = kzalloc_obj(*pll);
  204. if (!pll)
  205. return ERR_PTR(-ENOMEM);
  206. pll->base = base;
  207. if (type == IMX_PLLV4_IMX8ULP ||
  208. type == IMX_PLLV4_IMX8ULP_1GHZ) {
  209. pll->cfg_offset = IMX8ULP_PLL_CFG_OFFSET;
  210. pll->num_offset = IMX8ULP_PLL_NUM_OFFSET;
  211. pll->denom_offset = IMX8ULP_PLL_DENOM_OFFSET;
  212. if (type == IMX_PLLV4_IMX8ULP_1GHZ)
  213. pll->use_mult_range = true;
  214. } else {
  215. pll->cfg_offset = PLL_CFG_OFFSET;
  216. pll->num_offset = PLL_NUM_OFFSET;
  217. pll->denom_offset = PLL_DENOM_OFFSET;
  218. }
  219. init.name = name;
  220. init.ops = &clk_pllv4_ops;
  221. init.parent_names = &parent_name;
  222. init.num_parents = 1;
  223. init.flags = CLK_SET_RATE_GATE;
  224. pll->hw.init = &init;
  225. hw = &pll->hw;
  226. ret = clk_hw_register(NULL, hw);
  227. if (ret) {
  228. kfree(pll);
  229. hw = ERR_PTR(ret);
  230. }
  231. return hw;
  232. }
  233. EXPORT_SYMBOL_GPL(imx_clk_hw_pllv4);