clk-super.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/io.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/slab.h>
  10. #include <linux/clk-provider.h>
  11. #include "clk.h"
  12. #define SUPER_STATE_IDLE 0
  13. #define SUPER_STATE_RUN 1
  14. #define SUPER_STATE_IRQ 2
  15. #define SUPER_STATE_FIQ 3
  16. #define SUPER_STATE_SHIFT 28
  17. #define SUPER_STATE_MASK ((BIT(SUPER_STATE_IDLE) | BIT(SUPER_STATE_RUN) | \
  18. BIT(SUPER_STATE_IRQ) | BIT(SUPER_STATE_FIQ)) \
  19. << SUPER_STATE_SHIFT)
  20. #define SUPER_LP_DIV2_BYPASS (1 << 16)
  21. #define super_state(s) (BIT(s) << SUPER_STATE_SHIFT)
  22. #define super_state_to_src_shift(m, s) ((m->width * s))
  23. #define super_state_to_src_mask(m) (((1 << m->width) - 1))
  24. #define CCLK_SRC_PLLP_OUT0 4
  25. #define CCLK_SRC_PLLP_OUT4 5
  26. static u8 clk_super_get_parent(struct clk_hw *hw)
  27. {
  28. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  29. u32 val, state;
  30. u8 source, shift;
  31. val = readl_relaxed(mux->reg);
  32. state = val & SUPER_STATE_MASK;
  33. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  34. (state != super_state(SUPER_STATE_IDLE)));
  35. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  36. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  37. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  38. source = (val >> shift) & super_state_to_src_mask(mux);
  39. /*
  40. * If LP_DIV2_BYPASS is not set and PLLX is current parent then
  41. * PLLX/2 is the input source to CCLKLP.
  42. */
  43. if ((mux->flags & TEGRA_DIVIDER_2) && !(val & SUPER_LP_DIV2_BYPASS) &&
  44. (source == mux->pllx_index))
  45. source = mux->div2_index;
  46. return source;
  47. }
  48. static int clk_super_set_parent(struct clk_hw *hw, u8 index)
  49. {
  50. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  51. u32 val, state;
  52. int err = 0;
  53. u8 parent_index, shift;
  54. unsigned long flags = 0;
  55. if (mux->lock)
  56. spin_lock_irqsave(mux->lock, flags);
  57. val = readl_relaxed(mux->reg);
  58. state = val & SUPER_STATE_MASK;
  59. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  60. (state != super_state(SUPER_STATE_IDLE)));
  61. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  62. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  63. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  64. /*
  65. * For LP mode super-clock switch between PLLX direct
  66. * and divided-by-2 outputs is allowed only when other
  67. * than PLLX clock source is current parent.
  68. */
  69. if ((mux->flags & TEGRA_DIVIDER_2) && ((index == mux->div2_index) ||
  70. (index == mux->pllx_index))) {
  71. parent_index = clk_super_get_parent(hw);
  72. if ((parent_index == mux->div2_index) ||
  73. (parent_index == mux->pllx_index)) {
  74. err = -EINVAL;
  75. goto out;
  76. }
  77. val ^= SUPER_LP_DIV2_BYPASS;
  78. writel_relaxed(val, mux->reg);
  79. udelay(2);
  80. if (index == mux->div2_index)
  81. index = mux->pllx_index;
  82. }
  83. /* enable PLLP branches to CPU before selecting PLLP source */
  84. if ((mux->flags & TEGRA210_CPU_CLK) &&
  85. (index == CCLK_SRC_PLLP_OUT0 || index == CCLK_SRC_PLLP_OUT4))
  86. tegra_clk_set_pllp_out_cpu(true);
  87. val &= ~((super_state_to_src_mask(mux)) << shift);
  88. val |= (index & (super_state_to_src_mask(mux))) << shift;
  89. writel_relaxed(val, mux->reg);
  90. udelay(2);
  91. /* disable PLLP branches to CPU if not used */
  92. if ((mux->flags & TEGRA210_CPU_CLK) &&
  93. index != CCLK_SRC_PLLP_OUT0 && index != CCLK_SRC_PLLP_OUT4)
  94. tegra_clk_set_pllp_out_cpu(false);
  95. out:
  96. if (mux->lock)
  97. spin_unlock_irqrestore(mux->lock, flags);
  98. return err;
  99. }
  100. static void clk_super_mux_restore_context(struct clk_hw *hw)
  101. {
  102. int parent_id;
  103. parent_id = clk_hw_get_parent_index(hw);
  104. if (WARN_ON(parent_id < 0))
  105. return;
  106. clk_super_set_parent(hw, parent_id);
  107. }
  108. static const struct clk_ops tegra_clk_super_mux_ops = {
  109. .get_parent = clk_super_get_parent,
  110. .set_parent = clk_super_set_parent,
  111. .restore_context = clk_super_mux_restore_context,
  112. };
  113. static long clk_super_round_rate(struct clk_hw *hw, unsigned long rate,
  114. unsigned long *parent_rate)
  115. {
  116. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  117. struct clk_hw *div_hw = &super->frac_div.hw;
  118. __clk_hw_set_clk(div_hw, hw);
  119. return super->div_ops->round_rate(div_hw, rate, parent_rate);
  120. }
  121. static unsigned long clk_super_recalc_rate(struct clk_hw *hw,
  122. unsigned long parent_rate)
  123. {
  124. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  125. struct clk_hw *div_hw = &super->frac_div.hw;
  126. __clk_hw_set_clk(div_hw, hw);
  127. return super->div_ops->recalc_rate(div_hw, parent_rate);
  128. }
  129. static int clk_super_set_rate(struct clk_hw *hw, unsigned long rate,
  130. unsigned long parent_rate)
  131. {
  132. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  133. struct clk_hw *div_hw = &super->frac_div.hw;
  134. __clk_hw_set_clk(div_hw, hw);
  135. return super->div_ops->set_rate(div_hw, rate, parent_rate);
  136. }
  137. static void clk_super_restore_context(struct clk_hw *hw)
  138. {
  139. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  140. struct clk_hw *div_hw = &super->frac_div.hw;
  141. int parent_id;
  142. parent_id = clk_hw_get_parent_index(hw);
  143. if (WARN_ON(parent_id < 0))
  144. return;
  145. super->div_ops->restore_context(div_hw);
  146. clk_super_set_parent(hw, parent_id);
  147. }
  148. const struct clk_ops tegra_clk_super_ops = {
  149. .get_parent = clk_super_get_parent,
  150. .set_parent = clk_super_set_parent,
  151. .set_rate = clk_super_set_rate,
  152. .round_rate = clk_super_round_rate,
  153. .recalc_rate = clk_super_recalc_rate,
  154. .restore_context = clk_super_restore_context,
  155. };
  156. struct clk *tegra_clk_register_super_mux(const char *name,
  157. const char **parent_names, u8 num_parents,
  158. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  159. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
  160. {
  161. struct tegra_clk_super_mux *super;
  162. struct clk *clk;
  163. struct clk_init_data init;
  164. super = kzalloc(sizeof(*super), GFP_KERNEL);
  165. if (!super)
  166. return ERR_PTR(-ENOMEM);
  167. init.name = name;
  168. init.ops = &tegra_clk_super_mux_ops;
  169. init.flags = flags;
  170. init.parent_names = parent_names;
  171. init.num_parents = num_parents;
  172. super->reg = reg;
  173. super->pllx_index = pllx_index;
  174. super->div2_index = div2_index;
  175. super->lock = lock;
  176. super->width = width;
  177. super->flags = clk_super_flags;
  178. /* Data in .init is copied by clk_register(), so stack variable OK */
  179. super->hw.init = &init;
  180. clk = tegra_clk_dev_register(&super->hw);
  181. if (IS_ERR(clk))
  182. kfree(super);
  183. return clk;
  184. }
  185. struct clk *tegra_clk_register_super_clk(const char *name,
  186. const char * const *parent_names, u8 num_parents,
  187. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  188. spinlock_t *lock)
  189. {
  190. struct tegra_clk_super_mux *super;
  191. struct clk *clk;
  192. struct clk_init_data init;
  193. super = kzalloc(sizeof(*super), GFP_KERNEL);
  194. if (!super)
  195. return ERR_PTR(-ENOMEM);
  196. init.name = name;
  197. init.ops = &tegra_clk_super_ops;
  198. init.flags = flags;
  199. init.parent_names = parent_names;
  200. init.num_parents = num_parents;
  201. super->reg = reg;
  202. super->lock = lock;
  203. super->width = 4;
  204. super->flags = clk_super_flags;
  205. super->frac_div.reg = reg + 4;
  206. super->frac_div.shift = 16;
  207. super->frac_div.width = 8;
  208. super->frac_div.frac_width = 1;
  209. super->frac_div.lock = lock;
  210. super->div_ops = &tegra_clk_frac_div_ops;
  211. /* Data in .init is copied by clk_register(), so stack variable OK */
  212. super->hw.init = &init;
  213. clk = clk_register(NULL, &super->hw);
  214. if (IS_ERR(clk))
  215. kfree(super);
  216. return clk;
  217. }