clk-super.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. .determine_rate = clk_hw_determine_rate_no_reparent,
  110. .get_parent = clk_super_get_parent,
  111. .set_parent = clk_super_set_parent,
  112. .restore_context = clk_super_mux_restore_context,
  113. };
  114. static int clk_super_determine_rate(struct clk_hw *hw,
  115. struct clk_rate_request *req)
  116. {
  117. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  118. struct clk_hw *div_hw = &super->frac_div.hw;
  119. unsigned long rate;
  120. __clk_hw_set_clk(div_hw, hw);
  121. rate = super->div_ops->round_rate(div_hw, req->rate,
  122. &req->best_parent_rate);
  123. if (rate < 0)
  124. return rate;
  125. req->rate = rate;
  126. return 0;
  127. }
  128. static unsigned long clk_super_recalc_rate(struct clk_hw *hw,
  129. unsigned long parent_rate)
  130. {
  131. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  132. struct clk_hw *div_hw = &super->frac_div.hw;
  133. __clk_hw_set_clk(div_hw, hw);
  134. return super->div_ops->recalc_rate(div_hw, parent_rate);
  135. }
  136. static int clk_super_set_rate(struct clk_hw *hw, unsigned long rate,
  137. unsigned long parent_rate)
  138. {
  139. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  140. struct clk_hw *div_hw = &super->frac_div.hw;
  141. __clk_hw_set_clk(div_hw, hw);
  142. return super->div_ops->set_rate(div_hw, rate, parent_rate);
  143. }
  144. static void clk_super_restore_context(struct clk_hw *hw)
  145. {
  146. struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
  147. struct clk_hw *div_hw = &super->frac_div.hw;
  148. int parent_id;
  149. parent_id = clk_hw_get_parent_index(hw);
  150. if (WARN_ON(parent_id < 0))
  151. return;
  152. super->div_ops->restore_context(div_hw);
  153. clk_super_set_parent(hw, parent_id);
  154. }
  155. const struct clk_ops tegra_clk_super_ops = {
  156. .get_parent = clk_super_get_parent,
  157. .set_parent = clk_super_set_parent,
  158. .set_rate = clk_super_set_rate,
  159. .determine_rate = clk_super_determine_rate,
  160. .recalc_rate = clk_super_recalc_rate,
  161. .restore_context = clk_super_restore_context,
  162. };
  163. struct clk *tegra_clk_register_super_mux(const char *name,
  164. const char **parent_names, u8 num_parents,
  165. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  166. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
  167. {
  168. struct tegra_clk_super_mux *super;
  169. struct clk *clk;
  170. struct clk_init_data init;
  171. super = kzalloc(sizeof(*super), GFP_KERNEL);
  172. if (!super)
  173. return ERR_PTR(-ENOMEM);
  174. init.name = name;
  175. init.ops = &tegra_clk_super_mux_ops;
  176. init.flags = flags;
  177. init.parent_names = parent_names;
  178. init.num_parents = num_parents;
  179. super->reg = reg;
  180. super->pllx_index = pllx_index;
  181. super->div2_index = div2_index;
  182. super->lock = lock;
  183. super->width = width;
  184. super->flags = clk_super_flags;
  185. /* Data in .init is copied by clk_register(), so stack variable OK */
  186. super->hw.init = &init;
  187. clk = tegra_clk_dev_register(&super->hw);
  188. if (IS_ERR(clk))
  189. kfree(super);
  190. return clk;
  191. }
  192. struct clk *tegra_clk_register_super_clk(const char *name,
  193. const char * const *parent_names, u8 num_parents,
  194. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  195. spinlock_t *lock)
  196. {
  197. struct tegra_clk_super_mux *super;
  198. struct clk *clk;
  199. struct clk_init_data init;
  200. super = kzalloc(sizeof(*super), GFP_KERNEL);
  201. if (!super)
  202. return ERR_PTR(-ENOMEM);
  203. init.name = name;
  204. init.ops = &tegra_clk_super_ops;
  205. init.flags = flags;
  206. init.parent_names = parent_names;
  207. init.num_parents = num_parents;
  208. super->reg = reg;
  209. super->lock = lock;
  210. super->width = 4;
  211. super->flags = clk_super_flags;
  212. super->frac_div.reg = reg + 4;
  213. super->frac_div.shift = 16;
  214. super->frac_div.width = 8;
  215. super->frac_div.frac_width = 1;
  216. super->frac_div.lock = lock;
  217. super->div_ops = &tegra_clk_frac_div_ops;
  218. /* Data in .init is copied by clk_register(), so stack variable OK */
  219. super->hw.init = &init;
  220. clk = clk_register(NULL, &super->hw);
  221. if (IS_ERR(clk))
  222. kfree(super);
  223. return clk;
  224. }