clk-pllv2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <linux/kernel.h>
  2. #include <linux/clk.h>
  3. #include <linux/io.h>
  4. #include <linux/errno.h>
  5. #include <linux/delay.h>
  6. #include <linux/slab.h>
  7. #include <linux/err.h>
  8. #include <asm/div64.h>
  9. #include "clk.h"
  10. #define to_clk_pllv2(clk) (container_of(clk, struct clk_pllv2, clk))
  11. /* PLL Register Offsets */
  12. #define MXC_PLL_DP_CTL 0x00
  13. #define MXC_PLL_DP_CONFIG 0x04
  14. #define MXC_PLL_DP_OP 0x08
  15. #define MXC_PLL_DP_MFD 0x0C
  16. #define MXC_PLL_DP_MFN 0x10
  17. #define MXC_PLL_DP_MFNMINUS 0x14
  18. #define MXC_PLL_DP_MFNPLUS 0x18
  19. #define MXC_PLL_DP_HFS_OP 0x1C
  20. #define MXC_PLL_DP_HFS_MFD 0x20
  21. #define MXC_PLL_DP_HFS_MFN 0x24
  22. #define MXC_PLL_DP_MFN_TOGC 0x28
  23. #define MXC_PLL_DP_DESTAT 0x2c
  24. /* PLL Register Bit definitions */
  25. #define MXC_PLL_DP_CTL_MUL_CTRL 0x2000
  26. #define MXC_PLL_DP_CTL_DPDCK0_2_EN 0x1000
  27. #define MXC_PLL_DP_CTL_DPDCK0_2_OFFSET 12
  28. #define MXC_PLL_DP_CTL_ADE 0x800
  29. #define MXC_PLL_DP_CTL_REF_CLK_DIV 0x400
  30. #define MXC_PLL_DP_CTL_REF_CLK_SEL_MASK (3 << 8)
  31. #define MXC_PLL_DP_CTL_REF_CLK_SEL_OFFSET 8
  32. #define MXC_PLL_DP_CTL_HFSM 0x80
  33. #define MXC_PLL_DP_CTL_PRE 0x40
  34. #define MXC_PLL_DP_CTL_UPEN 0x20
  35. #define MXC_PLL_DP_CTL_RST 0x10
  36. #define MXC_PLL_DP_CTL_RCP 0x8
  37. #define MXC_PLL_DP_CTL_PLM 0x4
  38. #define MXC_PLL_DP_CTL_BRM0 0x2
  39. #define MXC_PLL_DP_CTL_LRF 0x1
  40. #define MXC_PLL_DP_CONFIG_BIST 0x8
  41. #define MXC_PLL_DP_CONFIG_SJC_CE 0x4
  42. #define MXC_PLL_DP_CONFIG_AREN 0x2
  43. #define MXC_PLL_DP_CONFIG_LDREQ 0x1
  44. #define MXC_PLL_DP_OP_MFI_OFFSET 4
  45. #define MXC_PLL_DP_OP_MFI_MASK (0xF << 4)
  46. #define MXC_PLL_DP_OP_PDF_OFFSET 0
  47. #define MXC_PLL_DP_OP_PDF_MASK 0xF
  48. #define MXC_PLL_DP_MFD_OFFSET 0
  49. #define MXC_PLL_DP_MFD_MASK 0x07FFFFFF
  50. #define MXC_PLL_DP_MFN_OFFSET 0x0
  51. #define MXC_PLL_DP_MFN_MASK 0x07FFFFFF
  52. #define MXC_PLL_DP_MFN_TOGC_TOG_DIS (1 << 17)
  53. #define MXC_PLL_DP_MFN_TOGC_TOG_EN (1 << 16)
  54. #define MXC_PLL_DP_MFN_TOGC_CNT_OFFSET 0x0
  55. #define MXC_PLL_DP_MFN_TOGC_CNT_MASK 0xFFFF
  56. #define MXC_PLL_DP_DESTAT_TOG_SEL (1 << 31)
  57. #define MXC_PLL_DP_DESTAT_MFN 0x07FFFFFF
  58. #define MAX_DPLL_WAIT_TRIES 1000 /* 1000 * udelay(1) = 1ms */
  59. struct clk_pllv2 {
  60. struct clk_hw hw;
  61. void __iomem *base;
  62. };
  63. static unsigned long __clk_pllv2_recalc_rate(unsigned long parent_rate,
  64. u32 dp_ctl, u32 dp_op, u32 dp_mfd, u32 dp_mfn)
  65. {
  66. long mfi, mfn, mfd, pdf, ref_clk;
  67. unsigned long dbl;
  68. u64 temp;
  69. dbl = dp_ctl & MXC_PLL_DP_CTL_DPDCK0_2_EN;
  70. pdf = dp_op & MXC_PLL_DP_OP_PDF_MASK;
  71. mfi = (dp_op & MXC_PLL_DP_OP_MFI_MASK) >> MXC_PLL_DP_OP_MFI_OFFSET;
  72. mfi = (mfi <= 5) ? 5 : mfi;
  73. mfd = dp_mfd & MXC_PLL_DP_MFD_MASK;
  74. mfn = dp_mfn & MXC_PLL_DP_MFN_MASK;
  75. mfn = sign_extend32(mfn, 26);
  76. ref_clk = 2 * parent_rate;
  77. if (dbl != 0)
  78. ref_clk *= 2;
  79. ref_clk /= (pdf + 1);
  80. temp = (u64) ref_clk * abs(mfn);
  81. do_div(temp, mfd + 1);
  82. if (mfn < 0)
  83. temp = (ref_clk * mfi) - temp;
  84. else
  85. temp = (ref_clk * mfi) + temp;
  86. return temp;
  87. }
  88. static unsigned long clk_pllv2_recalc_rate(struct clk_hw *hw,
  89. unsigned long parent_rate)
  90. {
  91. u32 dp_op, dp_mfd, dp_mfn, dp_ctl;
  92. void __iomem *pllbase;
  93. struct clk_pllv2 *pll = to_clk_pllv2(hw);
  94. pllbase = pll->base;
  95. dp_ctl = __raw_readl(pllbase + MXC_PLL_DP_CTL);
  96. dp_op = __raw_readl(pllbase + MXC_PLL_DP_OP);
  97. dp_mfd = __raw_readl(pllbase + MXC_PLL_DP_MFD);
  98. dp_mfn = __raw_readl(pllbase + MXC_PLL_DP_MFN);
  99. return __clk_pllv2_recalc_rate(parent_rate, dp_ctl, dp_op, dp_mfd, dp_mfn);
  100. }
  101. static int __clk_pllv2_set_rate(unsigned long rate, unsigned long parent_rate,
  102. u32 *dp_op, u32 *dp_mfd, u32 *dp_mfn)
  103. {
  104. u32 reg;
  105. long mfi, pdf, mfn, mfd = 999999;
  106. u64 temp64;
  107. unsigned long quad_parent_rate;
  108. quad_parent_rate = 4 * parent_rate;
  109. pdf = mfi = -1;
  110. while (++pdf < 16 && mfi < 5)
  111. mfi = rate * (pdf+1) / quad_parent_rate;
  112. if (mfi > 15)
  113. return -EINVAL;
  114. pdf--;
  115. temp64 = rate * (pdf + 1) - quad_parent_rate * mfi;
  116. do_div(temp64, quad_parent_rate / 1000000);
  117. mfn = (long)temp64;
  118. reg = mfi << 4 | pdf;
  119. *dp_op = reg;
  120. *dp_mfd = mfd;
  121. *dp_mfn = mfn;
  122. return 0;
  123. }
  124. static int clk_pllv2_set_rate(struct clk_hw *hw, unsigned long rate,
  125. unsigned long parent_rate)
  126. {
  127. struct clk_pllv2 *pll = to_clk_pllv2(hw);
  128. void __iomem *pllbase;
  129. u32 dp_ctl, dp_op, dp_mfd, dp_mfn;
  130. int ret;
  131. pllbase = pll->base;
  132. ret = __clk_pllv2_set_rate(rate, parent_rate, &dp_op, &dp_mfd, &dp_mfn);
  133. if (ret)
  134. return ret;
  135. dp_ctl = __raw_readl(pllbase + MXC_PLL_DP_CTL);
  136. /* use dpdck0_2 */
  137. __raw_writel(dp_ctl | 0x1000L, pllbase + MXC_PLL_DP_CTL);
  138. __raw_writel(dp_op, pllbase + MXC_PLL_DP_OP);
  139. __raw_writel(dp_mfd, pllbase + MXC_PLL_DP_MFD);
  140. __raw_writel(dp_mfn, pllbase + MXC_PLL_DP_MFN);
  141. return 0;
  142. }
  143. static long clk_pllv2_round_rate(struct clk_hw *hw, unsigned long rate,
  144. unsigned long *prate)
  145. {
  146. u32 dp_op, dp_mfd, dp_mfn;
  147. __clk_pllv2_set_rate(rate, *prate, &dp_op, &dp_mfd, &dp_mfn);
  148. return __clk_pllv2_recalc_rate(*prate, MXC_PLL_DP_CTL_DPDCK0_2_EN,
  149. dp_op, dp_mfd, dp_mfn);
  150. }
  151. static int clk_pllv2_prepare(struct clk_hw *hw)
  152. {
  153. struct clk_pllv2 *pll = to_clk_pllv2(hw);
  154. u32 reg;
  155. void __iomem *pllbase;
  156. int i = 0;
  157. pllbase = pll->base;
  158. reg = __raw_readl(pllbase + MXC_PLL_DP_CTL) | MXC_PLL_DP_CTL_UPEN;
  159. __raw_writel(reg, pllbase + MXC_PLL_DP_CTL);
  160. /* Wait for lock */
  161. do {
  162. reg = __raw_readl(pllbase + MXC_PLL_DP_CTL);
  163. if (reg & MXC_PLL_DP_CTL_LRF)
  164. break;
  165. udelay(1);
  166. } while (++i < MAX_DPLL_WAIT_TRIES);
  167. if (i == MAX_DPLL_WAIT_TRIES) {
  168. pr_err("MX5: pll locking failed\n");
  169. return -EINVAL;
  170. }
  171. return 0;
  172. }
  173. static void clk_pllv2_unprepare(struct clk_hw *hw)
  174. {
  175. struct clk_pllv2 *pll = to_clk_pllv2(hw);
  176. u32 reg;
  177. void __iomem *pllbase;
  178. pllbase = pll->base;
  179. reg = __raw_readl(pllbase + MXC_PLL_DP_CTL) & ~MXC_PLL_DP_CTL_UPEN;
  180. __raw_writel(reg, pllbase + MXC_PLL_DP_CTL);
  181. }
  182. static struct clk_ops clk_pllv2_ops = {
  183. .prepare = clk_pllv2_prepare,
  184. .unprepare = clk_pllv2_unprepare,
  185. .recalc_rate = clk_pllv2_recalc_rate,
  186. .round_rate = clk_pllv2_round_rate,
  187. .set_rate = clk_pllv2_set_rate,
  188. };
  189. struct clk *imx_clk_pllv2(const char *name, const char *parent,
  190. void __iomem *base)
  191. {
  192. struct clk_pllv2 *pll;
  193. struct clk *clk;
  194. struct clk_init_data init;
  195. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  196. if (!pll)
  197. return ERR_PTR(-ENOMEM);
  198. pll->base = base;
  199. init.name = name;
  200. init.ops = &clk_pllv2_ops;
  201. init.flags = 0;
  202. init.parent_names = &parent;
  203. init.num_parents = 1;
  204. pll->hw.init = &init;
  205. clk = clk_register(NULL, &pll->hw);
  206. if (IS_ERR(clk))
  207. kfree(pll);
  208. return clk;
  209. }