pll.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. /**
  51. * zynq_pll_round_rate() - Round a clock frequency
  52. * @hw: Handle between common and hardware-specific interfaces
  53. * @rate: Desired clock frequency
  54. * @prate: Clock frequency of parent clock
  55. * Returns frequency closest to @rate the hardware can generate.
  56. */
  57. static long zynq_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *prate)
  59. {
  60. u32 fbdiv;
  61. fbdiv = DIV_ROUND_CLOSEST(rate, *prate);
  62. if (fbdiv < 13)
  63. fbdiv = 13;
  64. else if (fbdiv > 66)
  65. fbdiv = 66;
  66. return *prate * fbdiv;
  67. }
  68. /**
  69. * zynq_pll_recalc_rate() - Recalculate clock frequency
  70. * @hw: Handle between common and hardware-specific interfaces
  71. * @parent_rate: Clock frequency of parent clock
  72. * Returns current clock frequency.
  73. */
  74. static unsigned long zynq_pll_recalc_rate(struct clk_hw *hw,
  75. unsigned long parent_rate)
  76. {
  77. struct zynq_pll *clk = to_zynq_pll(hw);
  78. u32 fbdiv;
  79. /*
  80. * makes probably sense to redundantly save fbdiv in the struct
  81. * zynq_pll to save the IO access.
  82. */
  83. fbdiv = (readl(clk->pll_ctrl) & PLLCTRL_FBDIV_MASK) >>
  84. PLLCTRL_FBDIV_SHIFT;
  85. return parent_rate * fbdiv;
  86. }
  87. /**
  88. * zynq_pll_is_enabled - Check if a clock is enabled
  89. * @hw: Handle between common and hardware-specific interfaces
  90. * Returns 1 if the clock is enabled, 0 otherwise.
  91. *
  92. * Not sure this is a good idea, but since disabled means bypassed for
  93. * this clock implementation we say we are always enabled.
  94. */
  95. static int zynq_pll_is_enabled(struct clk_hw *hw)
  96. {
  97. unsigned long flags = 0;
  98. u32 reg;
  99. struct zynq_pll *clk = to_zynq_pll(hw);
  100. spin_lock_irqsave(clk->lock, flags);
  101. reg = readl(clk->pll_ctrl);
  102. spin_unlock_irqrestore(clk->lock, flags);
  103. return !(reg & (PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK));
  104. }
  105. /**
  106. * zynq_pll_enable - Enable clock
  107. * @hw: Handle between common and hardware-specific interfaces
  108. * Returns 0 on success
  109. */
  110. static int zynq_pll_enable(struct clk_hw *hw)
  111. {
  112. unsigned long flags = 0;
  113. u32 reg;
  114. struct zynq_pll *clk = to_zynq_pll(hw);
  115. if (zynq_pll_is_enabled(hw))
  116. return 0;
  117. pr_info("PLL: enable\n");
  118. /* Power up PLL and wait for lock */
  119. spin_lock_irqsave(clk->lock, flags);
  120. reg = readl(clk->pll_ctrl);
  121. reg &= ~(PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK);
  122. writel(reg, clk->pll_ctrl);
  123. while (!(readl(clk->pll_status) & (1 << clk->lockbit)))
  124. ;
  125. spin_unlock_irqrestore(clk->lock, flags);
  126. return 0;
  127. }
  128. /**
  129. * zynq_pll_disable - Disable clock
  130. * @hw: Handle between common and hardware-specific interfaces
  131. * Returns 0 on success
  132. */
  133. static void zynq_pll_disable(struct clk_hw *hw)
  134. {
  135. unsigned long flags = 0;
  136. u32 reg;
  137. struct zynq_pll *clk = to_zynq_pll(hw);
  138. if (!zynq_pll_is_enabled(hw))
  139. return;
  140. pr_info("PLL: shutdown\n");
  141. /* shut down PLL */
  142. spin_lock_irqsave(clk->lock, flags);
  143. reg = readl(clk->pll_ctrl);
  144. reg |= PLLCTRL_RESET_MASK | PLLCTRL_PWRDWN_MASK;
  145. writel(reg, clk->pll_ctrl);
  146. spin_unlock_irqrestore(clk->lock, flags);
  147. }
  148. static const struct clk_ops zynq_pll_ops = {
  149. .enable = zynq_pll_enable,
  150. .disable = zynq_pll_disable,
  151. .is_enabled = zynq_pll_is_enabled,
  152. .round_rate = zynq_pll_round_rate,
  153. .recalc_rate = zynq_pll_recalc_rate
  154. };
  155. /**
  156. * clk_register_zynq_pll() - Register PLL with the clock framework
  157. * @np Pointer to the DT device node
  158. */
  159. struct clk *clk_register_zynq_pll(const char *name, const char *parent,
  160. void __iomem *pll_ctrl, void __iomem *pll_status, u8 lock_index,
  161. spinlock_t *lock)
  162. {
  163. struct zynq_pll *pll;
  164. struct clk *clk;
  165. u32 reg;
  166. const char *parent_arr[1] = {parent};
  167. unsigned long flags = 0;
  168. struct clk_init_data initd = {
  169. .name = name,
  170. .parent_names = parent_arr,
  171. .ops = &zynq_pll_ops,
  172. .num_parents = 1,
  173. .flags = 0
  174. };
  175. pll = kmalloc(sizeof(*pll), GFP_KERNEL);
  176. if (!pll) {
  177. pr_err("%s: Could not allocate Zynq PLL clk.\n", __func__);
  178. return ERR_PTR(-ENOMEM);
  179. }
  180. /* Populate the struct */
  181. pll->hw.init = &initd;
  182. pll->pll_ctrl = pll_ctrl;
  183. pll->pll_status = pll_status;
  184. pll->lockbit = lock_index;
  185. pll->lock = lock;
  186. spin_lock_irqsave(pll->lock, flags);
  187. reg = readl(pll->pll_ctrl);
  188. reg &= ~PLLCTRL_BPQUAL_MASK;
  189. writel(reg, pll->pll_ctrl);
  190. spin_unlock_irqrestore(pll->lock, flags);
  191. clk = clk_register(NULL, &pll->hw);
  192. if (WARN_ON(IS_ERR(clk)))
  193. goto free_pll;
  194. return clk;
  195. free_pll:
  196. kfree(pll);
  197. return clk;
  198. }