clk-iproc-armpll.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2014 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of 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/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/of_address.h>
  21. #define IPROC_CLK_MAX_FREQ_POLICY 0x3
  22. #define IPROC_CLK_POLICY_FREQ_OFFSET 0x008
  23. #define IPROC_CLK_POLICY_FREQ_POLICY_FREQ_SHIFT 8
  24. #define IPROC_CLK_POLICY_FREQ_POLICY_FREQ_MASK 0x7
  25. #define IPROC_CLK_PLLARMA_OFFSET 0xc00
  26. #define IPROC_CLK_PLLARMA_LOCK_SHIFT 28
  27. #define IPROC_CLK_PLLARMA_PDIV_SHIFT 24
  28. #define IPROC_CLK_PLLARMA_PDIV_MASK 0xf
  29. #define IPROC_CLK_PLLARMA_NDIV_INT_SHIFT 8
  30. #define IPROC_CLK_PLLARMA_NDIV_INT_MASK 0x3ff
  31. #define IPROC_CLK_PLLARMB_OFFSET 0xc04
  32. #define IPROC_CLK_PLLARMB_NDIV_FRAC_MASK 0xfffff
  33. #define IPROC_CLK_PLLARMC_OFFSET 0xc08
  34. #define IPROC_CLK_PLLARMC_BYPCLK_EN_SHIFT 8
  35. #define IPROC_CLK_PLLARMC_MDIV_MASK 0xff
  36. #define IPROC_CLK_PLLARMCTL5_OFFSET 0xc20
  37. #define IPROC_CLK_PLLARMCTL5_H_MDIV_MASK 0xff
  38. #define IPROC_CLK_PLLARM_OFFSET_OFFSET 0xc24
  39. #define IPROC_CLK_PLLARM_SW_CTL_SHIFT 29
  40. #define IPROC_CLK_PLLARM_NDIV_INT_OFFSET_SHIFT 20
  41. #define IPROC_CLK_PLLARM_NDIV_INT_OFFSET_MASK 0xff
  42. #define IPROC_CLK_PLLARM_NDIV_FRAC_OFFSET_MASK 0xfffff
  43. #define IPROC_CLK_ARM_DIV_OFFSET 0xe00
  44. #define IPROC_CLK_ARM_DIV_PLL_SELECT_OVERRIDE_SHIFT 4
  45. #define IPROC_CLK_ARM_DIV_ARM_PLL_SELECT_MASK 0xf
  46. #define IPROC_CLK_POLICY_DBG_OFFSET 0xec0
  47. #define IPROC_CLK_POLICY_DBG_ACT_FREQ_SHIFT 12
  48. #define IPROC_CLK_POLICY_DBG_ACT_FREQ_MASK 0x7
  49. enum iproc_arm_pll_fid {
  50. ARM_PLL_FID_CRYSTAL_CLK = 0,
  51. ARM_PLL_FID_SYS_CLK = 2,
  52. ARM_PLL_FID_CH0_SLOW_CLK = 6,
  53. ARM_PLL_FID_CH1_FAST_CLK = 7
  54. };
  55. struct iproc_arm_pll {
  56. struct clk_hw hw;
  57. void __iomem *base;
  58. unsigned long rate;
  59. };
  60. #define to_iproc_arm_pll(hw) container_of(hw, struct iproc_arm_pll, hw)
  61. static unsigned int __get_fid(struct iproc_arm_pll *pll)
  62. {
  63. u32 val;
  64. unsigned int policy, fid, active_fid;
  65. val = readl(pll->base + IPROC_CLK_ARM_DIV_OFFSET);
  66. if (val & (1 << IPROC_CLK_ARM_DIV_PLL_SELECT_OVERRIDE_SHIFT))
  67. policy = val & IPROC_CLK_ARM_DIV_ARM_PLL_SELECT_MASK;
  68. else
  69. policy = 0;
  70. /* something is seriously wrong */
  71. BUG_ON(policy > IPROC_CLK_MAX_FREQ_POLICY);
  72. val = readl(pll->base + IPROC_CLK_POLICY_FREQ_OFFSET);
  73. fid = (val >> (IPROC_CLK_POLICY_FREQ_POLICY_FREQ_SHIFT * policy)) &
  74. IPROC_CLK_POLICY_FREQ_POLICY_FREQ_MASK;
  75. val = readl(pll->base + IPROC_CLK_POLICY_DBG_OFFSET);
  76. active_fid = IPROC_CLK_POLICY_DBG_ACT_FREQ_MASK &
  77. (val >> IPROC_CLK_POLICY_DBG_ACT_FREQ_SHIFT);
  78. if (fid != active_fid) {
  79. pr_debug("%s: fid override %u->%u\n", __func__, fid,
  80. active_fid);
  81. fid = active_fid;
  82. }
  83. pr_debug("%s: active fid: %u\n", __func__, fid);
  84. return fid;
  85. }
  86. /*
  87. * Determine the mdiv (post divider) based on the frequency ID being used.
  88. * There are 4 sources that can be used to derive the output clock rate:
  89. * - 25 MHz Crystal
  90. * - System clock
  91. * - PLL channel 0 (slow clock)
  92. * - PLL channel 1 (fast clock)
  93. */
  94. static int __get_mdiv(struct iproc_arm_pll *pll)
  95. {
  96. unsigned int fid;
  97. int mdiv;
  98. u32 val;
  99. fid = __get_fid(pll);
  100. switch (fid) {
  101. case ARM_PLL_FID_CRYSTAL_CLK:
  102. case ARM_PLL_FID_SYS_CLK:
  103. mdiv = 1;
  104. break;
  105. case ARM_PLL_FID_CH0_SLOW_CLK:
  106. val = readl(pll->base + IPROC_CLK_PLLARMC_OFFSET);
  107. mdiv = val & IPROC_CLK_PLLARMC_MDIV_MASK;
  108. if (mdiv == 0)
  109. mdiv = 256;
  110. break;
  111. case ARM_PLL_FID_CH1_FAST_CLK:
  112. val = readl(pll->base + IPROC_CLK_PLLARMCTL5_OFFSET);
  113. mdiv = val & IPROC_CLK_PLLARMCTL5_H_MDIV_MASK;
  114. if (mdiv == 0)
  115. mdiv = 256;
  116. break;
  117. default:
  118. mdiv = -EFAULT;
  119. }
  120. return mdiv;
  121. }
  122. static unsigned int __get_ndiv(struct iproc_arm_pll *pll)
  123. {
  124. u32 val;
  125. unsigned int ndiv_int, ndiv_frac, ndiv;
  126. val = readl(pll->base + IPROC_CLK_PLLARM_OFFSET_OFFSET);
  127. if (val & (1 << IPROC_CLK_PLLARM_SW_CTL_SHIFT)) {
  128. /*
  129. * offset mode is active. Read the ndiv from the PLLARM OFFSET
  130. * register
  131. */
  132. ndiv_int = (val >> IPROC_CLK_PLLARM_NDIV_INT_OFFSET_SHIFT) &
  133. IPROC_CLK_PLLARM_NDIV_INT_OFFSET_MASK;
  134. if (ndiv_int == 0)
  135. ndiv_int = 256;
  136. ndiv_frac = val & IPROC_CLK_PLLARM_NDIV_FRAC_OFFSET_MASK;
  137. } else {
  138. /* offset mode not active */
  139. val = readl(pll->base + IPROC_CLK_PLLARMA_OFFSET);
  140. ndiv_int = (val >> IPROC_CLK_PLLARMA_NDIV_INT_SHIFT) &
  141. IPROC_CLK_PLLARMA_NDIV_INT_MASK;
  142. if (ndiv_int == 0)
  143. ndiv_int = 1024;
  144. val = readl(pll->base + IPROC_CLK_PLLARMB_OFFSET);
  145. ndiv_frac = val & IPROC_CLK_PLLARMB_NDIV_FRAC_MASK;
  146. }
  147. ndiv = (ndiv_int << 20) | ndiv_frac;
  148. return ndiv;
  149. }
  150. /*
  151. * The output frequency of the ARM PLL is calculated based on the ARM PLL
  152. * divider values:
  153. * pdiv = ARM PLL pre-divider
  154. * ndiv = ARM PLL multiplier
  155. * mdiv = ARM PLL post divider
  156. *
  157. * The frequency is calculated by:
  158. * ((ndiv * parent clock rate) / pdiv) / mdiv
  159. */
  160. static unsigned long iproc_arm_pll_recalc_rate(struct clk_hw *hw,
  161. unsigned long parent_rate)
  162. {
  163. struct iproc_arm_pll *pll = to_iproc_arm_pll(hw);
  164. u32 val;
  165. int mdiv;
  166. u64 ndiv;
  167. unsigned int pdiv;
  168. /* in bypass mode, use parent rate */
  169. val = readl(pll->base + IPROC_CLK_PLLARMC_OFFSET);
  170. if (val & (1 << IPROC_CLK_PLLARMC_BYPCLK_EN_SHIFT)) {
  171. pll->rate = parent_rate;
  172. return pll->rate;
  173. }
  174. /* PLL needs to be locked */
  175. val = readl(pll->base + IPROC_CLK_PLLARMA_OFFSET);
  176. if (!(val & (1 << IPROC_CLK_PLLARMA_LOCK_SHIFT))) {
  177. pll->rate = 0;
  178. return 0;
  179. }
  180. pdiv = (val >> IPROC_CLK_PLLARMA_PDIV_SHIFT) &
  181. IPROC_CLK_PLLARMA_PDIV_MASK;
  182. if (pdiv == 0)
  183. pdiv = 16;
  184. ndiv = __get_ndiv(pll);
  185. mdiv = __get_mdiv(pll);
  186. if (mdiv <= 0) {
  187. pll->rate = 0;
  188. return 0;
  189. }
  190. pll->rate = (ndiv * parent_rate) >> 20;
  191. pll->rate = (pll->rate / pdiv) / mdiv;
  192. pr_debug("%s: ARM PLL rate: %lu. parent rate: %lu\n", __func__,
  193. pll->rate, parent_rate);
  194. pr_debug("%s: ndiv_int: %u, pdiv: %u, mdiv: %d\n", __func__,
  195. (unsigned int)(ndiv >> 20), pdiv, mdiv);
  196. return pll->rate;
  197. }
  198. static const struct clk_ops iproc_arm_pll_ops = {
  199. .recalc_rate = iproc_arm_pll_recalc_rate,
  200. };
  201. void __init iproc_armpll_setup(struct device_node *node)
  202. {
  203. int ret;
  204. struct clk *clk;
  205. struct iproc_arm_pll *pll;
  206. struct clk_init_data init;
  207. const char *parent_name;
  208. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  209. if (WARN_ON(!pll))
  210. return;
  211. pll->base = of_iomap(node, 0);
  212. if (WARN_ON(!pll->base))
  213. goto err_free_pll;
  214. init.name = node->name;
  215. init.ops = &iproc_arm_pll_ops;
  216. init.flags = 0;
  217. parent_name = of_clk_get_parent_name(node, 0);
  218. init.parent_names = (parent_name ? &parent_name : NULL);
  219. init.num_parents = (parent_name ? 1 : 0);
  220. pll->hw.init = &init;
  221. clk = clk_register(NULL, &pll->hw);
  222. if (WARN_ON(IS_ERR(clk)))
  223. goto err_iounmap;
  224. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  225. if (WARN_ON(ret))
  226. goto err_clk_unregister;
  227. return;
  228. err_clk_unregister:
  229. clk_unregister(clk);
  230. err_iounmap:
  231. iounmap(pll->base);
  232. err_free_pll:
  233. kfree(pll);
  234. }