1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015-2020, NVIDIA CORPORATION. All rights reserved.
6 #include <linux/bitfield.h>
8 #include <linux/clk-provider.h>
9 #include <linux/clk/tegra.h>
10 #include <linux/device.h>
11 #include <linux/module.h>
13 #include <linux/slab.h>
15 #define CLK_SOURCE_EMC 0x19c
16 #define CLK_SOURCE_EMC_2X_CLK_SRC GENMASK(31, 29)
17 #define CLK_SOURCE_EMC_MC_EMC_SAME_FREQ BIT(16)
18 #define CLK_SOURCE_EMC_2X_CLK_DIVISOR GENMASK(7, 0)
20 #define CLK_SRC_PLLM 0
21 #define CLK_SRC_PLLC 1
22 #define CLK_SRC_PLLP 2
23 #define CLK_SRC_CLK_M 3
24 #define CLK_SRC_PLLM_UD 4
25 #define CLK_SRC_PLLMB_UD 5
26 #define CLK_SRC_PLLMB 6
27 #define CLK_SRC_PLLP_UD 7
29 struct tegra210_clk_emc {
33 struct tegra210_clk_emc_provider *provider;
35 struct clk *parents[8];
38 static inline struct tegra210_clk_emc *
39 to_tegra210_clk_emc(struct clk_hw *hw)
41 return container_of(hw, struct tegra210_clk_emc, hw);
44 static const char *tegra210_clk_emc_parents[] = {
45 "pll_m", "pll_c", "pll_p", "clk_m", "pll_m_ud", "pll_mb_ud",
49 static u8 tegra210_clk_emc_get_parent(struct clk_hw *hw)
51 struct tegra210_clk_emc *emc = to_tegra210_clk_emc(hw);
55 value = readl_relaxed(emc->regs + CLK_SOURCE_EMC);
56 src = FIELD_GET(CLK_SOURCE_EMC_2X_CLK_SRC, value);
61 static unsigned long tegra210_clk_emc_recalc_rate(struct clk_hw *hw,
62 unsigned long parent_rate)
64 struct tegra210_clk_emc *emc = to_tegra210_clk_emc(hw);
68 * CCF assumes that neither the parent nor its rate will change during
69 * ->set_rate(), so the parent rate passed in here was cached from the
70 * parent before the ->set_rate() call.
72 * This can lead to wrong results being reported for the EMC clock if
73 * the parent and/or parent rate have changed as part of the EMC rate
74 * change sequence. Fix this by overriding the parent clock with what
75 * we know to be the correct value after the rate change.
77 parent_rate = clk_hw_get_rate(clk_hw_get_parent(hw));
79 value = readl_relaxed(emc->regs + CLK_SOURCE_EMC);
81 div = FIELD_GET(CLK_SOURCE_EMC_2X_CLK_DIVISOR, value);
84 return DIV_ROUND_UP(parent_rate * 2, div);
87 static long tegra210_clk_emc_round_rate(struct clk_hw *hw, unsigned long rate,
90 struct tegra210_clk_emc *emc = to_tegra210_clk_emc(hw);
91 struct tegra210_clk_emc_provider *provider = emc->provider;
94 if (!provider || !provider->configs || provider->num_configs == 0)
95 return clk_hw_get_rate(hw);
97 for (i = 0; i < provider->num_configs; i++) {
98 if (provider->configs[i].rate >= rate)
99 return provider->configs[i].rate;
102 return provider->configs[i - 1].rate;
105 static struct clk *tegra210_clk_emc_find_parent(struct tegra210_clk_emc *emc,
108 struct clk_hw *parent = clk_hw_get_parent_by_index(&emc->hw, index);
109 const char *name = clk_hw_get_name(parent);
111 /* XXX implement cache? */
113 return __clk_lookup(name);
116 static int tegra210_clk_emc_set_rate(struct clk_hw *hw, unsigned long rate,
117 unsigned long parent_rate)
119 struct tegra210_clk_emc *emc = to_tegra210_clk_emc(hw);
120 struct tegra210_clk_emc_provider *provider = emc->provider;
121 struct tegra210_clk_emc_config *config;
122 struct device *dev = provider->dev;
123 struct clk_hw *old, *new, *parent;
124 u8 old_idx, new_idx, index;
129 if (!provider || !provider->configs || provider->num_configs == 0)
132 for (i = 0; i < provider->num_configs; i++) {
133 if (provider->configs[i].rate >= rate) {
134 config = &provider->configs[i];
139 if (i == provider->num_configs)
140 config = &provider->configs[i - 1];
142 old_idx = tegra210_clk_emc_get_parent(hw);
143 new_idx = FIELD_GET(CLK_SOURCE_EMC_2X_CLK_SRC, config->value);
145 old = clk_hw_get_parent_by_index(hw, old_idx);
146 new = clk_hw_get_parent_by_index(hw, new_idx);
148 /* if the rate has changed... */
149 if (config->parent_rate != clk_hw_get_rate(old)) {
150 /* ... but the clock source remains the same ... */
151 if (new_idx == old_idx) {
152 /* ... switch to the alternative clock source. */
155 new_idx = CLK_SRC_PLLMB;
158 case CLK_SRC_PLLM_UD:
159 new_idx = CLK_SRC_PLLMB_UD;
162 case CLK_SRC_PLLMB_UD:
163 new_idx = CLK_SRC_PLLM_UD;
167 new_idx = CLK_SRC_PLLM;
172 * This should never happen because we can't deal with
175 if (WARN_ON(new_idx == old_idx))
178 new = clk_hw_get_parent_by_index(hw, new_idx);
188 clk = tegra210_clk_emc_find_parent(emc, index);
191 dev_err(dev, "failed to get parent clock for index %u: %d\n",
196 /* set the new parent clock to the required rate */
197 if (clk_get_rate(clk) != config->parent_rate) {
198 err = clk_set_rate(clk, config->parent_rate);
200 dev_err(dev, "failed to set rate %lu Hz for %pC: %d\n",
201 config->parent_rate, clk, err);
206 /* enable the new parent clock */
208 err = clk_prepare_enable(clk);
210 dev_err(dev, "failed to enable parent clock %pC: %d\n",
216 /* update the EMC source configuration to reflect the new parent */
217 config->value &= ~CLK_SOURCE_EMC_2X_CLK_SRC;
218 config->value |= FIELD_PREP(CLK_SOURCE_EMC_2X_CLK_SRC, index);
221 * Finally, switch the EMC programming with both old and new parent
224 err = provider->set_rate(dev, config);
226 dev_err(dev, "failed to set EMC rate to %lu Hz: %d\n", rate,
230 * If we're unable to switch to the new EMC frequency, we no
231 * longer need the new parent to be enabled.
234 clk_disable_unprepare(clk);
239 /* reparent to new parent clock and disable the old parent clock */
241 clk = tegra210_clk_emc_find_parent(emc, old_idx);
245 "failed to get parent clock for index %u: %d\n",
250 clk_hw_reparent(hw, parent);
251 clk_disable_unprepare(clk);
257 static const struct clk_ops tegra210_clk_emc_ops = {
258 .get_parent = tegra210_clk_emc_get_parent,
259 .recalc_rate = tegra210_clk_emc_recalc_rate,
260 .round_rate = tegra210_clk_emc_round_rate,
261 .set_rate = tegra210_clk_emc_set_rate,
264 struct clk *tegra210_clk_register_emc(struct device_node *np,
267 struct tegra210_clk_emc *emc;
268 struct clk_init_data init;
271 emc = kzalloc(sizeof(*emc), GFP_KERNEL);
273 return ERR_PTR(-ENOMEM);
278 init.ops = &tegra210_clk_emc_ops;
279 init.flags = CLK_IS_CRITICAL | CLK_GET_RATE_NOCACHE;
280 init.parent_names = tegra210_clk_emc_parents;
281 init.num_parents = ARRAY_SIZE(tegra210_clk_emc_parents);
282 emc->hw.init = &init;
284 clk = clk_register(NULL, &emc->hw);
293 int tegra210_clk_emc_attach(struct clk *clk,
294 struct tegra210_clk_emc_provider *provider)
296 struct clk_hw *hw = __clk_get_hw(clk);
297 struct tegra210_clk_emc *emc = to_tegra210_clk_emc(hw);
298 struct device *dev = provider->dev;
302 if (!try_module_get(provider->owner))
305 for (i = 0; i < provider->num_configs; i++) {
306 struct tegra210_clk_emc_config *config = &provider->configs[i];
307 struct clk_hw *parent;
311 div = FIELD_GET(CLK_SOURCE_EMC_2X_CLK_DIVISOR, config->value);
312 src = FIELD_GET(CLK_SOURCE_EMC_2X_CLK_SRC, config->value);
314 /* do basic sanity checking on the EMC timings */
316 dev_err(dev, "invalid odd divider %u for rate %lu Hz\n",
322 same_freq = config->value & CLK_SOURCE_EMC_MC_EMC_SAME_FREQ;
324 if (same_freq != config->same_freq) {
326 "ambiguous EMC to MC ratio for rate %lu Hz\n",
332 parent = clk_hw_get_parent_by_index(hw, src);
333 config->parent = src;
335 if (src == CLK_SRC_PLLM || src == CLK_SRC_PLLM_UD) {
336 config->parent_rate = config->rate * (1 + div / 2);
338 unsigned long rate = config->rate * (1 + div / 2);
340 config->parent_rate = clk_hw_get_rate(parent);
342 if (config->parent_rate != rate) {
344 "rate %lu Hz does not match input\n",
352 emc->provider = provider;
357 module_put(provider->owner);
360 EXPORT_SYMBOL_GPL(tegra210_clk_emc_attach);
362 void tegra210_clk_emc_detach(struct clk *clk)
364 struct tegra210_clk_emc *emc = to_tegra210_clk_emc(__clk_get_hw(clk));
366 module_put(emc->provider->owner);
367 emc->provider = NULL;
369 EXPORT_SYMBOL_GPL(tegra210_clk_emc_detach);