clk-composite-8m.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2018 NXP
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/errno.h>
  7. #include <linux/export.h>
  8. #include <linux/io.h>
  9. #include <linux/slab.h>
  10. #include "clk.h"
  11. #define PCG_PREDIV_SHIFT 16
  12. #define PCG_PREDIV_WIDTH 3
  13. #define PCG_PREDIV_MAX 8
  14. #define PCG_DIV_SHIFT 0
  15. #define PCG_CORE_DIV_WIDTH 3
  16. #define PCG_DIV_WIDTH 6
  17. #define PCG_DIV_MAX 64
  18. #define PCG_PCS_SHIFT 24
  19. #define PCG_PCS_MASK 0x7
  20. #define PCG_CGC_SHIFT 28
  21. static unsigned long imx8m_clk_composite_divider_recalc_rate(struct clk_hw *hw,
  22. unsigned long parent_rate)
  23. {
  24. struct clk_divider *divider = to_clk_divider(hw);
  25. unsigned long prediv_rate;
  26. unsigned int prediv_value;
  27. unsigned int div_value;
  28. prediv_value = readl(divider->reg) >> divider->shift;
  29. prediv_value &= clk_div_mask(divider->width);
  30. prediv_rate = divider_recalc_rate(hw, parent_rate, prediv_value,
  31. NULL, divider->flags,
  32. divider->width);
  33. div_value = readl(divider->reg) >> PCG_DIV_SHIFT;
  34. div_value &= clk_div_mask(PCG_DIV_WIDTH);
  35. return divider_recalc_rate(hw, prediv_rate, div_value, NULL,
  36. divider->flags, PCG_DIV_WIDTH);
  37. }
  38. static int imx8m_clk_composite_compute_dividers(unsigned long rate,
  39. unsigned long parent_rate,
  40. int *prediv, int *postdiv)
  41. {
  42. int div1, div2;
  43. int error = INT_MAX;
  44. int ret = -EINVAL;
  45. *prediv = 1;
  46. *postdiv = 1;
  47. for (div1 = 1; div1 <= PCG_PREDIV_MAX; div1++) {
  48. for (div2 = 1; div2 <= PCG_DIV_MAX; div2++) {
  49. int new_error = ((parent_rate / div1) / div2) - rate;
  50. if (abs(new_error) < abs(error)) {
  51. *prediv = div1;
  52. *postdiv = div2;
  53. error = new_error;
  54. ret = 0;
  55. }
  56. }
  57. }
  58. return ret;
  59. }
  60. static long imx8m_clk_composite_divider_round_rate(struct clk_hw *hw,
  61. unsigned long rate,
  62. unsigned long *prate)
  63. {
  64. int prediv_value;
  65. int div_value;
  66. imx8m_clk_composite_compute_dividers(rate, *prate,
  67. &prediv_value, &div_value);
  68. rate = DIV_ROUND_UP(*prate, prediv_value);
  69. return DIV_ROUND_UP(rate, div_value);
  70. }
  71. static int imx8m_clk_composite_divider_set_rate(struct clk_hw *hw,
  72. unsigned long rate,
  73. unsigned long parent_rate)
  74. {
  75. struct clk_divider *divider = to_clk_divider(hw);
  76. unsigned long flags;
  77. int prediv_value;
  78. int div_value;
  79. int ret;
  80. u32 orig, val;
  81. ret = imx8m_clk_composite_compute_dividers(rate, parent_rate,
  82. &prediv_value, &div_value);
  83. if (ret)
  84. return -EINVAL;
  85. spin_lock_irqsave(divider->lock, flags);
  86. orig = readl(divider->reg);
  87. val = orig & ~((clk_div_mask(divider->width) << divider->shift) |
  88. (clk_div_mask(PCG_DIV_WIDTH) << PCG_DIV_SHIFT));
  89. val |= (u32)(prediv_value - 1) << divider->shift;
  90. val |= (u32)(div_value - 1) << PCG_DIV_SHIFT;
  91. if (val != orig)
  92. writel(val, divider->reg);
  93. spin_unlock_irqrestore(divider->lock, flags);
  94. return ret;
  95. }
  96. static const struct clk_ops imx8m_clk_composite_divider_ops = {
  97. .recalc_rate = imx8m_clk_composite_divider_recalc_rate,
  98. .round_rate = imx8m_clk_composite_divider_round_rate,
  99. .set_rate = imx8m_clk_composite_divider_set_rate,
  100. };
  101. static u8 imx8m_clk_composite_mux_get_parent(struct clk_hw *hw)
  102. {
  103. return clk_mux_ops.get_parent(hw);
  104. }
  105. static int imx8m_clk_composite_mux_set_parent(struct clk_hw *hw, u8 index)
  106. {
  107. struct clk_mux *mux = to_clk_mux(hw);
  108. u32 val = clk_mux_index_to_val(mux->table, mux->flags, index);
  109. unsigned long flags = 0;
  110. u32 reg;
  111. if (mux->lock)
  112. spin_lock_irqsave(mux->lock, flags);
  113. reg = readl(mux->reg);
  114. reg &= ~(mux->mask << mux->shift);
  115. val = val << mux->shift;
  116. reg |= val;
  117. /*
  118. * write twice to make sure non-target interface
  119. * SEL_A/B point the same clk input.
  120. */
  121. writel(reg, mux->reg);
  122. writel(reg, mux->reg);
  123. if (mux->lock)
  124. spin_unlock_irqrestore(mux->lock, flags);
  125. return 0;
  126. }
  127. static int
  128. imx8m_clk_composite_mux_determine_rate(struct clk_hw *hw,
  129. struct clk_rate_request *req)
  130. {
  131. return clk_mux_ops.determine_rate(hw, req);
  132. }
  133. static const struct clk_ops imx8m_clk_composite_mux_ops = {
  134. .get_parent = imx8m_clk_composite_mux_get_parent,
  135. .set_parent = imx8m_clk_composite_mux_set_parent,
  136. .determine_rate = imx8m_clk_composite_mux_determine_rate,
  137. };
  138. struct clk_hw *__imx8m_clk_hw_composite(const char *name,
  139. const char * const *parent_names,
  140. int num_parents, void __iomem *reg,
  141. u32 composite_flags,
  142. unsigned long flags)
  143. {
  144. struct clk_hw *hw = ERR_PTR(-ENOMEM), *mux_hw;
  145. struct clk_hw *div_hw, *gate_hw = NULL;
  146. struct clk_divider *div = NULL;
  147. struct clk_gate *gate = NULL;
  148. struct clk_mux *mux = NULL;
  149. const struct clk_ops *divider_ops;
  150. const struct clk_ops *mux_ops;
  151. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  152. if (!mux)
  153. goto fail;
  154. mux_hw = &mux->hw;
  155. mux->reg = reg;
  156. mux->shift = PCG_PCS_SHIFT;
  157. mux->mask = PCG_PCS_MASK;
  158. mux->lock = &imx_ccm_lock;
  159. div = kzalloc(sizeof(*div), GFP_KERNEL);
  160. if (!div)
  161. goto fail;
  162. div_hw = &div->hw;
  163. div->reg = reg;
  164. if (composite_flags & IMX_COMPOSITE_CORE) {
  165. div->shift = PCG_DIV_SHIFT;
  166. div->width = PCG_CORE_DIV_WIDTH;
  167. divider_ops = &clk_divider_ops;
  168. mux_ops = &imx8m_clk_composite_mux_ops;
  169. } else if (composite_flags & IMX_COMPOSITE_BUS) {
  170. div->shift = PCG_PREDIV_SHIFT;
  171. div->width = PCG_PREDIV_WIDTH;
  172. divider_ops = &imx8m_clk_composite_divider_ops;
  173. mux_ops = &imx8m_clk_composite_mux_ops;
  174. } else {
  175. div->shift = PCG_PREDIV_SHIFT;
  176. div->width = PCG_PREDIV_WIDTH;
  177. divider_ops = &imx8m_clk_composite_divider_ops;
  178. mux_ops = &clk_mux_ops;
  179. if (!(composite_flags & IMX_COMPOSITE_FW_MANAGED))
  180. flags |= CLK_SET_PARENT_GATE;
  181. }
  182. div->lock = &imx_ccm_lock;
  183. div->flags = CLK_DIVIDER_ROUND_CLOSEST;
  184. /* skip registering the gate ops if M4 is enabled */
  185. if (!mcore_booted) {
  186. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  187. if (!gate)
  188. goto fail;
  189. gate_hw = &gate->hw;
  190. gate->reg = reg;
  191. gate->bit_idx = PCG_CGC_SHIFT;
  192. gate->lock = &imx_ccm_lock;
  193. }
  194. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  195. mux_hw, mux_ops, div_hw,
  196. divider_ops, gate_hw, &clk_gate_ops, flags);
  197. if (IS_ERR(hw))
  198. goto fail;
  199. return hw;
  200. fail:
  201. kfree(gate);
  202. kfree(div);
  203. kfree(mux);
  204. return ERR_CAST(hw);
  205. }
  206. EXPORT_SYMBOL_GPL(__imx8m_clk_hw_composite);