clk-composite-8m.c 7.6 KB

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