pll.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Zynq PLL driver
  3. *
  4. * Copyright (C) 2013 Xilinx
  5. *
  6. * Sören Brinkmann <soren.brinkmann@xilinx.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License v2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include <linux/clk/zynq.h>
  22. #include <linux/clk-provider.h>
  23. #include <linux/slab.h>
  24. #include <linux/io.h>
  25. /**
  26. * struct zynq_pll
  27. * @hw: Handle between common and hardware-specific interfaces
  28. * @pll_ctrl: PLL control register
  29. * @pll_status: PLL status register
  30. * @lock: Register lock
  31. * @lockbit: Indicates the associated PLL_LOCKED bit in the PLL status
  32. * register.
  33. */
  34. struct zynq_pll {
  35. struct clk_hw hw;
  36. void __iomem *pll_ctrl;
  37. void __iomem *pll_status;
  38. spinlock_t *lock;
  39. u8 lockbit;
  40. };
  41. #define to_zynq_pll(_hw) container_of(_hw, struct zynq_pll, hw)
  42. /* Register bitfield defines */
  43. #define PLLCTRL_FBDIV_MASK 0x7f000
  44. #define PLLCTRL_FBDIV_SHIFT 12
  45. #define PLLCTRL_BPQUAL_MASK (1 << 3)
  46. #define PLLCTRL_PWRDWN_MASK 2
  47. #define PLLCTRL_PWRDWN_SHIFT 1
  48. #define PLLCTRL_RESET_MASK 1
  49. #define PLLCTRL_RESET_SHIFT 0
  50. #define PLL_FBDIV_MIN 13
  51. #define PLL_FBDIV_MAX 66
  52. /**
  53. * zynq_pll_round_rate() - Round a clock frequency
  54. * @hw: Handle between common and hardware-specific interfaces
  55. * @rate: Desired clock frequency
  56. * @prate: Clock frequency of parent clock
  57. * Returns frequency closest to @rate the hardware can generate.
  58. */
  59. static long zynq_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  60. unsigned long *prate)
  61. {
  62. u32 fbdiv;
  63. fbdiv = DIV_ROUND_CLOSEST(rate, *prate);
  64. if (fbdiv < PLL_FBDIV_MIN)
  65. fbdiv = PLL_FBDIV_MIN;
  66. else if (fbdiv > PLL_FBDIV_MAX)
  67. fbdiv = PLL_FBDIV_MAX;
  68. return *prate * fbdiv;
  69. }
  70. /**
  71. * zynq_pll_recalc_rate() - Recalculate clock frequency
  72. * @hw: Handle between common and hardware-specific interfaces
  73. * @parent_rate: Clock frequency of parent clock
  74. * Returns current clock frequency.
  75. */
  76. static unsigned long zynq_pll_recalc_rate(struct clk_hw *hw,
  77. unsigned long parent_rate)
  78. {
  79. struct zynq_pll *clk = to_zynq_pll(hw);
  80. u32 fbdiv;
  81. /*
  82. * makes probably sense to redundantly save fbdiv in the struct
  83. * zynq_pll to save the IO access.
  84. */
  85. fbdiv = (clk_readl(clk->pll_ctrl) & PLLCTRL_FBDIV_MASK) >>
  86. PLLCTRL_FBDIV_SHIFT;
  87. return parent_rate * fbdiv;
  88. }
  89. /**
  90. * zynq_pll_is_enabled - Check if a clock is enabled
  91. * @hw: Handle between common and hardware-specific interfaces
  92. * Returns 1 if the clock is enabled, 0 otherwise.
  93. *
  94. * Not sure this is a good idea, but since disabled means bypassed for
  95. * this clock implementation we say we are always enabled.
  96. */
  97. static int zynq_pll_is_enabled(struct clk_hw *hw)
  98. {
  99. unsigned long flags = 0;
  100. u32 reg;
  101. struct zynq_pll *clk = to_zynq_pll(hw);
  102. spin_lock_irqsave(clk->lock, flags);
  103. reg = clk_readl(clk->pll_ctrl);
  104. spin_unlock_irqrestore(clk->lock, flags);
  105. return !(reg & (PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK));
  106. }
  107. /**
  108. * zynq_pll_enable - Enable clock
  109. * @hw: Handle between common and hardware-specific interfaces
  110. * Returns 0 on success
  111. */
  112. static int zynq_pll_enable(struct clk_hw *hw)
  113. {
  114. unsigned long flags = 0;
  115. u32 reg;
  116. struct zynq_pll *clk = to_zynq_pll(hw);
  117. if (zynq_pll_is_enabled(hw))
  118. return 0;
  119. pr_info("PLL: enable\n");
  120. /* Power up PLL and wait for lock */
  121. spin_lock_irqsave(clk->lock, flags);
  122. reg = clk_readl(clk->pll_ctrl);
  123. reg &= ~(PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK);
  124. clk_writel(reg, clk->pll_ctrl);
  125. while (!(clk_readl(clk->pll_status) & (1 << clk->lockbit)))
  126. ;
  127. spin_unlock_irqrestore(clk->lock, flags);
  128. return 0;
  129. }
  130. /**
  131. * zynq_pll_disable - Disable clock
  132. * @hw: Handle between common and hardware-specific interfaces
  133. * Returns 0 on success
  134. */
  135. static void zynq_pll_disable(struct clk_hw *hw)
  136. {
  137. unsigned long flags = 0;
  138. u32 reg;
  139. struct zynq_pll *clk = to_zynq_pll(hw);
  140. if (!zynq_pll_is_enabled(hw))
  141. return;
  142. pr_info("PLL: shutdown\n");
  143. /* shut down PLL */
  144. spin_lock_irqsave(clk->lock, flags);
  145. reg = clk_readl(clk->pll_ctrl);
  146. reg |= PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK;
  147. clk_writel(reg, clk->pll_ctrl);
  148. spin_unlock_irqrestore(clk->lock, flags);
  149. }
  150. static const struct clk_ops zynq_pll_ops = {
  151. .enable = zynq_pll_enable,
  152. .disable = zynq_pll_disable,
  153. .is_enabled = zynq_pll_is_enabled,
  154. .round_rate = zynq_pll_round_rate,
  155. .recalc_rate = zynq_pll_recalc_rate
  156. };
  157. /**
  158. * clk_register_zynq_pll() - Register PLL with the clock framework
  159. * @name PLL name
  160. * @parent Parent clock name
  161. * @pll_ctrl Pointer to PLL control register
  162. * @pll_status Pointer to PLL status register
  163. * @lock_index Bit index to this PLL's lock status bit in @pll_status
  164. * @lock Register lock
  165. * Returns handle to the registered clock.
  166. */
  167. struct clk *clk_register_zynq_pll(const char *name, const char *parent,
  168. void __iomem *pll_ctrl, void __iomem *pll_status, u8 lock_index,
  169. spinlock_t *lock)
  170. {
  171. struct zynq_pll *pll;
  172. struct clk *clk;
  173. u32 reg;
  174. const char *parent_arr[1] = {parent};
  175. unsigned long flags = 0;
  176. struct clk_init_data initd = {
  177. .name = name,
  178. .parent_names = parent_arr,
  179. .ops = &zynq_pll_ops,
  180. .num_parents = 1,
  181. .flags = 0
  182. };
  183. pll = kmalloc(sizeof(*pll), GFP_KERNEL);
  184. if (!pll)
  185. return ERR_PTR(-ENOMEM);
  186. /* Populate the struct */
  187. pll->hw.init = &initd;
  188. pll->pll_ctrl = pll_ctrl;
  189. pll->pll_status = pll_status;
  190. pll->lockbit = lock_index;
  191. pll->lock = lock;
  192. spin_lock_irqsave(pll->lock, flags);
  193. reg = clk_readl(pll->pll_ctrl);
  194. reg &= ~PLLCTRL_BPQUAL_MASK;
  195. clk_writel(reg, pll->pll_ctrl);
  196. spin_unlock_irqrestore(pll->lock, flags);
  197. clk = clk_register(NULL, &pll->hw);
  198. if (WARN_ON(IS_ERR(clk)))
  199. goto free_pll;
  200. return clk;
  201. free_pll:
  202. kfree(pll);
  203. return clk;
  204. }