clk-tegra20-emc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Based on drivers/clk/tegra/clk-emc.c
  4. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author: Dmitry Osipenko <[email protected]>
  7. * Copyright (C) 2019 GRATE-DRIVER project
  8. */
  9. #define pr_fmt(fmt) "tegra-emc-clk: " fmt
  10. #include <linux/bits.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/clk/tegra.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include "clk.h"
  19. #define CLK_SOURCE_EMC_2X_CLK_DIVISOR_MASK GENMASK(7, 0)
  20. #define CLK_SOURCE_EMC_2X_CLK_SRC_MASK GENMASK(31, 30)
  21. #define CLK_SOURCE_EMC_2X_CLK_SRC_SHIFT 30
  22. #define MC_EMC_SAME_FREQ BIT(16)
  23. #define USE_PLLM_UD BIT(29)
  24. #define EMC_SRC_PLL_M 0
  25. #define EMC_SRC_PLL_C 1
  26. #define EMC_SRC_PLL_P 2
  27. #define EMC_SRC_CLK_M 3
  28. static const char * const emc_parent_clk_names[] = {
  29. "pll_m", "pll_c", "pll_p", "clk_m",
  30. };
  31. struct tegra_clk_emc {
  32. struct clk_hw hw;
  33. void __iomem *reg;
  34. bool mc_same_freq;
  35. bool want_low_jitter;
  36. tegra20_clk_emc_round_cb *round_cb;
  37. void *cb_arg;
  38. };
  39. static inline struct tegra_clk_emc *to_tegra_clk_emc(struct clk_hw *hw)
  40. {
  41. return container_of(hw, struct tegra_clk_emc, hw);
  42. }
  43. static unsigned long emc_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct tegra_clk_emc *emc = to_tegra_clk_emc(hw);
  47. u32 val, div;
  48. val = readl_relaxed(emc->reg);
  49. div = val & CLK_SOURCE_EMC_2X_CLK_DIVISOR_MASK;
  50. return DIV_ROUND_UP(parent_rate * 2, div + 2);
  51. }
  52. static u8 emc_get_parent(struct clk_hw *hw)
  53. {
  54. struct tegra_clk_emc *emc = to_tegra_clk_emc(hw);
  55. return readl_relaxed(emc->reg) >> CLK_SOURCE_EMC_2X_CLK_SRC_SHIFT;
  56. }
  57. static int emc_set_parent(struct clk_hw *hw, u8 index)
  58. {
  59. struct tegra_clk_emc *emc = to_tegra_clk_emc(hw);
  60. u32 val, div;
  61. val = readl_relaxed(emc->reg);
  62. val &= ~CLK_SOURCE_EMC_2X_CLK_SRC_MASK;
  63. val |= index << CLK_SOURCE_EMC_2X_CLK_SRC_SHIFT;
  64. div = val & CLK_SOURCE_EMC_2X_CLK_DIVISOR_MASK;
  65. if (index == EMC_SRC_PLL_M && div == 0 && emc->want_low_jitter)
  66. val |= USE_PLLM_UD;
  67. else
  68. val &= ~USE_PLLM_UD;
  69. if (emc->mc_same_freq)
  70. val |= MC_EMC_SAME_FREQ;
  71. else
  72. val &= ~MC_EMC_SAME_FREQ;
  73. writel_relaxed(val, emc->reg);
  74. fence_udelay(1, emc->reg);
  75. return 0;
  76. }
  77. static int emc_set_rate(struct clk_hw *hw, unsigned long rate,
  78. unsigned long parent_rate)
  79. {
  80. struct tegra_clk_emc *emc = to_tegra_clk_emc(hw);
  81. unsigned int index;
  82. u32 val, div;
  83. div = div_frac_get(rate, parent_rate, 8, 1, 0);
  84. val = readl_relaxed(emc->reg);
  85. val &= ~CLK_SOURCE_EMC_2X_CLK_DIVISOR_MASK;
  86. val |= div;
  87. index = val >> CLK_SOURCE_EMC_2X_CLK_SRC_SHIFT;
  88. if (index == EMC_SRC_PLL_M && div == 0 && emc->want_low_jitter)
  89. val |= USE_PLLM_UD;
  90. else
  91. val &= ~USE_PLLM_UD;
  92. if (emc->mc_same_freq)
  93. val |= MC_EMC_SAME_FREQ;
  94. else
  95. val &= ~MC_EMC_SAME_FREQ;
  96. writel_relaxed(val, emc->reg);
  97. fence_udelay(1, emc->reg);
  98. return 0;
  99. }
  100. static int emc_set_rate_and_parent(struct clk_hw *hw,
  101. unsigned long rate,
  102. unsigned long parent_rate,
  103. u8 index)
  104. {
  105. struct tegra_clk_emc *emc = to_tegra_clk_emc(hw);
  106. u32 val, div;
  107. div = div_frac_get(rate, parent_rate, 8, 1, 0);
  108. val = readl_relaxed(emc->reg);
  109. val &= ~CLK_SOURCE_EMC_2X_CLK_SRC_MASK;
  110. val |= index << CLK_SOURCE_EMC_2X_CLK_SRC_SHIFT;
  111. val &= ~CLK_SOURCE_EMC_2X_CLK_DIVISOR_MASK;
  112. val |= div;
  113. if (index == EMC_SRC_PLL_M && div == 0 && emc->want_low_jitter)
  114. val |= USE_PLLM_UD;
  115. else
  116. val &= ~USE_PLLM_UD;
  117. if (emc->mc_same_freq)
  118. val |= MC_EMC_SAME_FREQ;
  119. else
  120. val &= ~MC_EMC_SAME_FREQ;
  121. writel_relaxed(val, emc->reg);
  122. fence_udelay(1, emc->reg);
  123. return 0;
  124. }
  125. static int emc_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  126. {
  127. struct tegra_clk_emc *emc = to_tegra_clk_emc(hw);
  128. struct clk_hw *parent_hw;
  129. unsigned long divided_rate;
  130. unsigned long parent_rate;
  131. unsigned int i;
  132. long emc_rate;
  133. int div;
  134. emc_rate = emc->round_cb(req->rate, req->min_rate, req->max_rate,
  135. emc->cb_arg);
  136. if (emc_rate < 0)
  137. return emc_rate;
  138. for (i = 0; i < ARRAY_SIZE(emc_parent_clk_names); i++) {
  139. parent_hw = clk_hw_get_parent_by_index(hw, i);
  140. if (req->best_parent_hw == parent_hw)
  141. parent_rate = req->best_parent_rate;
  142. else
  143. parent_rate = clk_hw_get_rate(parent_hw);
  144. if (emc_rate > parent_rate)
  145. continue;
  146. div = div_frac_get(emc_rate, parent_rate, 8, 1, 0);
  147. divided_rate = DIV_ROUND_UP(parent_rate * 2, div + 2);
  148. if (divided_rate != emc_rate)
  149. continue;
  150. req->best_parent_rate = parent_rate;
  151. req->best_parent_hw = parent_hw;
  152. req->rate = emc_rate;
  153. break;
  154. }
  155. if (i == ARRAY_SIZE(emc_parent_clk_names)) {
  156. pr_err_once("can't find parent for rate %lu emc_rate %lu\n",
  157. req->rate, emc_rate);
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. static const struct clk_ops tegra_clk_emc_ops = {
  163. .recalc_rate = emc_recalc_rate,
  164. .get_parent = emc_get_parent,
  165. .set_parent = emc_set_parent,
  166. .set_rate = emc_set_rate,
  167. .set_rate_and_parent = emc_set_rate_and_parent,
  168. .determine_rate = emc_determine_rate,
  169. };
  170. void tegra20_clk_set_emc_round_callback(tegra20_clk_emc_round_cb *round_cb,
  171. void *cb_arg)
  172. {
  173. struct clk *clk = __clk_lookup("emc");
  174. struct tegra_clk_emc *emc;
  175. struct clk_hw *hw;
  176. if (clk) {
  177. hw = __clk_get_hw(clk);
  178. emc = to_tegra_clk_emc(hw);
  179. emc->round_cb = round_cb;
  180. emc->cb_arg = cb_arg;
  181. }
  182. }
  183. EXPORT_SYMBOL_GPL(tegra20_clk_set_emc_round_callback);
  184. bool tegra20_clk_emc_driver_available(struct clk_hw *emc_hw)
  185. {
  186. return to_tegra_clk_emc(emc_hw)->round_cb != NULL;
  187. }
  188. struct clk *tegra20_clk_register_emc(void __iomem *ioaddr, bool low_jitter)
  189. {
  190. struct tegra_clk_emc *emc;
  191. struct clk_init_data init;
  192. struct clk *clk;
  193. emc = kzalloc(sizeof(*emc), GFP_KERNEL);
  194. if (!emc)
  195. return NULL;
  196. /*
  197. * EMC stands for External Memory Controller.
  198. *
  199. * We don't want EMC clock to be disabled ever by gating its
  200. * parent and whatnot because system is busted immediately in that
  201. * case, hence the clock is marked as critical.
  202. */
  203. init.name = "emc";
  204. init.ops = &tegra_clk_emc_ops;
  205. init.flags = CLK_IS_CRITICAL;
  206. init.parent_names = emc_parent_clk_names;
  207. init.num_parents = ARRAY_SIZE(emc_parent_clk_names);
  208. emc->reg = ioaddr;
  209. emc->hw.init = &init;
  210. emc->want_low_jitter = low_jitter;
  211. clk = clk_register(NULL, &emc->hw);
  212. if (IS_ERR(clk)) {
  213. kfree(emc);
  214. return NULL;
  215. }
  216. return clk;
  217. }
  218. int tegra20_clk_prepare_emc_mc_same_freq(struct clk *emc_clk, bool same)
  219. {
  220. struct tegra_clk_emc *emc;
  221. struct clk_hw *hw;
  222. if (!emc_clk)
  223. return -EINVAL;
  224. hw = __clk_get_hw(emc_clk);
  225. emc = to_tegra_clk_emc(hw);
  226. emc->mc_same_freq = same;
  227. return 0;
  228. }
  229. EXPORT_SYMBOL_GPL(tegra20_clk_prepare_emc_mc_same_freq);