clk.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bits.h>
  3. #include <linux/clk.h>
  4. #include <linux/clk-provider.h>
  5. #include <linux/err.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include "clk.h"
  12. #define CCM_CCDR 0x4
  13. #define CCDR_MMDC_CH0_MASK BIT(17)
  14. #define CCDR_MMDC_CH1_MASK BIT(16)
  15. DEFINE_SPINLOCK(imx_ccm_lock);
  16. EXPORT_SYMBOL_GPL(imx_ccm_lock);
  17. bool mcore_booted;
  18. EXPORT_SYMBOL_GPL(mcore_booted);
  19. void imx_unregister_hw_clocks(struct clk_hw *hws[], unsigned int count)
  20. {
  21. unsigned int i;
  22. for (i = 0; i < count; i++)
  23. clk_hw_unregister(hws[i]);
  24. }
  25. EXPORT_SYMBOL_GPL(imx_unregister_hw_clocks);
  26. void imx_mmdc_mask_handshake(void __iomem *ccm_base,
  27. unsigned int chn)
  28. {
  29. unsigned int reg;
  30. reg = readl_relaxed(ccm_base + CCM_CCDR);
  31. reg |= chn == 0 ? CCDR_MMDC_CH0_MASK : CCDR_MMDC_CH1_MASK;
  32. writel_relaxed(reg, ccm_base + CCM_CCDR);
  33. }
  34. void imx_check_clocks(struct clk *clks[], unsigned int count)
  35. {
  36. unsigned i;
  37. for (i = 0; i < count; i++)
  38. if (IS_ERR(clks[i]))
  39. pr_err("i.MX clk %u: register failed with %ld\n",
  40. i, PTR_ERR(clks[i]));
  41. }
  42. void imx_check_clk_hws(struct clk_hw *clks[], unsigned int count)
  43. {
  44. unsigned int i;
  45. for (i = 0; i < count; i++)
  46. if (IS_ERR(clks[i]))
  47. pr_err("i.MX clk %u: register failed with %ld\n",
  48. i, PTR_ERR(clks[i]));
  49. }
  50. EXPORT_SYMBOL_GPL(imx_check_clk_hws);
  51. static struct clk *imx_obtain_fixed_clock_from_dt(const char *name)
  52. {
  53. struct of_phandle_args phandle;
  54. struct clk *clk = ERR_PTR(-ENODEV);
  55. char *path;
  56. path = kasprintf(GFP_KERNEL, "/clocks/%s", name);
  57. if (!path)
  58. return ERR_PTR(-ENOMEM);
  59. phandle.np = of_find_node_by_path(path);
  60. kfree(path);
  61. if (phandle.np) {
  62. clk = of_clk_get_from_provider(&phandle);
  63. of_node_put(phandle.np);
  64. }
  65. return clk;
  66. }
  67. struct clk *imx_obtain_fixed_clock(
  68. const char *name, unsigned long rate)
  69. {
  70. struct clk *clk;
  71. clk = imx_obtain_fixed_clock_from_dt(name);
  72. if (IS_ERR(clk))
  73. clk = imx_clk_fixed(name, rate);
  74. return clk;
  75. }
  76. struct clk_hw *imx_obtain_fixed_clock_hw(
  77. const char *name, unsigned long rate)
  78. {
  79. struct clk *clk;
  80. clk = imx_obtain_fixed_clock_from_dt(name);
  81. if (IS_ERR(clk))
  82. clk = imx_clk_fixed(name, rate);
  83. return __clk_get_hw(clk);
  84. }
  85. struct clk_hw *imx_obtain_fixed_of_clock(struct device_node *np,
  86. const char *name, unsigned long rate)
  87. {
  88. struct clk *clk = of_clk_get_by_name(np, name);
  89. struct clk_hw *hw;
  90. if (IS_ERR(clk))
  91. hw = imx_obtain_fixed_clock_hw(name, rate);
  92. else
  93. hw = __clk_get_hw(clk);
  94. return hw;
  95. }
  96. struct clk_hw *imx_get_clk_hw_by_name(struct device_node *np, const char *name)
  97. {
  98. struct clk *clk;
  99. clk = of_clk_get_by_name(np, name);
  100. if (IS_ERR(clk))
  101. return ERR_PTR(-ENOENT);
  102. return __clk_get_hw(clk);
  103. }
  104. EXPORT_SYMBOL_GPL(imx_get_clk_hw_by_name);
  105. /*
  106. * This fixups the register CCM_CSCMR1 write value.
  107. * The write/read/divider values of the aclk_podf field
  108. * of that register have the relationship described by
  109. * the following table:
  110. *
  111. * write value read value divider
  112. * 3b'000 3b'110 7
  113. * 3b'001 3b'111 8
  114. * 3b'010 3b'100 5
  115. * 3b'011 3b'101 6
  116. * 3b'100 3b'010 3
  117. * 3b'101 3b'011 4
  118. * 3b'110 3b'000 1
  119. * 3b'111 3b'001 2(default)
  120. *
  121. * That's why we do the xor operation below.
  122. */
  123. #define CSCMR1_FIXUP 0x00600000
  124. void imx_cscmr1_fixup(u32 *val)
  125. {
  126. *val ^= CSCMR1_FIXUP;
  127. return;
  128. }
  129. #ifndef MODULE
  130. static bool imx_keep_uart_clocks;
  131. static int imx_enabled_uart_clocks;
  132. static struct clk **imx_uart_clocks;
  133. static int __init imx_keep_uart_clocks_param(char *str)
  134. {
  135. imx_keep_uart_clocks = 1;
  136. return 0;
  137. }
  138. __setup_param("earlycon", imx_keep_uart_earlycon,
  139. imx_keep_uart_clocks_param, 0);
  140. __setup_param("earlyprintk", imx_keep_uart_earlyprintk,
  141. imx_keep_uart_clocks_param, 0);
  142. void imx_register_uart_clocks(void)
  143. {
  144. unsigned int num __maybe_unused;
  145. imx_enabled_uart_clocks = 0;
  146. /* i.MX boards use device trees now. For build tests without CONFIG_OF, do nothing */
  147. #ifdef CONFIG_OF
  148. if (imx_keep_uart_clocks) {
  149. int i;
  150. num = of_clk_get_parent_count(of_stdout);
  151. if (!num)
  152. return;
  153. if (!of_stdout)
  154. return;
  155. imx_uart_clocks = kcalloc(num, sizeof(struct clk *), GFP_KERNEL);
  156. if (!imx_uart_clocks)
  157. return;
  158. for (i = 0; i < num; i++) {
  159. imx_uart_clocks[imx_enabled_uart_clocks] = of_clk_get(of_stdout, i);
  160. /* Stop if there are no more of_stdout references */
  161. if (IS_ERR(imx_uart_clocks[imx_enabled_uart_clocks]))
  162. return;
  163. /* Only enable the clock if it's not NULL */
  164. if (imx_uart_clocks[imx_enabled_uart_clocks])
  165. clk_prepare_enable(imx_uart_clocks[imx_enabled_uart_clocks++]);
  166. }
  167. }
  168. #endif
  169. }
  170. static int __init imx_clk_disable_uart(void)
  171. {
  172. if (imx_keep_uart_clocks && imx_enabled_uart_clocks) {
  173. int i;
  174. for (i = 0; i < imx_enabled_uart_clocks; i++) {
  175. clk_disable_unprepare(imx_uart_clocks[i]);
  176. clk_put(imx_uart_clocks[i]);
  177. }
  178. }
  179. kfree(imx_uart_clocks);
  180. return 0;
  181. }
  182. late_initcall_sync(imx_clk_disable_uart);
  183. #endif
  184. MODULE_DESCRIPTION("Common clock support for NXP i.MX SoC family");
  185. MODULE_LICENSE("GPL v2");