clk-composite-8m.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 int imx8m_clk_composite_divider_set_rate(struct clk_hw *hw,
  61. unsigned long rate,
  62. unsigned long parent_rate)
  63. {
  64. struct clk_divider *divider = to_clk_divider(hw);
  65. unsigned long flags;
  66. int prediv_value;
  67. int div_value;
  68. int ret;
  69. u32 orig, val;
  70. ret = imx8m_clk_composite_compute_dividers(rate, parent_rate,
  71. &prediv_value, &div_value);
  72. if (ret)
  73. return -EINVAL;
  74. spin_lock_irqsave(divider->lock, flags);
  75. orig = readl(divider->reg);
  76. val = orig & ~((clk_div_mask(divider->width) << divider->shift) |
  77. (clk_div_mask(PCG_DIV_WIDTH) << PCG_DIV_SHIFT));
  78. val |= (u32)(prediv_value - 1) << divider->shift;
  79. val |= (u32)(div_value - 1) << PCG_DIV_SHIFT;
  80. if (val != orig)
  81. writel(val, divider->reg);
  82. spin_unlock_irqrestore(divider->lock, flags);
  83. return ret;
  84. }
  85. static int imx8m_divider_determine_rate(struct clk_hw *hw,
  86. struct clk_rate_request *req)
  87. {
  88. struct clk_divider *divider = to_clk_divider(hw);
  89. int prediv_value;
  90. int div_value;
  91. /* if read only, just return current value */
  92. if (divider->flags & CLK_DIVIDER_READ_ONLY) {
  93. u32 val;
  94. val = readl(divider->reg);
  95. prediv_value = val >> divider->shift;
  96. prediv_value &= clk_div_mask(divider->width);
  97. prediv_value++;
  98. div_value = val >> PCG_DIV_SHIFT;
  99. div_value &= clk_div_mask(PCG_DIV_WIDTH);
  100. div_value++;
  101. return divider_ro_determine_rate(hw, req, divider->table,
  102. PCG_PREDIV_WIDTH + PCG_DIV_WIDTH,
  103. divider->flags, prediv_value * div_value);
  104. }
  105. return divider_determine_rate(hw, req, divider->table,
  106. PCG_PREDIV_WIDTH + PCG_DIV_WIDTH,
  107. divider->flags);
  108. }
  109. static const struct clk_ops imx8m_clk_composite_divider_ops = {
  110. .recalc_rate = imx8m_clk_composite_divider_recalc_rate,
  111. .set_rate = imx8m_clk_composite_divider_set_rate,
  112. .determine_rate = imx8m_divider_determine_rate,
  113. };
  114. static u8 imx8m_clk_composite_mux_get_parent(struct clk_hw *hw)
  115. {
  116. return clk_mux_ops.get_parent(hw);
  117. }
  118. static int imx8m_clk_composite_mux_set_parent(struct clk_hw *hw, u8 index)
  119. {
  120. struct clk_mux *mux = to_clk_mux(hw);
  121. u32 val = clk_mux_index_to_val(mux->table, mux->flags, index);
  122. unsigned long flags = 0;
  123. u32 reg;
  124. if (mux->lock)
  125. spin_lock_irqsave(mux->lock, flags);
  126. reg = readl(mux->reg);
  127. reg &= ~(mux->mask << mux->shift);
  128. val = val << mux->shift;
  129. reg |= val;
  130. /*
  131. * write twice to make sure non-target interface
  132. * SEL_A/B point the same clk input.
  133. */
  134. writel(reg, mux->reg);
  135. writel(reg, mux->reg);
  136. if (mux->lock)
  137. spin_unlock_irqrestore(mux->lock, flags);
  138. return 0;
  139. }
  140. static int
  141. imx8m_clk_composite_mux_determine_rate(struct clk_hw *hw,
  142. struct clk_rate_request *req)
  143. {
  144. return clk_mux_ops.determine_rate(hw, req);
  145. }
  146. static const struct clk_ops imx8m_clk_composite_mux_ops = {
  147. .get_parent = imx8m_clk_composite_mux_get_parent,
  148. .set_parent = imx8m_clk_composite_mux_set_parent,
  149. .determine_rate = imx8m_clk_composite_mux_determine_rate,
  150. };
  151. static int imx8m_clk_composite_gate_enable(struct clk_hw *hw)
  152. {
  153. struct clk_gate *gate = to_clk_gate(hw);
  154. unsigned long flags;
  155. u32 val;
  156. spin_lock_irqsave(gate->lock, flags);
  157. val = readl(gate->reg);
  158. val |= BIT(gate->bit_idx);
  159. writel(val, gate->reg);
  160. spin_unlock_irqrestore(gate->lock, flags);
  161. return 0;
  162. }
  163. static void imx8m_clk_composite_gate_disable(struct clk_hw *hw)
  164. {
  165. /* composite clk requires the disable hook */
  166. }
  167. static const struct clk_ops imx8m_clk_composite_gate_ops = {
  168. .enable = imx8m_clk_composite_gate_enable,
  169. .disable = imx8m_clk_composite_gate_disable,
  170. .is_enabled = clk_gate_is_enabled,
  171. };
  172. struct clk_hw *__imx8m_clk_hw_composite(const char *name,
  173. const char * const *parent_names,
  174. int num_parents, void __iomem *reg,
  175. u32 composite_flags,
  176. unsigned long flags)
  177. {
  178. struct clk_hw *hw = ERR_PTR(-ENOMEM), *mux_hw;
  179. struct clk_hw *div_hw, *gate_hw = NULL;
  180. struct clk_divider *div;
  181. struct clk_gate *gate = NULL;
  182. struct clk_mux *mux;
  183. const struct clk_ops *divider_ops;
  184. const struct clk_ops *mux_ops;
  185. const struct clk_ops *gate_ops;
  186. mux = kzalloc_obj(*mux);
  187. if (!mux)
  188. return ERR_CAST(hw);
  189. mux_hw = &mux->hw;
  190. mux->reg = reg;
  191. mux->shift = PCG_PCS_SHIFT;
  192. mux->mask = PCG_PCS_MASK;
  193. mux->lock = &imx_ccm_lock;
  194. div = kzalloc_obj(*div);
  195. if (!div)
  196. goto free_mux;
  197. div_hw = &div->hw;
  198. div->reg = reg;
  199. if (composite_flags & IMX_COMPOSITE_CORE) {
  200. div->shift = PCG_DIV_SHIFT;
  201. div->width = PCG_CORE_DIV_WIDTH;
  202. divider_ops = &clk_divider_ops;
  203. mux_ops = &imx8m_clk_composite_mux_ops;
  204. } else if (composite_flags & IMX_COMPOSITE_BUS) {
  205. div->shift = PCG_PREDIV_SHIFT;
  206. div->width = PCG_PREDIV_WIDTH;
  207. divider_ops = &imx8m_clk_composite_divider_ops;
  208. mux_ops = &imx8m_clk_composite_mux_ops;
  209. } else {
  210. div->shift = PCG_PREDIV_SHIFT;
  211. div->width = PCG_PREDIV_WIDTH;
  212. divider_ops = &imx8m_clk_composite_divider_ops;
  213. mux_ops = &clk_mux_ops;
  214. if (!(composite_flags & IMX_COMPOSITE_FW_MANAGED))
  215. flags |= CLK_SET_PARENT_GATE;
  216. }
  217. div->lock = &imx_ccm_lock;
  218. div->flags = CLK_DIVIDER_ROUND_CLOSEST;
  219. /* skip registering the gate ops if M4 is enabled */
  220. gate = kzalloc_obj(*gate);
  221. if (!gate)
  222. goto free_div;
  223. gate_hw = &gate->hw;
  224. gate->reg = reg;
  225. gate->bit_idx = PCG_CGC_SHIFT;
  226. gate->lock = &imx_ccm_lock;
  227. if (!mcore_booted)
  228. gate_ops = &clk_gate_ops;
  229. else
  230. gate_ops = &imx8m_clk_composite_gate_ops;
  231. hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
  232. mux_hw, mux_ops, div_hw,
  233. divider_ops, gate_hw, gate_ops, flags);
  234. if (IS_ERR(hw))
  235. goto free_gate;
  236. return hw;
  237. free_gate:
  238. kfree(gate);
  239. free_div:
  240. kfree(div);
  241. free_mux:
  242. kfree(mux);
  243. return ERR_CAST(hw);
  244. }
  245. EXPORT_SYMBOL_GPL(__imx8m_clk_hw_composite);