clk.c 4.9 KB

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